These are chat archives for ramda/ramda

18th
Jul 2016
Rob Halff
@rhalff
Jul 18 2016 00:56
@Emilios1995 the has from lodash fp seems to work fine: has('request.headers')({request: {headers: {}}})) or has(['request', 'headers'])({request: {headers: {}}}))
pathSatisfies seems to be made for more complex comparisons.

Right now I'm struggling to create a pure function of this:

const def = {
    a: 'set-me',
    b: {
        c: 'use-this-value'
    }
};

(def) => {
  def.a = def.b.c;
  return def;
}

Seems simple but I cannot find the right combination.

Emilio Srougo
@Emilios1995
Jul 18 2016 01:06
@rhalff I would use lenses for this.
Rob Halff
@rhalff
Jul 18 2016 01:08
@Emilios1995 yeah I've tried, but still not sure how to compose it so I get the def back with the updated version.
Emilio Srougo
@Emilios1995
Jul 18 2016 01:27
@rhalff quick example http://goo.gl/1l6l2w
Rob Halff
@rhalff
Jul 18 2016 01:36
@Emilios1995 thanks, that should work, the only thing I dislike is def needs to be passed in twice, as in it's not merely flowing through.
Scott Christopher
@scott-christopher
Jul 18 2016 02:11
@rhalff You can use converge to help out there.
@ram-bot
const def = {
  b: {
    c: 'use-this-value'
  }
}

const fn = converge(assoc('a'), [path(['b', 'c']), identity])

fn(def)
ram-bot
@ram-bot
Jul 18 2016 02:12
{ b: { c: 'use-this-value' }, a: 'use-this-value' }
Scott Christopher
@scott-christopher
Jul 18 2016 02:13
Or (perhaps a little more confusingly) you could use the chain implementation for functions to remove the use of identity in converge.
@ram-bot
const def = {
  b: {
    c: 'use-this-value'
  }
}
const fn = chain(path(['b', 'c']), assoc('a'))
fn(def)
ram-bot
@ram-bot
Jul 18 2016 02:13
{ b: { c: 'use-this-value' }, a: 'use-this-value' }
Rob Halff
@rhalff
Jul 18 2016 02:20
@scott-christopher thanks a lot, both look good, I've neither tried chain or converge yet.
Ryan Zeigler
@rzeigler
Jul 18 2016 02:22
could someone explain to me the correct way to use tryCatch, what I thought was fairly simple from the docs doesn't actually seem to work in the repl. In particular when I load the example I get output that seems wrong.
> R.tryCatch(R.prop('x'), R.F, {x: true})
[Function]
James Forbes
@JAForbes
Jul 18 2016 02:42
@scott-christopher I'm still trying to wrap my head around this, I've read everything you guys have said but it still hasn't clicked :)
Emilio Srougo
@Emilios1995
Jul 18 2016 03:08
@scott-christopher what's the chain implementation for functions?
Emilio Srougo
@Emilios1995
Jul 18 2016 03:26
@scott-christopher Oh I see. It's something like this
Function.prototype.chain = f => x => this(f(x))(x)
Is there a name for this kind of combinator?
Brad Compton (he/him)
@Bradcomp
Jul 18 2016 03:30
@ram-bot
R.tryCatch(R.prop('x'), R.F)({x: true})
ram-bot
@ram-bot
Jul 18 2016 03:30
true
Brad Compton (he/him)
@Bradcomp
Jul 18 2016 03:31
R.tryCatch
Brad Compton (he/him)
@Bradcomp
Jul 18 2016 03:32
@arzig R.tryCatch returns a function that wraps the original function. Because of this, you can't pass in your function arguments to tryCatch, but need to pass them to the function it returns.
JavaScript being JavaScript, it doesn't have any problem with you passing three variables to a function that only takes two though, so your original function you constructed will do the same thing as mine if you pass an object to both of them :-D
Scott Christopher
@scott-christopher
Jul 18 2016 04:53
If you're familiar with the converge function, practically you can think of ap and chain over functions like so:
// f :: a -> b -> c
// g :: a -> b
ap(f, g) = converge(f, [identity, g])
// f :: b -> a -> c
// g :: a -> b
chain(g, f) = converge(f, [g, identity]);
I'm not sure whether there is a named combinator that represents the chain implementation. (I'm not very familiar with combinatory logic)
James Forbes
@JAForbes
Jul 18 2016 06:59
@scott-christopher thank you! That is a great example, I'm still not 100% there but I can see the light.
Rafe
@rjmk
Jul 18 2016 08:28

@JAForbes I don't know if this is helpful (let me know if not), but here's the type substitution:

