functional-programming

Is there a Variadic Version of either (R.either)?

五迷三道 提交于 2021-02-07 15:09:58
问题 I have a need for a variadic version of R.either . After doing some searching around the web, I have not found a solution. R.anyPass would work but it returns a Boolean instead of the original value. Is there already a solution that I have overlooked? If not, what would be the most optimal way to write a variadic either utility function? An example: const test = variadicEither(R.multiply(0), R.add(-1), R.add(1), R.add(2)) test(1) // => 2 回答1: You could use a combination of reduce + reduced :

Practical use of fold/reduce in functional languages

喜欢而已 提交于 2021-02-07 12:16:31
问题 Fold (aka reduce ) is considered a very important higher order function. Map can be expressed in terms of fold (see here). But it sounds more academical than practical to me. A typical use could be to get the sum, or product, or maximum of numbers, but these functions usually accept any number of arguments. So why write (fold + 0 '(2 3 5)) when (+ 2 3 5) works fine. My question is, in what situation is it easiest or most natural to use fold ? 回答1: The point of fold is that it's more abstract.

Practical use of fold/reduce in functional languages

无人久伴 提交于 2021-02-07 12:15:39
问题 Fold (aka reduce ) is considered a very important higher order function. Map can be expressed in terms of fold (see here). But it sounds more academical than practical to me. A typical use could be to get the sum, or product, or maximum of numbers, but these functions usually accept any number of arguments. So why write (fold + 0 '(2 3 5)) when (+ 2 3 5) works fine. My question is, in what situation is it easiest or most natural to use fold ? 回答1: The point of fold is that it's more abstract.

What strategy to turn non-pure functions into a pure functions in JavaScript

雨燕双飞 提交于 2021-02-07 09:47:36
问题 I'm starting to learn functional programming in javascript. This might be a silly question but what I'm trying to solve a non-pure function written in a functional way. My question is what strategy should be used to accomplish this in a functional programming paradigm. const crypto = require('crypto'); const encrypt = (data, publicKey) => { if (publicKey === undefined ) throw 'Missing public key.'; const bufferToEncrypt = Buffer.from(data); const encrypted = crypto.publicEncrypt({ key:

Java: sorting an array with lambda expression?

て烟熏妆下的殇ゞ 提交于 2021-02-07 09:17:05
问题 I've recently got into functional programming and Java 8 lambdas. I have an array of ints and I want to sort it in an ascending order. The way I am trying to do this with lambda is as follows: Arrays.stream(intArray).sorted((x, y) -> Integer.compare(x, y) == -1); The issue with this is that my compiler says: Error:(12, 32) java: method sorted in interface java.util.stream.IntStream cannot be applied to given types; required: no arguments found: (x,y)->Int[...]== -1 reason: actual and formal

Java: sorting an array with lambda expression?

百般思念 提交于 2021-02-07 09:16:11
问题 I've recently got into functional programming and Java 8 lambdas. I have an array of ints and I want to sort it in an ascending order. The way I am trying to do this with lambda is as follows: Arrays.stream(intArray).sorted((x, y) -> Integer.compare(x, y) == -1); The issue with this is that my compiler says: Error:(12, 32) java: method sorted in interface java.util.stream.IntStream cannot be applied to given types; required: no arguments found: (x,y)->Int[...]== -1 reason: actual and formal

Do we need fixed point combinators in C#?

删除回忆录丶 提交于 2021-02-07 08:57:15
问题 I was playing with recursive lambdas in C# and have found two approaches to do this on the web. One approach uses fixed point combinator and the other does not. In the code below f1 is built using combinator, and f2 is defined directly. My question is, do we need fixed point combinators in C# or the language already provides all we need, so we can leave them alone? class Program { static Func<T, T> F<T>(Func<Func<T,T>,Func<T,T>> f) { return x => f(F(f))(x); } static void Main(string[] args) {

Clojure: How to create a function at runtime

情到浓时终转凉″ 提交于 2021-02-07 06:27:12
问题 I want to generate a fn totally at runtime (i.e. the name and the arg symbols are decided at runtime, not in code) What's the best way to achieve this ? For example how can I implement the following function ? (defn gen-fn [name arg-symbols body] ... ... which would be used like this: (gen-fn "my-func-name" (symbol "x") (symbol "y") (println "this is body. x=" x)) Note that function name, the args and the body are not coded but can be decided at runtime 回答1: (defn gen-fn [n as b] (let [n

(compose) in Common Lisp

巧了我就是萌 提交于 2021-02-07 06:17:46
问题 We find this function builder to realize composition in P.Graham's "ANSI Common Lisp" (page 110). The arguments are n>0 quoted function names. I don't understand it completely, so I'll quote the code here and specify my questions underneath it: (defun compose (&rest fns) (destructuring-bind (fn1 . rest) (reverse fns) #'(lambda (&rest args) (reduce #'(lambda (v f) (funcall f v)) rest :initial-value (apply fn1 args))))) The argument list to compose is reversed and unpacked, its (now first)

(compose) in Common Lisp

好久不见. 提交于 2021-02-07 06:16:43
问题 We find this function builder to realize composition in P.Graham's "ANSI Common Lisp" (page 110). The arguments are n>0 quoted function names. I don't understand it completely, so I'll quote the code here and specify my questions underneath it: (defun compose (&rest fns) (destructuring-bind (fn1 . rest) (reverse fns) #'(lambda (&rest args) (reduce #'(lambda (v f) (funcall f v)) rest :initial-value (apply fn1 args))))) The argument list to compose is reversed and unpacked, its (now first)