### (draft) Functional JS

#### by ilyavf

* This is a post in progress*.

# Some terminology and definitions

## Monoid and Semigroup

### Examples first:

– The integers under addition is a monoid.

– The positive numbers under addition is a semigroup (no identity element).

### Definitions:

** Monoid** is a system (a type plus an operation) that obeys the following three rules [1]:

– Rule 1 (Closure): The result of combining two things is always another one of the things: 1 + 2 = 3

– Rule 2 (Associativity): When combining more than two things, which pairwise combination you do first doesn’t matter: 1 + (2 + 3) = (1 + 2) + 3

– Rule 3 (Identity element): There is a special thing called “zero” such that when you combine any thing with “zero” you get the original thing back: 1 + 0 = 1

** Semigroup** is a system that obeys the first two rules but lacks an identity element.

#Maybe monad

var log = function (x) { console.log(x); }; _.map(log, [1,2,3]) >>> 1 >>> 2 >>> 3 >>> [1, 2, 3] _.map(log, null) >>> TypeError: Cannot read property 'length' of null _.maybe(_.map)(log, [1,2,3]) >>> 1 >>> 2 >>> 3 >>> [1, 2, 3] _.maybe(_.map)(log, null) >>> undefined

In the example above I used version 0.2 of ramda.js. In other libraries maybe monad wraps a value so that you can compose it with other functions. ~~But in ramda it wraps a function that you want to behave correctly if there is a null argument instead of expected.~~

*UPDATE: This was true for version 0.2 of ramda but then the “maybe” method was removed and is no longer a part of the library.*

# Links

– http://fsharpforfunandprofit.com/posts/monoids-without-tears/

– http://buzzdecafe.github.io/code/2014/05/16/introducing-ramda/

– http://osteele.com/sources/javascript/functional/

Hi, one of the Ramda developers here.

Ramda has not implemented the algebraic types yet, although we expect that to be in place quite soon. For the moment, it’s taking place in a separate repository, but we will integrate it as soon as we can. That repository is at https://github.com/buzzdecafe/foolsparadise, and we would love any help other felt like offering.

There is an old `maybe` function in place, but it has nothing to do with Functors or Monads. It’s not very useful; we’ll pull that one out ASAP.

We would expect that when the new types are in place, you could do something like this:

R.map(log, Maybe([1, 2, 3])); // note, `log` better return something…

This would be pretty well equivalent to

var maybeList = Maybe([1, 2, 3]);

maybeList.map(log);

We’re hoping to have something like this in place in the next several weeks.

Hi Scott, thank you for your comment. I was surprised when I saw it since at this point the post is in an early draft stage and was not intended to be published for public ).

I am very glad you told me about Ramda plans to implement the monad, because I am actively looking for a functional library to use for my projects. So far only Oliver Steele’s one seems to be completed and production ready (I only hesitate to use it because its quite old and thus might be not optimized for modern browsers as it could be, as well as it modifies prototypes of the native objects which I don’t like).

And I will definitely check out the foolsparadise project, thank you for sharing. And for the note about the log… )

(I don’t know how I managed to see your article before it was ready. Since we first launched, we’ve been monitoring farily carefully for references to Ramda. Yours made it onto Google or Twitter or somewhere.)

There are other FP libraries available. A short list is included in a talk I gave recently. [1] It should probably have also included wu.js, although I haven’t spent enough time to analyze it myself…

As to other Monad-wielding libraries, there are quite a few at this point, mostly implementing the fantasy-land specification. [2] Probably the most complete is the collection of libraries from Folktale [3]. But there are others on the fantasy-land site, and Brian Lonsdorf’s version [4] is pretty interesting.

If you’re interested, I just published an article on some of the motivation for Ramda:

http://fr.umio.us/why-ramda/

Cheers!

[1]: http://scott.sauyet.com/Javascript/Talk/2014/03/12/#slide-35

[2]: https://github.com/fantasyland/fantasy-land

[3]: https://github.com/folktale

[4]: https://github.com/DrBoolean/pointfree-fantasy