KleveR_

Untitled

Nov 8th, 2020
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. object Zad1 extends App {
  2.  
  3.   def sum(l: List[Option[Int]]): Option[Int] = {
  4.     @annotation.tailrec
  5.     def sumTailRec(list: List[Option[Int]], accum: Option[Int]): Option[Int] = {
  6.       list match {
  7.         case Nil     => accum
  8.         case head :: tail => sumTailRec(tail, (accum ++ head).reduceOption(_ + _))
  9.       }
  10.     }
  11.     sumTailRec(l, None)
  12.   }
  13.  
  14.   val lista: List[Option[Int]] = List(Some(1), Some(2), Some(3), None)
  15.   println(sum(lista))
  16.  
  17. }
  18. // #########################################
  19.  
  20. object Zad2 extends App {
  21.  
  22.   def compose[A, B, C](f: A => B, g: B => C): A => C = arg => g(f(arg))
  23.  
  24.   def prod[A,B,C,D](f: A => C, g: B => D): (A, B) => (C, D) = (x,y) => (f(x),g(y))
  25.  
  26.   def lift[A,T](op: (T,T) => T)(f: A => T, g: A => T): A => T = x => op(f(x), g(x))
  27.  
  28.   def fun1(a: Int): Int = a + 2
  29.   def fun2(b: Int): Int = b - 6
  30.   def funop(c : Int, d: Int): Int = c * d
  31.  
  32.   println(compose(fun1, fun2)(2))
  33.   println(prod(fun1, fun2)(2, 1))
  34.   println(lift(funop)(fun1, fun2)(2))
  35.  
  36. }
  37.  
  38.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×