r/functionalprogramming • • Mar 10 '19

Question What are your thoughts on design patterns and functional programming?

Hi, I'm gonna write my thesis about design pattern in functional programming. The reason I chose such topic is to explore functional programming. As I had started learning, I saw that many things, which can be described as design patterns are done with monads (different kinds). Although there are desired ways of doing something, I got an impression that there are not much categorized design patterns in functional programming. Do you have any thoughts about design pattern and functional programming? Or maybe how they relates to OOP patterns? Any sources will be much appreciated. Thanks!

25 Upvotes

5 comments sorted by

12

u/qqwy Mar 10 '19 edited Mar 10 '19

'Design' patterns come in different flavours. Some of these make only sense in Object-Oriented programming, others make sense in across different paradigms but quite a few of those are already followed automatically ('path of the least resistance') when programming functionally so they don't usually are mentioned. Yet others are even more general about structuring your application in a larger sense, and these definitely apply regardless of paradigms.

An interesting talk about some of the more general design patterns and how they might apply when programming in the functional language Elixir. <- I highly recommend this talk.

In the end, there is this fun Koan that objects are a poor man's closures and closures are a poor man's objects so it is possible to express concepts from one setting in the other. However, it is quite common that there are more natural ways to do the same thing. For instance, there are quite a few OOP-patterns that are basically a wrapper around the concepts of currying and partial application.

2

u/MarTango Mar 11 '19

Nice talk, thanks for sharing!

15

u/submain Mar 10 '19 edited Mar 10 '19

FP's basic unit, the function, was built to be naturally composable: a referentially transparent computation that receives one argument, returns one result, has an identity, and a composition operator. OOP's basic unit, the object, was built to encapsulate state. Here, composability is an after thought. Hence, OOP design patterns were created as an attempt to establish composition rules in a system that was not built to have them at the core. This is why you won't see "design patterns" in FP: you don't need it, because composition arises naturally from the exploration of the fundamental algebra of the system. Scott Wlaschin has a great talk about that: https://vimeo.com/113588389

5

u/[deleted] Mar 11 '19

Some recommendations of the design patterns book are workarounds to shortcomings of object oriented languages:

  • Program to an interface: the single inheritance model common in object oriented languages is too restrictive.

  • Composition over inheritance: inheritance is not great for sharing code.

Some design patterns themselves are also workarounds for such shortcomings:

  • Factory-related patterns: create an object embedding its contextual requirements, as opposed to creating a monad to implicitly pass around computational context.

  • Prototypes, singletons, state, memento: workarounds to try to make sense of complexity brought in by excessive mutability.

  • Adapter, bridge, decorator, composite: workarounds for inadequate inheritance model.

Only some behavioural patterns make sense in the functional world:

  • Observer: useful when working with actors.

  • Iterator, visitor: available in functional languages as functors, lenses, traversable and foldable abstractions.

Most design patterns are useless to functional programmers, and that their moral equivalent in functional programming languages is usually just called an abstraction, and tends to be more mathematically rigorous.

2

u/videoj Mar 13 '19

Scott Wlaschin of F# for Fun and Profit did a talk on this that you might find interest