These are chat archives for ramda/ramda

22nd
Apr 2018
Richard Van Camp
@RikuVan
Apr 22 2018 11:23
I am going to do a talk on Ramda lenses. I am wondering how many actually use Ramda lenses and why or why not (as opposed to the long list of other ways you can do getters and setters in Ramda)?
Salvatore Tedde
@microcipcip
Apr 22 2018 15:46
@RikuVan lenses are great for Redux immutable state in React, this may be of interest to you: https://vanslaars.io/post/setstate-lenses/
Pedr Browne
@Undistraction
Apr 22 2018 17:34
@RikuVan I'm using lenses more and more. I think one of the reasons why people don't pick them up straight away is that their advantages over R.has and R.assoc are not immediately clear - it just looks like a longer-winded way to do the same thing. Also in case you haven't seen, we have some nice lens-related functions over in Ramda Adjunct including RA.lensTraverse which will be in the 2.7.0 release in the next few days. Article here.
Vesa Karvonen
@polytypic
Apr 22 2018 18:15

@Undistraction Haven't seen Ramda Adjunct before, so I took a quick look. The implementation of lensIso does not compose. Consider the following Partial Lenses example:

const iso = L.compose(L.uriComponent, L.json())
const data = {x: 1}
L.get(iso, L.get(L.inverse(iso), data))
// {x: 1}

Notice how a new isomorphism is formed by composition and the composition can also be inverted. The same cannot be done with the current implementation in Ramda Adjunct. I don't think that the van Laarhoven representation actually allows you to implement total isomorphisms. In Partial Lenses isomorphisms are actually partial isomorphisms, which allows them to be inverted even with the basic van Laarhoven representation.

Vesa Karvonen
@polytypic
Apr 22 2018 18:28
The traversal support in Ramda Adjunct also appears to be, well, wrong. The problem is that the applicative to be used by a traversal should come from the operation and not be a parameter of the traversal. This way the same traversal can be used with different operations. The R.view operation, for example, simply cannot be (correctly) used as such with traversals, because it uses the constant functor. A view like operation on traversals needs to use an applicative rather than a functor. You can build applicatives from monoids, for example, to build fold like operations.
Vesa Karvonen
@polytypic
Apr 22 2018 18:36

Consider the following Partial Lenses example:

const Sum = {
  empty: () => 0,
  concat: (l, r) => l + r
}

const Append = {
  empty: () => [],
  concat: (l, r) => l.concat(r)
}

const ConcatOf = monoid => ({
  map: (f, x) => x,
  of: _ => monoid.empty(),
  ap: (x, y) => monoid.concat(x, y)
})

const traversal = L.compose(L.elems, L.elems)
const data = [[1], [], [2, 3]]

console.log('sum', L.traverse(ConcatOf(Sum), x => x, traversal, data) )
// 6

console.log('append', L.traverse(ConcatOf(Append), x => x, traversal, data) )
// [1, 2, 3]

const Identity = ({
  map: (f, x) => f(x),
  of: x => x,
  ap: (f, x) => f(x)
})

console.log('over', L.traverse(Identity, x => -x, traversal, data) )
// [[-1], [], [-2, -3]]

The same traversal is used with three different applicatives: first with an applicative built from the sum monoid, then with an applicative build from the append monoid, and finally using the identity applicative.

Vesa Karvonen
@polytypic
Apr 22 2018 18:51
Sorry for the spam, but here is a page I recently wrote on Partial Lenses Implementation.