Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. def unfold[A, S](z: S)(f: S => Option[(A, S)]): Stream[A] = f(z) match {
  2. case Some((a, s)) => Stream.cons(a, unfold(s)(f))
  3. case None => Stream.empty
  4. }
  5.  
  6. trait F[C] {
  7.  
  8. def map[A](a: List[C])(f: C => A): Stream[A] = {
  9.  
  10. def foo(li: List[C]): Option[(A, List[C])] = li match {
  11. case head :: body => Some(f(head) -> body)
  12. case Nil => None
  13. }
  14.  
  15. unfold(a)(foo)
  16. }
  17.  
  18. }
  19.  
  20. // Проверка
  21. val stream = new F[Int]{}.map(List(1, 2, 3))(_ * 2)
  22. stream.mkString(",")
  23. //res0: String = 2,4,6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement