Functional Injection

When writing object-oriented software, I often use dependency injection:

  • To compose high-level functions from lower-level capabilities: My account management service uses repositories and validation services, not their implementation.

  • to isolate components from their dependencies: my account management service uses its dependencies through interfaces so that I can swap implementations, a layout for unit testing, etc.

What patterns exist in functional programming languages ​​to achieve these goals?

edit: the commentator correctly asks: "but how can I simply skip functions?" I think the following comment about grouping functions strikes a nail on the head - a service is a set of functions with a common set of dependencies that I can process as an atomic group.

In Clojure, it seems that the protocols allow this well, but I'm really interested in how the problem is solved more generally ...

+7
source share
3 answers

On a small scale, things like currying and as-parameters functions reduce the need for module dependencies. On a large scale, things like Standard ML Functors are very useful for this purpose. Racket has a system called units, which also does a good job of this.

+3
source

Some time ago I read a post describing how dependency injection can be seen as currying in functional programming. I think this is very interesting, and it gives a good look at this topic.

+8
source

I developed a small library that I found useful for DI in a functionally-inspired (JavaScript) environment, and this is nothing special, just a method that I like.

0
source

All Articles