These are chat archives for ramda/ramda

5th
Aug 2016
Eshan Singh
@naiveaiguy
Aug 05 2016 03:19
Hey, why isn't it possible to do something like this: let isEven = R.equals(R.modulo(__, 2), 0);? isEven just becomes the value false, not a function.
@ram-bot
let isEven = R.equals(R.modulo(__, 2), 0);
isEven
ram-bot
@ram-bot
Aug 05 2016 03:20
Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
Eshan Singh
@naiveaiguy
Aug 05 2016 03:21
@ram-bot
var isEven = R.equals(R.modulo(__, 2), 0);
isEven
ram-bot
@ram-bot
Aug 05 2016 03:21
false
Eshan Singh
@naiveaiguy
Aug 05 2016 03:21
@ram-bot
var isEven = R.equals(R.modulo(__, 2), 0);
isEven(3)
ram-bot
@ram-bot
Aug 05 2016 03:21
isEven is not a function
Scott Christopher
@scott-christopher
Aug 05 2016 03:24
@eshansingh The placeholder is one level too deep for what you're trying to do. What you have there is effectively comparing a function to the value 0
Eshan Singh
@naiveaiguy
Aug 05 2016 03:25
So then is it possible to do this "composing" of functions?
Brad Compton (he/him)
@Bradcomp
Aug 05 2016 03:27
@ram-bot
R.compose(R.equals(0), R.modulo(R.__, 2))(2)
ram-bot
@ram-bot
Aug 05 2016 03:27
true
Eshan Singh
@naiveaiguy
Aug 05 2016 03:27
Woah
Thanks
R.compose
zeecher Game
@zeecher
Aug 05 2016 08:12
@jamesbrown0 thank you
This message was deleted
// how to convert from this:
{
id: 55
name: Alex,
occupation: manager
}

//to this:
{
  id: 55,
  details:  {
      name: Alex,
       occupation: manager
  }
}
Tim Navrotskyy
@dypsilon
Aug 05 2016 09:51

Hi everyone, I have the following code:

const validatePassword = R.pipe(
  assertPropIsDefined('password'),
  assertPropIsLongEnough('password')
);

Is it possible to factor out the prop name (in this case password) out of this function?

Bravi
@Bravilogy
Aug 05 2016 09:52
do you mean something like assertPasswordIsDefined() ?
or just have it dynamic?
Tim Navrotskyy
@dypsilon
Aug 05 2016 09:53
What I want is something like
const validate = R.pipe(
  assertPropIsDefined,
  assertPropIsLongEnough
);

const result = validate('password')({'password': 'somepasswordhere'});
just remove the repetition
Bravi
@Bravilogy
Aug 05 2016 09:55
you could do
const validate = (prop) => R.pipe(
   assertPropIsDefined(prop),
   assertPropIsLongEnough(prop)
);
Tim Navrotskyy
@dypsilon
Aug 05 2016 09:55
I thought converge does something like this, but it doesn't work this way, apparently.
Bravi
@Bravilogy
Aug 05 2016 09:56
or maybe
hmm
Tim Navrotskyy
@dypsilon
Aug 05 2016 09:56
@Bravilogy is there a way to do it pointfree, this pattern emerges somewhat often in my work.
Bravi
@Bravilogy
Aug 05 2016 09:57
I'm guessing assertPropIsDefined is curried, right? first argument is the property and the second one is the actual validation
Tim Navrotskyy
@dypsilon
Aug 05 2016 09:59
the second argument is an object to validate against
assertPropIsDefined('password', { 'password': 'somethinghere' })
Bravi
@Bravilogy
Aug 05 2016 10:00
yeah, right. so the code I wrote above should get you to this:
const result = validate('password')({'password': 'somepasswordhere'});
what is meant by pointfree?
Tim Navrotskyy
@dypsilon
Aug 05 2016 10:01
pointfree means basically without passing prop variable explicitly
James Tindal
@james-tindal
Aug 05 2016 10:01
propSatisfies( and( assertPropIsDefined, assertPropIsLongEnough ), 'password' )
Bravi
@Bravilogy
Aug 05 2016 10:02
ah I see
Tim Navrotskyy
@dypsilon
Aug 05 2016 10:04
@jamesbrown0 hm, thank you, propSatisfies won't work in my case because both functions return a future, they are not predicates. But this gives me some ideas how to rewrite my code...
Bravi
@Bravilogy
Aug 05 2016 10:06
@jamesbrown0 that's a nice approach, I didn't know about that! thanks from me too :)
gotta love ramda
James Tindal
@james-tindal
Aug 05 2016 10:08
haha it is fun
Bravi
@Bravilogy
Aug 05 2016 10:08
how different is R.and from R.both?
they seem to be doing the same thing
James Tindal
@james-tindal
Aug 05 2016 10:10
I'd never heard of both, but Both works like &&, whereas and takes two predicates(( any -> boolean )) and returns a predicate. and is predicate composition. both does what && does.
and always returns a boolean-returning function
Bravi
@Bravilogy
Aug 05 2016 10:11
ah right
but in this case both and and would work I guess
damn I need to go through the documentation again :D
James Tindal
@james-tindal
Aug 05 2016 10:13
'foo' && 'woo'  //=> 'woo'
James Tindal
@james-tindal
Aug 05 2016 10:28
@zeecher
const a =
{ id: 55
, name: 'Alex'
, occupation: 'manager'
}

const revolve = mapObj => src =>
  map( v => v( src ), mapObj )

const arrange = revolve(
{ id: prop( 'id' )
, details: revolve(
  { name: prop( 'name' )
  , occupation: prop( 'occupation' )
  })
})

arrange(a)
James Tindal
@james-tindal
Aug 05 2016 10:37
Or a bit more fancy:
const a =
{ id: 55
, name: 'Alex'
, occupation: 'manager'
}

const isFunction = x => typeof x === 'function'
const isPlaceholder = has( '@@functional/placeholder' )

const revolve = curry(( mapObj, src ) => mapObjIndexed
( ( v, k ) =>
    isFunction( v ) ? v( src )
  : isPlaceholder( v ) ? prop( k, src )
  : v
, mapObj ))

const arrange = revolve(
{ id: __
, details: revolve(
  { name: __
  , occupation: __
  })
})

arrange(a)
zeecher Game
@zeecher
Aug 05 2016 10:41
@jamesbrown0 , thanks, trying to digest it )
James Tindal
@james-tindal
Aug 05 2016 11:56
@zeecher I refactored your other thing. Let me know if it works.
const isFunction = x => typeof x === 'function'
const isPlaceholder = has( '@@functional/placeholder' )
const revolve = curry(( mapObj, src ) => mapObjIndexed
( ( v, k ) =>
    isFunction( v ) ? v( src )
  : isPlaceholder( v ) ? prop( k, src )
  : v
, mapObj ))

compose
( uniq
, map( revolve(
  { match_id: __
  , title: either( prop( 'front_title' ), prop( 'back_title' ))
  , template: either( prop( 'front_template' ), prop( 'back_template' ))
  }))

)(matchLines)
zeecher Game
@zeecher
Aug 05 2016 12:02
@jamesbrown0, Yes it works, thank you very much
@jamesbrown0 , still couldn't wrap my head around, your 'Or a bit more fancy' solution
zeecher Game
@zeecher
Aug 05 2016 12:26
@jamesbrown0, bingo I got it
James Tindal
@james-tindal
Aug 05 2016 12:31
Haha, good work. I'm happy to explain anything if you'd like.