From 49fae7d6e494de6b7abcea02bb9499d7f11b393b Mon Sep 17 00:00:00 2001 From: buraq Date: Tue, 30 Sep 2003 13:14:28 +0000 Subject: helper classes for pattern matching on arrays a... helper classes for pattern matching on arrays and strings --- sources/scala/IterableArray.scala | 32 ++++++++++++++++++++++++++++++++ sources/scala/IterableString.scala | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 sources/scala/IterableArray.scala create mode 100644 sources/scala/IterableString.scala (limited to 'sources') 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 +
+new IterableArray( args ).match {
+      case Seq(x) => System.out.println("contains 1 arg");
+      case Seq() => System.out.println("no arguments given");
+};
+
+*/ +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 +
+ val s = "supercalifragilistic-expialidocious";
+    new IterableString( s ).match {
+      case Seq(_ *, 'f','r','a', _ *) => System.out.println("fra");
+      case _ => System.out.println("this never happens");
+  }
+
+*/ +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 ); +} -- cgit v1.2.3