These are chat archives for ramda/ramda

11th
Oct 2017
Philipp Wille
@Yord
Oct 11 2017 05:19
@lmatteis There are several ways to implement paths, my favourites are:
const paths = useWith(
  lift(path),
  [identity, of]
)

const paths = uncurryN(2)(
  o(juxt, map(path))
)

const paths = useWith(
  unapply(reduce(ap, [path])),
  [identity, of]
)
They can all be applied in two flavours:
paths(selections)(obj)
paths(selections, obj)
(Note that some uses of useWith and uncurryN are just so both these cases are possible)
Example:
const obj = {
  a: { c: 1 },
  b: { d: 2 }
}

const selections = [['a', 'c'], ['b', 'd']]

const paths = useWith(
  lift(path),
  [identity, of]
)

paths(selections, obj) //=> [1, 2]
paths(selections)(obj) //=> [1, 2]
Adam Szaraniec
@mimol91
Oct 11 2017 10:21
Hey. I m trying go to something like join (sql)
I've did https://goo.gl/tB36JF but it seems that ramda join does not work like that, Is there any 'ready' function in ramda?
Denis Stoyanov
@xgrommx
Oct 11 2017 10:41
@mimol91 what is your expected result?
Adam Szaraniec
@mimol91
Oct 11 2017 10:45
// Result
//{"id":1, "name":"bob", "tags":[{"id":1, "name":"foo"}]}
I thought its a pretty common case.
Denis Stoyanov
@xgrommx
Oct 11 2017 10:53
@mimol91 compose(map(ap(o => p => assoc('tags', p, o), pick(['id', 'name']))), innerJoin(joinFn))(users, tags) :smile:
Adam Szaraniec
@mimol91
Oct 11 2017 11:18
Thanks, small change to fit more to my case and works as charm
const merge_ = (joinFn, field) => 
    compose(map(ap(o => p => assoc(field, p, o), omit([field]))), innerJoin(joinFn))

merge_(joinFn, 'tags')(users, tags)
arian‮
@arian-swydo
Oct 11 2017 11:50
not as good as using innerjoin, but came up with this other way: https://goo.gl/VkMiDe
Scott Sauyet
@CrossEye
Oct 11 2017 12:44
Yes, I think innerJoin might not have been properly named. It's more a glorified filter.
Alec
@alavkx
Oct 11 2017 15:44
I made a function to avoid branching when constructing an object in a composition, called it derive. Could anyone lend their opinion?
Alec
@alavkx
Oct 11 2017 15:53
worried it may be a bit.. excessive?
arian‮
@arian-swydo
Oct 11 2017 17:17
not sure if you want one function to do this many things? but if it works for you, then there's nothing wrong with having one function do many things. I would say the merging of objects seems unnecessary though.
that being said, checking for object by using the Object() ctor has many edge cases, checking for object literals isn't as easy as this and you should probably avoid it or maybe use lodash's isPlainObject implementation
also I would avoid checking function ctor by using toString as this is heavily dependant on host (browser etc.) implementation
the correct way is to compare it to something you know will evaluate to that string, that way you can be sure that it will not create false negatives (doesn't recognize it as a function because the string isn't the same)
but I think using is(Function) will do fine
arian‮
@arian-swydo
Oct 11 2017 17:25
also, I see people abusing switch statements like so in C as well, but it's not necessary as what you're doing can be substituted with a simple object mapping, using ramda's type function for instance, something like obj={string: pick}; return obj[type(arg)]
also I'm not heavily read into the spread operator, but there might be a chance that you mutate the object passed into reduce, similarly the applied function can mutate the object, which is something I would avoid
Chasiwpaw
@Chasiwpaw
Oct 11 2017 18:30

const book = [{id: 1, bookName: "Pokemon"}, {id: 2, bookName: "Naruto"}, {id:3, bookName: "One piece"}, {id:4, bookName: "Dragonball"}]
const shelf = [{id: 1, books: [1,2]}, {id:2, books:[3,4]}]

Does anyone know how to replace the book property with the contents of the book object with Ramda? I've tried doing nested R.map, but it doesnt seem to work...
const populateBook = bookArray => bookItem => R.set(booksLens, filteredBooks(bookArray, bookItem), bookItem) // hasn't reached here yet
const loopBooks = shelfItem => R.map(populateBook(book),shelfItem.books) // shelfItem.books undefined
const loopShelf = R.map(loopBooks(), shelf) // works

arian‮
@arian-swydo
Oct 11 2017 18:36
someone asked the same thing about 9 hours ago in this chat, if you scroll up you can find it, they used innerJoin to achieve it
i posted this solution but it's not as good as the other: https://goo.gl/VkMiDe
Chasiwpaw
@Chasiwpaw
Oct 11 2017 18:38
cool! Thanks!
Alec
@alavkx
Oct 11 2017 18:40
Thanks @arian-swydo, your feedback is helpful :)
Barney Carroll
@barneycarroll
Oct 11 2017 21:43
@alavkx I agree with @arian-swydo about avoiding switch statements, and actually multipart if / else if / else sequences too. They are precarious & difficult to read. My personal preference for sequences of conditions is to write arrays of [condition, outcome] pairs, where condition and outcome are functions that receive the references you need to query, lift that combo out to a discrete reference, and to map or fold over them as desired at the call site
Jason Shin
@JasonShin
Oct 11 2017 21:49
Hey guys, what's the difference between Future and IO?
Barney Carroll
@barneycarroll
Oct 11 2017 21:54
Futures have failure semantics, IOs don't. IOs are not necessarily asynchronous, and are used to trigger side effects, whereas Futures are expected to produce values
Jason Shin
@JasonShin
Oct 11 2017 21:56
Right, interesting
Jason Shin
@JasonShin
Oct 11 2017 22:12
It sounds to me it's a functional way to encapsulate side effects, which implements Fantasy Specs
but it's not asynchronous unlike Future
Barney Carroll
@barneycarroll
Oct 11 2017 22:24
Yeah that sounds about right