functional-programming

How to use Ramda pipe?

守給你的承諾、 提交于 2019-12-13 03:59:30
问题 Background I am trying to compose 2 functions using Ramda, but I am having issue with pipe , meaning I don't know how to use it. Code Let's imagine I have a function that returns an array: var createQuery = params => [ getSQLQuery( params ), [ getMarket() ] ]; var getSQLQuery = ( { lang } ) => `My query is ${lang}`; var getMarket = () => "en_en" So, when calling createQuery({ lang: "es" }) I would have the following output: [ "My query is es", ["en_en"] ] ; Now, let's also assume I am a nasty

Conforming my readPackageFiles to fit inside “Parallel” function

旧时模样 提交于 2019-12-13 03:43:48
问题 I got a lot of help from this SO question around grabbing directories and searching files. I'm trying to conform my readPackageFiles function to use the Parallel function given to me; however, I'm struggling to it it work. Error: UnhandledPromiseRejectionWarning: TypeError: Cannot use 'in' operator to search for 'throws' in 1 at readFile (/Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/node_modules/jsonfile/index.js:22:16) at Promise (/Users/harwood/udev/dotcom

Recursive Multiplication in Scheme (trouble with negatives)

丶灬走出姿态 提交于 2019-12-13 03:35:31
问题 I am trying to multiply in Scheme using recursion, and I am able to do so with positive numbers but not negatives (they get stuck in an infinite loop). I assume the problem comes in the 3rd and 4th lines, which I wrote in an attempt to be able to handle negatives. Any help would be appreciated :) (define Multiply(lambda (x y) (if (eq? x 0) 0 (if (eq? y 0) 0 ;if either x or y are zero, return 0 (if (> 0 x) (- 0 (+ x (Multiply x (- y 1)))) (if (> 0 y) (- 0 (+ x (Multiply x (- y 1)))) (+ x

Function that will execute function depending on value. Functional programming

筅森魡賤 提交于 2019-12-13 03:33:55
问题 I have two functions and they are executed depending of if statement. E.g.: if(value) { doA() } else { doB() } How to write function or object that will take the result and decide whether or not execute each function. I want to receive something like this: exists(result).doA() nothing(result).doB() I want to learn some functional programming in JavaScrit so I woud appreciate any source from which I can learn FP in JavaScript. 回答1: You could write something like this, for example: function

Haskell's infix operator for Javascript

寵の児 提交于 2019-12-13 03:23:51
问题 Is there an equivalent construct in Javascript. If not, how would you create one? Here's a straightforward explanation of what the infix operator does in Haskell: What does the : infix operator do in Haskell? 回答1: JavaScript doesn't have a list type, but it has Array s. You can use... var newArr = [val].concat(arr); Alternatively, you could use unshift() to prepend to an array, but it mutates the original. JavaScript doesn't have a : operator, operator overloading or methods that look like

Transmit parameter to the inner-most call of a composed function

折月煮酒 提交于 2019-12-13 03:00:47
问题 I'm building a data pipelining tool and want to be able to compile a series of functions together. All the functions act on an iterable, and pass an iterable as a yielded value. So if f(x) filters, and g(x) edits, and h(x) generates a random value, then I want to be able to compose combinations of these so I can call f(g(h(x))) or h(f(g(x))) as per requirements. I'd like to be able to prepare these compositions (they have different parameter signatures beyond the initial iterable) before

Find Path to the First Occurrence in a Nested Data Structure

久未见 提交于 2019-12-13 02:46:23
问题 Consider the following numbers nested by vectors in a tree-structure (def tree [7 9 [7 5 3 [4 6 9] 9 3] 1 [2 7 9 9]]) My goal is to find the path to the first even number that can be found by traversing the tree: In the upper example this would be 4, the path from the root to this node would be [2 3 0] (def result [2 3 0]) I got some difficulties writing a function tho archive this. However, the following function finds the first even number, not its path: (defn find-even [x] (if (vector? x)

Rebuilding _zip in Javascript using map, arbitrary arguments

痴心易碎 提交于 2019-12-13 02:37:10
问题 I'm trying to learn JavaScript well and am practicing rebuilding some underscore functions. I'm trying to rebuild zip using map where there is an arbitrary number of arguments. Here is the solution I came up with, with pluck. I know that the underscore implementation itself uses _pluck, which internally uses Map, so I wanted to see if it was possible to do this with map... _.zip = function() { var argumentsArray = Array.prototype.slice.call(arguments); var longestArray = argumentsArray.sort

Update the values of a list with their absolute values

 ̄綄美尐妖づ 提交于 2019-12-13 02:26:06
问题 Newbie to scala. I am trying to make this code to work for a few hours now . It is intended to update the List[Int](list of integers) with absolute values of the integers. Took a long time to figure out that List is immutable, so found that ListBuffer can be the saviour, but eventually in returning it back into the List form is seeing some issue i guess. def f (arr:List[Int]) : List[Int] = { val list = new scala.collection.mutable.ListBuffer[Int](); val len = arr.length; for ( i <- 0 to len)

Name for a function that maps and returns a resolved array of promises?

送分小仙女□ 提交于 2019-12-13 02:12:29
问题 Maybe this is a stupid ask but I recently found myself using this abstraction often: async function giveMeAName(cbAsync, initValue) { return await Promise.all( initValue.map(cbAsync), ); } Question: Is this a common task to anyone else? If so, does it have a name? If not, maybe it's only partly realize, so does it remind you of anything? Otherwise, I can just delete the question. Currently I'm using this function for this set of instructions. Code below will get all directories of a path and