Guest User

Untitled

a guest
Mar 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. //import scalaz._
  2. //import scalaz.syntax.arrow._
  3. //
  4. //def eff[F[_, _]: Arrow, A, B](a: F[A, B])(effFn: F[B, Unit]): F[A, B] = {
  5. // val z = Arrow[F].arr(identity[B]) &&& Arrow[F].arr(identity[B])
  6. //
  7. // a >>> z >>> effFn.second[B].mapsnd(_._1)
  8. //}
  9.  
  10.  
  11. import cats.arrow._
  12. import cats.syntax.functor._
  13. import cats.syntax.profunctor._
  14. import cats.syntax.strong._
  15. import cats.syntax.compose._
  16. import scala.language.higherKinds
  17.  
  18. def effImpl[F[_, _]: Arrow, A, B](a: F[A, B])(eff: F[B, Unit]): F[A, B] = {
  19. val forkedA: F[A, (B, B)] =
  20. a.rmap(mkTuple)
  21.  
  22. val effOnFirst: F[(B, B), (Unit, B)] =
  23. eff.first
  24.  
  25. forkedA >>> effOnFirst >>> discardFirst
  26. }
  27.  
  28. def mkTuple[A](a: A): (A, A) =
  29. (a, a)
  30.  
  31. def discardFirst[F[_, _]: Arrow, A, B]: F[(A, B), B] = {
  32. Arrow[F].lift(_._2)
  33. }
  34.  
  35. implicit class ArrowEff[F[_, _]: Arrow, A, B](fab: F[A, B]) {
  36. def eff(eff: F[B, Unit]): F[A, B] = {
  37. effImpl(fab)(eff)
  38. }
  39. }
  40.  
  41.  
  42. object Abc {
  43. import cats.instances.all._
  44.  
  45.  
  46. def a = { x: Int => x + 1 }
  47. .andThen(_ * 5)
  48. .andThen(_ ^ 2)
  49. .eff(x => println(s"Current value: $x"))
  50. .andThen(Seq.fill(_)('Z'))
  51. .andThen(_.mkString)
  52. .eff(println(_))
  53. .andThen(_.length)
  54. .eff(println(_))
  55. .apply(1)
  56.  
  57.  
  58. def b = { x: Int => x + 1 }
  59. .>>>(_ * 5)
  60. .>>>(_ ^ 2)
  61. .eff(x => println(s"Current value: $x"))
  62. .>>>(Seq.fill(_)('Z'))
  63. .>>>(_.mkString)
  64. .eff(println(_))
  65. .>>>(_.length)
  66. .eff(println(_))
  67. .apply(1)
  68.  
  69.  
  70. def c = { x: Int => x + 1 }
  71. .map(_ * 5)
  72. .map(_ ^ 2)
  73. .eff(x => println(s"Current value: $x"))
  74. .map(Seq.fill(_)('Z'))
  75. .map(_.mkString)
  76. .eff(println(_))
  77. .map(_.length)
  78. .eff(println(_))
  79. .apply(1)
  80. }
  81.  
  82. Abc.a
  83. Abc.b
  84. Abc.c
Add Comment
Please, Sign In to add comment