Why are fairy tales and ramanda so different?

I am learning javascript FP by reading the DrBoolean book .

I was looking for a functional programming library. I found Ramda and a folk tale. Both claim to be a functional programming library.

But they are so different

  • It seems that Ramda has utilities for working with the list: display, reduction, filtering and pure functions: curry, compilation. It does not contain anything to deal with a monad, a functor.

  • However, Folktale does not contain any utility for the list or functions. It seems that some algebraic structures are implemented in javascript, such as monad: Maybe, Task ...

In fact, I found more libraries, they all seem to fall into two categories. We emphasize that Lodash is very similar to Ramda. Fantasy Land, senseless fantasy like a fairy tale.

Can these very different libraries be called functional, and if so, what makes each of them a functional library?

+72
javascript functional-programming
Oct 08 '15 at 22:58
source share
2 answers

Functionality

There is no clear boundary between what defines functional programming or a functional library. Some functions of functional languages ​​are built into Javascript:

  • First-class features of a higher order
  • Lambdas / Anonymous functions, with closure

Others can be executed in Javascript with some caution:

  • Invariance
  • Link transparency

The rest are part of ES6 and are partially or fully available right now:

  • Compact, even concise features
  • Perform recursion using tail call optimization

And there are many others that are really inaccessible to Javascript:

  • Pattern matching
  • lazy appreciation
  • Homoiconicity

Then the library can choose and choose which functions it is trying to support and yet it is reasonable to call it "functional."

Specifications Fantasy-land

Fantasy-land is a specification of a number of standard types transferred from mathematical category theory and abstract algebra to functional programming, such as Monoid , Functor and Monad . These types are rather abstract and perhaps more familiar concepts are spreading. For example, functors are containers that can be map thrown using a function, the way that an array can be map redone using Array.prototype.map .

Folk tale

Folktale is a collection of types that implement various parts of the Fantasy-land specification and a small collection of helper functions. These types are like Maybe , Either , Task (very similar to what is called "Future" elsewhere, and a more legitimate cousin of promise), and Validation

Folktale is perhaps the most famous implementation of the Fantasy-land specification, and it is highly respected. But there is no such thing as a final or standard implementation; fantasy-land defines only abstract types, and the implementation, of course, should create such concrete types. Folktale claims to be a functional library: it provides data types that are commonly found in functional programming languages ​​that greatly simplify functional mode programming.

This example from the Folktale documentation shows how this can be used:

 // We load the library by "require"-ing it var Maybe = require('data.maybe') // Returns Maybe.Just(x) if some `x` passes the predicate test // Otherwise returns Maybe.Nothing() function find(predicate, xs) { return xs.reduce(function(result, x) { return result.orElse(function() { return predicate(x)? Maybe.Just(x) : /* otherwise */ Maybe.Nothing() }) }, Maybe.Nothing()) } var numbers = [1, 2, 3, 4, 5] var anyGreaterThan2 = find(function(a) { return a > 2 }, numbers) // => Maybe.Just(3) var anyGreaterThan8 = find(function(a) { return a > 8 }, numbers) // => Maybe.Nothing 

Ramda

Ramda (disclaimer: I am one of the authors) is a completely different type of library. It does not provide you with new types. 1 Instead, it provides functions that make it easier to work with existing types. It is built around the concepts of compiling smaller functions into larger ones, working with immutable data, and preventing side effects.

Ramda works especially on lists, but also on objects, and sometimes on lines. He also delegates many of his challenges in such a way that he will interact with Folktale or other Fantasy-land realities. For example, the Ramda map function works similarly to the function on Array.prototype , therefore R.map(square, [1, 2, 3, 4]); //=> [1, 4, 9, 16] R.map(square, [1, 2, 3, 4]); //=> [1, 4, 9, 16] . But since Folktale Maybe implements the Fantasy-land Functor specification, which also points to a map, you can also use the Ramda map with it:

 R.map(square, Maybe.Just(5)); //=> Maybe.Just(25); R.map(square, Maybe.Nothing); //=> Maybe.Nothing 

Ramda claims to be a functional library that simplifies function compilation by never mutating your data and not representing just pure functions. A typical use of Ramda would be to create a more complex function, making it smaller, as shown in the Ramda philosophy article

 // :: [Comment] -> [Number] var userRatingForComments = R.pipe( R.pluck('username') // [Comment] -> [String] R.map(R.propOf(users)), // [String] -> [User] R.pluck('rating'), // [User] -> [Number] ); 

Other libraries

In fact, I found more libraries, all of them fall into two categories. emphasize, lodash is very similar to Ramda. Fantasy-land, pointfree-fantasy is like a fairy tale.

This is not very accurate. First of all, Fantasy-land is just a specification that libraries can decide to implement for different types. Folktale is one of many implementations of this specification, probably the best, certainly one of the most mature. Pointfree-fantasy and ramda-fantasy are different, and there are many more .

Underscore and lodash are superficially similar to Ramda because they are grab-bag libraries, providing many functions with much less connectivity than something like Folktale. And even specific functionality often overlaps with Ramda. But at a deeper level, Ramda has very different problems from these libraries. Ramda's closest cousins ​​are probably libraries like FKit , Fnuc, and Wu.js.

Bilby is in its own category, providing a number of tools, such as those provided by Ramda, as well as some types compatible with Fantasy-land. (The author of Bilby is also the original author of Fantasy Land.)

Your challenge

All these libraries have the right to be called functional, although they vary greatly in functional approach and degree of functional commitment.

Some of these libraries work really well together. Ramda should work well with Folktale or other Fantasy-land realities. Since their problems hardly coincide, they really do not conflict, but Ramda does this enough to make the interaction relatively smooth. This is probably less true for some other combinations you might choose, but the simple ES6 ES6 syntax can also distract some of the pain from integration.

The choice of library, or even the style of the library to use, will depend on your project and your preferences. There are many good options, and the number is growing, and many of them are improving significantly. This is a good time for functional programming in JS.




1 Well, there is a side project of ramda-fantasy that does something similar to what Folktale does, but it is not part of the main library.

+142
Oct 14 '15 at 16:00
source share

A wonderful answer even after 3 years has passed.

0
Jan 13 '19 at 2:20
source share



All Articles