Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created Jun 16, 2015
Embed
What would you like to do?
[info] Loading project definition from /home/shigemk2/projects/github.com/fpinscala/fpinscala/project
[info] Set current project to fpinscala (in build file:/home/shigemk2/projects/github.com/fpinscala/fpinscala/)
> console
[info] Updating {file:/home/shigemk2/projects/github.com/fpinscala/fpinscala/}fpinscala...
[info] Resolving org.scala-lang#scala-library;2.11.5 ...
M[info] Resolving org.scala-lang#scala-compiler;2.11.5 ...
M[info] Resolving org.scala-lang#scala-reflect;2.11.5 ...
M[info] Resolving org.scala-lang.modules#scala-xml_2.11;1.0.3 ...
M[info] Resolving org.scala-lang.modules#scala-parser-combinators_2.11;1.0.3 ...
M[info] Resolving jline#jline;2.12 ...
[info] Done updating.
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.11.5 (OpenJDK 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.
scala> def maybeTwice2(b: Boolean, i: => Int) = {
| lazy val j = i
| if(b) j+j else 0
| }
maybeTwice2: (b: Boolean, i: => Int)Int
scala> myaybemaybeTwice2()t)r)u)e) ){)})))),) ){)})f})a})l})s})e}),}) })})})})})})})})}){})p})r})i})n})t})(}))})')})H)})i)}))}))}))})")})H)})i)})")}),)}) )}))}))});}) })4})2})})})1})+})2})4})})})4})2})
Hires0: Int = 86
scala> maybeTwice2(true, {print("Hi"); 1+42})l("Hi"); 1+42})n("Hi"); 1+42})
Hi
res1: Int = 86
scala> maybeTwice2(true, {println("Hi"); 1+42}), {println("Hi"); 1+42}), {println("Hi"); 1+42}), {println("Hi"); 1+42}), {println("Hi"); 1+42})f, {println("Hi"); 1+42})a, {println("Hi"); 1+42})l, {println("Hi"); 1+42})s, {println("Hi"); 1+42})e, {println("Hi"); 1+42})
res2: Int = 0
scala>
[success] Total time: 410 s, completed 2015/06/16 19:48:32
> console (reverse-i-search)`':  (reverse-i-search)`a': answers/console (reverse-i-search)`an': answers/console (reverse-i-search)`ans': answers/console (reverse-i-search)`answ': answers/console (reverse-i-search)`answe': answers/console (reverse-i-search)`answer': answers/console > answers/console
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.11.5 (OpenJDK 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.
scala>  (reverse-i-search)`':  (reverse-i-search)`i': maybeTwice2(false, {println("Hi"); 1+42}) (reverse-i-search)`im': import fpinscala.gettingstarted._ (reverse-i-search)`imp': import fpinscala.gettingstarted._ (reverse-i-search)`impo': import fpinscala.gettingstarted._ (reverse-i-search)`impor': import fpinscala.gettingstarted._ (reverse-i-search)`import': import fpinscala.gettingstarted._ scala> import fpinscala.gettingstarted._lazlaziness._
import fpinscala.laziness._
scala> val a = Stream()1),)2),))
a: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> a.heheadOption
def headOption: Option[A]
scala> a.headOption
res0: Option[Int] = Some(1)
scala> a.papappend
def append[B >: A](s: => fpinscala.laziness.Stream[B]): Stream[B]
scala> a.append.
append flatMap map takeViaUnfold toListRecursive
asInstanceOf foldRight mapViaUnfold takeWhile toString
drop forAll scanRight takeWhileViaUnfold zip
exists hasSubsequence startsWith takeWhile_1 zipAll
filter headOption tails toList zipWith
find isInstanceOf take toListFast zipWithAll
scala> a.val x = Cons(() => expensive(x), tl)
<console>:10: error: not found: value expensive
val x = Cons(() => expensive(x), tl)
^
<console>:10: error: not found: value tl
val x = Cons(() => expensive(x), tl)
^
scala> val h1 = x.headOption
<console>:10: error: not found: value x
val h1 = x.headOption
^
scala> val h2 = x.headOption
<console>:10: error: not found: value x
val h2 = x.headOption
^
scala> val h2 = x.headOption1 = x.headOptionx = Cons(() => expensive(x), tl)a.headOptionval x = Cons(() => expensive(x), tl)h1 = x.headOptionx = Cons(() => expensive(x), tl)Stream()1),)2),)3),)3))4),)5),)6),)7),)7))8),)9)vStream(1,2,3,4,5,6,7,8,9)aStream(1,2,3,4,5,6,7,8,9)lStream(1,2,3,4,5,6,7,8,9) Stream(1,2,3,4,5,6,7,8,9)aStream(1,2,3,4,5,6,7,8,9) Stream(1,2,3,4,5,6,7,8,9)=Stream(1,2,3,4,5,6,7,8,9) Stream(1,2,3,4,5,6,7,8,9)
a: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> val a = Stream(1,2,3,4,5,6,7,8,9)(1,2,3,4,5,6,7,8,9)(1,2,3,4,5,6,7,8,9)(1,2,3,4,5,6,7,8,9)(1,2,3,4,5,6,7,8,9)(1,2,3,4,5,6,7,8,9)(1,2,3,4,5,6,7,8,9)L(1,2,3,4,5,6,7,8,9)i(1,2,3,4,5,6,7,8,9)s(1,2,3,4,5,6,7,8,9)t(1,2,3,4,5,6,7,8,9),3,4,5,6,7,8,9)3,4,5,6,7,8,9),4,5,6,7,8,9)4,5,6,7,8,9),5,6,7,8,9)5,6,7,8,9),6,7,8,9)6,7,8,9),7,8,9)7,8,9),8,9)8,9),9)9)9) 9)t9)o9) 9)(1 to 9)(1 to 9)(1 to 9)(1 to 9)S(1 to 9)e(1 to 9)q(1 to 9))1)0)0)
a: Seq[scala.collection.immutable.Range.Inclusive] = List(Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100))
scala> val a = Seq(1 to 100))0)0)0))
a: Seq[scala.collection.immutable.Range.Inclusive] = List(Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 1...
scala> val a = Seq(1 to 1000))tream(1,2,3,4,5,6,7,8,9)
a: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> a.toList
res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a.drop()2)
res2: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> a.drop(2).toList
res3: List[Int] = List(3, 4, 5, 6, 7, 8, 9)
scala> a.drop(2).toList(2).toList(2).toList(2).toList(2).toListt(2).toLista(2).toListk(2).toListe(2).toList
res4: List[Int] = List(1, 2)
scala> a.takelWhitakeWhile
takeWhile takeWhileViaUnfold takeWhile_1
scala> a.takeWhile()x) )=)>) )x) )>) )0).toList
res5: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> a.takeWhile(x => x > 0).toList).toList1).toList
res6: List[Int] = List()
scala> a.takeWhile(x => x > 1).toList 1).toList< 1).toList).toList5).toList
res7: List[Int] = List(1, 2, 3, 4)
scala> Stream(1,2,3).take(2).toList
res8: List[Int] = List(1, 2)
scala> Stream(1,2,3).take(2).toList(2).toList(2).toList(2).toList(2).toListd(2).toListr(2).toListo(2).toListp(2).toList
res9: List[Int] = List(3)
scala> Stream()1),)2),)3).ex<console>:1: error: ';' expected but ')' found.
3)
^

