Skip to content

Instantly share code, notes, and snippets.

@dacr
Created March 17, 2013 09:39
Show Gist options
  • Save dacr/5180825 to your computer and use it in GitHub Desktop.
Save dacr/5180825 to your computer and use it in GitHub Desktop.
list, view and stream - An example to understand the differences
#!/bin/sh
exec scala -deprecation -nocompdaemon "$0" "$@"
!#
val l=List(0, 1, 2)
var x=1
val m1=l.map(_ +x) // c1
val v1=l.view.map(_+x) // c2
val s1=l.toStream.map(_+x) // c3
x=2
println("--------- x=2 ---------")
println("the list :"+m1) // List(1, 2, 3) c4
println("from view :"+v1.toList) // List(2, 3, 4) c5
println("from stream :"+s1.toList) // List(1, 3, 4) c6
println("--------- x=3 ---------")
x=3
println("the list :"+m1) // List(1, 2, 3) c7
println("from view :"+v1.toList) // List(3, 4, 5) c8
println("from stream :"+s1.toList) // List(1, 3, 4) c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment