These are chat archives for ramda/ramda

18th
Oct 2016
Scott Sauyet
@CrossEye
Oct 18 2016 02:40
If anyone has experience with Ramda Fantasy Futures and unit tests, there's an open question on Stack Overflow looking for an answer.
Travis LaDuke
@laduke
Oct 18 2016 05:18
For just messing around, does it matter which ADT library I try to use (ramda fantasy, santuary, etc...)?
maybe ramda-fantasy has the least friction with ramda?
David Chambers
@davidchambers
Oct 18 2016 07:13
Which data types would you like to use, @laduke?
Keith Alexander
@kwijibo
Oct 18 2016 07:18
@laduke they're all good and frictionless. Ramda fantasy is in the Ramda repl and has good examples in the documentation.
Sanctuary also good docs and error messages, maybe in the repl too? but covers fewer types.
David Chambers
@davidchambers
Oct 18 2016 07:43
@kwijibo, S is also exposed by the Ramda REPL (and R is exposed on the Sanctuary website).
Keith Alexander
@kwijibo
Oct 18 2016 07:45
nice, thought so
Keith Alexander
@kwijibo
Oct 18 2016 08:42
Interesting: https://goo.gl/QmxQ5y
Ramda-fantasy Maybe.chain accepts Sanctuary's Maybes, but Sanctuary doesn't accept Ramda-fantasy Maybes
(I expected the latter, but not the former)
David Chambers
@davidchambers
Oct 18 2016 08:45
I imagine RF's Maybe.chain accepts anything. ;)
Travis LaDuke
@laduke
Oct 18 2016 14:21
At the moment, Maybe and Either
Ryan Zeigler
@rzeigler
Oct 18 2016 14:57
so, i have been experimenting with redux, I will say that a reducer that just runs provided lens functions is a lot more pleasant than a humongous action interpreter
ramda to the rescue :-)
Denis Stoyanov
@xgrommx
Oct 18 2016 15:58
@rzeigler sometime you don't need redux, setState has callback. https://jsbin.com/yasulac/2/edit?html,js,output
Ryan Zeigler
@rzeigler
Oct 18 2016 17:04
i'm actually working in angular 2 like a wierdo for this experiment
so I'll have to see if their implementation also exposes setState correctly
David Chambers
@davidchambers
Oct 18 2016 20:21

At the moment, Maybe and Either

In that case, @laduke, I suggest Sanctuary. With RF it's easy to write code with subtle type errors. For example:

@ram-bot
//    parseFloat_ :: String -> Maybe Number
const parseFloat_ = s => {
  const n = parseFloat(s);
  return isNaN(n) ? Maybe.Nothing() : Maybe.Just(n);
};

[R.chain(parseFloat_, Either.Left('XXX')),
 R.chain(parseFloat_, Either.Right('42'))];
ram-bot
@ram-bot
Oct 18 2016 20:21
[ _Left { value: 'XXX' }, _Just { value: 42 } ]
David Chambers
@davidchambers
Oct 18 2016 20:22
Note that in one case the result is of type Either String b; in the other, Maybe Number. Sanctuary would not allow this:
@ram-bot
R.chain(S.parseFloat, S.Right('42'));
ram-bot
@ram-bot
Oct 18 2016 20:23
Invalid value

Either#chain :: Either a b -> Function -> Either a c
                                          ^^^^^^^^^^
                                              1

1)  Just(42) :: Maybe Number, Maybe FiniteNumber, Maybe NonZeroFiniteNumber, Maybe Integer, Maybe ValidNumber

The value at position 1 is not a member of ‘Either a c’.
Travis LaDuke
@laduke
Oct 18 2016 20:35
I'm trying to compose a few functions and the second to last one returns a promise, but the rest don't. On first attempt, I can't just use composeP. What do people usually do?
Just make the other thing(s) return promises?
David Chambers
@davidchambers
Oct 18 2016 20:39
Good question, @laduke. You've run into exactly the reason I don't use promises! Let me show you how this would work with a principled data type such as a future.
Let's assume the last function in the composition is of type String -> Integer.
Let's assume the penultimate function (which currently returns a promise) is of type String -> Future Error String.
We have one function producing a value of type Future Error String, but the next function expects a value of type String. How do we reconcile this mismatch?
The answer is R.chain.
The function's general type is Chain m => (a -> m b) -> m a -> m b.
So what happens if we apply R.chain to our String -> Integer function?
Oops. In this case we actually want R.map.
The function's general type is Functor f => (a -> b) -> f a -> f b.
David Chambers
@davidchambers
Oct 18 2016 20:44
So what happens if we apply R.map to our String -> Integer function?
We'll get back a function of type Functor f => f String -> f Integer.
Future Error String matches Functor f => f String!
Travis LaDuke
@laduke
Oct 18 2016 20:45
o_0
David Chambers
@davidchambers
Oct 18 2016 20:46
Let's start with a simpler example of replacing the type-class constraint (Functor f).
Travis LaDuke
@laduke
Oct 18 2016 20:47
I think I'm following, but it'll take me some time to get working
David Chambers
@davidchambers
Oct 18 2016 20:47
Let's replace Functor f => f with Array. This gives (a -> b) -> Array a -> Array b, which is the type of map specialized to Array.
We could just as easily replace Functor f => f with Maybe, giving (a -> b) -> Maybe a -> Maybe b.
Specializing to Future is a tiny bit different, because Future has two type parameters (one for the "failure" type and one for the "success" type).
Travis LaDuke
@laduke
Oct 18 2016 20:49
I may have XY'd you
David Chambers
@davidchambers
Oct 18 2016 20:49
XY?
Travis LaDuke
@laduke
Oct 18 2016 20:50
I was trying to wrap a mongo call that returns a promise
xy problem
David Chambers
@davidchambers
Oct 18 2016 20:50
Yeah. My suggestion is to wrap promise-returning functions so they return futures instead. ;)
Travis LaDuke
@laduke
Oct 18 2016 20:50
oh gotcha
not "use futures instead"
David Chambers
@davidchambers
Oct 18 2016 20:51
Then interacting with the return values becomes a joy as one can use R.map and R.chain and avoid most of the complexity of promises.
Travis LaDuke
@laduke
Oct 18 2016 20:53
gracias
David Chambers
@davidchambers
Oct 18 2016 20:53
You may be interested in futurize.
You're welcome. :)
Brad Compton (he/him)
@Bradcomp
Oct 18 2016 20:54
@laduke It's not that up to date, but you may be interested in the code I used to wrap mongo calls in futures: https://github.com/Bradcomp/egghunt-server/blob/master/lib/mongo.js
Travis LaDuke
@laduke
Oct 18 2016 20:56
whoa
Brad Compton (he/him)
@Bradcomp
Oct 18 2016 20:56
Sorry, it's a little dense ;).
Travis LaDuke
@laduke
Oct 18 2016 20:58
I was imagining what I would write would look like: a little bigger
(a lot)
Brad Compton (he/him)
@Bradcomp
Oct 18 2016 20:58
The important thing to note is that in most cases it will return a Future, but in the case of a findOne it returns a Future e (Maybe doc)
Travis LaDuke
@laduke
Oct 18 2016 22:37
@Bradcomp how did not need to use mongo's ObjectId() ?
Brad Compton (he/him)
@Bradcomp
Oct 18 2016 22:38
What do you mean?
Travis LaDuke
@laduke
Oct 18 2016 22:39
Their auto-generated _id's are special objects
looks like you make your own ids.
Brad Compton (he/him)
@Bradcomp
Oct 18 2016 22:40
I just don't use the auto-generated ones
Travis LaDuke
@laduke
Oct 18 2016 22:40
:)
Brad Compton (he/him)
@Bradcomp
Oct 18 2016 22:40
They're still there though
Travis LaDuke
@laduke
Oct 18 2016 22:41
I was just excited that I put their function in an Either and it worked.
Brad Compton (he/him)
@Bradcomp
Oct 18 2016 22:42
Awesome!