These are chat archives for ramda/ramda

21st
Feb 2018
joao@kapmug
@jay-jlm
Feb 21 2018 03:31
@davidchambers , was wondering if Monet.js was any last compatible with Ramda.js and the FantasyLand spec than Sanctuary.js and if that would be the reason why it (Monet.js) is not mentioned in ramda-fantasy'sReadME as an alternative library. Later I've found out that Monet.js has been refactoring the codebase towards compatibility with the FL spec.. not sure how far they got thou.
Ian Hofmann-Hicks
@evilsoft
Feb 21 2018 05:22
Would like to add "ramda transducer support" to a library of mine. Is there any lib or anything that encapsulates your xf bits? also are there plans to change that interface?
or is it pretty solid as is?
David Chambers
@davidchambers
Feb 21 2018 06:26
I haven't used Monet, @jay-jlm, so I don't know.
Anton
@you-fail-me
Feb 21 2018 15:29
Hey guys. I'm getting started with ramda, hope I'll be able to get going faster! my first (noobish) question: if I have e.g. R.apply(R.merge)([ /* items */ ] where items are objects. it works fine if there're > 1 items in the array, but returns a fn if there's only one (which makes sense since fns are curried). How can I properly and cleanly handle the case that I can end up with 1 item in that array, and in such case I don't need a fn to be returned but rather the item itself (so its kinda merged with nothing, i.e. is just the same item)?
Mike Lambert
@lax4mike
Feb 21 2018 15:32
hmm, well, R.merge only works with 2 objects
R.apply(R.merge)([ { a: "a"}, { b: "b"}, { c: "c"} ]);
that's missing "c", right?
you can probably use R.reduce instead
R.reduce(R.merge, {})
Anton
@you-fail-me
Feb 21 2018 15:33
if I have more than 1 obj it's ok. If I have only R.apply(R.merge)([ {a: 'a' ]) it's not
reduce really helped. thanks!
Mike Lambert
@lax4mike
Feb 21 2018 15:35
:thumbsup:
Anton
@you-fail-me
Feb 21 2018 15:36
do u mind taking a look at my first exercise and point me to stuff I do wrong / smells etc? it's really very brief, but the code was more verbose than using native JS reduce, which makes me think I misuse smth
Mike Lambert
@lax4mike
Feb 21 2018 15:38
sure
Anton
@you-fail-me
Feb 21 2018 15:39
thanks a lot! https://goo.gl/aRFyrm
Mike Lambert
@lax4mike
Feb 21 2018 15:41
that looks ok to me
by "verbose" you mean like 2 lines?
Anton
@you-fail-me
Feb 21 2018 15:42
e.g. with native reduce it's:
obj  => ({
    renderErrors: Object.keys(obj.errors).reduce((acc, curr) => {
      acc[curr] = !!obj.errors[curr] && !!obj.touched[curr]
      return acc;
    }, {})
which is much more dense
so I was afraid I'm doing it wrong
Mike Lambert
@lax4mike
Feb 21 2018 15:43
there are always different ways to do the same thing
Anton
@you-fail-me
Feb 21 2018 15:47
ok, thanks for swift and meaningful help!
Mike Lambert
@lax4mike
Feb 21 2018 15:47
it's kinda fun to try to solve a problem in a couple different ways
Anton
@you-fail-me
Feb 21 2018 15:48
yeah, but I guess u understand my feeling - I'm a bit new to it and if it's more LOC it creates a feeling that it's suboptimal. I really like the declarative way though - just laws how the data changes instead of instructions how to change
Mike Lambert
@lax4mike
Feb 21 2018 15:49
readability and maintainability is much more important than LOC
Anton
@you-fail-me
Feb 21 2018 15:51
btw, how do u usually prove that to people who be like "I dunno this lib and that's why I find it's just additional complexity, I dunno this lib so it decreases readability, so I don't wanna have this in our codebase"? :laughing:
Mike Lambert
@lax4mike
Feb 21 2018 15:52
haha, that's a hard one
i gave a presentation at work outlining pure functions, currying, and composition using Ramda
to show how it worked and show the benefits
Anton
@you-fail-me
Feb 21 2018 15:53
do u have it somewhere? I'd read that
Anton
@you-fail-me
Feb 21 2018 15:54
thanks :beers:
Mike Lambert
@lax4mike
Feb 21 2018 17:04
@you-fail-me here's another method:
const res = R.converge(
  R.mergeWith((error, touched) => {
    return error !== "" && touched
  }),
  [R.prop("errors"), R.prop("touched")]
)(obj);
Anton
@you-fail-me
Feb 21 2018 17:05
wow! looks cute! thanks for that, will analyze it
Mike Lambert
@lax4mike
Feb 21 2018 17:05
the output is { "email": true, "name": true, "password": false }
so you can do with that some other stuff in a R.compose
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:26
hello, I'm using Maybe from ramda-fantasy. My question is more on a usage pattern and not Maybe itself. Say I have a function which looks like
findFn = arr => obj => find(elem => elem.a === obj.a, arr). My arr is in a Maybe and so is the obj. So, seems like an obvious fit for ap. So, then
Just(findFn).ap(arrMaybe).ap(objMaybe)
the problem i have is, since find can return undefined, I could get a Just(undefined)
now, I could chain the result with chain(Maybe.toMaybe), but that seems like an extra step
is there a way to write this such that the result is a Just when something is found and Nothing when not found
Mike Lambert
@lax4mike
Feb 21 2018 17:28
Your findFn could wrap the result
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:29
so have findFn return a Maybe?
Mike Lambert
@lax4mike
Feb 21 2018 17:29
i haven't used ramda-fantasy, but i've used folktale which has http://folktale.origamitower.com/api/v2.0.0/en/folktale.maybe.fromnullable.html
yeah, findFn should return a Maybe
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:29
I guess that does make sense
Anton
@you-fail-me
Feb 21 2018 17:29
@lax4mike btw, that solution breaks if I have smth in errors which I don't have in touched, e.g.
const obj = {
  errors: {
    email: "email error",
    name: "",
    password: "pass error"
  },
  touched: {
    email: false,
    name: false,
  }
};
Mike Lambert
@lax4mike
Feb 21 2018 17:29
findFn = arr => obj => Maybe.fromNullable(find(elem => elem.a === obj.a, arr))
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:29
since in sanctuary, functions like head do return a Maybe
Mike Lambert
@lax4mike
Feb 21 2018 17:30
something like that
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:30
ok .. I guess that is a good pattern to follow. When dealing with uncertain return values, maybe best to result Maybe type
Mike Lambert
@lax4mike
Feb 21 2018 17:31
yup, that's what sanctuary is trying to do
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:31
but I was just wondering if that breaks the assumption that the function could have been used with regular types, as well as with Boxed types
if I called findFn with just the array and obj, I would get undefined
Mike Lambert
@lax4mike
Feb 21 2018 17:32
i dunno, you can name it safeFind
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:32
ah! .. yeah, that's a good idea
and I guess if someone wanted to use it with plain array an obj (not boxed in a Maybe), they could just use the other static helper methods to figure it out. Ok, Thanks!
Mike Lambert
@lax4mike
Feb 21 2018 17:34
one of the points of using Maybe is to force the consumer to deal with the potential of Nothing
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:37
thanks for the help! I like the idea of naming the function with a 'safe' prefix. Are there any other naming conventions you can suggest? Do you add a 'maybe' as suffix to variable or parameter names to hint at the type?
Mike Lambert
@lax4mike
Feb 21 2018 17:38
yeah, that could def make your code more readable
also, i would check out folktale, i don't think ramda-fantasy is maintained anymore
@you-fail-me that's the behavior of mergeWith, which is probably not a good solution if the keys will differ
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:48
@lax4mike you're right. It isn't. Just starting out with it to learn these concepts first :-). Umm .. having the findFn return a Maybe makes the expression Just(findFn).ap(arrMaybe).ap(objMaybe) return Just (Just (something))
Mike Lambert
@lax4mike
Feb 21 2018 17:49
you would do findFn(arr).ap(...)
oh, it's also takes an obj
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:50
and arr is in a Maybe , so is obj
I think that's where I was going with my earlier statement of the assumption that these functions can work with un-boxed values as well
Mike Lambert
@lax4mike
Feb 21 2018 17:56
oh, i see, you're using applicatives
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:58
yes
Mike Lambert
@lax4mike
Feb 21 2018 17:58
hmm
Bijoy Thomas
@bijoythomas
Feb 21 2018 17:58
since the args are Maybe's, ap seems the obvious choice
Mike Lambert
@lax4mike
Feb 21 2018 17:59
i'm not sure if folktale has .ap
Brad Compton (he/him)
@Bradcomp
Feb 21 2018 17:59
It does
at least the old version does
I assume the newer versions do to, as FL compatibility was a consideration
(the documentation for folktale needs work)
Mike Lambert
@lax4mike
Feb 21 2018 18:06
this is an interesting problem...
i guess you can do unsafeGet to peel off the outer Just
Bijoy Thomas
@bijoythomas
Feb 21 2018 18:46
ok .. I think for now I'll just extract it out like Just(safeFindFn).ap(arrMaybe).ap(objmaybe).chain(identity)
Mike Lambert
@lax4mike
Feb 21 2018 18:59
yeah, this behavior makes sense though
because your arr and obj could be in a different functor, like Result or something
then it would return a Result.Ok(Maybe.Just("answer"))