These are chat archives for ramda/ramda

5th
May 2015
Jethro Larson
@jethrolarson
May 05 2015 02:01
Indeed
Brandon Wilhite
@JediMindtrick
May 05 2015 02:20
One of the things that bugs me a tad about FP is that debugging always seems harder than it should be, even though in a lot of ways it should be easier...so that looks like a good find
Scott Sauyet
@CrossEye
May 05 2015 02:47
Debugging FP is definitely best when you don't have investigate library code but can simply take that for granted. But many debugging tools are designed for imperative systems and don't deal well with declarative ones. (Ever try to debug XSLT or SQL?) Of course the ideal is expressed by the Tony Hoare quote: "[T]here are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies." FP is an attempt to achieve the former.
Hardy Jones
@joneshf
May 05 2015 03:35
holy crap!
that is cool
Brandon Wilhite
@JediMindtrick
May 05 2015 11:39
@CrossEye I 100% agree with everything you said. I also think FP does make massive strides in helping us build simple code (but not always easy). Still I have to wonder if we can still do better. Specifically I'm thinking of some of the things the LightTable IDE was trying to accomplish by printing out intermediate results in composed/chained functions. So part of me is sitting here wondering if I can modify this library to do that for me when I want to.
I may take a crack at it.
Raine Virta
@raine
May 05 2015 11:40
@JediMindtrick you mean like this? https://github.com/raine/treis
Brandon Wilhite
@JediMindtrick
May 05 2015 11:41
lol @raine ummm...yes! Thanks!
Brandon Wilhite
@JediMindtrick
May 05 2015 11:53
I think this will help me a lot.
Hardy Jones
@joneshf
May 05 2015 12:14
@JediMindtrick have you looked at racket?
Brandon Wilhite
@JediMindtrick
May 05 2015 12:19
Nope.
But I will :)
So far my fp journey has included dives into both Clojure and Scala, but at work I'm stuck with javascript and C#, so I don't have as much opportunity to apply as I'd like
This is why I was happy to find Ramda, b/c I can certainly fit that to my js...and it's sorta more correct than lo-dash, for example. Just meaning I never felt like I could get my code as succinct with lo-dash as I should have been able.
Racket looks interesting though.
Raine Virta
@raine
May 05 2015 12:25
I was thinking about having a look at racket accompanied with the little schemer
Hardy Jones
@joneshf
May 05 2015 12:29
@JediMindtrick oh, well that's the problem, you've been looking at clojure and scala, no wonder it's hard to debug ;)
Brandon Wilhite
@JediMindtrick
May 05 2015 12:52
@joneshf :)
Michael Hurley
@buzzdecafe
May 05 2015 13:29
<3 racket
Dalibor Novak
@BorePlusPlus
May 05 2015 17:06
What is the most elegant way to write a function that takes an array and returns undefined if that array is empty?
Hardy Jones
@joneshf
May 05 2015 17:29
@BorePlusPlus what does it return if the array is not empty?
Dalibor Novak
@BorePlusPlus
May 05 2015 17:29
array itself
the proverbial issue of incomplete requirements eh?
Hardy Jones
@joneshf
May 05 2015 17:30
Why not:
function wat(xs) {
  return xs.length === 0 ? undefined : xs;
}
Dalibor Novak
@BorePlusPlus
May 05 2015 17:31
This is almost what I have now - I was wondering if there is a more elegant way...
Viktor Fröberg
@vikfroberg
May 05 2015 17:32
What's not elegant about it?
Dalibor Novak
@BorePlusPlus
May 05 2015 17:34
it seems to take more space than needed :wink: In my brief wonder through clojure land I found seq quite handy. It it essentially doing what I want to do...
Hardy Jones
@joneshf
May 05 2015 17:35

My feeling is that perhaps this function is either a simplification of the actual task, or it's not actually necessary.

Surely you're going to do something with the result of that function, maybe you're going to continue with the task, or maybe you're going to display an error and get out of there, or maybe you're going to move to a differnt computation.

Does that seem right?

