by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jan 31 2019 22:17
    CrossEye commented #2779
  • Jan 31 2019 21:04
    ArturAralin commented #2779
  • Jan 31 2019 20:08
    CrossEye commented #2779
  • Jan 31 2019 18:56
    buzzdecafe commented #2631
  • Jan 31 2019 18:09
    ArturAralin commented #2779
  • Jan 31 2019 16:18
    CrossEye commented #2779
  • Jan 31 2019 16:10
    CrossEye commented #2631
  • Jan 31 2019 16:06
    CrossEye commented #2777
  • Jan 31 2019 14:44
    ArturAralin opened #2779
  • Jan 31 2019 07:39
    inferusvv commented #2631
  • Jan 31 2019 03:07
    sespinozj commented #2771
  • Jan 31 2019 02:33
    machad0 commented #2771
  • Jan 31 2019 02:26
    JeffreyChan commented #2777
  • Jan 30 2019 14:30
    CrossEye closed #2777
  • Jan 30 2019 12:13
    vanyadymousky updated the wiki
  • Jan 30 2019 01:42
    JeffreyChan commented #2777
  • Jan 29 2019 21:06
    vanyadymousky updated the wiki
  • Jan 29 2019 16:28
    CrossEye commented #2777
  • Jan 29 2019 15:50
    mbostock commented #2772
  • Jan 29 2019 15:48
    CrossEye commented #2772
Brad Compton (he/him)
@Bradcomp
@kpruehss You could look into using allPass or anyPass to construct your filtering function
Karsten Pruehss
@kpruehss
@Bradcomp awesome. Just pulled up the documentation on it and I reckon allPass, composed with propEq looks exactly like what I'll need. I'll play around in a REPL with it to get my head around it. Thank you!
Brad Compton (he/him)
@Bradcomp
Nice! Lemme know if you get stuck. :bowtie:
Karsten Pruehss
@kpruehss

@Bradcomp I had a play around with it and came up with this:

const typeFilter = R.propEq('requestTypeId')
const contactFilter = R.propEq('contactName')

const filterArr = [typeFilter('7bca3a2a-aff6-449a-a59f-da320963d6d2'), contactFilter('vv')]

const filters = R.allPass(filterArr)

R.filter(filters, arr) // arr is the array of objects

It produces the desired result, and now I just have to figure out how to construct the filterArr array. I'm thinking of storing the filter values in an object and using mapObjIndexed to construct it, or is there a better way to do this?

Ben Briggs
@ben-eb
@kpruehss You could try an array of pairs e.g. [['requestTypeId', '7bca3....']] then map(apply(propEq)) to it - assuming your predicates are all propEq
Karsten Pruehss
@kpruehss
@ben-eb Thanks for the suggestion, I will look up how that works. It might just help me simplify my code abit. I have a total of 6 potential filters to apply, but only 4 of them will use propEq. The other 2 are more of a free-text type-ahead filter. Currently, I have it implemented via a factory function that produces propEq predicate functions as well as 2 curried es6 functions using .includes() and I'm assigning them to a filter list object when a change event is registered for a filter field. I then map over that object to generate the array for allPass, filtering out undefined fields (ie. no filter selected for a given attribute).
Ben Briggs
@ben-eb
You could even do something like [['eq', 'requestTypeId', '7bca3...']] and map the first parameter to the predicate you want to use
e.g: { eq: R.propEq, other: customFn }
Ben Briggs
@ben-eb
Then it's like map(([f, prop, value]) => fns[f](prop, value))
Ben Briggs
@ben-eb
const fns = {
  eq: R.propEq
};

const filters = [['eq', 'foo', 'bar'], ['eq', 'quux', true]];

const filterFn = R.compose(
  R.allPass,
  R.map(([f, prop, value]) => fns[f](prop, value))
)(filters);

R.filter(filterFn)([
  { foo: 'bar', quux: true },
  { foo: 'bar' },
  { quux: true }
]);
Ben Briggs
@ben-eb
Depending on your generator you could drop the fns map and pass the function references around it instead
map(([f, prop, value]) => f(prop, value))
Karsten Pruehss
@kpruehss
wow ok. I'm going to need a minute to wrap my head around this.
Ben Briggs
@ben-eb
Sure, no worries :)
Karsten Pruehss
@kpruehss
ok i think I slowly get whats happening here. the fns object holds the various types of predicates i may want to use, in this case eq for propEq and say, cf for a custom filter. I then push an array of strings laying out the predicate type, the prop and the value, and push that onto the filters array when a filter is selected. the filterFn then composes the allPass with the map to generate a single array of predicates to pass to filter. Is that roughly whats happening?
Ben Briggs
@ben-eb
Precisely! :)
Karsten Pruehss
@kpruehss
cool!
Ben Briggs
@ben-eb
And like I mentioned you could even drop this fn map and pass the functions around, e.g [[R.propEq, 'foo', 'bar']]
Karsten Pruehss
@kpruehss
this will also make it easy to add more filter predicate types later on by just tacking them onto fns. very cool!
Ben Briggs
@ben-eb
Depends on how you're generating this list really :)
Cool, hope it works out! :D
Karsten Pruehss
@kpruehss
thank you!! Now to the refactor :D
I think I prefer the idea of using the map tbh :). Feels more like I have a single point for filter definitions. Without the map, I'd have to pass fat-arrow functions in the array for custom filters, or am I misunderstanding something?
Ben Briggs
@ben-eb
Functions are first class citizens in JS, so you can do that. Either inline or using a reference, e.g:
const customFn = x => x;

[[customFn, 'a', 'b']] // reference
[[x => x, 'a', 'b']] // inline
Karsten Pruehss
@kpruehss
oh wait nevermind. As you pointed out, the generator would handle that. Still feels easier to follow for me using the map
Ben Briggs
@ben-eb
Yep. If it were me I'd probably prefer the map too
Karsten Pruehss
@kpruehss
seriously, thank you! This really helped me shift my thinking a bit
Ben Briggs
@ben-eb
@kpruehss You're very welcome :)
Marc Fogleman
@MFogleman

I have an applySpec and Promise problem

I am calling a database, getting back the document. I need to do a bunch of transforms based off the data gotten back, so I'm using an applySpec. It works great. But one of the fields in my spec needs to make a GET request to another server, and get the response. applySpec does not wait on promises. I figure there are 4 possible solutions

  1. There is a combination of wrapping this in async/awaits and R.then, to make this wait on the result, and I just dont know what it is
  2. This is a terrible anti pattern that I shouldnt be using at all
  3. Someone wrote a utility to solve this exactly (I found promise-apply-spec, but I think it may be a bit heavy for what I want)
  4. I'm going to need to hand roll either my own version of applySpec, or wrappers to handle this

If anyone has more experience in this or similar issues, feedback and guidance is appreciated

Ben Briggs
@ben-eb
@MFogleman You could also look into using a lazy data structure e.g. https://github.com/fluture-js/Fluture - not sure how well it would work with applySpec though to be honest
coffeefedora
@coffeefedora
i have a common paradigm, wondering if this has a functional term and maybe has a ramda function for it. I frequently do something like this: myObject[key] = myObject[key] || [];
so basically, get an existing array there or start one. i also do an object version of it
it's not exactly a long thing to type, but i was curious
kinda looks like a mergeRight maybe? myObject[key] = R.mergeRight(myObject[key], []);
but i'd love to just do: Init(myObject[key], []); and then its done
coffeefedora
@coffeefedora
although i'm dumb, it'd be Init(myObject, key, []);
Ben Briggs
@ben-eb
@coffeefedora I'd probably look at doing this when you want to extract the value out, then you can do propOr. e.g:
const results = R.propOr([], key, myObject);
In FP land we don't want to mutate our original object, so I think an Init function that has that interface would not be a good practice
coffeefedora
@coffeefedora
yeah that kind of makes sense. i set it the way i do to make looping easier when i am adding to the array
maybe i can re-organize my code and make it a little bit differently
Ben Briggs
@ben-eb
Yup :smile:
Hyunseok Oh
@algoshipda
  R.compose(
    R.fromPairs,
    R.zip(['x', 'y', 'z']),
    R.props(['a', 'b', 'c']),
  )
any equivalent function?
Twizzes
@Twizzes
@algoshipda That looks good to me, it seems to me there wouldn't be any much easier ways to specify a mapping like that. Are you looking for something that exhibits the same behavior in a different way?
Hyunseok Oh
@algoshipda
@Twizzes Can't with just one ramda function?
Ben Briggs
@ben-eb
@algoshipda 4 -> 3 with R.o(R.zipObj(['x', 'y', 'z']), R.props(['a', 'b', 'c']))
Oscar Bolmsten
@oscar-b
Is there a "preferred" style for pipe/compose when auto formatting? Any maintainer who would care to chime in? prettier/prettier#6921
Twizzes
@Twizzes
sorry if sanctuary is verbotten
Charles Hughes
@chughes87
anyone know a nice way to update all the keys in a given object? I need to prefix them all with a static string.. Object.entries and then R.zip comes to mind but I wish there was a cleaner way...