These are chat archives for ramda/ramda

8th
Feb 2017
Michael Robinson
@faceleg
Feb 08 2017 00:56
Is there a way to compose lenses in a way that doesn't break if a given path doesn't exist?
Kurt Milam
@kurtmilam
Feb 08 2017 01:39
Any tips on factoring `state` out of the functions where it's used at https://goo.gl/tXXwR8 so I can `memoize` them based on the other (constant) parameters they accept?
James Forbes
@JAForbes
Feb 08 2017 03:03

@faceleg yeah, just construct its via `pathOr` instead of `path`

e.g.

``````const lensPathOr = (otherwise, x) => lens( pathOr(otherwise, x), assocPath(x) )

var a = lensPathOr([], 'a')

view(a, { b: 'no a property '}) //=> []

over(a, concat(['hi']), { b: 'no a property' })

//=> {"a": ["hi"], "b": "no a property"}``````
Bernhard Wang
@zwc
Feb 08 2017 03:31
How I can simplify this? I've tried all sorts of mapping ...
``````const position = [0, 0];
const move = [1, 2];
for(let i=0;i<2;i++)
position[i] = R.add(position[i], move[i]);``````
Feb 08 2017 03:46
@ram-bot
``zipWith(add, [0, 0], [1, 2])``
ram-bot
@ram-bot
Feb 08 2017 03:46
``[ 1, 2 ]``
Feb 08 2017 03:49
`R.zipWith`
ram-bot
@ram-bot
Feb 08 2017 03:49
Feb 08 2017 03:52
@zwc `zipWith` is what you are looking for
Bernhard Wang
@zwc
Feb 08 2017 04:00
@Bradcomp : Thanks, works great! :D
Rick Medina
@rickmed
Feb 08 2017 06:28
hey y'all! I would love to hear your thoughts on this ramda/ramda#2083
Keith Alexander
@kwijibo
Feb 08 2017 09:31
@rickmed seems like "should we get rid of transducers?" and "should we get rid of placeholders?" is a common theme in ramda issues
i'm not sure what your proposal is though :) is it to do either/both of those?
or simplify the map implementation?
Guido Schmitz
Feb 08 2017 11:19
Hi guys!
Is there any way I can get rid of passing the `email` to this predicate?
``(email) => !R.equals(R.prop('teamIds', networkMatch(email)), R.prop('externalTeamIds', externalMatch(email))),``
Matt McKellar-Spence
@MattMS
Feb 08 2017 11:25
``converge(equals, [pipe(networkMatch, prop('teamIds')), pipe(externalMatch, prop('externalTeamIds'))])``
Guido Schmitz
Feb 08 2017 11:25
ah great
I'll look into converge
Matt McKellar-Spence
@MattMS
Feb 08 2017 11:26
:+1: although I just pulled that code off the top of my head, so unsure if it will work :stuck_out_tongue:
Guido Schmitz
Feb 08 2017 11:27
Thanks! BTW I love this lib and the functional programming paradigm. Been into it a lot lately and really serious about it for some weeks now. What do you think about Elixir or ClojureScript? Trying to find a good language where I can create some web API with and it fully functional.
Matt McKellar-Spence
@MattMS
Feb 08 2017 11:27
I haven't used either, but I've heard good things about them, and Elm.
Keith Alexander
@kwijibo
Feb 08 2017 11:31
If you like Elixir, might be worth looking at Elixirscript
Guido Schmitz
Feb 08 2017 11:33
not sure yet, I've been looking into Elixir and it seems really great
Especially while using the Phoenix Web Framework
And on top of Erlang VM, which is pretty dang fast
Keith Alexander
@kwijibo
Feb 08 2017 11:34
there's also a nice comparison of js, purescript, elm and GHCJS http://mutanatum.com/posts/2017-01-12-Browser-FP-Head-to-Head.html
Rick Medina
@rickmed
Feb 08 2017 12:13
@kwijibo my specific proposal changes some internal mechanism in Ramda to not provide transducer or placeholder by default but a way to easy extend Ramda if someone wanted those 2 features
I chose map arbitrarily as an example on how it would be done
Mick Dekkers
@mickdekkers
Feb 08 2017 15:01
Hello! I'm fairly new to Ramda/fp and I was wondering if someone could help me.
I'm looking for a function that will run another function and inspect its return value. If that return value satisfies a condition then I want it to return that return value, otherwise identity