scala> Stream(1,2,3).exists()2)
<console>:11: error: type mismatch;
found : Int(2)
required: Int => Boolean
Stream(1,2,3).exists(2)
^
scala> Stream(1,2,3).exists(2))x) )=) ))>) )x) )>) )2)
res11: Boolean = true
scala> Stream(1,2,3).exists(x => x > 2) 2)= 2)= 2)
res12: Boolean = true
scala> Stream(1,2,3).exists(x => x == 2) 2) 2)> 2)
res13: Boolean = true
scala> Stream(1,2,3).exists(x => x > 2))3)
res14: Boolean = false
scala> Stream()1),)2),)3)forA.forA<console>:1: error: ';' expected but ')' found.
3)
^

scala> Stream(1,2,3).exists(x => x > 3)x > 32)(x => x > 2)(x => x > 2)(x => x > 2)(x => x > 2)(x => x > 2)(x => x > 2)f(x => x > 2)o(x => x > 2)r(x => x > 2)A(x => x > 2)l(x => x > 2)l(x => x > 2)
res15: Boolean = false
scala> Stream(1,2,3).forAll(x => x > 2) 2)< 2)
res16: Boolean = false
scala> Stream(1,2,3).forAll(x => x < 2),).forAll(x => x < 2)4).forAll(x => x < 2),).forAll(x => x < 2)4).forAll(x => x < 2),).forAll(x => x < 2)4).forAll(x => x < 2)).forAll(x => x < 2)).forAll(x => x < 2)).forAll(x => x < 2)5).forAll(x => x < 2),).forAll(x => x < 2)6).forAll(x => x < 2)
res17: Boolean = false
scala> Stream(1,2,3,4,5,6).forAll(x => x < 2))7)
res18: Boolean = true
scala> Stream()1),)2),)3),)4),)5).takeWhi<console>:1: error: ';' expected but ')' found.
5)
^

