diff options
author | buraq <buraq@epfl.ch> | 2003-09-30 13:14:28 +0000 |
---|---|---|
committer | buraq <buraq@epfl.ch> | 2003-09-30 13:14:28 +0000 |
commit | 49fae7d6e494de6b7abcea02bb9499d7f11b393b (patch) | |
tree | 512d73964a3adb13fa54d6f17160f668a9cecbf9 /sources | |
parent | 95ced83e5a9cf159427b67054f05c2da0bb6ad81 (diff) | |
download | scala-49fae7d6e494de6b7abcea02bb9499d7f11b393b.tar.gz scala-49fae7d6e494de6b7abcea02bb9499d7f11b393b.tar.bz2 scala-49fae7d6e494de6b7abcea02bb9499d7f11b393b.zip |
helper classes for pattern matching on arrays a...
helper classes for pattern matching on arrays and strings
Diffstat (limited to 'sources')
-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 ); +} |