These are chat archives for ramda/ramda

1st
Sep 2017
Barry G
@bgits
Sep 01 2017 14:26
instead of doing address && someFunction(address) is there a way to just pass if address is undefined pointfree?
Kurt Milam
@kurtmilam
Sep 01 2017 14:31
@bgits you could use ifElse, unless or when.
unless( isNil, fn )
Barry G
@bgits
Sep 01 2017 14:33
sweet
Kurt Milam
@kurtmilam
Sep 01 2017 14:33
isNil also matches null, so it may not be exactly what you want.
I generally declare a reusable isUndefined function if I need to test specifically for undefined.
(and / or an isDefined function, in which case you'd use when rather than unless):
when( isDefined, fn )
hiredgunhouse
@hiredgunhouse
Sep 01 2017 14:41
hi, I'm new to Ramda and I'm wondering whether there is something that I'd describe as revere to R.map, my case is that I want to map array of functions over a single value and get an array of results ?
the best thing I can think of is using R.reduce, but this does not show the intent as well as reversed map would
Michael Rosata
@mrosata
Sep 01 2017 14:43
@hiredgunhouse I think you want something like ap
hiredgunhouse
@hiredgunhouse
Sep 01 2017 14:45
hmmm... from the examples it does not look like it would fit my case
Michael Rosata
@mrosata
Sep 01 2017 14:46
@hiredgunhouse
const singleValue = 100
const arrayOfFns = [R.add(10), R.add(100), R.multiply(400)]
ap(arrayOfFns, [singleValue]) // [110, 200, 40000]
hiredgunhouse
@hiredgunhouse
Sep 01 2017 14:46
I need R.map(value, functions) -> results, so that each function is called once and its result is put into the array alongside other results
hmm... ok, this would work
Michael Rosata
@mrosata
Sep 01 2017 14:47
you could do const notMapNotAp = (value, fns) => ap(fns, [value])
or actually just flip(ap) :)
aka: flap
hiredgunhouse
@hiredgunhouse
Sep 01 2017 14:49
yes, sorry, I read the examples wrong, this is actually exactly what I was looking for
Kurt Milam
@kurtmilam
Sep 01 2017 14:49
@hiredgunhouse take a look at juxt, as well.
Michael Rosata
@mrosata
Sep 01 2017 14:50
@kurtmilam yea that might be better actually
Kurt Milam
@kurtmilam
Sep 01 2017 14:50
It is similar to ap, but is sometimes a better fit.
@mrosata I forget about juxt far too often and end up unnecessarily wrapping my value in an array, then unwrapping it after the ap call :)
hiredgunhouse
@hiredgunhouse
Sep 01 2017 14:51
juxt seems to work for me too
Michael Rosata
@mrosata
Sep 01 2017 14:51
:)
hiredgunhouse
@hiredgunhouse
Sep 01 2017 14:51
that's awesome, thank you guys!
Kurt Milam
@kurtmilam
Sep 01 2017 14:51
:thumbsup: Usually just a question of what your input value looks like. y/w!
Michael Rosata
@mrosata
Sep 01 2017 14:54
yea, looking back on the code in 3 months or a year, it would be easier to figure out what it is doing using juxt rather than ap (when dealing with function to input relationships of many to one)
EmilLindfors
@EmilLindfors
Sep 01 2017 15:36
I've got a question, when i try to get a zero value, my function just returns an empty array - why is that? (I'm quite new to this, so the code can probably be optimized at lot as well ;) )
const countedArray = [
    {index : 1, arr: [{first : 5},{second : 2},{third : 10},{fourth : 0},{fifth : 1}]},
    {index : 2, arr: [{first : 2},{second : 7},{third : 0},{fourth : 0},{fifth : 1}]},
    {index : 3, arr: [{first : 1},{second : 23},{third : 8},{fourth : 0},{fifth : 1}]},
];

const found = (name, index, innerArray, value) => R.pipe(
    R.find(R.propEq(name, index)),
    R.prop(innerArray),
    R.find(R.prop(value)),
    R.toPairs
);

found("index", 2, "arr", "third")(countedArray);
the goal btw is to import the data into this structure:
const activeCount = {
        first: 0,
        second: 0,
        third: 0,
        fourth: 0,
        fifth: 0
    }
pankaj-pp
@pankaj-pp
Sep 01 2017 15:40
@ram-bot
const countedArray = [
    {index : 1, arr: [{first : 5},{second : 2},{third : 10},{fourth : 0},{fifth : 1}]},
    {index : 2, arr: [{first : 2},{second : 7},{third : 0},{fourth : 0},{fifth : 1}]},
    {index : 3, arr: [{first : 1},{second : 23},{third : 8},{fourth : 0},{fifth : 1}]},
];

const found = (name, index, innerArray, value) => R.pipe(
    R.find(R.propEq(name, index)),
    R.prop(innerArray),
    R.find(R.has(value)),
    R.toPairs
);

found("index", 2, "arr", "third")(countedArray);
Philipp Wille
@Yord
Sep 01 2017 15:42
@EmilLindfors You get an empty list because R.find(R.prop(value)) returns undefined which is put into toPairs that then returns an empty list (did not know that btw. :D something learned today)
pankaj-pp
@pankaj-pp
Sep 01 2017 15:43
@EmilLindfors use R.has instead of R.prop
Philipp Wille
@Yord
Sep 01 2017 15:44
Switching R.find(R.prop(value)) with R.find(R.has(value)) like @pankaj-pp suggested seems to do the trick, since find wants a function that returns a boolean
EmilLindfors
@EmilLindfors
Sep 01 2017 15:46
Thank you guys, much appreciated. Now i gotta figure out how to merge these values with the activeCount object in an elegant way :)