These are chat archives for ramda/ramda

25th
Aug 2016
Kurt Milam
@kurtmilam
Aug 25 2016 12:28
Sanctuary vs. Folktale - are there compelling reasons to choose one over the other?
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 13:03
That's a great quiestion @kurtmilam. I've seen a lot of people in here talk about Sanctuary
Kurt Milam
@kurtmilam
Aug 25 2016 13:09
I see that Sanctuary is loaded in ramda-bot. Wonder if that's an official seal of approval.
James Forbes
@JAForbes
Aug 25 2016 13:27
I've used both and liked both @kurtmilam
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 14:12

@kurtmilam @aaronmcadam Sanctuary is very opinionated in a way that other libraries aren't, which may dictate whether it's right for you.

Most notably it has runtime type-checking for all its functions, and provides type-safe versions of many of Ramda's functions.

R.head([])   // => undefined

S.head([])   //=> Nothing()

If you want that level of safety, Sanctuary is a good choice. Folktale and Ramda-Fantasy are both good choices if you just want the container types.

Aaron Mc Adam
@aaronmcadam
Aug 25 2016 14:13
Thanks for that @Bradcomp!
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 14:18
For the record, I've used Sanctuary and Folktale in projects. I don't think one is better than the other, just different.
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 14:18
My brain isn't working, is there a nicer way to express this?
const url = path ? `/${path}` : '/';
I think I'd prefer Sanctuary's guidance, but I can see where Folktale is useful for when you know what you're doing a bit more
James Forbes
@JAForbes
Aug 25 2016 14:38

Don't know about better, but

'/'+[path]

will resolve to '/' if path is undefined

but kind of a hack
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 14:40
yea, I was getting '/undefined' :D
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 14:57
const url = `/${path || ''}`
James Forbes
@JAForbes
Aug 25 2016 15:01
that too!
even though the +[possiblyUndefinedString] thing is hack, it has always had a warm place in my heart because in documentation optional arguments are often denoted with square brackets.'users/'+[id]+[query]
so if '/users' or /users/<id> or /users/<query> or /users/<id><query> are all valid, then it works for me
but I like @Bradcomp's better
Derek Duncan
@derek-duncan
Aug 25 2016 15:19
is anyone up to code review some functional code for me? just getting started with it, and some constructive criticism would be really helpful!
Kurt Milam
@kurtmilam
Aug 25 2016 15:48
@derek-duncan Make readStream immutable (replaces lines 6-14 in index.js):
const readStream =  fileName
  ? fs.createReadStream(path.resolve(__dirname, fileName))
  : process.stdin
Derek Duncan
@derek-duncan
Aug 25 2016 15:50
+1 @kurtmilam
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 15:51
@derek-duncan I have to say well done on the documentation, it's quite easy to read through your project
I use JSDoc myself, but I do wonder whether I should use Hindley-Milner instead
Or alongside, but that seems a bit redundant
Just in case anybody doesn't know what I mean: https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch7.html
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 15:53
@aaronmcadam I'll sometimes put the HM sig in the description for my JSDoc sig ;-P
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 15:53
interesting!
Derek Duncan
@derek-duncan
Aug 25 2016 15:54
best of both worlds! @Bradcomp
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 15:54
Do you have a heuristic for when you do that @Bradcomp ?
Derek Duncan
@derek-duncan
Aug 25 2016 15:55
I'm coming from OO/Factory paradigms with js, so even thinking functional is a whole new world
i want to explore Hindley-Milner when I'm more productive in fp
JSDoc for now :fire:
Derek Duncan
@derek-duncan
Aug 25 2016 16:02
is there a better way to merge the objects here? essentially, only the props with Number values should merge with addition. here's the merging function
/**
 * Merges duplicate team records into a single record
 * @param {Object} acc
 * @param {Object} team
 * @return {Object}
 */
