diff options
author | michelou <michelou@epfl.ch> | 2007-12-13 18:23:16 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2007-12-13 18:23:16 +0000 |
commit | 3bb414bd27255d47069dcc406bee695a271c8278 (patch) | |
tree | 1d515d2505213e3af5526c06fb5c9e7fbe3df47d | |
parent | 1822dfc8c0888420fd6d373b7a87bf4d471306a9 (diff) | |
download | scala-3bb414bd27255d47069dcc406bee695a271c8278.tar.gz scala-3bb414bd27255d47069dcc406bee695a271c8278.tar.bz2 scala-3bb414bd27255d47069dcc406bee695a271c8278.zip |
propagated Stream.flatMap and Android examples
-rw-r--r-- | build.xml | 53 | ||||
-rw-r--r-- | src/library/scala/Stream.scala | 37 | ||||
-rw-r--r-- | test/files/run/streams.scala | 2 |
3 files changed, 59 insertions, 33 deletions
@@ -820,8 +820,6 @@ CLDC ANDROID ============================================================================ --> - <property name="dx.jar" value="${android.home}/tools/lib/dx.jar"/> - <target name="android.sources" description="Create the source directory for Android library" > @@ -843,7 +841,7 @@ ANDROID </copy> </target> - <target name="android.libraries" + <target name="android.libraries" depends="setup.quick, android.sources" description="Builds the Scala library for Android"> <fail message="Android home is not set or could not find android.jar in ${android.home}"> @@ -897,9 +895,9 @@ ANDROID </quick> </target> - <target name="android" depends="android.libraries" + <target name="android.build" depends="android.libraries" description="Convert this project's .class files into .dex files"> - <java jar="${dx.jar}" fork="true" failonerror="true"> + <java jar="${dx.jar}" fork="true" failonerror="true"> <jvmarg value="-Djava.ext.dirs=${android.home}${file.separator}tools${file.separator}lib"/> <jvmarg value="-Xmx384M"/> <arg value="--dex"/> @@ -918,7 +916,22 @@ ANDROID basedir="${android.dir}/lib/library" includes="scala/**/*.class" /> - <!-- examples ? --> + </target> + + <target name="android" depends="init"> + <condition property="android.supported"> + <and> + <not><contains string="${java.version}" substring="1.4"/></not> + <contains string="${java.vendor}" substring="Sun"/> + </and> + </condition> + <if><isset property="android.supported"/><then> + <antcall target="android.build"> + <param name="dx.jar" value="${android.home}/tools/lib/dx.jar"/> + </antcall> + </then><else> + <echo message="The Android library build requires Sun Java 1.5 or newer"/> + </else></if> </target> <!-- =========================================================================== @@ -1751,7 +1764,7 @@ GENERATES A DISTRIBUTION <libset dir="${dist.current.dir}/lib" includes="${lib.jar.name},${dbc.jar.name}"/> <srcset dir="${dist.current.dir}/lib" includes="${libsrc.jar.name},${dbcsrc.jar.name}"/> </quicksbaz> - <!-- Create the Scala developper package --> + <!-- Create the Scala developer package --> <quicksbaz file="${dist.dir}/scala-devel-${version.number}.sbp" adfile="${dist.dir}/scala-devel-${version.number}.advert" @@ -1812,15 +1825,23 @@ GENERATES A DISTRIBUTION <libset dir="${cldc.dir}/lib" includes="*.jar"/> </quicksbaz> <!-- Creates the Android package --> - <quicksbaz - file="${dist.dir}/scala-android-${version.number}.sbp" - adfile="${dist.dir}/scala-android-${version.number}.advert" - name="scala-android" - version="${version.number}" - desc="The Scala Android package contains everything needed to use Scala on Android." - link="${sbaz.universe}/scala-android-${version.number}.sbp"> - <libset dir="${android.dir}/lib" includes="*.jar"/> - </quicksbaz> + <if><isset property="android.supported"/><then> + <copy todir="${dist.current.dir}/doc/scala-android/examples"> + <fileset dir="${docs.dir}/android-examples"> + <exclude name="**/R.java"/> + </fileset> + </copy> + <quicksbaz + file="${dist.dir}/scala-android-${version.number}.sbp" + adfile="${dist.dir}/scala-android-${version.number}.advert" + name="scala-android" + version="${version.number}" + desc="The Scala Android package contains everything needed to use Scala on Android." + link="${sbaz.universe}/scala-android-${version.number}.sbp"> + <libset dir="${android.dir}/lib" includes="*.jar"/> + <docset dir="${dist.current.dir}/doc/scala-android" includes="**"/> + </quicksbaz> + </then></if> </target> <target name="build.archive" depends="build.dist"> diff --git a/src/library/scala/Stream.scala b/src/library/scala/Stream.scala index a771e1d217..1415e3ad06 100644 --- a/src/library/scala/Stream.scala +++ b/src/library/scala/Stream.scala @@ -58,7 +58,7 @@ object Stream { } } - def unapply[A](str: Stream[A]): Option[(A,Stream[A])] = + def unapply[A](str: Stream[A]): Option[(A, Stream[A])] = if(str.isEmpty) None else @@ -113,7 +113,7 @@ object Stream { def range(start: Int, end: Int, step: Int): Stream[Int] = { def loop(lo: Int): Stream[Int] = if (lo >= end) empty - else cons(lo, loop(lo + step)); + else cons(lo, loop(lo + step)) loop(start) } @@ -131,7 +131,7 @@ object Stream { def range(start: Int, end: Int, step: Int => Int): Stream[Int] = { def loop(lo: Int): Stream[Int] = if (lo >= end) empty - else cons(lo, loop(step(lo))); + else cons(lo, loop(step(lo))) loop(start) } @@ -170,7 +170,7 @@ object Stream { * @return the stream composed of n elements all equal to elem */ def make[A](n: Int, elem: A): Stream[A] = - Stream.const(elem).take(n) + Stream.const(elem) take n } /** @@ -237,7 +237,7 @@ trait Stream[+A] extends Seq.Projection[A] { * @param rest The stream that gets appended to this stream */ override def append[B >: A](rest: => Iterable[B]): Stream[B] = - if (isEmpty) rest.toStream else Stream.cons(head, tail.append(rest)) + if (isEmpty) rest.toStream else Stream.cons(head, tail append rest) /** An iterator returning the elements of this stream one by one. @@ -288,7 +288,7 @@ trait Stream[+A] extends Seq.Projection[A] { */ override def take(n: Int): Stream[A] = if (isEmpty || n <= 0) Stream.empty - else Stream.cons(head, tail.take(n-1)) + else Stream.cons(head, tail take (n-1)) /** Returns the stream without its <code>n</code> first elements. * If the stream has less than <code>n</code> elements, the empty stream is returned. @@ -313,7 +313,7 @@ trait Stream[+A] extends Seq.Projection[A] { */ override def takeWhile(p: A => Boolean): Stream[A] = if (isEmpty || !p(head)) Stream.empty - else Stream.cons(head, tail.takeWhile(p)) + else Stream.cons(head, tail takeWhile p) /** Returns the longest suffix of this stream whose first element * does not satisfy the predicate <code>p</code>. @@ -337,7 +337,7 @@ trait Stream[+A] extends Seq.Projection[A] { */ override def map[B](f: A => B): Stream[B] = if (isEmpty) Stream.empty - else Stream.cons(f(head), tail.map(f)) + else Stream.cons(f(head), tail map f) /** Apply the given function <code>f</code> to each element of this stream * (while respecting the order of the elements). @@ -374,11 +374,10 @@ trait Stream[+A] extends Seq.Projection[A] { * predicate <code>p</code>. */ override def forall(p: A => Boolean): Boolean = { - def loop(s: Stream[A]): Boolean = { + def loop(s: Stream[A]): Boolean = if (s.isEmpty) true else if (p(s.head)) loop(s.tail) else false - } loop(this) } @@ -390,11 +389,10 @@ trait Stream[+A] extends Seq.Projection[A] { * satisfies the predicate <code>p</code>. */ override def exists(p: A => Boolean): Boolean = { - def loop(s: Stream[A]): Boolean = { + def loop(s: Stream[A]): Boolean = if (s.isEmpty) false else if (p(s.head)) true else loop(s.tail) - } loop(this) } @@ -433,7 +431,14 @@ trait Stream[+A] extends Seq.Projection[A] { */ override def flatMap[B](f: A => Iterable[B]): Stream[B] = if (isEmpty) Stream.empty - else Stream.fromIterator(f(head).elements) append (tail flatMap f) + else { + val s: Stream[B] = f(head) match { + case x: Stream[_] => x + case y: List[_] => y.toStream + case z => z.toList.toStream + } + s append (tail flatMap f) + } override def toStream = this @@ -466,9 +471,9 @@ trait Stream[+A] extends Seq.Projection[A] { * <code>Stream(a<sub>0</sub>, ..., a<sub>m</sub>) * zip Stream(b<sub>0</sub>, ..., b<sub>n</sub>)</code> is invoked. */ - def zip[B](that: Stream[B]): Stream[Tuple2[A, B]] = + def zip[B](that: Stream[B]): Stream[(A, B)] = if (this.isEmpty || that.isEmpty) Stream.empty - else Stream.cons(Tuple2(this.head, that.head), this.tail.zip(that.tail)) + else Stream.cons((this.head, that.head), this.tail zip that.tail) /** Returns a stream that pairs each element of this stream @@ -477,7 +482,7 @@ trait Stream[+A] extends Seq.Projection[A] { * @return the stream <code>Stream({a<sub>0</sub>,0}, {a<sub>0</sub>,1},...)</code> * where <code>a<sub>i</sub></code> are the elements of this stream. */ - def zipWithIndex: Stream[Tuple2[A, Int]] = + def zipWithIndex: Stream[(A, Int)] = zip(Stream.from(0)) /** Prints elements of this stream one by one, separated by commas */ diff --git a/test/files/run/streams.scala b/test/files/run/streams.scala index 1844fdb6ab..19bb9c943d 100644 --- a/test/files/run/streams.scala +++ b/test/files/run/streams.scala @@ -22,7 +22,7 @@ object Test extends Application { println(s2.dropWhile(_ > 0)) println - val s3 = Stream.range(1, 100000) + val s3 = Stream.range(1, 100000) // ticket #153: Stackoverflow println(s3.length) // ticket #153 |