Changeset 6680 for lang/scala/sandbox

Show
Ignore:
Timestamp:
02/14/08 20:52:00 (10 months ago)
Author:
keisuken
Message:

Loop object update. forEach, whileEach, doWhileEach method add.

Location:
lang/scala/sandbox
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • lang/scala/sandbox/src/jp/ne/cappuccino/keisuken/scl/lang/Loop.scala

    r6679 r6680  
    33object Loop { 
    44 
    5   class Break[T](var cond: Boolean, var value: Option[T]) { 
     5  class BreakWithValue[T](var cond: Boolean, var value: Option[T]) { 
    66    def this() = this(true, None) 
    77    def apply(): Unit = {cond = false; value = None} 
     
    99  } 
    1010 
    11   def foreach[A, B] 
    12     (iterable: Iterable[A])(f: (A, Break[B]) => Unit): Option[B] = { 
    13     val break = new Break[B]() 
     11  class Break(var cond: Boolean) { 
     12    def this() = this(true) 
     13    def apply(): Unit = cond = false 
     14  } 
     15 
     16  def forEach[A](iterable: Iterable[A])(p: (A, Break) => Unit): Unit = { 
     17    val break = new Break() 
    1418    val iterator = iterable.elements 
    15     while(break.cond && iterator.hasNext) { 
    16       val value = iterator.next 
    17       if(value != null) { 
    18         f(value, break) 
    19       } else { 
    20         break() 
    21       } 
    22     } 
     19    while(break.cond && iterator.hasNext) 
     20      p(iterator.next, break) 
     21  } 
     22 
     23  def findFor[A, B] 
     24    (iterable: Iterable[A])(p: (A, BreakWithValue[B]) => Unit): Option[B] = { 
     25    val break = new BreakWithValue[B]() 
     26    val iterator = iterable.elements 
     27    while(break.cond && iterator.hasNext) 
     28      p(iterator.next, break) 
    2329    break.value 
    2430  } 
    2531 
    26   def findWhile[A](cond: => Boolean)(f: Break[A] => Unit): Option[A] = { 
    27     val break = new Break[A]() 
    28     while(break.cond && cond) f(break) 
     32  def whileEach[A](cond: => Boolean)(p: Break => Unit): Unit = { 
     33    val break = new Break() 
     34    while(break.cond && cond) p(break) 
     35  } 
     36 
     37  def findWhile[A] 
     38    (cond: => Boolean)(p: BreakWithValue[A] => Unit): Option[A] = { 
     39    val break = new BreakWithValue[A]() 
     40    while(break.cond && cond) p(break) 
    2941    break.value 
    3042  } 
    3143 
    32   def findDoWhile[A](cond: => Boolean)(f: Break[A] => Unit): Option[A] = { 
    33     val break = new Break[A]() 
    34     do {f(break)} while(break.cond && cond) 
     44  def doWhileEach[A](cond: => Boolean)(p: Break => Unit): Unit = { 
     45    val break = new Break() 
     46    do p(break) while(break.cond && cond) 
     47  } 
     48 
     49  def findDoWhile[A] 
     50    (cond: => Boolean)(p: BreakWithValue[A] => Unit): Option[A] = { 
     51    val break = new BreakWithValue[A]() 
     52    do p(break) while(break.cond && cond) 
    3553    break.value 
    3654  } 
     
    3856  def main(args: Array[String]) { 
    3957 
     58    forEach(1 to 10) {(i, break) => 
     59      if(i == 5) { 
     60        println(i) 
     61        break() 
     62      } 
     63    } 
     64 
    4065    var i = 0 
     66    whileEach(i < 10) {break => 
     67      if(i >= 5) { 
     68        println(i) 
     69        break() 
     70      } else 
     71        i += 1 
     72    } 
     73 
     74    i = 0 
     75    doWhileEach({i += 1; i < 10}) {break => 
     76      if(i >= 5) { 
     77        println(i) 
     78        break() 
     79      } 
     80    } 
     81 
     82    i = 0 
    4183    var result1 = findWhile[Int](true) {break => 
    4284      if(i >= 5) 
     
    71113    } 
    72114 
    73     val result4 = foreach[String, String]( 
     115    val result4 = findFor[String, String]( 
    74116      Array("aaa", "bbb", "ccc")) {(value, break) => 
    75117      if(value.equals("ddd")) 
     
    78120    println(result4) 
    79121 
    80     val result5 = foreach[String,String](Array("aaa", "bbb", "ccc")) 
     122    val result5 = findFor[String,String](Array("aaa", "bbb", "ccc")) 
    81123    {(value, break) => 
    82124      if(value.equals("bbb")) 
     
    87129    } 
    88130 
    89     val result6 = foreach[Int,Int](1 to 10) {(i, break) => 
     131    val result6 = findFor[Int,Int](1 to 10) {(i, break) => 
    90132      if(i % 2 == 1) break(i) 
    91133    }