ramda.js

Ramda recommendation for removing duplicates from a slightly nested array

落花浮王杯 提交于 2019-12-04 04:21:17
问题 We're trying to utilise Ramda to avoid some brute-force programming. We have an array of objects that can look like this: [ {id: "001", failedReason: [1000]}, {id: "001", failedReason: [1001]}, {id: "001", failedReason: [1002]}, {id: "001", failedReason: [1000]}, {id: "001", failedReason: [1000, 1003]}, {id: "002", failedReason: [1000]} ] and we'd like to transform it so that it looks like this: [ {id: "001", failedReason: [1000, 1001, 1002, 1003]}, {id: "002", failedReason: [1000]} ]

How can I access iteration index in Ramda.map

扶醉桌前 提交于 2019-12-03 22:07:37
I used to write something like _.map(items, (item, index) => {}); with lodash. Usually I don't need index but sometimes it's useful. I'm migrating to Ramda now: R.map((item, index) => {}, items); index is undefined . Sure, I can create variable index in upper scope and increment it every time in map body but it's kinda wrong from FP point of view that Ramda stands for. So is there's any build in way of getting iteration index? Check out addIndex : Creates a new list iteration function from an existing one by adding two new parameters to its callback function: the current index, and the entire

Ramda js: lens for deeply nested objects with nested arrays of objects

試著忘記壹切 提交于 2019-12-03 05:46:43
问题 Using Ramda.js (and lenses), I want to modify the JavaScript object below to change "NAME:VERSION1" to "NAME:VERSION2" for the object that has ID= "/1/B/i". I want to use a lens because I want to just change one deeply nested value, but otherwise retain the entire structure unchanged. I don't want to use lensIndex because I never know what order the arrays will be in, so instead, I want to "find" the object in an array by looking for its "id" fields. Can I do this with lenses, or should I do

Ramda js: lens for deeply nested objects with nested arrays of objects

試著忘記壹切 提交于 2019-12-02 18:14:50
Using Ramda.js (and lenses), I want to modify the JavaScript object below to change "NAME:VERSION1" to "NAME:VERSION2" for the object that has ID= "/1/B/i". I want to use a lens because I want to just change one deeply nested value, but otherwise retain the entire structure unchanged. I don't want to use lensIndex because I never know what order the arrays will be in, so instead, I want to "find" the object in an array by looking for its "id" fields. Can I do this with lenses, or should I do it a different way? { "id": "/1", "groups": [ { "id": "/1/A", "apps": [ { "id": "/1/A/i", "more nested

Using Ramda, and pointfree style, how can I copy the first item of an array to the end of it?

一曲冷凌霜 提交于 2019-12-02 07:23:31
I want to take an array [1, 2, 3] and return [1, 2, 3, 1] . I'm using Ramda, and I can get the desired result like this: const fn = arr => R.append(R.prop(0, arr), arr); But I'd like to do it point-free. Here's the closest I've gotten: const fn = R.compose(R.append, R.prop(0)); fn(arr)(arr) But that looks silly. What am I missing? Thanks! converge can be very helpful for things like this. const rotate = R.converge(R.append, [R.head, R.identity]) rotate([1, 2, 3]); //=> [1, 2, 3, 1] The S combinator is useful here: S.S(S.C(R.append), R.head, [1, 2, 3]); // => [1, 2, 3, 1] 来源: https:/

Can I make this function defenition even shorter?

血红的双手。 提交于 2019-12-02 01:02:08
问题 I am trying move more towards functional programming in my javascript applications. I currently use the library ramda as a base lib for this. My desire: Create a function findWithId(id, list) which returns the item in the list with the property _id matching the input id. Make the implementation of it as short as possible, relying on existing code as much as possible. Acheived so far: My base is R.find which has this defenition find :: (a -> Boolean) -> [a] -> a | undefined I tried some

When is it appropriate to choose point-free style vs a data-centric style in functional programming?

女生的网名这么多〃 提交于 2019-12-01 22:19:48
In case it matters this is about functional programming in JavaScript and in my examples I’ll be using Ramda. While everybody at work has fully embraced functional programming, there’s also a lot of discussions around how to do it “right”. These two functions will do exactly the same thing: take a list and return a new list in which all strings have been trimmed. // data-centric style const trimList = list => R.map(R.trim, list); // point-free style const trimList = R.map(R.trim); So far so good. However with a more complex example, the difference between the two styles is striking: take a

Sort an array of objects based on another array of ids

主宰稳场 提交于 2019-12-01 20:55:29
I have 2 arrays a = [2,3,1,4] b = [{id: 1}, {id: 2}, {id: 3}, {id: 4}] How do I get b sorted based on a ? My desired output would be c = [{id: 2}, {id: 3}, {id: 1}, {id: 4}] I would prefer to use Ramda or regular JS. Ramda really shines for these types of problems. Where the size of the data is small, we can use a simple reduce function, and indexOf helper. // match id of object to required index and insert var sortInsert = function (acc, cur) { var toIdx = R.indexOf(cur.id, a); acc[toIdx] = cur; return acc; }; // point-free sort function created var sort = R.reduce(sortInsert, []); // execute

Ramda currying: how to apply argument to multiple parameters

橙三吉。 提交于 2019-12-01 09:10:49
I have a situation where I need to do this: const f = (obj) => assoc('list', createList(obj), obj) Due to the fact that I need the argument for the second and the third parameter, prohibits me from doing something like: const f = assoc('list', somehowGetObj()) I also tried this, but that didn't work: const f = assoc('list', createList(__)) const f = converge(assoc, [createList, identity]) Is there a proper way to do this by currying? Another option is chain(createList, assoc('list')) which you can see in action on the Ramda REPL . Update For further explanation of how this works, I'll use the

Ramda currying: how to apply argument to multiple parameters

自闭症网瘾萝莉.ら 提交于 2019-12-01 07:19:07
问题 I have a situation where I need to do this: const f = (obj) => assoc('list', createList(obj), obj) Due to the fact that I need the argument for the second and the third parameter, prohibits me from doing something like: const f = assoc('list', somehowGetObj()) I also tried this, but that didn't work: const f = assoc('list', createList(__)) const f = converge(assoc, [createList, identity]) Is there a proper way to do this by currying? 回答1: Another option is chain(createList, assoc('list'))