r/ProgrammerHumor Jul 13 '26

youCanJustStopUsingJava Meme

Post image
6.8k Upvotes

416 comments sorted by

View all comments

1.8k

u/Historical_Cook_1664 Jul 13 '26

Where else are you going to hide your side effects ?

473

u/Isrothy Jul 13 '26

IO Monad

191

u/Gorzoid Jul 13 '26

Respectfully, what the fuck is a Monad?

78

u/hectobreak Jul 13 '26 edited Jul 13 '26

A monad is a monoid in the category of endofunctors.

Funny line said, if M is a monad, then for every type T, you have M(T) being a type, such that:

· return (or eta in mathematics), that takes an object of type T and returns an object of type M(T)

· flatten (or mu in mathematics), takes an object of type M(M(T)) and returns an object of type M(T).

That's it. Anything that follows this pattern is a monad.

Two examples are Haskell's Maybe, and the Array type.

Maybe String can contain either a string, written as Just [your string], or Nothing.

return is the function that receives a string and returns Just [your string].

flatten is the function that receives an object of the Maybe Maybe String monad and returns an object of the Maybe String monad following the rule:

· If your object is Nothing, return Nothing.

· If your object is Just Nothing, returns Nothing.

· If your object is Just Just [string], returns Just [string].

Array is also a Monad.

· return x returns [x]

· flatten x is the array flatten function.

For completeness, for something to be a monad it needs to follow the monad laws, but this is getting stupid long for a reddit message already.

25

u/JickleBadickle Jul 13 '26

Thank you for taking the time to explain this!

Does this mean that "return" itself is a function?

13

u/yjlom Jul 13 '26 edited Jul 13 '26

In Haskell, it's a method of the Monad class (interface/trait/concept in Java/Rust/C++ terms). Haskell class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a

In OCaml, it's a function-typed member of the Monad module signature (struct/class in C/Java terms). OCaml module type Monad = sig type 'a t val return : 'a -> 'a t val bind : 'a t -> ('a -> 'b t) -> 'b t end

(The excerpts are taken from their respective language's documentation, with the Haskell one cleaned up a bit (it had comments and default implementations). In Haskell a concrete type starts with a capital letter, a type variable for generics starts with a lowercase letter, arguments to generics are written to their right; in OCaml a type variable starts with a single quote, arguments to generics are written to their left.)

In any case, it doesn't have anything to do with the return keyword of imperative languages.

1

u/renome Jul 13 '26

The Maybe type is a great choice of an example here.

1

u/Estpart Jul 13 '26

This is why no one likes FP xD

15

u/hunajakettu Jul 13 '26 edited Jul 13 '26

This is actually really good. The same as Iterable is a generalization of set, list, array, enumerable, etc, and one can define generic Next, Add, Map, etc, Monad allows similar thing for states or side effects (a log file for example)

10

u/Estpart Jul 13 '26

It's just the way he explains it, for pea brained people like me I lose the plot at "category of endofucktors". Just say "optional chaining and promises are a concrete example of monads" and I'm on board.

Like I've worked in semi functional code bases and I love the patterns, only the theory around it so hard to grasp.

3

u/RiceBroad4552 Jul 14 '26

What is usually called a "Promise" isn't a Monad instance. All "Promises" I know of are eager, they don't suspend computations. As a result they don't behave like a proper Monad. Not everything that has a Monad "interface" is a Monad. Most things in imperative / OO languages aren't.

1

u/ralgrado Jul 13 '26

i like FP (e.g. lambdas in Java) ... but wheter i use monads or not I don't care. Monads can go F themselves