scala> Stream.()()1),)2),)3),)4)5),))),)5).val a = Stream()1),)2),)3),)4),)5)
a: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> a.
append flatMap map takeViaUnfold toListRecursive
asInstanceOf foldRight mapViaUnfold takeWhile toString
drop forAll scanRight takeWhileViaUnfold zip
exists hasSubsequence startsWith takeWhile_1 zipAll
filter headOption tails toList zipWith
find isInstanceOf take toListFast zipWithAll
scala> a.foltaketake
take takeViaUnfold takeWhile takeWhileViaUnfold takeWhile_1
scala> a.takeWhitakeWhile
takeWhile takeWhileViaUnfold takeWhile_1
scala> a.takeWhile_1()x) )=)>) ) ))x) )>) )2)
res19: fpinscala.laziness.Stream[Int] = Empty
scala> a.takeWhile_1(x => x > 2).toList
res20: List[Int] = List()
scala> a.takeWhile_1(x => x > 2).toList).toList0).toList
res21: List[Int] = List(1, 2, 3, 4, 5)
scala> a.headheadOption
res22: Option[Int] = Some(1)
scala> Stream().oeaheadOption
res23: Option[Nothing] = None
scala> List()1),)2),)3).headforlldRi<console>:1: error: ';' expected but ')' found.
3)
^

