Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package week5
- object map {
- def mapFun[T, U](xs: List[T], f: T => U): List[U] =
- (xs foldRight List[U]())(f(_)::_) //> mapFun: [T, U](xs: List[T], f: T => U)List[U]
- val nums = List(4,5,5,9) //> nums : List[Int] = List(4, 5, 5, 9)
- mapFun[Int,Int](nums, _+1) //> res0: List[Int] = List(5, 6, 6, 10)
- // deconstruc a list with a pair
- def lengthFun[T](xs: List[T]): Int =
- (xs foldRight 0)((_,y)=>1+y) //> lengthFun: [T](xs: List[T])Int
- lengthFun(nums) //> res1: Int = 4
- val s = "Hello You" //> s : String = Hello You
- s map (c=> List(".",c)) //> res2: scala.collection.immutable.IndexedSeq[List[Any]] = Vector(List(., H),
- //| List(., e), List(., l), List(., l), List(., o), List(., ), List(., Y), List
- //| (., o), List(., u))
- s flatMap (c=> List(".",c)) //> res3: scala.collection.immutable.IndexedSeq[Any] = Vector(., H, ., e, ., l,
- //| ., l, ., o, ., , ., Y, ., o, ., u)
- val ss = Set(4,5,54,4, "roto") //> ss : scala.collection.immutable.Set[Any] = Set(4, 5, 54, roto)
- ss filter (x=> x.isInstanceOf[Int]) //> res4: scala.collection.immutable.Set[Any] = Set(4, 5, 54)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement