to functional what design patterns are to OOP • Design patterns in OOP: Factory, Singleton, Bridge, Proxy, Flyweight ... • Works but reusability is bound to OOP concepts • FP – different approach
liftN • Example: @ val A = Applicative[Option] A: Applicative[Option] = cats.instances.OptionInstances$$anon$1@598f6c93 @ A.map3(1.some,2.some,4.some){ (a,b,c) = a |+| b |+| c } res3: Option[Int] = Some(7) case class ThreeNumbers(a:Int,b:Int,c:Int) @ A.map3(1.some,2.some,4.some)(ThreeNumbers) res5: Option[ThreeNumbers] = Some(ThreeNumbers(1, 2, 4)) @ A.map3(1.some,2.some,none)(ThreeNumbers) res5: Option[ThreeNumbers] = None • Validations – cats has good support • Futures – scalajs – need to fetch N json payloads and display them all
Group Semigroup Monoid Order PartialOrder Semilattice Alternative Applicative ApplicativeError Apply Bifoldable Bimonad Bitraverse Cartesian Cofl atMap Comonad ContravariantCartesian FlatMap Foldable Functor Inject InvariantMonoidal Monad MonadError MonoidK NotNull Reducible SemigroupK Show ApplicativeAsk Bifunctor Contravariant Invariant Profunctor Strong T raverse Arrow Category Choice Compose Cats T ype Classes kernel core/functor core/arrow core The highlighted type classes ar e the first ones you should learn. They’r e well documented and well-known so it’ s easy to get help. a |+ | b a === b a =!= b a |@ | b a *> b a <* b a <+> b a >>> b a <<< b a > b a >= b a < b a <= b Sync Async Ef fect LiftIO ef fect Some type classes intr oduce symbolic operators. NonEmptyT raverse InjectK CommutativeArrow CommutativeFlatMap CommutativeMonad ApplicativeLayer FunctorLayer ApplicativeLayerFunctor FunctorLayerFunctor ApplicativeLocal FunctorEmpty FunctorListen FunctorT ell FunctorRaise MonadLayer MonadLayerFunctor MonadLayerControl MonadState T raverseEmpty Functor Applicative Monad T raverse mtl MTL type classes do not extend cor e type classes dir ectly , but the effect is similar; the dashed line can be r ead “implies”. • Validation • Xor (right-biased Either) • Arrows • Free monad • State monad • Reader/Writer • Monad transformers • ... Used by other libs • Cats-effect • IO ops • Doobie • DB access • FS2 • Streams • http4s Credit to tpolecat for the image from:https://github.com/tpolecat/cats-infographic/
• Scala books – Funcional programming in Scala (the red book) – Functional and reactive domain modelling • Hearding the cats – blog series • Scalaz • It does not come by itself. Learning FP requires searching the path (MHO)