These are chat archives for ramda/ramda

17th
Mar 2015
Eero Saynatkari
@rue
Mar 17 2015 03:12
It does require a bit of an adjustment… I’d say trying to use compose or pipe might lead one to a good initial path. Of course, on the other hand there is the possibility of vastly overcomplicating things. Lots of rope with which to hang oneself.
James Forbes
@JAForbes
Mar 17 2015 04:38

@GrosSacASac

I think the main advantage comes from combining simple functions to great effect. The automatic currying just makes building those combinations less noisy.

Ramda is a general purpose library. But because it's focus is composition, it really makes JS feel a lot less like Java and more like LEGO.

I found most articles on Ramda to be quite high concept with a lot of variable names likes foo and bar and often defaulting to simple functions like add to illustrate concepts. I don't think that is as helpful to understand the value of Ramda. I wrote a post demonstrating a practical application of Ramda in "real world" situation: http://james-forbes.com/#!posts/ramdaScrape

Raine Virta
@raine
Mar 17 2015 07:34
JAForbes: cool, is the code font intentionally non-monospace?
it's funny to read
Walle Cyril
@GrosSacASac
Mar 17 2015 14:39
thank you very much forbes
ratios = R.map(R.always(0), DATA); //is that a good way to create an array full of 0
?
Jason Marmon
@jtmarmon
Mar 17 2015 14:42
If you want an array of 0 you can do R.repeat(0, desiredArrayLength)
there are also a few real world-y examples at the bottom of this post: blog.jason.ly/why-you-should-use-ramdajs
Walle Cyril
@GrosSacASac
Mar 17 2015 14:43
ratios = R.repeat(0, DATA.length);
thanks
Jason Marmon
@jtmarmon
Mar 17 2015 15:40
is there a way to create a function using ramda that throws an error when called?
I have some test cases in mocha that use promises where I expect the response to be an error, meaning I need to throw an error on the .then case, so it would be nice to be able to do something like promiseWhichShouldFail.then(R.throw('Your tests suck.')).catch(...
Walle Cyril
@GrosSacASac
Mar 17 2015 16:05
you don t need Ramda to create functions like that
function () { throw new Error("Your test suck";}
Jason Marmon
@jtmarmon
Mar 17 2015 17:12
you also don't need ramda to create a list of zeros: var arr = []; while (count--) {arr.push(0);} but it certainly looks nicer doesn't it :stuck_out_tongue_closed_eyes:
John-David Dalton
@jdalton
Mar 17 2015 17:32
ES6 has Array#fill btw Array(3).fill(0);
Jason Marmon
@jtmarmon
Mar 17 2015 17:32
ahh es6. the promise land (literally and figuratively)
i need to start switching my code over...
John-David Dalton
@jdalton
Mar 17 2015 17:33
if you tweeted that I'd rt that lol
There's a String#repeat in ES6 too.

wow gitter is fancy. look at that embedding.

and yeah i've been meaning to learn yields generators and all that. kind of dreading converting my code to them

Martin Algesten
@algesten
Mar 17 2015 17:55
is into the only mutation function in ramda?
Jason Marmon
@jtmarmon
Mar 17 2015 17:58
@algesten as I just learned, lenses can mutate stuff as well
Martin Algesten
@algesten
Mar 17 2015 18:03
okay
Nate Wildermuth
@wildermuthn
Mar 17 2015 18:30
@jtmarmon I recommend http://babeljs.io (used to be 6to5 transpiler)
Danny Fritz
@dannyfritz
Mar 17 2015 20:24
anyone have a good idea on how to interpolate null values in an array?
[0, 1, null, null, 4, null, 8] => [0, 1, 2, 3, 4, 6, 8]
Walle Cyril
@GrosSacASac
Mar 17 2015 20:29
what do you mean with the word interpolate
Sergey Lapin
@lapanoid
Mar 17 2015 20:30
Can somebody explain why es6 is a way better than coffeescript? Except generators I accept that point)
Eero Saynatkari
@rue
Mar 17 2015 20:31
@lapanoid It’ll (eventually) run without separate compilation, which is objectively a good thing. In all other respects it is, in my opinion, inferior. Others may prefer it.
Sergey Lapin
@lapanoid
Mar 17 2015 20:33
@rue It is only point for early adopters ;)
I just find out for myself that generators is the only reason to switch from coffee
Walle Cyril
@GrosSacASac
Mar 17 2015 20:37
pretty much the same with typescript http://www.typescriptlang.org/
Martin Algesten
@algesten
Mar 17 2015 20:43
@lapanoid i suspect generators will be in coffee as well. in time.
Sergey Lapin
@lapanoid
Mar 17 2015 21:00
@algesten I hope so
@GrosSacASac I don't agree with you about typescript cause it's differs a lot from es5 or es6 or coffee, my confusion about es6 arises from great similarity with coffee.
Scott Sauyet
@CrossEye
Mar 17 2015 21:03
@dannyfritz are you looking to excise the values?
Danny Fritz
@dannyfritz
Mar 17 2015 21:04
what do you mean by excise? not looking to remove them
Scott Sauyet
@CrossEye
Mar 17 2015 21:04
That is do you want to return a list containing only the non-null values?
Then no. :smile:
Danny Fritz
@dannyfritz
Mar 17 2015 21:05
i think i've almost written what i need
pretty much, any time there are holes, fill them in with the interpolated values from the surrounding values
Scott Sauyet
@CrossEye
Mar 17 2015 21:06
Are you looking to get back [0, 1, 2, 3, 4, 6, 8]?
Danny Fritz
@dannyfritz
Mar 17 2015 21:06
[1, null, null, 4] would be [1, 2, 3, 4]
[1, null, 4] would be [1, 2.5, 4]
Martin Algesten
@algesten
Mar 17 2015 21:07
reduce combined with that flatMap which called something else in ramda...
Danny Fritz
@dannyfritz
Mar 17 2015 21:07
yeah, that sounds like what i'm doing @algesten. :)
Martin Algesten
@algesten
Mar 17 2015 21:08
chain! :D
Scott Sauyet
@CrossEye
Mar 17 2015 21:08
reduce isn't really enough. You'll need some local state to track the number of nulls.
Danny Fritz
@dannyfritz
Mar 17 2015 21:08
i'm doing 2 reduces in a row
yeah, first one goes through and reduces consecutive null's into an object with a count
Martin Algesten
@algesten
Mar 17 2015 21:08
should be possible one pass. but @CrossEye is right. we need one more var.
Danny Fritz
@dannyfritz
Mar 17 2015 21:09
the pass i'm writing now will expand it into interpolated values
Sergey Lapin
@lapanoid
Mar 17 2015 21:09
Maybe it cannot be solved so simple with a composition of funcs. U have to know smth about closesest values - neighbors
Danny Fritz
@dannyfritz
Mar 17 2015 21:09
one caveat is first or last value of the array cannot be null
Scott Sauyet
@CrossEye
Mar 17 2015 21:09
Makes sense. Could be done in a single pass with a more complex accumulator.
But then you'd have to do one more step to get your answer out.
Sergey Lapin
@lapanoid
Mar 17 2015 21:11
How to fill holes before u get next value? It cannot be one pass
Scott Sauyet
@CrossEye
Mar 17 2015 21:11
And what do you do with an initial or final null?
Martin Algesten
@algesten
Mar 17 2015 21:11
if only R.reduce gave us the index and entire array to look at in the callback :)
Scott Sauyet
@CrossEye
Mar 17 2015 21:13
There is a version of reduce like that. But we try not too talk about it in polite company.
Michael Hurley
@buzzdecafe
Mar 17 2015 21:17

