Ramda was my first library of functional programming and now I compare Sanctuary with Ramda. Maybe some my questions are too stupid, but I did not find best way to learn Sanctuary.
My question is follow:
How can I map
array in nested property of Object?
Ramda code for it:
const addOneForNumbers = R.over(R.lensProp('numbers'), R.map(R.add(1)))
addOneForNumbers({ numbers: [1, 2, 3, 4, 5] })
// {"numbers": [2, 3, 4, 5, 6]}
Is Sanctuary have tolls for the task?
In this case a Sanctuary-only solution exists, but lenses would be needed in the general case.
This particular problem could be solved in this way:
> S.map(S.map(S.add(1)), {numbers: [1, 2, 3, 4, 5]})
{numbers: [2, 3, 4, 5, 6]}
This relies on {numbers: [1, 2, 3, 4, 5]}
being a member of StrMap (Array Number)
. Since string maps are functors, we can map over the string map to access the array, then map over the array to access the numbers.
If the object were to have other fields of different types it would not be a string map. The type of {active: true, numbers: [1, 2, 3, 4, 5]}
is { active :: Boolean, numbers :: Array Number }
, a record type. Record types do not support mapping, so we'd require something like R.over
and R.lensProp
to apply a transformation to the value of the numbers
field. Sanctuary does not yet provide any functions for working with lenses. If you're interested in seeing these functions added to the library, consider commenting on sanctuary-js/sanctuary#177. 🔍