daily pastebin goal
19%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. scala> List(1,2,3,4).iterator.map((x: Int) => println(x))
  2.    
  3. 1
  4. 2
  5. 3
  6. 4
  7.    
  8. List(1,2,3,4).map((x: Int) => println(x))
  9. List(1,2,3,4).foreach((x: Int) => println(x))
  10. List(1,2,3,4).iterator.foreach((x: Int) => println(x))
  11.    
  12. def lazyMap[T, U](coll: Iterable[T], f: T => U) = new Iterable[U] {
  13.   def iterator = coll.iterator map f
  14. }
  15.  
  16. scala> lazyMap(List(1,2,3,4), (x: Int) => x + 1)
  17. res4: java.lang.Object with Iterable[Int] = (2, 3, 4, 5)
  18.    
  19. scala> List(1,2,3,4).iterator.map((x: Int) => println(x))
  20. res0: Iterator[Unit] = non-empty iterator
  21.  
  22. // nothing actually happened yet, just remember to do this printing things
  23.  
  24. scala> res0.toList
  25. 1
  26. 2
  27. 3
  28. 4
  29. res1: List[Unit] = List((), (), (), ())
  30.    
  31. scala> { lazyMap(List(1,2,3,4), {(x: Int) => println(x); x + 1}); 1 }
  32.    
  33. scala> val itr = List(1,2,3).iterator
  34. itr: Iterator[Int] = non-empty iterator
  35.    
  36. scala> val mappedItr = itr.map((x: Int) => println(x))
  37. mappedItr: Iterator[Unit] = non-empty iterator
  38.    
  39. scala> mappedItr.next
  40. 1
  41.    
  42. scala> mappedItr.next
  43. 2
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top