These are chat archives for ramda/ramda

5th
Jun 2016
Joy Krishna Mondal
@JoyKrishnaMondal
Jun 05 2016 00:37
is there a ramda function for lodash.defaults
Brad Compton (he/him)
@Bradcomp
Jun 05 2016 00:49
@JoyKrishnaMondal there's an implementation in the cookbook: https://github.com/ramda/ramda/wiki/Cookbook#set-properties-only-if-they-dont-exist
Joy Krishna Mondal
@JoyKrishnaMondal
Jun 05 2016 00:54
@Bradcomp thanks !
I just do not want to pull lodash for a single function
so its just flip
sorry
merge
Rob Halff
@rhalff
Jun 05 2016 00:55
@Bradcomp thanks for the example, it shows how to pre curry those functions, I was looking for that.
Arve Knudsen
@aknuds1
Jun 05 2016 12:36
Is there a way to map a sequence lazily and return the first element that fulfills a certain predicate?
I.e. map elements successfully until the predicate is fulfilled and then return the current mapped element
Lewis
@6ewis
Jun 05 2016 14:39
@aknuds1 can you give an example input and an example output. I don't follow
Arve Knudsen
@aknuds1
Jun 05 2016 14:41
@6ewis I achieved it by combining R.map and R.find: R.find(R.prop('isInvalid'), R.map(invalidTag), tags)
What I would like is to end the mapping once invalidTag(x).isInvalid evaluates to true
And return the mapped result (invalidTag(x))
Hardy Jones
@joneshf
Jun 05 2016 14:53
Without necessitating changes from ramda you can do this by using a more correct data structure.
Think of it this way: you're using a strictly evaluated data structure and wanting ramda to do lazy evaluation for you.
If you change the data structure to a lazily evaluated one, then ramda will do what you want.
Rob Halff
@rhalff
Jun 05 2016 16:23
How do I facilitate the same kind of api for a user classes and methods provide using functional programming. e.g. express middleware, you receive a res parameter and you can call all these useful methods on it.
would the functional approach be to provide an almost plain object as res parameter and then direct the user to import a response utility library containing the functions which commonly apply to a response object?
hmz, contracts and containers right, or something... :)
Brad Compton (he/him)
@Bradcomp
Jun 05 2016 18:16
@aknuds1 Is the mapping function mutating state on the items in your array? If not, can you update your predicate to operate directly on the original values, then use find, then pass the resultant item through your transformation?
Arve Knudsen
@aknuds1
Jun 05 2016 18:18
@Bradcomp No, it isn't
@Bradcomp Thing is, “the resultant item” is used both for the predicate and afterwards, so I want to map before filtering and then return the mapped value if you see what I mean
If I have to map after finding, I’ll have to map twice
Hope I understood you correctly
Brad Compton (he/him)
@Bradcomp
Jun 05 2016 18:24
I was thinking more of having a predicate function and a mapping function, with both operating on the items in the original array. The predicate function would check to see if an item in the array is invalid, and then you would just perform your mapping function on that single item.
Arve Knudsen
@aknuds1
Jun 05 2016 18:26
@Bradcomp Could you provide example code?
Not sure what you have in mind
Brad Compton (he/him)
@Bradcomp
Jun 05 2016 18:26
@aknuds1 Sure, gimme a few minutes
Arve Knudsen
@aknuds1
Jun 05 2016 18:27
The predicate function has to operate on the transformed object you see, in my case
It might not be fantastic design, but that is how it is
Brad Compton (he/him)
@Bradcomp
Jun 05 2016 18:30
@ram-bot
const inputArray = [
  {requiredField: 'Brad', otherField: 1},
  {requiredField: null, otherField: 2},
  {requiredField: 'Comp', otherField: 3}
];

const isInvalid = compose(isNil, prop('requiredField'))
const invalidTag = R.assoc('invalid', true);

compose(invalidTag, find(isInvalid))(inputArray)
ram-bot
@ram-bot
Jun 05 2016 18:30
{ requiredField: null, otherField: 2, invalid: true }
Brad Compton (he/him)
@Bradcomp
Jun 05 2016 18:30

The predicate function has to operate on the transformed object you see, in my case

I see

That definitely makes it trickier
Brad Compton (he/him)
@Bradcomp
Jun 05 2016 18:42

Here: http://goo.gl/bVLhX1

I may be hacking transduce, but note the log is only called twice

Brad Compton (he/him)
@Bradcomp
Jun 05 2016 18:49
@aknuds1 alternatively you could memoize your transformation function, but there are time and space costs associated with that.
Arve Knudsen
@aknuds1
Jun 05 2016 18:50
Thanks
Brad Compton (he/him)
@Bradcomp
Jun 05 2016 19:26
@rhalff I've put some thought into making a functional server framework for Node, I think there are a lot of different ways you could go with it. I like the idea of defining a data type that contains the {req, res} data you need, and then writing functions of the type {req, res} => Future error {req, res} that could composed to deal with middleware.
@rhalff I ran into this repo recently which is pretty neat: https://github.com/Avaq/Idealist
Rob Halff
@rhalff
Jun 05 2016 19:30
@Bradcomp well imagine all that express middleware out there, who all do something with req and res, it would be very inconvenient if there where no methods available on those objects themselves. All would have to include some kind of utility library, I think.
thanks for the link
Victor Gavro
@vgavro
Jun 05 2016 23:41
hey, small question. function(a) {return a} in ramda? can't find it in docs
Gabe Johnson
@gabejohnson
Jun 05 2016 23:43
identity
Victor Gavro
@vgavro
Jun 05 2016 23:43
tnx!) btw ramda is awesome
Gabe Johnson
@gabejohnson
Jun 05 2016 23:45
It is a pretty cool lib