These are chat archives for ramda/ramda

28th
May 2015
Jethro Larson
@jethrolarson
May 28 2015 02:10
I was going to cherry-pick debounce from another lib to buffer an input then looked at the impl. What the shit? Is it really this hard? I totally understand why this lib diverges from other lib.
I know that the answer to "Is it really this hard?" is "Yes, if you want to squeeze all the performance out.", but still...
Jethro Larson
@jethrolarson
May 28 2015 02:20
I guess a ramda-centric debounce would be a future, right?
Jethro Larson
@jethrolarson
May 28 2015 03:12
I created a tampermonkey userscript to filter the ramda docs differently. https://gist.github.com/jethrolarson/1b82720ed09195ee0a37
It supports searching by sig, function name, or type
If you have tampermonkey just click "raw" from the gist to install.
Raine Virta
@raine
May 28 2015 08:15
jethrolarson: you could add it to https://github.com/ramda/ramda/wiki#tools
Raine Virta
@raine
May 28 2015 08:32
hmm, is there something like f → [k] → {k: f k}
to do: pipe(map(repeat(__, 2)), fromPairs, mapObj(readFile))
Raine Virta
@raine
May 28 2015 10:59

R.compose(R.values, R.pickAll);

@buzzdecafe amazing

Michael Hurley
@buzzdecafe
May 28 2015 12:17
@raine I see you added a cookbook to the wiki! So far just one recipe :-)
Raine Virta
@raine
May 28 2015 12:53
yep. not sure what's a good format for that but i'm sure it will evolve
i think community-curated cookbook in the wiki is better and will inspire more contributions than creating a repo where people submit PRs and whatnot
Dejan Toteff
@selfrefactor
May 28 2015 12:59
What are the limitations of R.curry? I tried currying _.map of Lodash with no success.
Michael Hurley
@buzzdecafe
May 28 2015 13:34
Difficult if not impossible to curry with optional params.
John-David Dalton
@jdalton
May 28 2015 13:38
@peeping4dsun Check out lodash-fp. It's map and other methods are auto-curried iteratee first.
Brandon Wilhite
@JediMindtrick
May 28 2015 14:02
So looking at @davidchambers ' Sanctuary last night...it occurred to me that a combination of functions and monads might work really well for ETL scenarios.
Has anyone here done this?
Scott Sauyet
@CrossEye
May 28 2015 15:10
@raine: if nothing else, something is far, far better than nothing! Thanks for getting this rolling.
Raine Virta
@raine
May 28 2015 16:14
anyone have idea about f → [k] → {k: f k}? is there a name or some great pattern for it
David Chambers
@davidchambers
May 28 2015 16:16
@JediMindtrick, I had to Google "ETL", so I can't really comment. :)
Brandon Wilhite
@JediMindtrick
May 28 2015 16:32
Lol...I totally understand. It's not really my area, and yet I've found myself doing it the last 4+ mos. And the tools, at least on the MS stack, are pretty painful.
I think FP could really shine there, though, b/c there is so much pain.
Raine Virta
@raine
May 28 2015 17:23
while trivial to derive, i'd very much like to see [un]lines and [un]words in ramda
Asaf
@asaf-romano
May 28 2015 17:25
Is there a more elegant way to do something like this: http://bit.ly/1JXojpZ
Raine Virta
@raine
May 28 2015 17:25
or maybe they would belong to ramda-ext or ramda-contrib or something
Jethro Larson
@jethrolarson
May 28 2015 17:30
@asaf-romano maybe a reduce? R.reduce(function(acc, k){return R.assoc(k, 0, acc);},{}, keys);
I know the case where I'm converting an array to object or inverse is super common. Maybe there should be functions for it in ramda
Hardy Jones
@joneshf
May 28 2015 17:33
i guess it depends on what you consider elegant
Asaf
@asaf-romano
May 28 2015 17:34
i was thinking of something like this (or a similar approach combining merge and createMapEntry), but I'm somewhat worried about performance.
Hardy Jones
@joneshf
May 28 2015 17:35
like R.pipe(R.map(R.flip(R.createMapEntry)(0)), R.mergeAll)?
Asaf
@asaf-romano
May 28 2015 17:35
probably with some R. in there, yes.
Hardy Jones
@joneshf
May 28 2015 17:35
:)
Asaf
@asaf-romano
May 28 2015 17:36
R. i meant
sigh, it removes the underscores
it's the placeholder
:)
Hardy Jones
@joneshf
May 28 2015 17:37
i see it in irc
Asaf
@asaf-romano
May 28 2015 17:37
ah, I'm in gitter, which thinks it's useful to undeline a 0-length string :)
Raine Virta
@raine
May 28 2015 17:37
hah, me too, was confused
Hardy Jones
@joneshf
May 28 2015 17:38
ifyou're concerned about efficiency, @jethrolarson's suggestion seems good
Asaf
@asaf-romano
May 28 2015 17:38
how so? assoc clones each times, doesn't it?
there's somewhat horrible Map constructor in ES6 that could be a base for a function like this
hrm, maybe I should look at fromPairs
Jethro Larson
@jethrolarson
May 28 2015 17:41
you could replace assoc with acc[k] = 0
Asaf
@asaf-romano
May 28 2015 17:41
maybe fromPairs+useWith+createMapEntry
thanks for all of the suggestions
Jethro Larson
@jethrolarson
May 28 2015 17:44
If perf is of super concern you'll probably be falling back to a loop
Scott Sauyet
@CrossEye
May 28 2015 17:44
@raine: I don't have any particular name for it, nor do I recall a lot of uses for this, but I think I've had to do something like it before. One possible implementation is at http://bit.ly/1FQloO9.
var f = R.converge(R.compose(R.fromPairs, R.zip), R.nthArg(1), R.map);
var dasherize = R.pipe(R.replace(/([A-Z])/g, '-$1'), R.replace(/[-_\s]+/g, '-'), R.toLower);
f(dasherize, ['borderRadius', 'backgroundColor', 'textOverflow']);
//=> {"backgroundColor": "background-color", "borderRadius": "border-radius", "textOverflow": "text-overflow"}
Hardy Jones
@joneshf
May 28 2015 17:45
@jethrolarson yeah that's probably best
Raine Virta
@raine
May 28 2015 17:48
CrossEye: cheers. it's not quite clear at a glance what f does
Asaf
@asaf-romano
May 28 2015 17:48
sometime native stuff helps too: R.compose(R.fromPairs, R.map(k => [k, 0]))
Raine Virta
@raine
May 28 2015 17:50
or maybe i'm not used to reading converge
Scott Sauyet
@CrossEye
May 28 2015 17:52
@raine: hey, I didn't say it was a good solution. Anytime I have to resort to nthArg, I suspect that points-free is not the best approach.
I just noticed that the problem from @asaf-romano is a specialization of the one from @raine, using the function always (0). Hmmm.
Raine Virta
@raine
May 28 2015 17:55
yep, i noticed the similarity
Asaf
@asaf-romano
May 28 2015 17:57
hehe, yeah
Scott Sauyet
@CrossEye
May 28 2015 18:00
converge (f, g, h)(..args);  //=> f (g (..args), h (..args) )
Raine Virta
@raine
May 28 2015 18:01
is it possible that ramda is missing a function that makes both of these really simple
perhaps there's way to generalize the problem
Scott Sauyet
@CrossEye
May 28 2015 18:03
Always possible. I simply don't know what that abstracting is, or what to call it.
Asaf
@asaf-romano
May 28 2015 18:06
mapKeys?
second arg being the value function (given the key)
that's somewhat similar to mapObj
so in my case it would be mapKeys(["a", "b", "c"], always(0))
except the order of the args should be reversed
mapKeys(always(0), ["a", "b", "c"]), and then that's indeed pretty similar to mapObj
Scott Sauyet
@CrossEye
May 28 2015 18:12
Not bad at all.
Just discussing with @buzzdecafe, who has a much nicer reduce-based solution.
David Chambers
@davidchambers
May 28 2015 18:13
R.words and R.lines sound useful to me, @raine. I wonder whether we can agree on what constitutes a word and what constitutes a line. :)
Michael Hurley
@buzzdecafe
May 28 2015 18:13
Reduce(function(acc, elem){ return assoc(elem, f(elem), acc);}, {}, list)
sorry, on phone
Asaf
@asaf-romano
May 28 2015 19:00
@CrossEye reduce or not, this has to be done with converge, right?
replied there btw.
Raine Virta
@raine
May 28 2015 19:01
i think createMapEntry needs a shorter name
Michael Hurley
@buzzdecafe
May 28 2015 19:15
I think createMapEntry is redundant. Assoc FTE
Raine Virta
@raine
May 28 2015 19:18
not quite as clean though
need to use because {} comes last
Raine Virta
@raine
May 28 2015 19:24
David Chambers
@davidchambers
May 28 2015 21:27
The function was initially called R.mapEntry, but @CrossEye rightly pointed out the name should be a verb. Also R.mapEntry is potentially confusing alongside R.mapObj. I agree that a shorter name would be nice, but I don’t have any suggestions.
Scott Sauyet
@CrossEye
May 28 2015 21:44
I've several times absent-mindedly tried to use it as mapEntry, so the current name doesn't stick well for me.
Raine Virta
@raine
May 28 2015 22:24
it's difficult to remember
David Chambers
@davidchambers
May 28 2015 22:31
R.kv, perhaps, or R.keyVal?
Michael Hurley
@buzzdecafe
May 28 2015 22:31
i like kv or maybe pairUp
objectify ? :wink:
Raine Virta
@raine
May 28 2015 22:47
is there a solution when passing a function PFS would otherwise work but there's one extra argument in the front?
Hardy Jones
@joneshf
May 28 2015 22:49
PFS?
Raine Virta
@raine
May 28 2015 22:51
point-free style
Raine Virta
@raine
May 28 2015 22:56
like with filterIndexed you can't simply pass in a function that expects the index
Hardy Jones
@joneshf
May 28 2015 23:23
umm, so sort of
off hand you can get into semantic editor combinators, to do this sort of thing
but im not sure how it works with this auto-currying thing
i've never tried it
might not be an issue, might fail miserably
Scott Christopher
@scott-christopher
May 28 2015 23:32
Regarding mapping over a partial obj, you could also create a R.lens using R.pick such as:
var pickLens = (keys) => R.lens(
  R.pick(keys),
  (partialObj, obj) => R.merge(obj, R.pick(keys, partialObj))
)
Which can be used like:
var abLens = pickLens(['A', 'B']);
var obj = { 'A': 1, 'B': 2, 'C': 3 };

// Equivalent to R.pick
abLens(obj); //=> {"A": 1, "B": 2}

// Merge the provided object, restricted to the keys of the lens
abLens.set({ 'A': 9, 'D': 5 })(obj); //=> {"A": 9, "B": 2, "C": 3}

// Maps a function over a partial object filtered by keys
// of the lens then merged back into the provided object
var setToZero = R.mapObj(R.always(0));
abLens.map(setToZero)(obj); //=> {"A": 0, "B": 0, "C": 3}
And composed with other lenses like so:
var arrayOfObj = [{ 'A': 1, 'B': 2, 'C': 3 }, { 'A': 2, 'B': 4, 'C': 6 }];
var headABLens = R.composeL(abLens, R.lensIndex(0));

headABLens(arrayOfObj); //=> {"A": 1, "B": 2}
headABLens.set({ 'A': 9, 'D': 5 })(arrayOfObj); //=> [{"A": 9, "B": 2, "C": 3}, {"A": 2, "B": 4, "C": 6}]
headABLens.map(setToZero)(arrayOfObj); //=> [{"A": 0, "B": 0, "C": 3}, {"A": 2, "B": 4, "C": 6}]