@algesten

is into the only mutation function in ramda?

into should not be used to mutate values. That is abuse.

@jtmarmon

lenses can mutate stuff as well

same story here. We can't prevent you from using lenses to mutate, because this is JavaScript. but it is an anti-pattern.

Martin Algesten
@algesten
Mar 17 2015 21:17
@CrossEye ;)
@buzzdecafe i’m trying to get my head around into. or transducers (if that’s what it is). it looks like mutation to me :)
Danny Fritz
@dannyfritz
Mar 17 2015 21:21
not entirely sure of the robustness, but here it my first go at it: http://jsbin.com/sugesocege/3/edit?js,console
Jason Marmon
@jtmarmon
Mar 17 2015 21:45
What are your guys' thoughts on something like R.throw which would throw whatever you pass it as an exception. I'd find this quite useful for test cases and such
Danny Fritz
@dannyfritz
Mar 17 2015 21:49
@jtmarmon won't that make it look like errors are coming from the Ramda library all the time?
Jason Marmon
@jtmarmon
Mar 17 2015 21:50
hmm I suppose. but In test cases that doesn't really matter imo. just an extra line in your stack trace which you usually get with any assertion library
Danny Fritz
@dannyfritz
Mar 17 2015 21:55
i imagine that top line pointing to Ramda.js will cause plenty of confusion. ;)
Jason Marmon
@jtmarmon
Mar 17 2015 22:00
yeah i suppose since i'd only be using this for tests it just makes more sense as a utility function for my test suites
why won't you let me make everything ramda????????????? why do you hate ramda?????????? /s
Jethro Larson
@jethrolarson
Mar 17 2015 22:09
Fork it and add whatever you want :)
Sergey Lapin
@lapanoid
Mar 17 2015 22:25
I slightly understand trasdusers, could somebody tell me what lens is for http://ramdajs.com/docs/#lens ?
I mean in js, as I understood it is emulation of imperative mutation in haskell
Scott Sauyet
@CrossEye
Mar 17 2015 23:52
@dannyfritz, after errands and dinner, I created a somewhat different version:
var interpolate = function(data) {
  return R.reduce(function(soFar, val) {
    return R.isNil(val) ? 
      {nulls: soFar.nulls + 1, lastVal: soFar.lastVal, vals: soFar.vals} :
      {nulls: 0, lastVal: val, 
        vals: R.append(val, R.concat(soFar.vals, (soFar.nulls > 0) ? R.mapIndexed(function(_, idx) {
          return soFar.lastVal + (idx + 1) * (val - soFar.lastVal)/(soFar.nulls + 1);
        }, R.times(R.always(1), soFar.nulls)) : []))
      };
  }, {nulls: 0, lastVal: NaN, vals: []}, data).vals;
};
interpolate([1, 2, null, null, 4, null, 8); //=> [1, 2, 3, 4, 6, 8] 
But this breakdown might be a little cleaner:
var interp = function(start, end, count) {
  return (count > 0) ? R.mapIndexed(function(_, idx) {
    return start + (idx + 1) * (end - start)/(count + 1);
  }, R.times(R.always(1), count)) : [];
};

var interpolate = function(data) {
  return R.reduce(function(soFar, val) {
    return R.isNil(val) ? 
      {nulls: soFar.nulls + 1, lastVal: soFar.lastVal, vals: soFar.vals} :
      {nulls: 0, lastVal: val, 
        vals: R.append(val, R.concat(soFar.vals, interp(soFar.lastVal, val, soFar.nulls)))
      };
  }, {nulls: 0, lastVal: NaN, vals: []}, data).vals;
};
Walle Cyril
@GrosSacASac
Mar 17 2015 23:57
why do you not use R.ifElse instead of the ugly ? operator
Scott Sauyet
@CrossEye
Mar 17 2015 23:59
ifElse takes functions. These were plain expressions, which I didn't see any reason to wrap up.
You can see it in the REPL: http://tinyurl.com/lz7f5nt
This is not code to be proud of. It probably could be cleaned up a lot. But it seems to work. That's as far as I went.