These are chat archives for ramda/ramda

16th
Dec 2015
Hardy Jones
@joneshf
Dec 16 2015 03:39
@CrossEye have you seen this: https://www.youtube.com/watch?v=8bZh5LMaSmE ?
since you mentioned gilded rose :)
@Voxelot you could also think about your problem differently.
Hardy Jones
@joneshf
Dec 16 2015 03:44
@Voxelot if you take your array of strings, and convert it to an array of objects with the key id, then you're dealing with two arrays of the same type
or at least for this simple example you are.
then @scott-christopher 's suggestion seems almost trivial.
Hardy Jones
@joneshf
Dec 16 2015 03:50
const ids = ...;
const objs = ...;
const idDiff = (x, y) => x.id === y.id;
const filterObjects = R.curry((ids, objs) =>
  R.differenceWith(idDiff, objs, R.map(R.objOf('id'), ids));
);
const newObjects = filterObjects(ids, objs);
or so
Julien Goux
@jgoux
Dec 16 2015 07:45
Hello
Is it possible to define a lens based on dynamic value ?
Like, I want to define a lens on a collection of object, based on a object's id to find
  const findReasonIdx = R.find(R.propEq("id")(payload))
  const reasonIndexLens = R.lens(findReasonIdx, ?)
I think I get the first part, but I don't know for the association part
Hardy Jones
@joneshf
Dec 16 2015 08:04
@jgoux think about the types there.
Julien Goux
@jgoux
Dec 16 2015 08:04
@joneshf I'm pretty sure it's R.assoc, but I'm not sure about how to provide the findReasonIdx to it :p
R.pipe(findReasonIdx, R.assoc) ?
Hardy Jones
@joneshf
Dec 16 2015 08:06
findReasonIdx : [{id: Payload | ...}] -> {id: Payload | ...} and lens : (s -> a) -> ((a, s) -> s) -> Lens s a
Julien Goux
@jgoux
Dec 16 2015 08:06
it's R.findIndex btw, I made a mistake typing it
Hardy Jones
@joneshf
Dec 16 2015 08:06
So your s is [{id : Payload | ...}] and your a is {id : Payload | ...}
this means you need a function ({id : Payload | ...}, [{id : Payload | ...}]) -> [{id : Payload | ...}]
my immediate guess would be one of prepend or append.
but maybe you want to replace a value in that array.
so search the array for that id, and if it doesn't exist, insert it in there?
oh, that kind of changes things then.
but the same general logic aplies
line it up with what lens expects.
Julien Goux
@jgoux
Dec 16 2015 08:11
I want a "dynamic" lensIndex(), the index would be based on the R.find(R.propEq("id")(payload)) function
Hardy Jones
@joneshf
Dec 16 2015 08:11
I thoguth you said it was findIndex?
Julien Goux
@jgoux
Dec 16 2015 08:11
instead of providing a fixed value
lol I copied it from the conversation sorry xD
Yes it's findIndex :)
Hardy Jones
@joneshf
Dec 16 2015 08:12
so it's:
const findReasonIdx = R.findIndex(R.propEq('id')(payload));
?
Julien Goux
@jgoux
Dec 16 2015 08:12
Yes :)
Hardy Jones
@joneshf
Dec 16 2015 08:12
okay
so look at that type now.
findReasonIdx : [{id : Payload | ...}] -> Number
Julien Goux
@jgoux
Dec 16 2015 08:13
ok
Hardy Jones
@joneshf
Dec 16 2015 08:13
so the s is [{id : Payload | ...}], and the a is Number.
so the "setter" of lens has this type: (Number, [{id : Payload | ...}]) -> [{id : Payload | ...}]
Julien Goux
@jgoux
Dec 16 2015 08:15
yes :)
Hardy Jones
@joneshf
Dec 16 2015 08:15
So, what can you do with that?
it might be too late in the day for me, but it looks like, not much.
Julien Goux
@jgoux
Dec 16 2015 08:16
yes, I can't determine the Number
Hardy Jones
@joneshf
Dec 16 2015 08:16
you're given the number.
Julien Goux
@jgoux
Dec 16 2015 08:17
so I have to calculate it before defining the lens ?
Hardy Jones
@joneshf
Dec 16 2015 08:17
no, it's an argument to that function.
Julien Goux
@jgoux
Dec 16 2015 08:17
I'm not sure I follow
I have to transform (Number, [{id : Payload | ...}]) to accept my findReasonIdx function ?
Hardy Jones
@joneshf
Dec 16 2015 08:28
you have to provide a function that has two arguments, the first argument is a number, the second an array of objects with at least an id field. you need to return an arraay of objects with at least an id field.
for instance:
const reasonIndexLens = R.lens(findReasonIdx, (_, xs) => xs);
that fits the types, but i doubt it passes the laws
its also possible that you cannot create a true lens for your case.
does that make sense?
Julien Goux
@jgoux
Dec 16 2015 08:52
Yes :)
But for the first argument (the Number), what I don't understand is why I can't reuse findReasonIdx to get it ?
Hardy Jones
@joneshf
Dec 16 2015 14:42
because that's almost assuredly not going to be lawful.
Think of the setter part as someone saying, "I know the index I want you to set, and I know the array of things I want to change, now go do the work."
If you respond to that with, "Forget your index, I'm doing it my way." that's bound to fail.
Hardy Jones
@joneshf
Dec 16 2015 14:53
So with that in mind, and with the laws to be a valid lens, I think you only have one possible implementation.
Hardy Jones
@joneshf
Dec 16 2015 14:59
the laws are:
// 1. If you set, then get, you get back what you set.
R.view(l, R.set(l, n, xs)) == n
// 2. If you get, then set, you haven't changed anything.
R.set(l, R.view(l, xs), xs) == xs
// 3. If you set twice, the last set wins
R.set(l, y, R.set(l, x, xs)) == R.set(l, y, xs)
if you just ignore the index, and return the array, like I did up there, :point_up: December 16, 2015 12:28 AM, then you'll pass laws 2 and 3, but not law 1.
If you lookup the index in your own way, as you suggest, then same thing. You pass laws 2 and 3, but not law 1.
So, I think your only implementation has to set the element at that index no matter what.
otherwise, you're going to fail the first law.
Hardy Jones
@joneshf
Dec 16 2015 15:04
but thinking about it, I doubt that even this implementation will be lawful.
If there's no element with that id, you don't get back a number, do you?
I think what you want isn't a lens at all.
since you're not guaranteed to always have exactly one value.
Oh, wait. findIndex returns -1 for missing values.
Hell, I don't know.
Stefano Vozza
@svozza
Dec 16 2015 15:35
:laughing:
Julien Goux
@jgoux
Dec 16 2015 16:13
@joneshf thanks for the explanation
Julien Goux
@jgoux
Dec 16 2015 16:25
@joneshf So in the end lenses aren't the solution for manipulating deep data structures when there are lists involved ?
Hardy Jones
@joneshf
Dec 16 2015 16:27
A plain lens? no. A different thing built upon the foundation of lenses? yes.