Dalibor Novak
@BorePlusPlus
May 05 2015 17:35
I would like to assoc the result of this function into a map
Hardy Jones
@joneshf
May 05 2015 17:37
I see.
Dalibor Novak
@BorePlusPlus
May 05 2015 17:37
I will eventually emit this map as JSON and would prefer to omit the field if it is empty
which JSON.stringify will do for undefined values
Hardy Jones
@joneshf
May 05 2015 17:39
it seems like another option would be to not even add it to the object.
Dalibor Novak
@BorePlusPlus
May 05 2015 17:39
sure thing
Hardy Jones
@joneshf
May 05 2015 17:39
i guess it depends on the bigger picture.
Dalibor Novak
@BorePlusPlus
May 05 2015 17:39
which then leads to another if
disclosure: I find ifs a bit ugly
Hardy Jones
@joneshf
May 05 2015 17:40
well there's ways around that, depending on what you're working with.
Dalibor Novak
@BorePlusPlus
May 05 2015 17:41
vanilla js with ramda
Hardy Jones
@joneshf
May 05 2015 17:46
you can use ifElse, but I think that is less elegant:
R.ifElse(R.isEmpty, R.always(undefined), R.identity)
Dalibor Novak
@BorePlusPlus
May 05 2015 17:46
strangely enough I find it quite appealing (I like the uniformity of it - just function calls)
Hardy Jones
@joneshf
May 05 2015 17:46
Sorry, i meant what kind of data you're working with.
Dalibor Novak
@BorePlusPlus
May 05 2015 17:47
cheers for the find
Viktor Fröberg
@vikfroberg
May 05 2015 17:49
@joneshf I though of that too, you could also do it with R.cond, but I agree that it's less elegant.
Hardy Jones
@joneshf
May 05 2015 17:52
I think probably the most elegant approach is to delay this until JSON.stringify
JSON.stringify({foo: [1,2,3], bar: []}, function(k, xs) { return xs.length === 0 ? undefined : xs; }); //=> "{"foo":[1,2,3]}"
Dalibor Novak
@BorePlusPlus
May 05 2015 17:52
I ended up with
R.ifElse(R.isEmpty, R.always(value), R.assoc('dependencies', R.__, value))
Hardy Jones
@joneshf
May 05 2015 17:52
or something similar.
Dalibor Novak
@BorePlusPlus
May 05 2015 17:52
@joneshf I agree that is quite simple
Hardy Jones
@joneshf
May 05 2015 17:53
the reason I feel that's more elegant is that you're separating concerns better. The object you create can be interpreted a certain way by JSON.stringify, or a different way by some other "interpreter".
plus it makes the rest of your task simpler
Dalibor Novak
@BorePlusPlus
May 05 2015 17:54
I like it
Viktor Fröberg
@vikfroberg
May 05 2015 18:17
Is there anything I can do to make this more readable or is it good enough? Also I don't like that I'm accessing events and todos outside of the scope. http://bit.ly/1GXgBJC
Hardy Jones
@joneshf
May 05 2015 18:29
what scope do you mean?
Viktor Fröberg
@vikfroberg
May 05 2015 18:31
For example R.find(R.where({_id: todo._id}), events);
Hardy Jones
@joneshf
May 05 2015 18:33
sorry, what's out of scope in that line, events?
Viktor Fröberg
@vikfroberg
May 05 2015 18:35
Right, It's not clear where it comes from. I guess I could just wrap the map in a function that takes todos and events as argument.
Hardy Jones
@joneshf
May 05 2015 18:39
Yeah, you can do that. I don't know if there's much of a problem with having free variables in a function though. if you think about it, R is free as well.
Viktor Fröberg
@vikfroberg
May 05 2015 18:41
Yeah, but R is a library. My concern is basically that the function doesn't always return the same output when provided with the same input.
R is less likely to change, whereas events and todos are data which most certainly will change.
Hardy Jones
@joneshf
May 05 2015 18:46
ah, makes sense.
Viktor Fröberg
@vikfroberg
May 05 2015 18:48
Although in the case of findOrCreateTodo is feel quite ugly to pass todos as an argument.
it feels*
hmm, findOrCreateTodo is more of a database operation I guess ie. global state; so I guess it's fine in that case.
Scott Sauyet
@CrossEye
May 05 2015 19:41
Why not pass it in as a possibly-curried initial parameter? This might also make it more testable.
Hardy Jones
@joneshf
May 05 2015 19:46
Yeah, no reason to force impurity.
Viktor Fröberg
@vikfroberg
May 05 2015 19:55
I'm not sure I follow, could you give an example?
Viktor Fröberg
@vikfroberg
May 05 2015 20:05
Oh, I think I get it. Something like this? var findOrCreateTodo = R.curry(function(todos, _id, date) {})(todos);
Scott Sauyet
@CrossEye
May 05 2015 21:49
Well, not quite. Name the general function. Then call it with todos to get a more specific function if you like.
Viktor Fröberg
@vikfroberg
May 05 2015 21:51
Got it
Interesting solution, thanks for sharing!