diff options
-rw-r--r-- | sources/scala/IterableArray.scala | 32 | ||||
-rw-r--r-- | sources/scala/IterableString.scala | 33 |
2 files changed, 65 insertions, 0 deletions
diff --git a/sources/scala/IterableArray.scala b/sources/scala/IterableArray.scala new file mode 100644 index 0000000000..1ca108501b --- /dev/null +++ b/sources/scala/IterableArray.scala @@ -0,0 +1,32 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package scala; + +/* use this for pattern matching on arrays. examples +<pre> +new IterableArray( args ).match { + case Seq(x) => System.out.println("contains 1 arg"); + case Seq() => System.out.println("no arguments given"); +}; +</pre> +*/ +class IterableArray[A]( arr:Array[A] ) with Seq[A]{ + def elements = new Iterator[A] { + var i:int=0; + def hasNext = { + i < arr.length + } + def next:A = { + val res = arr( i ); i=i+1; res + } + } + def length = arr.length; + def apply( i:int ) = arr( i ); +} diff --git a/sources/scala/IterableString.scala b/sources/scala/IterableString.scala new file mode 100644 index 0000000000..e3e15e5757 --- /dev/null +++ b/sources/scala/IterableString.scala @@ -0,0 +1,33 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package scala; + +/* use this to for pattern matching on strings. example +<pre> + val s = "supercalifragilistic-expialidocious"; + new IterableString( s ).match { + case Seq(_ *, 'f','r','a', _ *) => System.out.println("fra"); + case _ => System.out.println("this never happens"); + } +</pre> +*/ +class IterableString( s:String ) with Seq[char]{ + def elements= new Iterator[char] { + var i:int=0; + def hasNext = { + i < s.length() + } + def next:char = { + val res = s.charAt( i ); i=i+1; res + } + } + def length = s.length(); + def apply( i:int ) = s.charAt( i ); +} |