These are chat archives for ramda/ramda

19th
Jun 2016
Scott Christopher
@scott-christopher
Jun 19 2016 00:42
@JoyKrishnaMondal: Perhaps an example showing what chain does just using the Function prototype instead of Reader might help.
//:: a -> (b -> a)
Function.of = a => _ => a

//:: a -> a
Function.ask = r => r

//:: (r -> a) -> r -> a
Function.asks = f => r => f(r)

//:: (r -> a) ~> (a -> (r -> b)) -> (r -> b)
Function.prototype.chain = function(f) {
  return r => f(this(r))(r)
}
I've added asks in there as a way to return some part of the "environment"
And then a trivial example:
//:: ({ NAME :: String }) -> String
const siteNameR = Function.asks(R.prop('NAME'))

//:: ({ TAGLINE :: String }) -> String
const siteTagLine = Function.asks(R.prop('TAGLINE'))

//:: ({ NAME :: String, TAGLINE :: String }) -> String
const heading = siteNameR.chain(
  name => siteTagLine.chain(
    tagline => Function.of(`<h1>${name}</h1><i>${tagline}</i>`)))

heading({ NAME: 'Some Website', TAGLINE: 'The place for things' })
// "<h1>Some Website</h1><i>The place for things</i>"
Joy Krishna Mondal
@JoyKrishnaMondal
Jun 19 2016 00:45
@scott-christopher thanks for the help !
Scott Christopher
@scott-christopher
Jun 19 2016 00:45
So you can see Reader doesn't do much beyond muddying up plain old function application.
Joy Krishna Mondal
@JoyKrishnaMondal
Jun 19 2016 00:46
let me try and rewrite it
Scott Christopher
@scott-christopher
Jun 19 2016 00:46
The Reader transformer can be a bit more useful, as it adds this "Dependency Injection" like behaviour to other monads
Joy Krishna Mondal
@JoyKrishnaMondal
Jun 19 2016 00:46
hmm
but my main question for that is now you have two ofs ?
Maybe.of
Reader.of
when you do ReaderwithMaybe.of
what magic happens ?
hmm. what is going on here :
f(this(r))(r)
// shouldn't it be this ?
f(this.r)(r)
Scott Christopher
@scott-christopher
Jun 19 2016 00:54
Replacing the arrow syntax:
Function.prototype.chain = function(f) {
  const firstFn = this;
  return function(r) {
    return f(firstFn(r))(r)
  }
}
In the version above, this is referencing the function that we're calling chain against.
And regarding needing of twice when combined with other monads, that's where the transformer can help out, because it takes care of combining the two.
Scott Christopher
@scott-christopher
Jun 19 2016 01:00

So using the ramda-fantasy example:

ReaderWithMaybe = Reader.T(Maybe)
rm42 = ReaderWithMaybe.of(42)

is equivalent to

rm42 = Reader.of(Maybe.of(42))
Joy Krishna Mondal
@JoyKrishnaMondal
Jun 19 2016 01:00
ahh
that makes so much more sense !
very clear !
Scott Christopher
@scott-christopher
Jun 19 2016 01:01
and the same happens with chain too
Joy Krishna Mondal
@JoyKrishnaMondal
Jun 19 2016 01:01
 ReaderWithMaybe.of(42) == Reader.of(Maybe.of(42))
Scott Christopher
@scott-christopher
Jun 19 2016 01:01
Effectively, yep.
They'll end up with slightly different types, but they're doing the same thing under the hood.
Joy Krishna Mondal
@JoyKrishnaMondal
Jun 19 2016 01:06
Function.prototype.chain = (f) -> (r) -> ((f (@ r) r)
r is the env ?
Scott Christopher
@scott-christopher
Jun 19 2016 01:13
yep
Baqer Mamouri
@bmamouri
Jun 19 2016 09:10
Hey guys, with R.concat how can you concat more than two lists?
Aldwin Vlasblom
@Avaq
Jun 19 2016 10:07
@bmamouri reduce(concat, []) or write your own util if the performance matters
Denis Stoyanov
@xgrommx
Jun 19 2016 10:32
@Avaq reduce is an operator of catamorphism really helpful :smile: