These are chat archives for ramda/ramda

8th
Oct 2016
Travis LaDuke
@laduke
Oct 08 2016 01:38
I think I'm going to start replying to a bunch of old easy javascript questions (that have jQuery answers) with ramda versions. heh
Travis LaDuke
@laduke
Oct 08 2016 02:04

could this be a transducer?

var kvToParam = R.mapObjIndexed((val, key) => {
  return '&' + key + '=' + encodeURIComponent(val);
});

var objToParams = R.compose(
  R.replace(/^&/, '?'),
  R.join(''),
  R.values,
  kvToParam
);

var o = {
  username: 'sloughfeg9',
  password: 'traveller'
};

objToParams(o);

https://goo.gl/KlGlzY

Brad Compton (he/him)
@Bradcomp
Oct 08 2016 02:55
This message was deleted
Travis LaDuke
@laduke
Oct 08 2016 03:00
I was re-reading an article, and yeah, it's probably a little over kill for something as small as making url params. I've just been waiting for an excuse to transduce something.
Brad Compton (he/him)
@Bradcomp
Oct 08 2016 03:02
Yeah, sorry, I messed it up.
I think transducers work best when you have multiple passes you need to make over a list
But here, you really just need one pass for kvToParams
The rest is manipulating strings or objects instead of lists
@ram-bot
var kvToParam = R.map(([key, val]) => {
  return key + '=' + encodeURIComponent(val);
});

var o = {
  username: 'sloughfeg9',
  password: 'traveller'
};

compose(s => `?${s}`, join('&'), kvToParam, toPairs)(o)
ram-bot
@ram-bot
Oct 08 2016 03:05
Unexpected token [
Travis LaDuke
@laduke
Oct 08 2016 03:05
ah that's a good way to think about it
Brad Compton (he/him)
@Bradcomp
Oct 08 2016 03:05
Apparently Ram-Bot doesn't destructure
Stefano Vozza
@svozza
Oct 08 2016 10:00
@kurtmilam is this for work or just a learning exercise? if you've to put a comment in to figure out what something is doing then it's generally a sign that your function shouldn't be point-free.
Markus Pfundstein
@MarkusPfundstein
Oct 08 2016 10:49
is there a function/hack to use pickAll with R.path ?
e.g. R.pickAll([ R.path([‘a’, ‘b’]), ‘c’, R.path([‘d’, ‘e’, ‘f’]))
on
{
    a : { b : 5 },
    c : 10,
    d : { e : { f : 15 } }
}
Matt McKellar-Spence
@MattMS
Oct 08 2016 12:06
@MarkusPfundstein what about juxt([R.path(['a', 'b']), prop('c'), R.path(['d', 'e', 'f'])])
Sorry that doesn't maintain the keys.
Markus Pfundstein
@MarkusPfundstein
Oct 08 2016 12:13
:( nearly ;-)
Denis Stoyanov
@xgrommx
Oct 08 2016 12:23
@MarkusPfundstein what is your case?
Markus Pfundstein
@MarkusPfundstein
Oct 08 2016 12:24
super simple actually
const payloadPath = R.path(['fields', 'payload']);
const deviceEUIPath = R.path(['devEUI']);
const counterPath = R.path(['counter']);
const gatewayEUIPath = R.path(['metadata', 'gateway_eui']);

function getInterestingData(message) {
        return {
            payload : payloadPath(message),
           deviceEUI : deviceEUIPath(message),
            counter : counterPath(message),
            gatewayEUI : gatewayEUIPath(message)
         }
}
was just wandering if there is a one liner for s.th. like this, so that I dont have to device all those paths
Matt McKellar-Spence
@MattMS
Oct 08 2016 12:27
getInterestingData = applySpec({payload: payloadPath, deviceEUI : deviceEUIPath,  counter : counterPath, gatewayEUI : gatewayEUIPath});
getInterestingData(message);
Denis Stoyanov
@xgrommx
Oct 08 2016 12:30
converge(compose(zipObj(['payload', 'deviceEUI', 'counter', 'getewayEUI']), unapply(identity)), [payloadPath, deviceEUIPath, counterPath, gatewayEUIPath])(message) smth like that
Markus Pfundstein
@MarkusPfundstein
Oct 08 2016 12:32
thx
nice, i like both
applySpec is a bit closer i think
Denis Stoyanov
@xgrommx
Oct 08 2016 12:33
I think api Ramda should be smaller. Almost all will be implemented with lift, converge and zip =)
Markus Pfundstein
@MarkusPfundstein
Oct 08 2016 12:33
:P
i am quite happy that is has so much convenience functions
Jigar Gosar
@jigargosar
Oct 08 2016 13:23
About lenses: is there a way to create lens of a deeply nested object in a tree like structure, which statisfies a predicate. e.g: propEq("id", 42)
so that it can be updated
without mutation
Matt McKellar-Spence
@MattMS
Oct 08 2016 14:28
@jigargosar pathEq
Jigar Gosar
@jigargosar
Oct 08 2016 14:52
@MattMS that works for known paths
the data structure I am using is tree like
I finally wrote a solution
took me few hours :(
const createDeepLensMatching = (pred, arr, kidsL)=> {
        const findIndexPath = (arr, idxPath)=> {
            const idx = findIndex(pred, arr)
            if (idx >= 0) {
                return append(idx, idxPath)
            } else {
                return reduceWithIndex((oldPath, obj, idx) => {
                    const newPath = findIndexPath(view(kidsL, obj), oldPath)
                    if (isEmpty(newPath)) {
                        return oldPath;
                    } else {
                        return reduced([...oldPath, idx, ...newPath]);
                    }
                }, idxPath, arr)
            }
        }

        const indexLenses = map(lensIndex, findIndexPath(arr, []))
        const lenses = intersperse(kidsL, indexLenses)
        return apply(compose, lenses)
    }
Jigar Gosar
@jigargosar
Oct 08 2016 15:00
example usage:
const notes = [
  {id:1, notes:[   {id:2, notes:[]}  , {id:3, notes:[]}  ]},
  {id:4, notes:[   {id:5, notes:[    {id:6, title:"view this note" notes:[]}   ]}   ]},
]
const mylens = createDeepLensMatching(propEq("id", 6), notes, lensProp("notes"))
console.log(view(mylens, notes))
@scott-christopher any comments :)