i.e.

``````(fn, predicate, value) => {
const result = fn(value)
return predicate(result) ? result : value
}``````

but more Ramda-ey

Keith Alexander
@kwijibo
Feb 08 2017 15:23
@rickmed +1
Kurt Milam
@kurtmilam
Feb 08 2017 15:24
@SoullessWaffle `ifElse` should work for you.
``ifElse( compose( predicate, fn ), fn, identity )``
Mick Dekkers
@mickdekkers
Feb 08 2017 15:27
@kurtmilam I'd prefer to call `fn` only once if possible
Feb 08 2017 15:27
`R.memoize`
ram-bot
@ram-bot
Feb 08 2017 15:27
Mick Dekkers
@mickdekkers
Feb 08 2017 15:29
@Bradcomp Does memoize clear its cache after a while? The values I'm working with (ISO date strings) are largely unique so the cache could fill up rather quickly
Kurt Milam
@kurtmilam
Feb 08 2017 15:40
@SoullessWaffle something like this should do the trick:
`compose( unnest, filter( predicate )( converge( pair, [ fn, identity ] ) ) )`
Mick Dekkers
@mickdekkers
Feb 08 2017 15:42
Great! Thanks @kurtmilam and @Bradcomp :)
Kurt Milam
@kurtmilam
Feb 08 2017 15:46
@SoullessWaffle https://goo.gl/1HlDp7
``````const isOdd = ( n ) => n % 2 === 1

const solution = compose( head, filter( isOdd ), converge( pair, [ inc, identity ] ) )

console.log( solution( 1 ) ) // -> 1
console.log( solution( 2 ) ) // -> 3``````
Kurt Milam
@kurtmilam
Feb 08 2017 15:56
Note: If neither `fn(value)` nor `value` satisfies the predicate, you will end up with `undefined`.
So the function isn't as general as it could be.
Kurt Milam
@kurtmilam
Feb 08 2017 16:06
@SoullessWaffle This one is more general and doesn't filter`value` by`predicate`: https://goo.gl/ocXXEA
``````const is2 = n => n === 2

const solution = p => f => x =>
ifElse( compose( p, prop('fx') )
, prop( 'fx' )
, prop( 'x' )
)( { x, fx: f(x) } )

console.log( solution( is2 )( inc )( 0 ) ) // -> 0
console.log( solution( is2 )( inc )( 1 ) ) // -> 2
console.log( solution( is2 )( inc )( 2 ) ) // -> 2
console.log( solution( is2 )( inc )( 3 ) ) // -> 3``````
Mick Dekkers
@mickdekkers
Feb 08 2017 16:09
@kurtmilam Clever use of an object there, I like it!
Kurt Milam
@kurtmilam
Feb 08 2017 16:09
Cool!
With a `pair` (`array`) and slightly less verbose: https://goo.gl/lJlz6x
``const solution = p => f => x => ifElse( compose( p, last ), last, head )( pair( x, f(x) ) )``
Bernhard Wang
@zwc
Feb 08 2017 16:54
I have a long array of strings (result.visits). All I want to do is find the first duplicate, the first occurrence in that array that has occured already. I created a table by using R.pickBy and then use R.has to filter... but it's not pretty. Is there any easy way to make it better?
``````const isDupe = (val, key) => val >= 2;
const dupeTable = R.pickBy(isDupe, R.countBy(R.toLower, result.visits));
const firstDupe = R.head(result.visits.filter(x => R.has(x, dupeTable)));``````
Bravi
@Bravilogy
Feb 08 2017 17:49
``````const buildAddressObject = address => {
const parts = address
.split(',')
.map(str => str.trim());
return {
street: parts[0],
city: parts[1],
state: parts[2],
zip: parts[parts.length - 1],
};
};``````
what function can I use from ramda for that last bit? (return part)
to basically sort of map things like that
Bernhard Wang
@zwc
Feb 08 2017 18:35
@Bravilogy :
If you first map so that the array contains them in order; you can use zipObj
``return R.zipObj(['street', 'city', 'state', 'zip'], parts);``
Bravi
@Bravilogy
Feb 08 2017 18:40
perfect, thanks!
Bernhard Wang
@zwc
Feb 08 2017 18:44
This gives you the first 3 and the last also,
``R.zipObj(['street', 'city', 'state', 'zip'], R.concat(R.slice(0, 3, parts), [ R.last(parts) ]));``