These are chat archives for ramda/ramda

3rd
Aug 2016
Rick Medina
@rickmed
Aug 03 2016 01:38
hello guys! how can I merge objects but the arrays being concatenated instead of overwritten (for the same keys)?
Brad Compton (he/him)
@Bradcomp
Aug 03 2016 01:42
R.mergeWith
Brad Compton (he/him)
@Bradcomp
Aug 03 2016 01:42
R.mergeWithKey
Brad Compton (he/him)
@Bradcomp
Aug 03 2016 01:43
@rickmed One of those should be able to do what you want :-D
Rick Medina
@rickmed
Aug 03 2016 01:46
@Bradcomp and if I have n number objects (as opposed to just 2)? sorry I'm very new to ramda/fp
btw is ram-bot a real bot???
lets say a list of objects
Brad Compton (he/him)
@Bradcomp
Aug 03 2016 01:47
I don't think there's a mergeAll version of mergeWith, but you could use reduce to do it...
R.reduce(R.mergeWith(R.concat), {}, [/* array of source objects*/])
Yes, it's a real bot :)
Rick Medina
@rickmed
Aug 03 2016 01:50
@Bradcomp thanks
awesome!
btw, is there a guide to understand the function signatures?
the syntax
Brad Compton (he/him)
@Bradcomp
Aug 03 2016 01:51
Rick Medina
@rickmed
Aug 03 2016 01:53
cool, thanks! Seems there is a lot to learn! :sweat_smile:
Brad Compton (he/him)
@Bradcomp
Aug 03 2016 01:55
For sure! The nice thing is that you can pick up a little at a time though. And there are plenty of people here to help along the way.
James Forbes
@JAForbes
Aug 03 2016 01:57
@rjmk that worked, but I don't understand why. So if it was an arity of 3, you'd need to curryN(2,...)?
Rick Medina
@rickmed
Aug 03 2016 01:57
@Bradcomp thanks!
Scott Christopher
@scott-christopher
Aug 03 2016 02:28
@rjmk @JAForbes The arity issue with ap over functions should be resolved in master. Got caught out by it myself recently.
James Forbes
@JAForbes
Aug 03 2016 02:47
@scott-christopher wow! Really looking forward to the next release in that case
I often reach for converge, but hesitate because of identity being an eyesore
Scott Christopher
@scott-christopher
Aug 03 2016 03:44
Yeah, I think we're probably due for another release.
James Forbes
@JAForbes
Aug 03 2016 03:53

@scott-christopher, @rjmk thanks for your help, I just used chain on a function in a practical context for work for the first time.

const signatures = {
    '504b0304': 'local file header'
    ,'504b0708': 'data descriptor'
    ,'504b0608': 'archive extra data'
    ,'504b0102': 'central file header'
    ,'504b0505': 'encryption digital'
    ,'50b40606': 'zip64 eocdr'
    ,'504b0706': 'zip64 eocdr locator'
    ,'504b0506': 'eocdr'
}

const rl = readline.createInterface({
    input: fs.createReadStream(filepath)
})
    .on('line', R.pipe(
        R.split(' ')
        ,R.zipObj(['signature', 'sinceLastHeader', 'byteOffset'])
        ,R.chain(
            R.pipe(
                R.prop('signature')
                ,R.prop(R.__, signatures)
                ,R.objOf('signatureName')
            )
            ,R.merge
        )
    ))

I had a space separated file describing headers within a zip file, and I wanted to annotate the human readable name for the signatures.

