r/functionalprogramming • mod • Oct 31 '20

Why I prefer functional programming

http://www.haskellforall.com/2020/10/why-i-prefer-functional-programming.html
30 Upvotes

6 comments sorted by

View all comments

2

u/drfisk Nov 01 '20

I agree with most of it, but regarding your point "Dependency injection? Use a higher order function instead", I disagree since dependency injection is all about automatic "wiring".

I think HOFs is more of a replacement for most interfaces.

4

u/EsperSpirit Nov 01 '20

Dependency injection is not about automated wiring. Even if you do manual wiring, you can still swap out dependencies if needed, which is usually important for testing isolated components among other things.

If you don't need to swap out dependencies then there is no reason to inject them in the first place.

3

u/drfisk Nov 01 '20

Ah. Yea, I see. I was thinking of dependency injection as a mechanism for automatically resolving dependencies based on types (usually using some sort of reflection) - I guess that term is Dependency Injection Framework.

...But if we're talking about inversion of control in general, I'm all onboard. That was the point I was trying to make about higher order functions replacing simple interfaces. No need to create a whole interface when a simple function or two will do the job.

3

u/ScientificBeastMode Nov 04 '20

Dependency injection is just the “strategy pattern” applied to object construction. And the “strategy pattern” is just an OO reinvention of higher-order functions. So HOF’s are the FP version of dependency injection.

Dependency injection is based on the idea of “inversion of control.” The idea is to make your architecture configurable at a high level, often at the top level of the program. You can specify the concrete implementations of pervasive functionality in a single place, and have that implementation decision cascade across your program.

The same is possible with HOF, and more generally with partially applied functions that are used pervasively.

So the auto-wiring is only a small convenience feature of some OO languages, but the underlying concept is much more fundamental.