3 ms·
Let's look at Dependency Injection using a functional analogy. Imagine you wrote a function "add1" which adds 1 to every element of a list and returns a new li
by kazinator 12y ago
Let's look at Dependency Injection using a functional analogy.
Imagine you wrote a function "add1" which adds 1 to every element of a list and returns a new list. This is inferior because it has a hard-coded dependency on the specific function object (lambda (arg) (+ 1 arg)).
The dependency inversion way is to write a function "map" which is "configured" by taking a functional argument. Then you call (map (lambda (arg) (+ 1 arg)) ...) if you want to add 1 to every element, but of course you can use other functions.
Now if we actually read the source code of this map, it is confusing compared to the code of add1. "What does this do? It calls the function that is passed in, but that could be anything! Why can't this damn thing just call a specific function? All this program ever needs is to add 1 to a list; why build this whole useless pattern?"