R.compose(
R.flip(R.all)(permissions),
R.flip(R.contains),
R.prop('permissions')
)
let isSuperset = (x, y) => R.equals(R.intersection(x, y), x)
isSuperset(A, B) === true
means that B is a superset of A
const xs = [ {id: 1}, {id: 2}, {id: 4}, {id: 4} ];
const ys = [ {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5} ];
R.equals(R.innerJoin(R.eqProps('id'), xs, ys), xs); // true
const ws = [5, 5, 4, 3, 2, 1];
const zs = [1 ,2 ,3 ,4 , 5, 6];
R.equals(R.innerJoin(R.equals, ws, zs), ws); // true
const xs = [ {id: 1}, {id: 2}, {id: 3}, {id: 4} ];
const ys = [ {id: 5}, {id: 4}, {id: 3}, {id: 2}, {id: 1}, {id: 0} ];
R.all(R.contains(R.__, ys), xs); // true
For example:
const segment = getRole('USER_ADMIN')(acl);
The 'acl' argument
getRole
?
getRole
in R.curry
const getAdmin = getRole("USER_ADMIN")
const getRole = (role) => R.find(
R.where({ roles: R.contains(role) })
);
getRole
with the list as an argument
const getRole = R.curry((role, list) => R.find(
R.where({ roles: R.contains(role) })
)(list));
getRole("USER_ADMIN", acl)
getAdmin
by partially applying it, like above
getRole
partially applyed with acl
?
R.__
const getRoleFromAcl = getRole(R.__, acl);
R.difference([{a: 1}, {b: 2}], [{a: 1}, {c: 3}]) //=> [{b: 2}]
useWith(
compose(
mergeAll,
map(apply(objOf)),
difference
), [toPairs, toPairs])({a: 10, b: 20}, {a: 10, b: 30})
[toPairs,toPairs]
)
sparse update
back. I have a function for it, but I thought this would be a good starting function for FP
id
of nested objects when a subproperty changes, etc.
take a look on
const isObject = compose(equals('Object'), type)
const isNumeric = v => !isNaN(v - parseFloat(v))
const groupObjBy = curry(compose(
map(fromPairs),
useWith(groupBy, [useWith(__, [last]), toPairs]),
))
const diffObj = compose(
evolve({
difference: map(({leftValue, rightValue}) => {
if (isObject(leftValue) && isObject(rightValue)) {
return diffObj(leftValue, rightValue)
} else {
return {leftValue, rightValue}
}
}),
common: map(prop("leftValue")),
onlyOnLeft: map(prop("leftValue")),
onlyOnRight: map(prop("rightValue"))
}),
groupObjBy(cond([
[
both(has("leftValue"), has("rightValue")),
compose(ifElse(apply(equals), always("common"), always("difference")), values)
],
[has("leftValue"), always("onlyOnLeft")],
[has("rightValue"), always("onlyOnRight")],
])),
useWith(mergeWith(merge), [map(objOf("leftValue")), map(objOf("rightValue"))])
)
This is advanced version of https://github.com/ramda/ramda/wiki/Cookbook#diffobjs---diffing-objects-similar-to-guavas-mapsdifference
My version works correctly with nested objects
Ctrl+F
able, so helpful
map
, filter
, reduce
, prop,
path,
pipeand
compose`, etc...
map(objOf("leftValue"))
and why you would even think of it, nice
map(x => ({"leftValue": x}))
the same
groupObjBy
beast
R.is
, can we use this with Array
?
R.is(Object, new String('')); //=> true
Array.isArray
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
R.is
R.is
because of .constuctor
and instanceOf
. I know there are gotcha's with those and i don't fully understand them
const input = {
first: [
{id: 'a'},
{id: 'b'},
{id: 'c'}
],
second: [
{id: 'a'},
{id: 'b'},
{id: 'c'}
],
third: [
{id: 'a'},
{id: 'b'},
{id: 'c'}
],
};
const output = [
{ first: 'a', second: 'b', third: 'c' },
{ first: 'a', second: 'b', third: 'c' },
{ first: 'a', second: 'b', third: 'c' },
];
{ first: 'a', second: 'a', third: 'a' }
?
const output = [
{ first: 'a', second: 'a', third: 'a' },
{ first: 'b', second: 'b', third: 'b' },
{ first: 'c', second: 'c', third: 'c' },
];
{id: "d"}
?
toPairs
and pulling out the keys and values that way would technically be safer than using keys
and values
, but every implementation preserves object ordering so that will work fine
converge(compose(fromPairs, zip), [keys, o(transpose, values)])(input)
is a tacit version if you're into that sort of thing
zip
and transpose
are probably the two keys to that problem
R.o
compose
and it would work the same, it's just shorter
o
is more restrictive than compose
though
R.o
confuses me, sometimes it doesn't work the same as R.compose
even when it's passed a single param
o(fromPairs, zip)
would break the function because zip
takes 2 parameters