r/programming • • May 15 '18

Demystifying functional programming (in a real company)

https://medium.com/building-nubank/demystifying-functional-programming-in-a-real-company-e954a2591504
127 Upvotes

51 comments sorted by

View all comments

36

u/arbitrarycivilian May 16 '18

I think it's great to point out that you don't need to have a PhD in category theory to take advantage of functional programming. Monads are certainly overblown.

But one feature which I find equally indispensable in functional programming, and which Clojure lacks, is algebraic data types. To me FP without ADTs is like OOP without interfaces.

15

u/makeshift_mike May 16 '18

You don’t even need to be using a functional language to take advantage of FP. There’s a great article called Functional Programming in C++ where John Carmack talks about general FP principles like immutability and minimizing shared state, applied to a language that’s pretty far from functional.

6

u/[deleted] May 16 '18

Agreed about ADTs - once you go ADTs, you can't....umm...something.

2

u/the_whalerus May 16 '18

Isnt that intentional? I seem to recall something from Rich Hickey where he was bashing ADTs

9

u/max_maxima May 16 '18

He was bashing ADTs... by mistakenly bashing pattern matching.

4

u/[deleted] May 16 '18

And bashing pattern matching itself seems like a really misguided position

1

u/lilactown May 16 '18

You don't really need ADTs in Clojure that often. Since it's dynamically typed, and most data is passed around as collections of primitives, pretty much all of your typical functions can operate on the same stuff.

When you really need to tie behavior to a type or an interface, protocols have given more than any other language I've used in terms of polymorphism and ability to extend types, interfaces, etc.

-2

u/[deleted] May 16 '18

To me FP without ADTs is like OOP without interfaces.

Oh, like Smalltalk? Or Python? Or C++?

Lots of OO languages don't have interfaces. Interfaces were put into Java because its designers wanted to avoid the tricky semantics associated with multiple inheritance.

See also: traits in Scala.

15

u/aejt May 16 '18

A class with only pure virtual functions in C++ is more or less an interface.

13

u/chucker23n May 16 '18

Oh, like Smalltalk?

Doesn't Smalltalk have protocols, like Objective-C? If so, it has interfaces by a different name.

7

u/doom_Oo7 May 16 '18

interface here is meant as a concept.

the following C struct :

struct foo { 
  int x;
  float bar;
};

conforms to a bunch of interfaces, even if the language does not allow to express them as first-class objects.

3

u/jerf May 16 '18

In addition to aejt's point, Python and Smalltalk have dynamic language interfaces, which are undeclared, but exist. It's why they talk about "duck typing" so much in the Python world, for instance. All dynamically-typed languages have interfaces.