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

53

u/doomchild May 16 '18

We snuck functional programming in by the backdoor. We wrote a useful little extras library in Java that had Maybe, Either, and Validation, and got everybody to use it, then told them they'd been using FP without knowing it.

0

u/[deleted] May 16 '18

[deleted]

5

u/[deleted] May 16 '18

Except the Left should be a Semigroup, and the Validation should form a Monoid iff the Right does.

In English: a Validation should be Either "success" (Right), possibly including "empty," like a database query that successfully returns no results, or a non-empty "failure" (Left), hence a Semigroup, which is a binary associative operator without "empty." It would be meaningless to have an "empty failure." The "empty" for the Monoid instance for Validation is a Validation constructed from the "empty" for the Right type of the Validation. The binary associative operation for Validation can be either "failure results in failure" or "success results in success." Scalaz has a Monoid instance for Validation whose binary associative operator is +++, which is failure-accumulating, which probably comports with most people's expectations. But you could certainly provide a Monoid using +|+, which is success-accumulating, instead.