ramda.js

Loop until… with Ramda

假如想象 提交于 2019-11-30 15:48:42
I was trying to refactor few pieces of code using Ramda and I was wondering, What could be a good approach in Ramda/Functional Programming to solve the following code: let arrayOfSomething = initArray(); for(let i = 0; SOME_INDEX_CONDITION(i)|| SOME_CONDITION(arrayOfSomething); i++) { const value = operation(arrayOfSomething); const nextValue = anotherOperation(value); arrayOfSomething = clone(nextValue) } So basically I want to iterate and apply the same pipe/composition of operations over arrayOfSomething until one of the conditions is satisfied. Is important that I am given the last value

Apply a list of functions to a value in Ramda

流过昼夜 提交于 2019-11-30 12:32:00
How would I best create this function in Ramda ? function get_list (value) { return [ first_transform(value), second_transform(value) ] } get_list(12) I guess this is the inverse of the map function. You've got a few options for this. Assuming your functions are already in a list: transforms = [first_transform, second_transform]; The first option is to use R.juxt , which does pretty much exactly what you're after by creating a new function that applies the list of given functions to the values received by the new function. get_list = R.juxt(transforms); Another option is R.ap , which applies a

How do I pass in multiple parameters into a Ramda compose chain?

廉价感情. 提交于 2019-11-30 10:34:35
Here are four functions I am trying to compose into a single endpoint string: const endpoint = str => `${str}` || 'default' const protocol = str => `https://${str}` const params = str => `${str}?sort=desc&part=true&` const query = str => `${str}query={ some:'value', another:'value'}` let finalEndpoint = R.compose(query, params, protocol, endpoint) var result = finalEndpoint('api.content.io') This composition works and returns the result I want which is: https://api.content.io?sort=desc&part=true&query={ some:'value', another:'value'} But notice how I have hard coded the values for params and

How to compare two array of object and get common objects

不羁岁月 提交于 2019-11-30 09:54:04
问题 Hello guys I have two arrays var elements = [{ "id": "id_1", "type": "input", "businesstype": { "type": "text" } }, { "type": "label", "id": "id_234" }, { "id": "id_16677", "type": "div", }, { "id": "id_155", "type": "input", "businesstype": { "type": "password" } } ] var filterArray=[{type:'input',businesstype:{type:'text'}},{type:'div'}] and want common obejct like var output = [{ "id": "id_1", "type": "input", "businesstype": { "type": "text" } }, { "id": "id_16677", "type": "div", } ] How

Why can't Promise.resolve be called as a function?

半世苍凉 提交于 2019-11-30 08:45:38
问题 Something that is bugging me and my colleague. Consider the following... const {map, compose} = require('ramda'); compose( console.log, map(Math.tan) )([1,2,3]); compose( console.log, map(v=>Promise.resolve(v)) )([4,5,6]); compose( console.log, map(Promise.resolve) )([7,8,9]); As you would expect, the tan of 1, 2 and 3 are output and so are the promises resolving 3, 4 and 5. But my question is... why does the third break? Why doesn't Promise.resolve behave in the same way as any other

Loop until… with Ramda

你说的曾经没有我的故事 提交于 2019-11-30 07:42:45
问题 I was trying to refactor few pieces of code using Ramda and I was wondering, What could be a good approach in Ramda/Functional Programming to solve the following code: let arrayOfSomething = initArray(); for(let i = 0; SOME_INDEX_CONDITION(i)|| SOME_CONDITION(arrayOfSomething); i++) { const value = operation(arrayOfSomething); const nextValue = anotherOperation(value); arrayOfSomething = clone(nextValue) } So basically I want to iterate and apply the same pipe/composition of operations over

Ramda: get objects from array by comparing with each item in another array

梦想的初衷 提交于 2019-11-29 23:40:37
问题 I've an array like: ids = [1,3,5]; and another array like: items: [ {id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}, {id: 4, name: 'd'}, {id: 5, name: 'e'}, {id: 6, name: 'f'} ]; What I want is another array like: array = [{id: 1, name: 'a'}, {id: 3, name: 'c'}, {id: 5, name: 'e'}]; I can't get my head around it. so far i tried like: console.log(R.filter(R.propEq('id', <donnow what shud be here>), items); console.log( R.pick(ids)(items)) 回答1: If you still want to do with Ramda:

How to compare two array of object and get common objects

断了今生、忘了曾经 提交于 2019-11-29 17:31:06
Hello guys I have two arrays var elements = [{ "id": "id_1", "type": "input", "businesstype": { "type": "text" } }, { "type": "label", "id": "id_234" }, { "id": "id_16677", "type": "div", }, { "id": "id_155", "type": "input", "businesstype": { "type": "password" } } ] var filterArray=[{type:'input',businesstype:{type:'text'}},{type:'div'}] and want common obejct like var output = [{ "id": "id_1", "type": "input", "businesstype": { "type": "text" } }, { "id": "id_16677", "type": "div", } ] How do I compare these two objects to get equal objects from elements. You could filter it with a

How do I pass in multiple parameters into a Ramda compose chain?

三世轮回 提交于 2019-11-29 15:52:40
问题 Here are four functions I am trying to compose into a single endpoint string: const endpoint = str => `${str}` || 'default' const protocol = str => `https://${str}` const params = str => `${str}?sort=desc&part=true&` const query = str => `${str}query={ some:'value', another:'value'}` let finalEndpoint = R.compose(query, params, protocol, endpoint) var result = finalEndpoint('api.content.io') This composition works and returns the result I want which is: https://api.content.io?sort=desc&part

Why can't Promise.resolve be called as a function?

安稳与你 提交于 2019-11-29 07:26:37
Something that is bugging me and my colleague. Consider the following... const {map, compose} = require('ramda'); compose( console.log, map(Math.tan) )([1,2,3]); compose( console.log, map(v=>Promise.resolve(v)) )([4,5,6]); compose( console.log, map(Promise.resolve) )([7,8,9]); As you would expect, the tan of 1, 2 and 3 are output and so are the promises resolving 3, 4 and 5. But my question is... why does the third break? Why doesn't Promise.resolve behave in the same way as any other function? [ 1.5574077246549023, -2.185039863261519, -0.1425465430742778 ] [ Promise { 4 }, Promise { 5 },