These are chat archives for ramda/ramda

17th
Jan 2018
BasH
@bas080
Jan 17 2018 02:26
So I like lenses and I use arrays. Thought it would be cool to have a lensFilter(<predFn>). The issue is that I have no idea how to implement this lens so that over would work on only the items that pass the predFn. Two questions. Should I just be using the map function. over(<lensToMyArray>, map(when(<predFn>, assoc(...))))? If I should implement the filterLens, how?
BasH
@bas080
Jan 17 2018 02:56
I thought of this. Instead of passing a value to set you pass a transformFn.
function lensFilter(predFn) {
  return lens(
    array => array.filter(predFn),
    (transformFn, array) => array.map(item => predFn(item) ? transformFn(item) : item)
  )
}
const lt2 = lensFilter(lt(3))

set(lt2, inc, list) // => [1,2,3,5,6],
over(lt2, array => inc, list) // =>[1,2,3,5,6],
BasH
@bas080
Jan 17 2018 05:04
This lens is not composable. Because it's expecting a function as value, and compose passes the get value of the previous lens. This value is likely not a function that describes how to transform each value in the filtered list.
Vesa Karvonen
@polytypic
Jan 17 2018 10:09
@bas080 What you want is called a traversal, which is a different kind of optic. Ramda does not provide traversals, but you can find them in other optics libraries such as Partial Lenses. Here is an example using Partial Lenses.
James Gardner
@james-gardner
Jan 17 2018 12:38
Hi, I'm getting a curried brain from trying to construct something simple like this:
const getRoleFromAcl = acl => role =>
    R.find( 
        R.where({ roles: R.contains(role) } 
    )
 );
R.find is of course a curried function so I'd like to know how to do something like getRoleFromAcl(acl)(role)
Are
@are1000
Jan 17 2018 12:49
What is the input and desired output?
@james-gardner
James Gardner
@james-gardner
Jan 17 2018 12:50
just true or false if the role is found anywhere within the acl
    const acl = [
        {
            roles:['guest', 'member'],
            allows:[
                { resources: ['test'], permissions: ['all'] }
            ]
        }
    ];
Are
@are1000
Jan 17 2018 12:50
You may consider renaming the function to isRoleInAcl instead
let isRoleInAcl = role => R.where({
  roles: R.contains(role)
})

console.log(isRoleInAcl(acl))
Oh wait, scrap that
@james-gardner this works:
let isRoleInAcl = role => R.find(
  R.where({
    roles: R.contains(role)
  })
)
Returns first found ACL, if doesn't match anything returns undefined
If you want just true or false you can do:
Are
@are1000
Jan 17 2018 12:55
replace R.find with R.any
let isRoleInAcl = role => R.any(
  R.where({
    roles: R.contains(role)
  })
)
James Gardner
@james-gardner
Jan 17 2018 12:56
Thanks @are1000 - that's epic!
Are
@are1000
Jan 17 2018 12:56
You can even ditch R.where because it is not needed here
James Gardner
@james-gardner
Jan 17 2018 12:57
oh?
just use { roles ... }
Are
@are1000
Jan 17 2018 12:57
no, wait a second
Are
@are1000
Jan 17 2018 13:13
@james-gardner let isRoleInAcl = R.compose(R.any, R.compose(R.flip(R.propSatisfies)('roles'), R.contains)) that one is tacit :P
James Gardner
@james-gardner
Jan 17 2018 13:15
cripes ...
Are
@are1000
Jan 17 2018 13:15
Oh wait
:facepalm:
Just do that:
let isRoleInAcl = R.compose(R.any, R.flip(R.propSatisfies)('roles'), R.contains)
Forgot that compose is variadic
BasH
@bas080
Jan 17 2018 13:44
@polytypic, thank you for the traversal suggestion.
Francisco
@franciscotln
Jan 17 2018 19:00
@james-gardner perhaps this: https://goo.gl/3VvDYy
Are
@are1000
Jan 17 2018 19:02
@franciscotln He didn't want to find the whole object, he just wanted to test it for existance
Francisco
@franciscotln
Jan 17 2018 19:31