currying

Proper Currying in C#

做~自己de王妃 提交于 2019-11-26 17:38:26
问题 Given a method DoSomething that takes a (parameterless) function and handles it in some way. Is there a better way to create the "overloads" for functions with parameters than the snippet below? public static TResult DoSomething<TResult>(Func<TResult> func) { //call func() and do something else } public static TResult DoSomething<T0, TResult>( Func<T0, TResult> func, T0 arg0) { return DoSomething(() => func(arg0)); } public static TResult DoSomething<T0, T1, TResult>( Func<T0, T1, TResult>

What's the difference between multiple parameters lists and multiple parameters per list in Scala?

纵饮孤独 提交于 2019-11-26 17:05:09
In Scala one can write (curried?) functions like this def curriedFunc(arg1: Int) (arg2: String) = { ... } What is the difference between the above curriedFunc function definition with two parameters lists and functions with multiple parameters in a single parameter list: def curriedFunc(arg1: Int, arg2: String) = { ... } From a mathematical point of view this is (curriedFunc(x))(y) and curriedFunc(x,y) but I can write def sum(x) (y) = x + y and the same will be def sum2(x, y) = x + y I know only one difference - this is partially applied functions. But both ways are equivalent for me. Are

Curry Function in Swift

别来无恙 提交于 2019-11-26 16:56:40
问题 I want to make a function that return a curry function like below func addTwoNumbers(a: Int)(b: Int) -> Int { return a + b } addTwoNumbers(4)(b: 6) // Result: 10 var add4 = addTwoNumbers(4) add4(b: 10) // returns 14 What is the return type of such function and how can I generate a function like this using a function that take Variadic parameters. func generateCurry(.../*Variadic parameters*/) -> .../*curry function type*/ { return ...//curry function } I want a generic solution and not take

Why does Scala provide both multiple parameters lists and multiple parameters per list? [duplicate]

半城伤御伤魂 提交于 2019-11-26 11:52:14
问题 This question already has answers here : What's the difference between multiple parameters lists and multiple parameters per list in Scala? (4 answers) Closed 6 years ago . Multiple parameters lists, e.g. def foo(a:Int)(b:Int) = {} and multiple parameters per list, e.g. def foo(a:Int, b:Int) = {} are semantically equivalent so far as I can tell, and most functional languages have only one way of declaring multiple parameters, e.g. F#. The only reason I can figure out for supporting both these

How do I define Lisp’s apply in Haskell?

心已入冬 提交于 2019-11-26 10:34:45
Shouldn’t this definition be allowed in a lazy language like Haskell in which functions are curried? apply f [] = f apply f (x:xs) = apply (f x) xs It’s basically a function that applies the given function to the given list of arguments and is very easily done in Lisp for example. Are there any workarounds? Don Stewart It is hard to give a static type to the apply function, since its type depends on the type of the (possibly heterogeneous) list argument. There are at least two ways one way to write this function in Haskell that I can think of: Using reflection We can defer type checking of the

Ordering of parameters to make use of currying

我的梦境 提交于 2019-11-26 10:08:21
问题 I have twice recently refactored code in order to change the order of parameters because there was too much code where hacks like flip or \\x -> foo bar x 42 were happening. When designing a function signature what principles will help me to make the best use of currying? 回答1: For languages that support currying and partial-application easily, there is one compelling series of arguments, originally from Chris Okasaki: Put the data structure as the last argument Why? You can then compose

Composing function composition: How does (.).(.) work?

ε祈祈猫儿з 提交于 2019-11-26 09:46:15
问题 (.) takes two functions that take one value and return a value: (.) :: (b -> c) -> (a -> b) -> a -> c Since (.) takes two arguments, I feel like (.).(.) should be invalid, but it\'s perfectly fine: (.).(.) :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c What is going on here? I realize this question is badly worded...all functions really just take one argument thanks to currying. Maybe a better way to say it is that the types don\'t match up. 回答1: Let's first play typechecker for the mechanical

How to correctly curry a function in JavaScript?

半腔热情 提交于 2019-11-26 09:38:00
问题 I wrote a simple curry function in JavaScript which works correctly for most cases: const add = curry((a, b, c) => a + b + c); const add2 = add(2); const add5 = add2(3); console.log(add5(5)); <script> const curried = Symbol(\"curried\"); Object.defineProperty(curry, curried, { value: true }); function curry(functor, ...initArgs) { if (arguments.length === 0) return curry; if (typeof functor !== \"function\") { const value = JSON.stringify(functor); throw new TypeError(`${value} is not a

What&#39;s the difference between multiple parameters lists and multiple parameters per list in Scala?

痞子三分冷 提交于 2019-11-26 08:53:14
问题 In Scala one can write (curried?) functions like this def curriedFunc(arg1: Int) (arg2: String) = { ... } What is the difference between the above curriedFunc function definition with two parameters lists and functions with multiple parameters in a single parameter list: def curriedFunc(arg1: Int, arg2: String) = { ... } From a mathematical point of view this is (curriedFunc(x))(y) and curriedFunc(x,y) but I can write def sum(x) (y) = x + y and the same will be def sum2(x, y) = x + y I know

How can currying be done in C++?

不羁的心 提交于 2019-11-26 08:03:17
问题 What is currying? How can currying be done in C++? Please Explain binders in STL container? 回答1: In short, currying takes a function f(x, y) and given a fixed Y , gives a new function g(x) where g(x) == f(x, Y) This new function may be called in situations where only one argument is supplied, and passes the call on to the original f function with the fixed Y argument. The binders in the STL allow you to do this for C++ functions. For example: #include <functional> #include <iostream> #include