Brad Compton (he/him)
@Bradcomp
Aug 03 2016 03:53
Thatr would also fix concat for the most recent node release!
Rafe
@rjmk
Aug 03 2016 09:00
@JAForbes The reason you need to reduce the arity by one is that one of the arguments to f in ap(f, g)(x) is going to be given to you by g(x). If f takes 2 arguments, ap(f,g)(x) === f(x, g(x)) is sufficient
+ nice use of chain!
Denis Stoyanov
@xgrommx
Aug 03 2016 10:23
@rjmk what is a ap? Applicative functor?
Michael Hurley
@buzzdecafe
Aug 03 2016 10:38
R.ap
Denis Stoyanov
@xgrommx
Aug 03 2016 10:44
@buzzdecafe I know, but ap fn a b => fn(a', b'), a' - each value of a, b' - each value of b
and a,b should be functors
Rafe
@rjmk
Aug 03 2016 10:52
@xgrommx ap Is the Ramda method R.ap as opposed to representing the applicative typeclass (is that what you're asking?)
Denis Stoyanov
@xgrommx
Aug 03 2016 11:42
@rjmk I'm not sure in this ap(f,g)(x) === f(x, g(x))
Oh! strange behaviour (*) <*> (*10) $ 10 => 1000
(*) 10 (10*10)
Aldwin Vlasblom
@Avaq
Aug 03 2016 12:10
Any way to write this point-free; a => b => f.map(f_ => f_(a)(b))? Its clearly a pattern but I don't recognize it. It seems like it should be doable with a simple operation like unlift2(f) or something.
Denis Stoyanov
@xgrommx
Aug 03 2016 12:12
@Avaq f is list?
Aldwin Vlasblom
@Avaq
Aug 03 2016 12:13
A Functor (a Maybe, actually).
So it has Apply and Chain too.
I've tried all sorts of sorcery with ap, but somehow I can't figure it out. I feel like I'm missing something blindingly obvious.
Denis Stoyanov
@xgrommx
Aug 03 2016 12:17
@Avaq in haskell it will be like let fn f a b = (f_ a b) <$> f ?
or let fn f a b = fmap (f_ a b) f
Aldwin Vlasblom
@Avaq
Aug 03 2016 12:18
Yeah, that's what I have, but can't it be point-free?
Denis Stoyanov
@xgrommx
Aug 03 2016 12:19
@Avaq let fn = flip (flip . (fmap .) . (f_))
Aldwin Vlasblom
@Avaq
Aug 03 2016 12:19
o.o Did you generate that somehow?
Denis Stoyanov
@xgrommx
Aug 03 2016 12:20
@Avaq sure, I use http://pointfree.io/
(flip . (fmap .) . (f_)) => compose(flip, (map ?), f_)
Aldwin Vlasblom
@Avaq
Aug 03 2016 12:21
There's such a symmetry to the expression f a b = fmap (f a b) f that it feels like I should be able to remove parts from both sides of the expression.
Denis Stoyanov
@xgrommx
Aug 03 2016 12:22
@Avaq maybe fmap (f a b) should be fmap (f_ a b)?
Aldwin Vlasblom
@Avaq
Aug 03 2016 12:23
Eh, I guess fmap (\f -> f a b)
Denis Stoyanov
@xgrommx
Aug 03 2016 12:23
@Avaq please provide all expression
Aldwin Vlasblom
@Avaq
Aug 03 2016 12:26

In JavaScript:

const f = Just(myBinaryFunction);
const fn = a => b => f.map(f_ => f_(a)(b));

In Haskell (I think, I'm new):

let fn a b = fmap (\f' -> f' a b) f where f = Just myBinaryFunction

I guess this is the same:

a => b => f.map(T(a)).map(T(b));

where T is the thrush combinator.

And this is also the same:
a => b => f.ap(f.of(a)).ap(f.of(b));
Denis Stoyanov
@xgrommx
Aug 03 2016 12:32
useWith(apply(lift(f)), [f.of, f.of])(a)(b) try this one
Aldwin Vlasblom
@Avaq
Aug 03 2016 12:33
So basically I have an applicative functor of a binary function, and I would like a general way to "unlift" that function such that the provided arguments do not need to be applicatives of the same type, but are applied as-is. The point is not to make it point-free, but to make it more concise. And hopefully learn some maths in the process. ^^
So "unlift" would look like: unlift2 :: Functor f => f (a -> b -> c) -> a -> b -> f c
I must be missing something.
Aldwin Vlasblom
@Avaq
Aug 03 2016 12:57
Am I asking a crazy question? Hoogle doesn't have stuff with that signature, despite it having a very straight forward implementation: let unlift f a = ($a) <$> f. I feel like I need this thing quite often, which makes me think I'm doing something very wrong.
Rafe
@rjmk
Aug 03 2016 12:59
@Avaq I don't think it's crazy at all. It's interesting as well, because this function doesn't require an Applicative instance, though it's doing something quite 'applicative-y'. I would be interested to know more about it if it ever turns up. I wonder if people just tend to do f <*> (pure a) <*> (pure b)
Aldwin Vlasblom
@Avaq
Aug 03 2016 13:44
@rjmk unlift2 can be defined as compose(compose(unlift))(unlift)
Denis Stoyanov
@xgrommx
Aug 03 2016 13:47
@Avaq :smile:
Risto Novik
@riston
Aug 03 2016 14:14
any node-config users here? How to cleanup the prototype object so I would get back pure js object without methods etc? Yes the JSON.parse(JSON.stringify(x)) does that but any other solutions?
Rick Medina
@rickmed
Aug 03 2016 14:51
@riston like this? "pureOBj" = Object.assign(Object.create(null), ObjectWithPrototype)
Denis Stoyanov
@xgrommx
Aug 03 2016 14:56
@rjmk please provide some case for f <*> (pure a) <*> (pure b)
Rafe
@rjmk
Aug 03 2016 15:03
@xgrommx Do you mean something like Just (+) <*> (pure 3) <*> (pure 2)?
It has the same behaviour as @Avaq's f.map(f_ => f_(a)(b)) (when we have an applicative instance)
Ryan Zeigler
@rzeigler
Aug 03 2016 20:08
what do you think the most concise way of merging 2 objects of the form {k: [v]} where I would like merge to concat the corresponding arrays
basically I'd like to be able to reduce the results of multiple calls to groupBy
Brad Compton (he/him)
@Bradcomp
Aug 03 2016 20:35
@arzig mergeWith(concat)?
Ryan Zeigler
@rzeigler
Aug 03 2016 20:35
i realized that the structure I showed was unclear
its actually, {k: {a, b, a: [v]}}
i think I really just do need to do a custom merge with
basically the nature of the merge means that all keys are fixed
and one of them is an array i want concat
Brad Compton (he/him)
@Bradcomp
Aug 03 2016 20:45
So for most of the fields it's a merge, but for one it's a mergeWtin(concat)?