const dedupeTeams = R.curry((acc, team) => {
  const currentTeam = acc[team.name] || {};

  const combinedTeam = R.mergeWith((val1, val2) => {
    if (R.is(Number, val1) && R.is(Number, val2)) {
      return val1 + val2;
    } else {
      return val1;
    }
  }, team, currentTeam);

  acc[team.name] = combinedTeam;
  return acc;
});
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 16:03
@aaronmcadam Exactly! I'll typically only do it if it seems to add clarity to what exists in the jsdoc sig.
Derek Duncan
@derek-duncan
Aug 25 2016 16:04
so dedupeTeams([{ name: 'obj1', score: 1 }, { name: 'obj1', score: 2 }]) would output [{ name: 'obj1', score: 3 }]
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 16:04
Cool, I think I'm the other way, I find it a bit difficult to write some HM for certain functions
@derek-duncan Have you tried uniqWith?
@Bradcomp Do you know of any tools that support doc generation from just Hindley-Milner?
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 16:06
I don't :(
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 16:06
Or do you think it's best to keep using JSDoc?
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 16:06
That's what we use.
There's a lot of tooling around it you can use
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 16:07
If you had a tool for HM, would you use that exclusively, or do you think JSDoc is still has its place?
Derek Duncan
@derek-duncan
Aug 25 2016 16:09
@aaronmcadam uniqWith looks interesting. that might work :thumbsup:
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 16:09
I see them as different use cases. HM is really good at being able to tell what a function does at a glance
JSDoc gives you a lot more ability to add detail / explanation
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 16:09
But then the JSDoc tells you the why
Yeah, which can sometimes be more important
I think I'll try phasing in HM where I can. If only there was a way to generate/lint it. It can get quite hard to write and read sometimes
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 16:11
@derek-duncan You could use groupBy to pull each team's records into an array, indexed by the team name
then map over that to get a {name: score} object
That might not get you where you want though
Derek Duncan
@derek-duncan
Aug 25 2016 16:15
@Bradcomp i haven't tried this approach yet. cool! I'll play around with it in the repl
sometimes the challenge in fp is finding the clearest solution
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 16:20
I find thinking about the problem in a functional way is the challenge
For example, thinking of your problem in terms of lists
and then trying to express the nature of the algorithm
and then somebody on here will suggest a Ramda function that blows your solution out of the water :D
Derek Duncan
@derek-duncan
Aug 25 2016 16:23
so functional programming is the challenge :wink:
thinking in lists seems like an inherit challenge of using fp in javascript, which generally relies on objects
haha yay for community! ^
Aaron Mc Adam
@aaronmcadam
Aug 25 2016 16:26
yeah, this gitter has been brilliant
Derek Duncan
@derek-duncan
Aug 25 2016 16:34
thanks for the help @aaronmcadam and @Bradcomp!
Matt McFarland
@MattMcFarland
Aug 25 2016 19:37
Hi all, I'm using R.chain and I wanted to see if there was a way to get an index within a function it will be calling?
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 19:44
@MattMcFarland You can use addIndex
R.addIndex
Brad Compton (he/him)
@Bradcomp
Aug 25 2016 19:45
@ram-bot
addIndex(chain)((a, b) => [a , b], [1,2,3])
ram-bot
@ram-bot
Aug 25 2016 19:45
[ 1, 0, 2, 1, 3, 2 ]
Matt McFarland
@MattMcFarland
Aug 25 2016 19:46
@Bradcomp Thanks that should work
Matt McFarland
@MattMcFarland
Aug 25 2016 20:23
Is there a way to find the duplicates in an array. Would be nice to return a new list of just the dups in the array.
Derek Duncan
@derek-duncan
Aug 25 2016 22:40
anyone have some explanations of R.transduce they could share? trying to wrap my head around it
James Forbes
@JAForbes
Aug 25 2016 23:47

@derek-duncan

Would something like this work?

R.mergeWith(
  R.ifElse(
     R.is(Number)
     ,R.add
     ,R.identity
  )
)