These are chat archives for ramda/ramda

3rd
Dec 2017
George Katsanos
@gkatsanos
Dec 03 2017 10:28
Hey everyone. I just watched this : https://www.youtube.com/watch?v=JRzly-t4Boc . I would lie if I would say this doesn't look cool and sex (already tempted to use it) BUT, and excuse my naivety, is it really increasing performance to replace native Array methods (already kindda functional) like .map and .filter with Ramda functions?
Thanks in advance for your replies!
Stefano Vozza
@svozza
Dec 03 2017 11:14
Do you have a performance problem in your code?
Barry G
@bgits
Dec 03 2017 12:45
It does not increase performance, but there are other reasons to used auto curried functions.
George Katsanos
@gkatsanos
Dec 03 2017 14:27
@svozza I'm just asking myself on a theoretical level - how can an external library improve my APP by replacing native methods (especially the new Array.prototype ones)
Scott Sauyet
@CrossEye
Dec 03 2017 16:23

@gkatsanos:

wouldn't using an external library instead of native bring a performance penalty to my APP?

Often enough, no. The simpler APIs of Ramda's functions allowed for simpler implementations, which meant that when we wrote them, they were often faster than their native counterparts. Of course, many of the JS engines have moved on since then, and I really don't know how they compare now.

We have added more to our functions as we went, and performance may have suffered.

But to me, the reason for using these functions have very little to do with speed.

  • map is not just "run a function against every element of an array and return an array of the results." Our function is much more general:

    const square = n => n * n
    map(square, [1, 2, 3]) //=> [1, 4, 9], as exptected, but also:
    map(square, Just(7)) //=> Just(49)
    map(square, Nothing) //=> Nothing
    map(square, Right(5)) //=> Right(25)
    map(square, Left('error')) //=> Left('error')
    map(square, Task((_, __) => 10)) //=> Task((_, __) => 100)
    map(square, n => n + 1)(3) //=> 16
  • map makes obvious the strong connection between an expression involving a value and a function which takes that value as a parameter:

    const withTax = map(addTax, itemsBought)
    
    // getWithTax :: [LineItem] -> [LineItem]
    const getWithTax = map(addTax)

    Compare this to native:

    const withTax = itemsBought.map(addTax)
    
    // getWithTax :: [LineItem] -> [LineItem]
    const getWithTax = itemsBought => itemsBought.map(addTax)
  • The function supplied to map is simpler, so you don't get as many silly results such as:

    ['1', '2', '3'].map(parseInt) //=> [1, NaN, NaN]
    map(parseInt, ['1', '2', '3']) //=> [1, 2, 3]
  • Functional programming is about many things, but chiefly it's about working with lawful, pure functions against immutable data. Ramda functions make this easy. This is native:

    const tutone = [8, 6, 7, 5, 3, 0, 9]
    totone.sort((a, b) => a - b) //=> [0, 3, 5, 6, 7, 8, 9]
    // But now 
    tutone //=> [0, 3, 5, 6, 7, 8, 9]

    This is Ramda:

    const tutone = [8, 6, 7, 5, 3, 0, 9]
    const jenny = sort((a, b) => a - b, tutone) 
    jenny //=> [0, 3, 5, 6, 7, 8, 9]
    // But still
    tutone //=> [8, 6, 7, 5, 3, 0, 9]

In fact, I wrote a whole article on Why Ramda?