These are chat archives for ramda/ramda

18th
Aug 2016
Anvesh Checka
@geraldchecka
Aug 18 2016 05:37

I'm new to FP. I'm trying to implement a small example that accepts a string and validates against multiple regular expressions.

<Array of Reg exp>.reduce((item) => {}, <Array accumulator for holding each regex result>)

Is this a sensible way to achieve it. Or if someone could share a better way to solve it using Ramda JS, I highly appreciate it
Rick Medina
@rickmed
Aug 18 2016 06:00
@geraldchecka maybe R.ap?
Scott Christopher
@scott-christopher
Aug 18 2016 06:05
@geraldchecka Check out R.allPass.
@ram-bot
const isValid = allPass([
  test(/oo/),
  test(/^m/)
]);
isValid('moo');
ram-bot
@ram-bot
Aug 18 2016 06:07
true
Ivan Demchenko
@raqystyle
Aug 18 2016 08:06
@joaomilho I personally find it very cool! I'm quite fond of Elm and I feel like I missed Act for JS. Have you thought about TypeScript for types?
Rafe
@rjmk
Aug 18 2016 08:11
@Bradcomp Not yet, but see: sanctuary-js/sanctuary#208. I'm hoping to finally have some time to do some PRs for this in the next month
Tushar Mathur
@tusharmath
Aug 18 2016 09:32
how return multiple IOtypes in ramda-fantasy?
If a function for instance wants to return multiple IOs ?
and all of them being independant of each other
PS: New to fantasy-land, so I might be asking a silly question.
I am using chain such as a.chain(R.always(b)) where a and b are both IO type
Tushar Mathur
@tusharmath
Aug 18 2016 09:39
Or may be I could just return an array
Tushar Mathur
@tusharmath
Aug 18 2016 10:08

Or how about —

export const CompositeIO = (...ios) => IO(() => ios.map(IO.runIO))

?

Scott Christopher
@scott-christopher
Aug 18 2016 10:31
@tusharmath You might be interested in R.sequence, which will take an array of applicatives (like IO) and return an instance of the applicative that now contains an array of the values. You can think of it as though it turns the list of applicatives inside out.
@ram-bot
const io = R.sequence(IO.of, [IO(Math.random), IO(Math.random)])
IO.runIO(io)
ram-bot
@ram-bot
Aug 18 2016 10:34
[ 0.017603277694433928, 0.23837881232611835 ]
Tushar Mathur
@tusharmath
Aug 18 2016 11:25
@scott-christopher thanks. Taking a look at it now
Martin Broder
@mrtnbroder
Aug 18 2016 11:53
Hmpf
@ram-bot S.isNothing(S.toMaybe(S.Nothing()))
ram-bot
@ram-bot
Aug 18 2016 11:53
false
Martin Broder
@mrtnbroder
Aug 18 2016 11:53
I would expect it to be true :O
Cristian Trifan
@crissdev
Aug 18 2016 13:39
Hello everybody. I'm trying to combine two lists (union) but I also want to merge the duplicates. Is there a way to do that?

e.g `
// Input
const list1 = [{id: 1, value: 'a'}, {id: 2, value: 'b'}];
const list2 = [{id: 1, value: 'c'}];

const result = [id: 1, value: 'c'}, {id: 2, value: 'b'}]
`