a Chain has to have one free type variable (like List needs to be applied to a type like String to become a type -- a list of strings). The function constructor (->) has two free type variables -- the type it takes in, and the type it produces. If you partially apply the function constructor to its first argument, you end up with a constructor with one free type argument. So, a function from a fixed type r can have Chain (and functor, and applicative ...) implemented for it. So forall a. r -> ais our basis of a chain implementation for functions.

Then we need to define chain :: (a -> Chain b) -> Chain a -> Chain b for our instance of Chain for functions. So we need a function that looks like chain :: (a -> (r -> b)) -> (r -> a) -> r -> b. So we need to end up with a b. We can see a way to do that with an r buried in the first argument -- but we need to give it an a first! Fortunately, we have a way to make an a with an r -- the second argument. So we feed r to the second argument to get the a, then feed the a and the r again to the first argument, and get our b!

Bravi
@Bravilogy
Jul 18 2016 09:25
hi guys. I'm trying to implement this type of search:
[
   {name: 'John', age: 28},
   {name: 'Jane', age: 26}
]

const find = (searchTerm) => {
   return R.filter(
            R.compose(
                R.any(
                    R.map((key, value) => {
                        R.propEq(key, value)
                    }, searchTerm)
                )
            ), this.data
        );
}

// and then something like
var result = find({age: 26});
I put this in a separate const find for this example
but this function actually exists within an object
so that's why I have this.data there
but this doesn't work. gives me a weird answer
Bravi
@Bravilogy
Jul 18 2016 09:48

nevermind :D it's so much simpler..

return R.filter(R.whereEq(searchTerm));

that's it..

Denis Stoyanov
@xgrommx
Jul 18 2016 09:50
As I told earlier, we don't need converge because we have lift and sequence + reduce fn
James Forbes
@JAForbes
Jul 18 2016 10:15
@rjmk that did help, I think I actually get it now! :) I'm going to try it out in the REPL for a while and let it sink in. But that makes a lot of sense. Thank you
Denis Stoyanov
@xgrommx
Jul 18 2016 10:19
full fp style :smile:
@ram-bot
filter(lift(and)(compose(not, modulo(__, 2)), flip(gt)(3)))(range(0, 100))
@ram-bot
filter(lift(and)(compose(not, modulo(__, 2)), flip(gt)(3)))(range(0, 100))
ram-bot
@ram-bot
Jul 18 2016 10:19
[ 4,
  6,
  8,
  10,
  12,
  14,
  16,
  18,
  20,
  22,
  24,
  26,
  28,
  30,
  32,
  34,
  36,
  38,
  40,
  42,
  44,
  46,
  48,
  50,
  52,
  54,
  56,
  58,
  60,
  62,
  64,
  66,
  68,
  70,
  72,
  74,
  76,
  78,
  80,
  82,
  84,
  86,
  88,
  90,
  92,
  94,
  96,
  98 ]
Denis Stoyanov
@xgrommx
Jul 18 2016 10:20
lifting for functions! because function has applicative functor
the same with converge filter(converge(and, [compose(not, modulo(__, 2)), flip(gt)(3)]))(range(0, 100))
Denis Stoyanov
@xgrommx
Jul 18 2016 10:26
converge = curry((f, fns) => lift(f)(...fns))
Bravi
@Bravilogy
Jul 18 2016 10:33
what's the opposite of intersection?
Denis Stoyanov
@xgrommx
Jul 18 2016 10:34
@Bravilogy maybe symmetricDifference
@Bravilogy or just difference
Bravi
@Bravilogy
Jul 18 2016 10:36
@xgrommx thank you! symmetric was what I needed
Bravi
@Bravilogy
Jul 18 2016 10:49
const listNonCommonProps = (props, item) => R.join(', ', R.symmetricDifference(props, R.keys(item)));
this stuff is so good
Ryan Zeigler
@rzeigler
Jul 18 2016 11:26
@Bradcomp so do the docs need updating then? Because they suggest its curried with arity of 3.
Emilio Srougo
@Emilios1995
Jul 18 2016 16:00
@xgrommx @scott-christopher are the implemetations of ap and chain on functions defined in fantasy-land? where do they come from?
Brad Compton (he/him)
@Bradcomp
Jul 18 2016 16:04
@arzig The examples are wrong in the docs. They don't match the description or the type signature. I just looked, and it appears the example has been updated in master: https://github.com/ramda/ramda/blob/master/src/tryCatch.js
Chet Harrison
@ChetHarrison
Jul 18 2016 19:43
For your viewing pleasure here is the second talk I did on FP https://www.youtube.com/watch?v=ZQSU4geXAxM