Changeset 6692 for lang/scala/sandbox
- Timestamp:
- 02/15/08 00:28:06 (10 months ago)
- Location:
- lang/scala/sandbox
- Files:
-
- 3 modified
-
lib/scala-sandbox.jar (modified) (previous)
-
src/jp/ne/cappuccino/keisuken/scl/lang/Loop.scala (modified) (7 diffs)
-
webpages/lib/scala-sandbox.jar (modified) (previous)
Legend:
- Unmodified
- Added
- Removed
-
lang/scala/sandbox/src/jp/ne/cappuccino/keisuken/scl/lang/Loop.scala
r6683 r6692 5 5 class BreakWithValue[T](var cond: Boolean, var value: Option[T]) { 6 6 def this() = this(true, None) 7 def apply(): Unit = {cond = false; value = None} 8 def apply(v: T): Unit = {cond = false; value = Some(v)} 7 def apply(): Unit = break 8 def apply(v: T): Unit = break(v) 9 def break: Unit = {cond = false; value = None} 10 def break(v: T): Unit = {cond = false; value = Some(v)} 9 11 } 10 12 11 13 class Break(var cond: Boolean) { 12 14 def this() = this(true) 13 def apply(): Unit = cond = false 14 } 15 16 def forEach[A](iterable: Iterable[A])(p: (A, Break) => Unit): Unit = { 15 def apply(): Unit = break 16 def break: Unit = cond = false 17 } 18 19 def forLoop 20 (init: => Unit, cond: => Boolean, update: => Unit) 21 (p: Break => Unit): Unit = { 22 val break = new Break() 23 init 24 while(break.cond && cond) { 25 p(break) 26 update 27 } 28 } 29 30 def forLoop[A] 31 (iterable: Iterable[A]) 32 (p: (A, Break) => Unit): Unit = { 17 33 val break = new Break() 18 34 val iterator = iterable.elements 19 35 while(break.cond && iterator.hasNext) 20 36 p(iterator.next, break) 37 } 38 39 def forLoopWithIndex[A] 40 (iterable: Iterable[A]) 41 (p: (A, Int, Break) => Unit): Unit = { 42 val break = new Break() 43 val iterator = iterable.elements 44 var index = 0 45 while(break.cond && iterator.hasNext) { 46 p(iterator.next, index, break) 47 index += 1 48 } 49 } 50 51 def findFor[A] 52 (init: => Unit, cond: => Boolean, update: => Unit) 53 (p: BreakWithValue[A] => Unit): Option[A] = { 54 val break = new BreakWithValue[A]() 55 init 56 while(break.cond && cond) { 57 p(break) 58 update 59 } 60 break.value 21 61 } 22 62 … … 27 67 while(break.cond && iterator.hasNext) 28 68 p(iterator.next, break) 69 break.value 70 } 71 72 73 def findForWithIndex[A, B] 74 (iterable: Iterable[A]) 75 (p: (A, Int, BreakWithValue[B]) => Unit): Option[B] = { 76 val break = new BreakWithValue[B]() 77 val iterator = iterable.elements 78 var index = 0 79 while(break.cond && iterator.hasNext) { 80 p(iterator.next, index, break) 81 index += 1 82 } 29 83 break.value 30 84 } … … 42 96 } 43 97 44 def doWhile (cond: => Boolean)(p: Break => Unit): Unit = {98 def doWhileLoop(cond: => Boolean)(p: Break => Unit): Unit = { 45 99 val break = new Break() 46 100 do p(break) while(break.cond && cond) … … 56 110 def main(args: Array[String]) { 57 111 58 forEach(1 to 10) {(i, break) => 59 if(i == 5) { 60 println(i) 112 println("---- forLoop(C)") 113 var i = 0 114 forLoop(i = 0, i < 10, i += 1) {break => 115 println(i) 116 if(i >= 4) break() 117 } 118 119 println("---- forLoop(iterator)") 120 forLoop(1 to 10) {(i, break) => 121 println(i) 122 if(i == 4) { 61 123 break() 62 124 } 63 125 } 64 126 65 var i = 0 127 println("---- forLoopWithIndex") 128 forLoopWithIndex(1 to 10) {(i, index, break) => 129 println("[" + i + "," + index + "]") 130 if(i == 4) { 131 break() 132 } 133 } 134 135 println("---- whileLoop") 136 i = 0 66 137 whileLoop(i < 10) {break => 138 println(i) 67 139 if(i >= 5) { 68 140 println(i) … … 72 144 } 73 145 74 i = 0 75 doWhile({i += 1; i < 10}) {break => 146 println("---- doWhileLoop") 147 i = 0 148 doWhileLoop({i += 1; i < 10}) {break => 76 149 if(i >= 5) { 77 150 println(i) … … 80 153 } 81 154 82 i = 0 83 var result1 = findWhile[Int](true) {break => 84 if(i >= 5) 85 break(i) 86 else 87 i += 1 88 } 89 result1 match { 90 case Some(result) => println(result) 91 case None => println("Not found") 92 } 93 94 i = 0 95 val result2 = findWhile[Int](i < 10) {break => 155 println("---- findFor(C)") 156 var iResult = findFor[Int](i = 0, i < 10, i += 1) {break => 157 if(i >= 4) break(i) 158 } 159 println(iResult) 160 161 println("---- findFor(iterator)") 162 var sResult = findFor[String,String](Array("abc", "cde", "efg")) 163 {(value, break) => 164 if(value == "cde") break(value) 165 } 166 sResult match { 167 case Some(result) => println(result) 168 case None => println("Not found") 169 } 170 171 println("---- findForWithIndex") 172 sResult = findForWithIndex[String,String](Array("abc", "cde", "efg")) 173 {(value, index, break) => 174 println("[" + index + "]=" + value) 175 if(value == "cde") break(value) 176 } 177 sResult match { 178 case Some(result) => println(result) 179 case None => println("Not found") 180 } 181 182 i = 0 183 iResult = findWhile[Int](i < 10) {break => 96 184 if(i == 20) 97 185 break(i) … … 99 187 i += 1 100 188 } 101 result2match {102 case Some(result) => println(result) 103 case None => println("Not found") 104 } 105 106 i = 0 107 val result3= findDoWhile[Int]({i += 1; i < 10}) {break =>189 iResult match { 190 case Some(result) => println(result) 191 case None => println("Not found") 192 } 193 194 i = 0 195 iResult = findDoWhile[Int]({i += 1; i < 10}) {break => 108 196 if(i == 5) break(i) 109 197 } 110 result3 match { 111 case Some(result) => println(result) 112 case None => println("Not found") 113 } 114 115 val result4 = findFor[String, String]( 116 Array("aaa", "bbb", "ccc")) {(value, break) => 117 if(value == "ddd") 118 break(value) 119 } 120 println(result4) 121 122 val result5 = findFor[String,String](Array("aaa", "bbb", "ccc")) 123 {(value, break) => 124 if(value == "bbb") 125 break(value) 126 } match { 127 case Some(result) => println(result) 128 case None => println("Not found") 129 } 130 131 val result6 = findFor[Int,Int](1 to 10) {(i, break) => 132 if(i % 2 == 1) break(i) 133 } 134 result6 match { 198 iResult match { 135 199 case Some(result) => println(result) 136 200 case None => println("Not found")
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)