...also, (new) items from list2 should be appended in result
Martin Broder
@mrtnbroder
Aug 18 2016 13:43
we really need const constant = (a) => () => a within ramda
e.g. for adjust(constant(a))(index)(arr)
Ryan Zeigler
@rzeigler
Aug 18 2016 13:47
@ram-bot R.always
ram-bot
@ram-bot
Aug 18 2016 13:47
[Function: f1]
Ryan Zeigler
@rzeigler
Aug 18 2016 13:47
that was unexpected
Martin Broder
@mrtnbroder
Aug 18 2016 13:47
oh :p
quietly leaves the room
Ryan Zeigler
@rzeigler
Aug 18 2016 13:48
i’m not sure why its called constant, maybe one of those randomly reserved js keywords
Vladimir Starkov
@iamstarkov
Aug 18 2016 13:48
@Bradcomp @rjmk afaik there is no pbt in ramda tests
it makes sense to apply pbt for compose
Martin Broder
@mrtnbroder
Aug 18 2016 14:01
@arzig constant is free to use
just, const is not
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 14:10
@arzig For getting the signature, you don't need to tag @ram-bot
Ryan Zeigler
@rzeigler
Aug 18 2016 14:10
ah
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 14:10
R.always
Ryan Zeigler
@rzeigler
Aug 18 2016 14:10
oh right
i asked him to eval
so, f1
Martin Broder
@mrtnbroder
Aug 18 2016 14:15
function Maybe(x, box) {
I see what you did there @davidchambers ( ͡° ͜ʖ ͡°)
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 14:18
@crissdev Check out unionWith? It will take the second item for items in the intersection.
@ram-bot
const list1 = [{id: 1, value: 'a'}, {id: 2, value: 'b'}];
const list2 = [{id: 1, value: 'c'}];

unionWith(eqProps('id'), list2, list1)
ram-bot
@ram-bot
Aug 18 2016 14:18
[ { id: 1, value: 'c' }, { id: 2, value: 'b' } ]
David Chambers
@davidchambers
Aug 18 2016 15:20

You're misunderstanding S.toMaybe, @mrtnbroder.

Takes a value and returns Nothing if the value is null or undefined; Just the value otherwise.

@ram-bot
S.toMaybe(null)
ram-bot
@ram-bot
Aug 18 2016 15:21
Nothing()
David Chambers
@davidchambers
Aug 18 2016 15:21
@ram-bot
S.toMaybe(undefined)
ram-bot
@ram-bot
Aug 18 2016 15:21
Nothing()
David Chambers
@davidchambers
Aug 18 2016 15:21
@ram-bot
S.toMaybe([1, 2, 3])
ram-bot
@ram-bot
Aug 18 2016 15:21
Just([1, 2, 3])
David Chambers
@davidchambers
Aug 18 2016 15:22
@ram-bot
S.toMaybe(S.Nothing())
ram-bot
@ram-bot
Aug 18 2016 15:22
Just(Nothing())
Martin Broder
@mrtnbroder
Aug 18 2016 15:23
mhmm
got it
Cristian Trifan
@crissdev
Aug 18 2016 15:56
@Bradcomp Thanks, but I want to merge the new data, not to overwrite the previous data. In the next example you can see the time property is lost.
@ram-bot
const list1 = [{id: 1, value: 'a', time: 1}, {id: 2, value: 'b'}];
const list2 = [{id: 1, value: 'c'}];

unionWith(eqProps('id'), list2, list1)
Martin Broder
@mrtnbroder
Aug 18 2016 16:25
why does update or adjust not create an index when the array is empty?
I'd expect that update(0)(0)([])outputs [0] but instead I get []
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 19:21
Is there a better way of doing this? const flattenObject = object => R.props(R.keys(object), object);
Kurt Milam
@kurtmilam
Aug 18 2016 19:22
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 19:24
Thanks @kurtmilam. Looks more complicated than what I have here
Looks like R.values is what I want
My junior colleague wrote some modules with limited Ramda experience while I was on holiday last week, so I'm just tidying it up as a way to teach
Aadi Deshpande
@cilquirm
Aug 18 2016 19:29
i apologize if this is a poor question: i'm trying to use ramda fantasy Maybe and I can't figure out how i can do a getOrElse with lazy evaluation on the else
Aadi Deshpande
@cilquirm
Aug 18 2016 19:49
conversely, is there a ramda function that converts a value into a function ?
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 19:51
@cilquirm Do you mean like R.always?
R.always
Aadi Deshpande
@cilquirm
Aug 18 2016 19:51
boom! yes, thanks!
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 19:54
I'm trying to compose a function that will look for a given prop, using propOr, else fall back to trying pathOr:
const findLabel = R.compose(
  R.pathOr(undefined, ['participant', 'name']),
  R.propOr(undefined, 'title')
);
But I don't know how to make that work with a point-free composition
I don't think I can do R.propOr(R.pathOr(…), 'title') because how would the inner pathOr get access to the argument?
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 19:57
R.either
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 19:58
Although, technically it's supposed to take boolean returning functions
So if 0 or '' is a valid value you may have trouble
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 19:59
I actually don't want it to return undefined, it's just the interface I was given
I don't think it's a good idea to bubble up nils like these
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:00
@ram-bot
R.either(R.prop('title'), R.path(['participant', 'name']))({participant: {name: 'Brad'}})
ram-bot
@ram-bot
Aug 18 2016 20:00
'Brad'
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:00
@ram-bot
R.either(R.prop('title'), R.path(['participant', 'name']))({participant: {name: 'Brad'}, title: 'Developer'})
ram-bot
@ram-bot
Aug 18 2016 20:00
'Developer'
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:01
hmm I'm not sure those semantics are what I need
There's 2 nodes in a redux store. One has an object with a title prop, the other has the nested name within participant.
If I'm reading that output above correctly, it's expecting to choose title if it exists
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:02
Correct
I was assuming one object
What's an example input / ouput?
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:03
yeah, this is building a set of breadcrumbs
ah! it's working now :)
haha thanks @Bradcomp :)
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:04
np!
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:05
Do you agree about avoiding the undefined?
Looks like I need it because of the async nature of the data… oh well
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:11
so prop and path should return undefined if the property doesn't exist anyway
I don't know if propOr(undefined) does anything for you
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:12
Yeah I know that's what Ramda does
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:13
I try to avoid undefined whenever possible
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:15
failed with "Cannot read property 'title' of undefined"
It's because they're breadcrumbs. If the page is refreshed or deep-linked, it's got to load the entity from the backend
So giving it an explicit fallback makes it not blow up
I really need to use containers with my redux selectors (I think)
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:19
Aah, I see now
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:23
Sorry I've not provided a full example, it's a little bit complicated
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:25
Yeah, sometimes it's hard to provide enough context without being overwhelming
Ryan Zeigler
@rzeigler
Aug 18 2016 20:26
i don’t suppose there’s a way of getting lenses to have similar behavior to path
there doesn’t appear to be an analog to traversal
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:26
lensPath?
Ryan Zeigler
@rzeigler
Aug 18 2016 20:26
although I’m not sure that it would be applicable
to the experiments...
i apparently just didn’t see that
now i’m curious how its implemented
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:29
Does it work for you?
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:29
lensPath uses path underneath
Ryan Zeigler
@rzeigler
Aug 18 2016 20:29
yes
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:29
BTW, path is safe in a way that prop isn't
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:29
hmm
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:29
@ram-bot
path(['title'], undefined)
ram-bot
@ram-bot
Aug 18 2016 20:29
undefined
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:30
Are you suggesting I use path to solve this fallback? :)
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:30
@ram-bot
prop('title', undefined)
ram-bot
@ram-bot
Aug 18 2016 20:30
Cannot read property 'title' of undefined
Ryan Zeigler
@rzeigler
Aug 18 2016 20:30
uh
does it not compose correctly though?
did I do something wrong?
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:31
How do you mean @arzig ?
Ryan Zeigler
@rzeigler
Aug 18 2016 20:31
i would expect the latter view/set to behave correctly
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:32
Does that work with other lenses?
Ryan Zeigler
@rzeigler
Aug 18 2016 20:32
then again, I lack enough understanding of the internals beyond the fact there’s some functor wizardry involving const and id
yes, see the top example
which gets printed 2nd for some reason
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:34
So the view doesn't throw, but the set fails
Ryan Zeigler
@rzeigler
Aug 18 2016 20:34
apparently
time to learn how the larhoven formulation actually works and see if I can figure this out
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:35
Thanks for that insight @Bradcomp! I was wondering why my pathOr wasn't blowing up :) (when I converted it to just path)
Brad Compton (he/him)
@Bradcomp
Aug 18 2016 20:35
:+1:
Aaron Mc Adam
@aaronmcadam
Aug 18 2016 20:36
It does read a little bit strangley: const getTitle = R.path(['title'])
But I think it's worth it due to not having to set a fallback myself
I suppose I could use unapply to give myself a safeProp function
@ram-bot
const safeProp = unapply(R.path)
safeProp('title')(undefined)
ram-bot
@ram-bot
Aug 18 2016 20:40
undefined