Advertisement
Guest User

Untitled

a guest
Jun 8th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.72 KB | None | 0 0
  1. class Ls [T](hd : Option[T], tl : Ls[T]){
  2.     def foldr[ACC](acc: ACC,fun:(ACC,Option[T])=>ACC):ACC = {
  3.         hd match {
  4.             case None => acc
  5.             case Some(x) => tl.foldr(fun(acc,Some(x)),fun)
  6.         }
  7.     }
  8.     def concat(x: T):Ls[T] = {
  9.         new Ls[T](Some(x),this)
  10.     }
  11.     override def clone() = {
  12.         this.foldr[Ls[T]](new Ls[T](None,null),(acc: Ls[T],x:Option[T]) => x match{
  13.             case None => acc
  14.             case Some(y) => acc.concat(y)
  15.         })
  16.     }
  17.     def len():Int = {
  18.         foldr[Int](0,(acc: Int,x:Option[T]) =>
  19.             x match{
  20.                 case None => acc
  21.                 case Some(y) => acc+1
  22.             }
  23.         )
  24.     }
  25. }
  26.  
  27.  
  28. object Main extends App{
  29.     println("test")
  30.     val ls = new Ls[Int](Some(1),new Ls[Int](Some(2),new Ls[Int](None,null)))
  31.     println(ls.concat(1).clone().len())
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement