Hacker News new | ask | show | jobs
by norir 549 days ago
The weird thing is the word monad and the technical formalism. Practically speaking, I really don't see why I decent junior can't understand

  x = [1, 2, 3].flatMap(el => if (el % 2 == 0) [] else [ el ]) // x = [1, 3]
vs

  x = []
  for (i = 1, i <= 3, i++) { if (i % 2) == 0) x.push(i) } // x = [1, 3]
or some such. There are advantages and disadvantages to either style of programming. I actually don't like pure functional or imperative style because of the respective tradeoffs each make (functional style requires either significant runtime overhead or heavyweight compiler optimizations and imperative gets messy very quickly and is typically difficult to reason about as program size expands).
2 comments

The problem is, that's not "monad"; that's the implementation of the monad interface specifically on an array. That's like claiming to understand "iterators" because you understand how to write an iterator implementation on an array, but then trying to take that understanding to an iterator that yields the primes in order. If you think "iterator == presenting each element of an array in order", and nothing else, you don't actually understand iterators, because presenting each element of an array in order is an iterator, not the concept of iterators. "flatMap" is a monad, it is not the concept of monad.
I think any language that has a flatmap type construct would also have filter functions that would be a lot clearer to read and write...

    (remove-if #'evenp '(1 2 3))
vs

    (mapcan
      (lambda (n) (if (evenp n) '() (list n)))
      '(1 2 3))
in Common Lisp for example.

(Yes I know the original is just a contrived example that you shouldn't read too much into, but...)