Skip to content
0:00 / 0:00

#scala#extensibleeffects#cakepattern

Extensible Effects: A Leaner Cake for Purely Functional Code – Edmund Noble

Typelevel

2.6K subscribers • 8 videos on Hallway

YouTube
  • Recommended by 1 person: 1 in engineering communities.
  • Presented by Edmund Noble

Advanced. Deep dive for people who know the field.

2.6K views9 years ago1 recommendation

Programming languagesSoftware designFunctional programmingSoftware architectureDesign patterns

Edmund Noble discusses extensible effects and tagless final encodings as alternatives to the Cake Pattern for dependency injection in Scala.

Purely functional algorithms and data structures are one thing, but purely functional program architectures are a completely different beast. Constructors and dependency injection frameworks compete in the object oriented landscape; in Scala, we have the Cake Pattern as well. Regardless, we aren’t doing purely functional programming just to pass around mutable objects with state, and the Cake Pattern has a similar problem with hiding effects from the user. Extensible effects provide not only a uniform interface to monadic effects, but a dependency injection mechanism that is aware of them. Finally tagless encodings provide an object-oriented view of the problem, which compared to the initial ADT encoding can be not only easier to understand for newcomers but more efficient. * Event * Typelevel Summit New York City, March 2017: typelevel.org/event/2017-03-summit-nyc/ * About Speaker * Edmund loves Scala and code in general, and he is intimately interested in how people code. Purely functional programming is his passion; he is particularly interested in new ways to constrain and abstract in programs. He contributes to a couple of the libraries under the typelevel umbrella, including cats and eff, the last of which he maintains. * Licensing * This recording is licensed under CC-BY 4.0 (creativecommons.org/licenses/by/4.0/).

Why it's here

1 person recommends this

What people say. Recommended as a helpful guide on using the free monad and extensible effects to model mutable resources cleanly.

u/tpolecatTypelevelin r/scala • Recommends

On “What is the FP way to model an object that is unavoidably mutable, like an http connection or something.”

The dense answer is: create a data type defining the operations you would like to perform *if you had a db connection* or other dangerous thing, then write your program in terms of the free monad over that type. This gives you a pure API that prevents leakage and forbids operations that you don't wish to expose. You can then interpret this program into a Kleisli arrow (a function `Connection => MA` for some monad `M` (`Task` or `IO` typically)) and run it to actually perform your real work. This isolates direct handling of the managed resource. I gave a talk about this strategy a few years ago. You may find it helpful. Note that the Coyoneda encoding I mention isn't necessary anymore ……