Advertisement
Lusien_Lashans

Compose Scala

Mar 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.85 KB | None | 0 0
  1. package exercises
  2.  
  3. //  while, return и var запрещены
  4. //  0.5 балла
  5. object Compose {
  6.  
  7.   def compose[A, B, C](f: B => C, g: A => B): A => C = {
  8.     f compose g
  9.   }
  10.  
  11.   //  реализуйте функцию
  12.   def composeFunc[R, T](f: R => T => T, g: (T => T) => R, h: R => String) = {
  13.     compose(h, compose(g , f))
  14.   }
  15.  
  16.   //  с помощью функции composeFunc и for'а выведите рез-т
  17.   val f: (Int => Double => Double) = (i: Int) => { (d: Double) => i * (math.Pi * d) }
  18.   val g: ((Double => Double) => Int) = (f: (Double => Double)) => f(42).toInt
  19.   val h: Int => String = (i: Int) => (i * 2).toString
  20.   val ints = List(1, 2, 3, 4)
  21.  
  22.   val rslt: List[String] =
  23.     for {
  24.       i <- ints
  25.     } yield (composeFunc(f, g, h).apply(i))
  26. }
  27.  
  28. object ComposeApp extends App {
  29.   import Compose._
  30.   println(rslt)
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement