These are chat archives for ramda/ramda

24th
May 2017
Rick Medina
@rickmed
May 24 2017 12:29
http://raganwald.com/2013/04/08/functional-vs-OOP.html has been on the twitters lately...
Bijoy Thomas
@bijoythomas
May 24 2017 13:03

Good Morning, Good Afternoon all .. I have a question on checking if an object satisfies all conditions. As an example:

compose(
  all(identity),
  ap([propEq('length', 5), is(String), ap(equals, toLower)]),
  of
)("hello")

It works but requires wrapping the string in an array. So, is there a better way of doing this and maybe a way more efficient so that the first falsey skips the later checks?

Ryan Zeigler
@rzeigler
May 24 2017 13:04
@bijoythomas not really relevant to the structure, but, there is an allPass
hopefully, it shortcircuits internally
Bijoy Thomas
@bijoythomas
May 24 2017 13:07
@rzeigler thank you! looks like it does.
Denis Stoyanov
@xgrommx
May 24 2017 13:35
This message was deleted
Stephan Meijer
@smeijer
May 24 2017 15:11
How do I turn this into something that I only need to provide z once?
x(y(z))(z)
David Chambers
@davidchambers
May 24 2017 15:26

That's R.ap(R.flip(x), y)(z), @smeijer. For example:

> R.ap(R.flip(R.concat), R.toUpper)('foo')
'FOOfoo'

It's nearly the S combinator (see S.ap for an explanation).

Actually, ignore the R.flip part:

> R.ap(R.concat, R.toUpper)('foo')
'fooFOO'

This is the S combinator.

Brad Compton (he/him)
@Bradcomp
May 24 2017 15:29
useWith(x, [y, a => a])(z) will work too!
@davidchambers It looks like Sanctuary drastically reduced the combinators it includes?
David Chambers
@davidchambers
May 24 2017 15:34
They're still there, just with different names and more general types. ;)
@silly-goat
S.ap(S.concat, S.toUpper)('foo')
Where did the :goat: go, I wonder?
See sanctuary-js/sanctuary#330, @Bradcomp.
Brad Compton (he/him)
@Bradcomp
May 24 2017 15:37
:goat: is in the Sanctuary gitter :stuck_out_tongue: but I think @ram-bot might be down too
R.ap
Yup
David Chambers
@davidchambers
May 24 2017 15:39
Oops. It was me that was lost. I've updated the link, @Bradcomp. I wrote #330 thinking I was in the Sanctuary room. I should have included the repository name.
Brad Compton (he/him)
@Bradcomp
May 24 2017 15:39
Haha, I found it anyway. Was just asking because I was going to point to S as well, then saw it was gone
David Chambers
@davidchambers
May 24 2017 16:00
I did the same thing!
Searching within the document for "combinator" finds the more general functions.
Joey Figaro
@joeyfigaro
May 24 2017 16:11
@Bradcomp If you’ve got some time at some point, would love your thoughts on this: https://goo.gl/2MD277
Basically that same array of monthly metrics exists on objects in an array - need to map over that new array, find the metrics key, map over that, and apply what we had done yesterday with the totaling…but also need to create objects for each period of time. Last six months, previous month, etc.
This message was deleted
little torn between evolve/assoc
Robert Mennell
@skatcat31
May 24 2017 16:16
@joeyfigaro sinds liek a case for converge: http://ramdajs.com/docs/#converge
Transform into what you need to opperate one, send it to your mulitple intermediate functions, send those outputs to an injestor, and then use the output of hte ingestor for context in the next function(or just return all transformations)
Joey Figaro
@joeyfigaro
May 24 2017 16:16
Thanks @skatcat31 - I’m unfamiliar with ingestor
Are you using it generally, or is it something more specific?
Jason Lengstorf
@jlengstorf
May 24 2017 16:19

I feel like I knew this once, but if I want to get the sum of the result of three different functions, what's the appropriate Ramda function for that?

const fn1 = prop('foo');
const fn2 = prop('bar');
const fn3 = prop('bat');
const values = WHAT_AM_I(reduce(add, 0), [fn1, fn2, fn3])(valueObj);

Goal: I want the output of fn1, fn2, and fn3 to be passed to reduce as a List.

Denis Stoyanov
@xgrommx
May 24 2017 16:19
@jlengstorf lens?
@jlengstorf please provide example and what do u want to get
Robert Mennell
@skatcat31
May 24 2017 16:20

generally. It's a function that you specify to unify inputs:

function ingest(...args){ return args }

This is a simple ingestor that will return an array of any an all arguments passed to it. Since converge will pass the array in order supplied, you should be able to build whatever you want with your ingestor function

Joey Figaro
@joeyfigaro
May 24 2017 16:22
@skatcat31 Excellent - thanks!
Denis Stoyanov
@xgrommx
May 24 2017 16:22
@skatcat31 unapply(identity)
Jason Lengstorf
@jlengstorf
May 24 2017 16:22
@xgrommx This is the outcome I want, but I feel like there's a way to skip the intermediate creation of an array: https://goo.gl/bhb5qn
Robert Mennell
@skatcat31
May 24 2017 16:23
@xgrommx yeah that is what I just showed XD
Denis Stoyanov
@xgrommx
May 24 2017 16:25
@jlengstorf compose(sum, sequence(always, [prop('foo'), prop('bar'), prop('baz')]))(valueObj)
Jason Lengstorf
@jlengstorf
May 24 2017 16:26
@xgrommx Oh, nice. I didn't realize sequence would do that.
Thanks!
Denis Stoyanov
@xgrommx
May 24 2017 16:29
@jlengstorf also y can use it more useful useWith((x, y) => sequence(always, x)(y), [map(prop), identity])(['foo', 'bar', 'baz'], valueObj)
Jason Lengstorf
@jlengstorf
May 24 2017 16:31
Nice. In this case, I just needed to get a subtotal from three other functions that calculate usage, which is called in applySpec — I'm taking a note on this for the future, though.
Denis Stoyanov
@xgrommx
May 24 2017 16:31
@jlengstorf also compose(sum, values, pickAll(['foo', 'bar', 'baz']))(valueObj)
@jlengstorf maybe u should use it!) compose(sum, props(['foo', 'bar', 'baz']))(valueObj) :smile:
Jason Lengstorf
@jlengstorf
May 24 2017 16:34
@xgrommx I've currently got it set up like this:
const getCostSummary = path(['summary', 'billable_usage']);
const getRuntimeTotalCost = compose(prop('runtime_cost'), getCostSummary);
const getServicesTotalCost = compose(prop('services_cost'), getCostSummary);
const getThirdPartyTotalCost = compose(prop('third_party_services_cost'), getCostSummary);
const getTotalCostEstimate = compose(sum, sequence(always, [
  getRuntimeTotalCost,
  getServicesTotalCost,
  getThirdPartyTotalCost,
]));
I can't think of how else I would be able to do it without making this horrendously hard for someone else to read.
Denis Stoyanov
@xgrommx
May 24 2017 16:36
@jlengstorf I think it is good
Jason Lengstorf
@jlengstorf
May 24 2017 16:36
Awesome. Thanks!
Denis Stoyanov
@xgrommx
May 24 2017 16:38
@jlengstorf zipWith(call, map(prop, ['foo', 'bar', 'baz']), repeat(valueObj, 3)) :smile:
@jlengstorf awful :smile: compose(sum, map(nth(1)), filter(compose(contains(__, ['foo', 'bar', 'baz']), head)), toPairs)(valueObj)
Jason Lengstorf
@jlengstorf
May 24 2017 16:41
Haha — you could store that one as jobSecurityFn
Bijoy Thomas
@bijoythomas
May 24 2017 16:41
lol
Otto Nascarella
@ottonascarella
May 24 2017 17:50
question, ramda-friends:
Promise.all vs R.pipeP....pros and cons
Brad Compton (he/him)
@Bradcomp
May 24 2017 17:51
They do different things. Promise.all is parallel, more similar to sequence
Otto Nascarella
@ottonascarella
May 24 2017 17:52
ok, then.
pipeP would be more like promise.then(p => anotherpromise.then(...something with first promise)....
what about traverse? how would you substitute Promise.all with traverse?
its kinda doable, right?
Brad Compton (he/him)
@Bradcomp
May 24 2017 17:54
No :(
Bijoy Thomas
@bijoythomas
May 24 2017 17:54
@ottonascarella you might want to look into futures and tasks .. don't think the native Promise works with traverse
Brad Compton (he/him)
@Bradcomp
May 24 2017 17:54
Well...
You'd need to add the relevant methods to the prototype
At which point I would suggest using Futures
Denis Stoyanov
@xgrommx
May 24 2017 17:58
@bijoythomas so if u extend Promise with ap and of u can use sequence
Object.assign(Promise.prototype, { ap: x => this.chain(f => x.chain(v => f(v))) }), Promise.of = Promise.resolve
sequence(Promise.of, [p1, p2..])
Bijoy Thomas
@bijoythomas
May 24 2017 18:02
is chain something separate?
Denis Stoyanov
@xgrommx
May 24 2017 18:03
what?
oh
instead of chain use then I forgot about it
Otto Nascarella
@ottonascarella
May 24 2017 18:05
@Bradcomp I imagined so, guys. So annoying Promises.
Denis Stoyanov
@xgrommx
May 24 2017 18:06
yes, use Fluture :smile:
Otto Nascarella
@ottonascarella
May 24 2017 18:06
@xgrommx nice. still...some people would say: dont extend Promise
Robert Mennell
@skatcat31
May 24 2017 18:06
That has always made me wonder... What's the performance of Fluture versus promises or gens?
I feel like it's just really for FP purposes?
Otto Nascarella
@ottonascarella
May 24 2017 18:06
I like the old data.task...its simpler...
There are some comments on perf in that article
wait, nevermind
Bijoy Thomas
@bijoythomas
May 24 2017 18:11
@xgrommx that's so cool .. just tried it out .. ap(Promise.of(inc), Promise.of(54)) .. it works! I did have to change the ap form fat arrow function to regular one to get the right this binding
Robert Mennell
@skatcat31
May 24 2017 18:11
@Bradcomp I keep forgetting people use things other than Bluebird for promises...
Denis Stoyanov
@xgrommx
May 24 2017 18:13
@bijoythomas I didn't test it))
Bijoy Thomas
@bijoythomas
May 24 2017 18:15
Ah ok .. I got excited and tried it out in the REPL right away :-)
Otto Nascarella
@ottonascarella
May 24 2017 18:18
@xgrommx nice this creed thing. quite small too...still, data.task is just too small to be forgot.
Robert Mennell
@skatcat31
May 24 2017 18:19
@xgrommx I thought arrow fuinctions don't play nicely with this refences?
Denis Stoyanov
@xgrommx
May 24 2017 18:21
@skatcat31 I wrote this code without editor :smile:
Robert Mennell
@skatcat31
May 24 2017 18:21
@xgrommx don't worry about it. I sitll end up rereading WTF this is constantly
Robert Mennell
@skatcat31
May 24 2017 18:32
for anyone else that is curious of the dangers of arrows functions and this: https://derickbailey.com/2015/09/28/do-es6-arrow-functions-really-solve-this-in-javascript/
Bijoy Thomas
@bijoythomas
May 24 2017 18:33
@xgrommx to get the sequence working on Promise, I had to add the map on it too:
Object.assign(Promise.prototype, {
  ap: function(x) {
    return this.
    then(f => x.then(v => f(v))) 
  },
  map: function(f) {
    return this.then(x => f(x))
  }
})
Promise.of = Promise.resolve.bind(Promise)

sequence(Promise.of, [Promise.resolve(1), Promise.resolve(2)])
thanks for getting me started on this interesting little exercise :-)
Denis Stoyanov
@xgrommx
May 24 2017 18:40
@bijoythomas https://goo.gl/ZjMniV
foldrM = cataM
Bijoy Thomas
@bijoythomas
May 24 2017 18:52
ok .. that is going to take me some time to grok
Denis Stoyanov
@xgrommx
May 24 2017 18:55
foldrM(\x a -> fmap (+a) x) 0 [[10], [20], [30]] -- ^ [60]
the same result fmap sum $ sequence [[10], [20], [30]]
fmap(foldr(+) 0) $ sequence [[10], [20], [30]]
as u can see we just lift foldr to monad container
Robert Mennell
@skatcat31
May 24 2017 19:17
@xgrommx creed is interesting, but I think I'd take the hit over using it and use fluture