You have functions with different dependencies, and want to compose them.
Remove dependencies from function arguments, and remodel the functions as partially-curried on those dependencies.
The reader monad lets us encode dependencies directly into the type, while providing composability.
Reader makes functions composable via a common super-dependency:
An example dependency, and a function that depends on it:
Another example dependency, and a function that depends on it:
An example program with two different dependencies;
Injecting the dependencies to run the program:
This file is literate Scala, and can be run using Codedown:
$ curl https://earldouglas.com/posts/itof/di-to-reader.md | codedown scala | xargs -0 scala -nc -e the networkHost can be found at localhost:8080 the database can be found at jdbc:h2:mem:db