scala> List(1,2,3).foldRi()ght()N)O)n)e)ne)one)()h)e)))h)e)a)d):):))) ):):) )a.map().toList
res24: List[Int] = List(1, 2, 3, 4, 5)
scala> a.map()x) )=)>) )x) )+) )1).toList
res25: List[Int] = List(2, 3, 4, 5, 6)
scala> a.map(x => x + 1).toListfilter()x) )=)>) )x) )+) )1))))>) )2).toList
res26: List[Int] = List(3, 4, 5)
scala> a.filter(x => x > 2).toListappappend
def append[B >: A](s: => fpinscala.laziness.Stream[B]): Stream[B]
scala> a.append()S)t)r)e)a)m)()))1))
res27: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> a.append(Stream(1)).toLIst
<console>:12: error: value toLIst is not a member of fpinscala.laziness.Stream[Int]
a.append(Stream(1)).toLIst
^
scala> a.append(Stream(1)).toLIststist
res29: List[Int] = List(1, 2, 3, 4, 5, 1)
scala> a
res30: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> a.toLIst
<console>:12: error: value toLIst is not a member of fpinscala.laziness.Stream[Int]
a.toLIst
^
scala> a.toLIstitoList
toList toListFast toListRecursive
scala> a.toList
res32: List[Int] = List(1, 2, 3, 4, 5)
scala> a.toListIst.append(Stream(1)).toList
res33: List[Int] = List(1, 2, 3, 4, 5, 1)
scala> a.flatMap()x) )=)>))))))flatMap
def flatMap[B](f: A => fpinscala.laziness.Stream[B]): Stream[B]
scala> a.flatMap()x) )=)>) )S)t)r)e)a)m)()))x)) ))+)) ))1))
res34: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> a.flatMap(x => Stream(x + 1)).toList
res35: List[Int] = List(2, 3, 4, 5, 6)
scala> Stream(1,2,3,4).map(_ + 10).filter(_ % 2 == 0).toList
res36: List[Int] = List(12, 14)
scala> a
res37: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> a.find()5)
<console>:12: error: type mismatch;
found : Int(5)
required: Int => Boolean
a.find(5)
^
scala> a.find(5)x => x == 5)
res39: Option[Int] = Some(5)
scala> a.find(x => x == 5))6)
res40: Option[Int] = None
scala> Stream(1,2,3,4).map(_ + 10).filter (_ % 2 == 0)
res41: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> Stream(1,2,3,4).map(_ + 10).filter (_ % 2 == 0).toList
res42: List[Int] = List(12, 14)
scala> Stream.onesvStream.onesaStream.oneslStream.ones Stream.onesaStream.ones-Stream.ones Stream.onesStream.onesStream.ones Stream.ones=Stream.ones Stream.ones
a: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> val a = Stream.onesa.toa.toList.take()3)
[info] Loading project definition from /home/shigemk2/projects/github.com/fpinscala/fpinscala/project
[info] Set current project to fpinscala (in build file:/home/shigemk2/projects/github.com/fpinscala/fpinscala/)
>  (reverse-i-search)`':  (reverse-i-search)`c': answers/console > answers/console
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.11.5 (OpenJDK 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.
scala>  (reverse-i-search)`':  (reverse-i-search)`i': a.toList.take(3) (reverse-i-search)`im': import fpinscala.laziness._ (reverse-i-search)`imp': import fpinscala.laziness._ (reverse-i-search)`impo': import fpinscala.laziness._ (reverse-i-search)`impor': import fpinscala.laziness._ (reverse-i-search)`import': import fpinscala.laziness._ scala> import fpinscala.laziness._
import fpinscala.laziness._
scala> val a = Stream.ones
a: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> a.take()3).toList
res0: List[Int] = List(1, 1, 1)
scala> a.take(3).toList).toList1).toList0).toList0).toList0).toList0).toList
res1: List[Int] = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
scala> onea.exissts()_) )%) )2) )!)=) )0)
res2: Boolean = true
scala> a.undlflold.unfoldS.unfoldt.unfoldr.unfolde.unfolda.unfoldm.unfoldold()S)t)r)e)a)m)()))1))()x) )=)>) )O)p)t)i)o)n)()))x)) ))+)) ))1))
<console>:11: error: type mismatch;
found : Int(1)
required: String
Stream.unfold(Stream(1))(x => Option(x + 1))
^
scala> Stream.unfold(Stream(1))(x => Option(x + 1))+ 1)) 1))1))))
<console>:11: error: type mismatch;
found : Option[fpinscala.laziness.Stream[Int]]
required: Option[(?, fpinscala.laziness.Stream[Int])]
Stream.unfold(Stream(1))(x => Option(x))
^
scala> Stream.unfold(Stream(1))(x => Option(x))(x))(x))(x))(x))(x))(x)),)) ))1))
<console>:11: error: type mismatch;
found : (fpinscala.laziness.Stream[Int], Int)
required: Option[(?, fpinscala.laziness.Stream[Int])]
Stream.unfold(Stream(1))(x => (x, 1))
^
scala> Stream.unfold(Stream(1))(x => (x, 1))O(x, 1))p(x, 1))t(x, 1))i(x, 1))o(x, 1))n(x, 1))(x, 1))(x, 1))(x, 1))(x, 1))(x, 1))(x, 1))S(x, 1))o(x, 1))m(x, 1))e(x, 1))
<console>:11: error: type mismatch;
found : Some[(fpinscala.laziness.Stream[Int], Int)]
required: Option[(?, fpinscala.laziness.Stream[Int])]
Stream.unfold(Stream(1))(x => Some(x, 1))
^
scala> Stream.unfold(Stream(1))(x => Some(x, 1))((x, 1)))))
<console>:11: error: type mismatch;
found : Some[(fpinscala.laziness.Stream[Int], Int)]
required: Option[(?, fpinscala.laziness.Stream[Int])]
Stream.unfold(Stream(1))(x => Some((x, 1)))
^
scala> Stream.unfold(Stream(1))(x => Some((x, 1)))(x, 1))
<console>:11: error: type mismatch;
found : Some[(fpinscala.laziness.Stream[Int], Int)]
required: Option[(?, fpinscala.laziness.Stream[Int])]
Stream.unfold(Stream(1))(x => Some(x, 1))
^
scala> Stream.unfold(Stream(1))(x => Some(x, 1)) 1)) 1))1)),)) ))x)): => Some(1, x)) => Some(1, x))I => Some(1, x))n => Some(1, x))t => Some(1, x))
<console>:1: error: ')' expected but '(' found.
Stream.unfold(Stream(1))(x: Int => Some(1, x))
^
<console>:1: error: ';' expected but ')' found.
Stream.unfold(Stream(1))(x: Int => Some(1, x))
^
scala> Stream.unfold(Stream(1))(x: Int => Some(1, x))(x: Int => Some(1, x))) => Some(1, x))
<console>:11: error: type mismatch;
found : Some[(Int, Int)]
required: Option[(?, fpinscala.laziness.Stream[Int])]
Stream.unfold(Stream(1))((x: Int) => Some(1, x))
^
scala> Stream.unfold(Stream(1))((x: Int) => Some(1, x)), x))x, x))))S))t))r))e))a))m))()))))x)))
<console>:11: error: type mismatch;
found : Int => Some[(Int, fpinscala.laziness.Stream[Int])]
required: fpinscala.laziness.Stream[Int] => Option[(?, fpinscala.laziness.Stream[Int])]
Stream.unfold(Stream(1))((x: Int) => Some(x, Stream(x)))
^
scala> Stream.unfold(Stream(1))((x: Int) => Some(x, Stream(x)))x)))x)))x)))x)))x)))x)))x)))Sx)))tx)))rx)))ex)))ax)))mx)))(x))))))1)))
<console>:11: error: type mismatch;
found : Int => Some[(Int, fpinscala.laziness.Stream[Int])]
required: fpinscala.laziness.Stream[Int] => Option[(?, fpinscala.laziness.Stream[Int])]
Stream.unfold(Stream(1))((x: Int) => Some(x, Stream(1)))
^
scala> Stream.unfold(Stream(1))((x: Int) => Some(x, Stream(1))) Stream(1))) Stream(1)))S Stream(1)))t Stream(1)))r Stream(1)))e Stream(1)))a Stream(1)))m Stream(1)))( Stream(1)))x Stream(1)))) Stream(1))), Stream(1))) => Some(Stream(x), Stream(1))) => Some(Stream(x), Stream(1))) => Some(Stream(x), Stream(1))) => Some(Stream(x), Stream(1))) => Some(Stream(x), Stream(1))) => Some(Stream(x), Stream(1)))x => Some(Stream(x), Stream(1)))
res12: fpinscala.laziness.Stream[fpinscala.laziness.Stream[fpinscala.laziness.Stream[Int]]] = Cons(<function0>,<function0>)
scala> Stream.unfold(Stream(1))(x => Some(Stream(x), Stream(1))).toList
[info] Loading project definition from /home/shigemk2/projects/github.com/fpinscala/fpinscala/project
[info] Set current project to fpinscala (in build file:/home/shigemk2/projects/github.com/fpinscala/fpinscala/)
> answers/console
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.11.5 (OpenJDK 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.
scala> Stream.unfold(Stream(1))(x => Some(Stream(x), Stream(1))).toList(x: Int) => Some(x, Stream(1)))x))) (reverse-i-search)`':  (reverse-i-search)`i': Stream.unfold(Stream(1))(x => Option(x)) (reverse-i-search)`im': import fpinscala.laziness._ (reverse-i-search)`imp': import fpinscala.laziness._ (reverse-i-search)`impo': import fpinscala.laziness._ (reverse-i-search)`impor': import fpinscala.laziness._ (reverse-i-search)`import': import fpinscala.laziness._ scala> import fpinscala.laziness._
import fpinscala.laziness._
scala> import fpinscala.laziness._Stream.unfold(Stream(1))(x => Some(Stream(x), Stream(1))).toListttoListatoListktoListetoList(toList)toList3)toList.toList
res0: List[fpinscala.laziness.Stream[fpinscala.laziness.Stream[Int]]] = List(Cons(<function0>,<function0>), Cons(<function0>,<function0>), Cons(<function0>,<function0>))
scala> Stream.unfold(Stream(1))(x => Some(Stream(x), Stream(1))).take(3).toList
res1: fpinscala.laziness.Stream[fpinscala.laziness.Stream[fpinscala.laziness.Stream[Int]]] = Cons(<function0>,<function0>)
scala> Stream.unfold(Stream(1))(x => Some(Stream(x), Stream(1))).take(3)ome(Stream(x), Stream(1))).take(3)me(Stream(x), Stream(1))).take(3)e(Stream(x), Stream(1))).take(3)(Stream(x), Stream(1))).take(3)Stream(x), Stream(1))).take(3)tream(x), Stream(1))).take(3)ream(x), Stream(1))).take(3)eam(x), Stream(1))).take(3)am(x), Stream(1))).take(3)m(x), Stream(1))).take(3)(x), Stream(1))).take(3)x), Stream(1))).take(3)), Stream(1))).take(3), Stream(1))).take(3) Stream(1))).take(3)Stream(1))).take(3)tream(1))).take(3)Stream.unfold(Stream(1))(x => Some(Stream(x), Stream(1))).take(3).toListx), Stream(1))).take(3).toListx), Stream(1))).take(3).toListx), Stream(1))).take(3).toListx), Stream(1))).take(3).toListx), Stream(1))).take(3).toListx), Stream(1))).take(3).toListx), Stream(1))).take(3).toList, Stream(1))).take(3).toList
res2: List[fpinscala.laziness.Stream[Int]] = List(Cons(<function0>,<function0>), Cons(<function0>,<function0>), Cons(<function0>,<function0>))
scala> Stream.unfold(Stream(1))(x => Some(x, Stream(1))).take(3).toList
res3: fpinscala.laziness.Stream[fpinscala.laziness.Stream[Int]] = Cons(<function0>,<function0>)
scala> Stream.unfold(Stream(1))(x => Some(x, Stream(1))).take(3).)(x => Some(x, Stream(1))).take(3).)(x => Some(x, Stream(1))).take(3).)(x => Some(x, Stream(1))).take(3).)(x => Some(x, Stream(1))).take(3).)(x => Some(x, Stream(1))).take(3).)(x => Some(x, Stream(1))).take(3).)(x => Some(x, Stream(1))).take(3).)(x => Some(x, Stream(1))).take(3).)(x => Some(x, Stream(1))).take(3).1)(x => Some(x, Stream(1))).take(3).
| toList
<console>:11: error: type mismatch;
found : Some[(Int, fpinscala.laziness.Stream[Int])]
required: Option[(?, Int)]
Stream.unfold(1)(x => Some(x, Stream(1))).take(3).
^
scala> toListStream.unfold(1)(x => Some(x, Stream(1))).take(3).toList)(x => Some(x, Stream(1))).take(3).toList0)(x => Some(x, Stream(1))).take(3).toList., Stream(1))).take(3).toListt, Stream(1))).take(3).toListo, Stream(1))).take(3).toListS, Stream(1))).take(3).toListt, Stream(1))).take(3).toListr, Stream(1))).take(3).toListi, Stream(1))).take(3).toListn, Stream(1))).take(3).toListg, Stream(1))).take(3).toListtream(1))).take(3).toListream(1))).take(3).toListeam(1))).take(3).toListam(1))).take(3).toListm(1))).take(3).toList(1))).take(3).toList1))).take(3).toList))).take(3).toList)).take(3).toListx)).take(3).toList+)).take(3).toList1)).take(3).toList
res5: List[String] = List(0, 1, 2)
scala> Stream.unfold(0)(x => Some(x.toString, x+1)).take(3).toList, x+1)).take(3).toList, x+1)).take(3).toList, x+1)).take(3).toList, x+1)).take(3).toList, x+1)).take(3).toList, x+1)).take(3).toListI, x+1)).take(3).toListn, x+1)).take(3).toListt, x+1)).take(3).toList
res6: List[Int] = List(0, 1, 2)
scala> Stream.unfold(0)(x => Some(x.toInt, x+1)).take(3).toList
res7: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> Stream.unfold(0)(x => Some(x.toInt, x+1)).take(3)b1)).take(3)1)).take(3), x+1)).take(3), x+1)).take(3), x+1)).take(3), x+1)).take(3), x+1)).take(3), x+1)).take(3).toList
res8: List[Int] = List(0, 1, 2)
scala> Stream.unfold(0)(x => Some(x, x+1)).take(3).toList: => Some(x, x+1)).take(3).toList => Some(x, x+1)).take(3).toList: => Some(x, x+1)).take(3).toList => Some(x, x+1)).take(3).toList => Some(x, x+1)).take(3).toList => Some(x, x+1)).take(3).toList)(x => Some(x, x+1)).take(3).toList")(x => Some(x, x+1)).take(3).toLista)(x => Some(x, x+1)).take(3).toList")(x => Some(x, x+1)).take(3).toList
res9: List[String] = List(a, a1, a11)
scala> val a = Stream()1),)2),)2),)))3),)4),)5)
a: fpinscala.laziness.Stream[Int] = Cons(<function0>,<function0>)
scala> a.hashasSubsequence()S)t)r)e)a)m)()4)))
res10: Boolean = true
scala> a.hasSubsequence(Stream(4))))6))
res11: Boolean = false
scala> a.hasSubsequence(Stream(6))6))6))6))6))6))6))6)))
<console>:12: error: type mismatch;
found : Int(6)
required: fpinscala.laziness.Stream[?]
a.hasSubsequence(6)
^
scala> a.hasSubsequence(6))S)t)r)e)a)m)()))2)),))3)),))4))
res13: Boolean = true
scala> a.hasSubsequence(Stream(2,3,4))))5))
res14: Boolean = false
scala> a.StstarstartsWith()3)
<console>:12: error: type mismatch;
found : Int(3)
required: fpinscala.laziness.Stream[?]
a.startsWith(3)
^
scala> a.startsWith(3))S)t)r)e)a)m)()))3))
res16: Boolean = false
scala> a.startsWith(Stream(3))13)),3))23)),3))33))))
res17: Boolean = true
scala> a.startsWith(Stream(1,2,3))a.Stream.ones.toListtake()3).toList
res18: List[Int] = List(1, 1, 1)
scala> Stream.ones.take(3).toList).toList4).toList).toList5).toList).toList1).toList0).toList
res19: List[Int] = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
scala> Stream.ones.take(10).toList.take(10).toList.take(10).toList.take(10).toList.take(10).toList .take(10).toListc .take(10).toListo .take(10).toListn .take(10).toLists .take(10).toList .take(10).toList .take(10).toList .take(10).toList .take(10).toListconsWrapper .take(10).toList .take(10).toList .take(10).toList .take(10).toList .take(10).toList .take(10).toList .take(10).toList .take(10).toListt .take(10).toLista .take(10).toListn .take(10).toListt .take(10).toList.take(10).toList(.take(10).toList).take(10).toList1).take(10).toList
res20: List[Int] = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
scala> Stream.constant(1).take(10).toList).take(10).toList1).take(10).toList0).take(10).toList
res21: List[Int] = List(10, 10, 10, 10, 10, 10, 10, 10, 10, 10)
scala> Stream.constant(10).take(10).toList).take(10).toList).take(10).toList9).take(10).toList9).take(10).toList
res22: List[Int] = List(99, 99, 99, 99, 99, 99, 99, 99, 99, 99)
scala> Stream.constant(99).take(10).toList).take(10).toList).take(10).toList").take(10).toListh).take(10).toListo).take(10).toListg).take(10).toListe).take(10).toListh).take(10).toListo).take(10).toListg).take(10).toListe).take(10).toListf).take(10).toListu).take(10).toListg).take(10).toLista).take(10).toList).take(10).toList).take(10).toList).take(10).toList).take(10).toList").take(10).toList
res23: List[String] = List(hogehoge, hogehoge, hogehoge, hogehoge, hogehoge, hogehoge, hogehoge, hogehoge, hogehoge, hogehoge)
scala> Stream.constant("hogehoge").take(10).toList).take(10).toList).take(10).toList).take(10).toList).take(10).toList).take(10).toList).take(10).toList).take(10).toList).take(10).toList).take(10).toList).take(10).toListL).take(10).toListi).take(10).toLists).take(10).toListt).take(10).toList().take(10).toList)).take(10).toList1)).take(10).toList,)).take(10).toList2)).take(10).toList,)).take(10).toList3)).take(10).toList
res24: List[List[Int]] = List(List(1, 2, 3), List(1, 2, 3), List(1, 2, 3), List(1, 2, 3), List(1, 2, 3), List(1, 2, 3), List(1, 2, 3), List(1, 2, 3), List(1, 2, 3), List(1, 2, 3))
scala> Stream.constant(List(1,2,3)).take(10).toList(List(1,2,3)).take(10).toList(List(1,2,3)).take(10).toList(List(1,2,3)).take(10).toList(List(1,2,3)).take(10).toList(List(1,2,3)).take(10).toList(List(1,2,3)).take(10).toList(List(1,2,3)).take(10).toList(List(1,2,3)).take(10).toListf(List(1,2,3)).take(10).toListo(List(1,2,3)).take(10).toListr(List(1,2,3)).take(10).toList(List(1,2,3)).take(10).toList(List(1,2,3)).take(10).toListr(List(1,2,3)).take(10).toListo(List(1,2,3)).take(10).toListm(List(1,2,3)).take(10).toList1).tototake()1)0).toList
res25: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> Stream.from(1).take(10).toList).take(10).toList2).take(10).toList
res26: List[Int] = List(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
scala> Stream.from(2).take(10).toList).take(10).toList3).take(10).toList0).take(10).toList
res27: List[Int] = List(30, 31, 32, 33, 34, 35, 36, 37, 38, 39)
scala> Stream.from(30).take(10).toList).take(10).toList).take(10).toList").take(10).toLists).take(10).toListt).take(10).toList).take(10).toList).take(10).toListh).take(10).toListo).take(10).toListg).take(10).toListe).take(10).toList").take(10).toList
<console>:11: error: type mismatch;
found : String("hoge")
required: Int
Stream.from("hoge").take(10).toList
^
scala> Stream.from("hoge").take(10).toListStream.from(30).take(10).toList2).take(10).toList30).take(10).toList(30).take(10).toList(30).take(10).toList(30).take(10).toList(30).take(10).toListf(30).take(10).toListi(30).take(10).toListb(30).take(10).toList.take(10).toList.take(10).toList.take(10).toList.take(10).toList.take(10).toListb.take(10).toList
<console>:11: error: value fib is not a member of object fpinscala.laziness.Stream
Stream.fib.take(10).toList
^
scala> Stream.fib.take(10).toLists.take(10).toList
res30: List[Int] = List(0, 1, 1, 2, 3, 5, 8, 13, 21, 34)
scala> Stream.unfoldViaMap (reverse-i-search)`':  (reverse-i-search)`u': a.hasSubsequence(Stream(2,3,5)) (reverse-i-search)`un': Stream.unfold("a")(x => Some(x, x+1)).take(3).toList (reverse-i-search)`unf': Stream.unfold("a")(x => Some(x, x+1)).take(3).toList (reverse-i-search)`unfo': Stream.unfold("a")(x => Some(x, x+1)).take(3).toList (reverse-i-search)`unfol': Stream.unfold("a")(x => Some(x, x+1)).take(3).toList (reverse-i-search)`unfold': Stream.unfold("a")(x => Some(x, x+1)).take(3).toList scala> Stream.unfold("a")(x => Some(x, x+1)).take(3).toList)(x => Some(x, x+1)).take(3).toList)(x => Some(x, x+1)).take(3).toList)(x => Some(x, x+1)).take(3).toList0)(x => Some(x, x+1)).take(3).toListV(0)(x => Some(x, x+1)).take(3).toListi(0)(x => Some(x, x+1)).take(3).toLista(0)(x => Some(x, x+1)).take(3).toListM(0)(x => Some(x, x+1)).take(3).toLista(0)(x => Some(x, x+1)).take(3).toListp(0)(x => Some(x, x+1)).take(3).toList
res31: List[Int] = List(0, 1, 2)
scala> Stream.unfoldViaMap(0)(x => Some(x, x+1)).take(3).toList(0)(x => Some(x, x+1)).take(3).toList(0)(x => Some(x, x+1)).take(3).toList(0)(x => Some(x, x+1)).take(3).toListF(0)(x => Some(x, x+1)).take(3).toListo(0)(x => Some(x, x+1)).take(3).toListl(0)(x => Some(x, x+1)).take(3).toListd(0)(x => Some(x, x+1)).take(3).toList
res32: List[Int] = List(0, 1, 2)
scala> Stream.fibsViaUnfold()0))1)0).totake()3).toList
<console>:11: error: fpinscala.laziness.Stream[Int] does not take parameters
Stream.fibsViaUnfold(10).take(3).toList
^
scala> Stream.fibsViaUnfold(10).take(3).toList).take(3).toList).take(3).toList).take(3).toList.take(3).toList
res34: List[Int] = List(0, 1, 1)
scala> Stream.fibsViaUnfold.take(3).toList).toList1).toList0).toList
res35: List[Int] = List(0, 1, 1, 2, 3, 5, 8, 13, 21, 34)
scala> Stream.fibsViaUnfold.take(10).toListStream(1,2,3,4,5).zipWith(Stream(1,2,3,4))((x,y) => x + y).toList
res36: List[Int] = List(2, 4, 6, 8)
scala> Stream(1,2,3,4,5).zipWith(Stream(1,2,3,4))((x,y) => x + y).toList y).toList* y).toList
res37: List[Int] = List(1, 4, 9, 16)
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment