currying

How does currying work?

可紊 提交于 2019-11-28 10:56:56
I'm very new to Haskell and FP in general. I've read many of the writings that describe what currying is, but I haven't found an explanation to how it actually works. Here is a function: (+) :: a -> (a -> a) If I do (+) 4 7 , the function takes 4 and returns a function that takes 7 and returns 11 . But what happens to 4 ? What does that first function do with 4 ? What does (a -> a) do with 7 ? Things get more confusing when I think about a more complicated function: max' :: Int -> (Int -> Int) max' m n | m > n = m | otherwise = n what does (Int -> Int) compare its parameter to? It only takes

What's the rationale behind curried functions in Scala?

核能气质少年 提交于 2019-11-28 09:19:14
问题 I am just new to Scala and it seems a little bit confusing to me why Scala provides "curried functions" such as: //curried function def add(lhs: Int)(rhs: Int) = lhs + rhs //so we can do partially binding like val add1 = add(1)_ Its confusing because Scala already provides 'partial application' to normal functions, e.g., //normal function def add(lhs: Int, rhs: Int) = lhs + rhs //also supports partially application val add1 = add(1, _: Int) So my question is: is there any other point of using

Why can't C# compiler infer generic-type delegate from function signature? [duplicate]

匆匆过客 提交于 2019-11-28 07:31:34
问题 This question already has an answer here: Why can't C# infer type from this seemingly simple, obvious case 5 answers I'm working with a function that takes two functions as parameters, and returns a new composed one: public static Action<T> Compose<T>(Action<T> first, Action second) { return new Action<T>(arg => { first(arg); second(); }); } I've noticed that the compiler complains if I don't specify T , when sending it a static or member function (as opposed to an actual Action<T> object):

“Uncurrying” an instance method in .NET

99封情书 提交于 2019-11-28 07:05:27
Can you create a delegate of an instance method without specifying the instance at creation time? In other words, can you create a "static" delegate that takes as it's first parameter the instance the method should be called on? For example, how can I construct the following delegate using reflection? Func<int, string> = i=>i.ToString(); I'm aware of the fact that I can use methodInfo.Invoke, but this is slower, and does not check for type-correctness until it is called. When you have the MethodInfo of a particular static method, it is possible to construct a delegate using Delegate

How to reconcile Javascript with currying and function composition

喜欢而已 提交于 2019-11-28 04:39:41
问题 I love currying but there are a couple of reasons why a lof of Javascript devs reject this technique: aesthetic concerns about the typical curry pattern: f(x) (y) (z) concerns about performance penalties due to the increased number of function calls concerns about debugging issues because of the many nested anonymous functions concerns about readability of point-free style (currying in connection with composition) Is there an approach that can mitigate these concerns so that my coworkers don

How should I make function curry?

拟墨画扇 提交于 2019-11-28 04:32:47
In C++14, what is a good way to curry functions or function objects? In particular, I have an overloaded function foo with some random number of overloads: some overloads may be found via ADL, others may be defined in a myriad of places. I have a helper object: static struct { template<class...Args> auto operator()(Args&&...args)const -> decltype(foo(std::forward<Args>(args)...)) { return (foo(std::forward<Args>(args)...));} } call_foo; that lets me pass the overload set around as a single object. If I wanted to curry foo , how should I do so? As curry and partial function application are

What is 'Currying'?

有些话、适合烂在心里 提交于 2019-11-27 23:24:45
I've seen references to curried functions in several articles and blogs but I can't find a good explanation (or at least one that makes sense!) Kyle Cronin Currying is when you break down a function that takes multiple arguments into a series of functions that each take only one argument. Here's an example in JavaScript: function add (a, b) { return a + b; } add(3, 4); // returns 7 This is a function that takes two arguments, a and b, and returns their sum. We will now curry this function: function add (a) { return function (b) { return a + b; } } This is a function that takes one argument, a,

Is it possible to curry method calls in PHP?

跟風遠走 提交于 2019-11-27 22:17:18
I have a SoapClient instance generated for a WSDL file. All except one of the method invocations require the username and the password to be passed id. Is there any way of currying the method calls so that I can omit the username and password? As of php 5.3 you can store an anonymous function in a variable. This anonymous function can call the "original" function with some predefined parameters. function foo($x, $y, $z) { echo "$x - $y - $z"; } $bar = function($z) { foo('A', 'B', $z); }; $bar('C'); edit: You can also use a closure to parametrise the creation of the anonymous function function

Currying decorator in python

这一生的挚爱 提交于 2019-11-27 20:11:00
I am trying to write a currying decorator in python, and I think I've got the general idea down, but still got some cases that aren't working right... def curry(fun): cache = [] numargs = fun.func_code.co_argcount def new_fun(*args, **kwargs): print args print kwargs cache.extend(list(args)) if len(cache) >= numargs: # easier to do it explicitly than with exceptions temp = [] for _ in xrange(numargs): temp.append(cache.pop()) fun(*temp) return new_fun @curry def myfun(a,b): print a,b While for the following case this works fine: myfun(5) myfun(5) For the following case it fails: myfun(6)(7)

Scala Functional Literals with Implicits

老子叫甜甜 提交于 2019-11-27 19:05:12
Forgive me if this has already been asked elsewhere. I have a Scala syntax question involving function-values and implicit parameters. I'm comfortable using implicits with Scala's currying feature. For instance if I had a sum function and wanted to make the second argument an implicit: scala> def sum(a: Int)(implicit b: Int) = a + b sum: (a: Int)(implicit b: Int)Int Is there a way to do this using the function-value syntax? Ignoring the implicit for a moment, I typically write curried function-values like this: scala> val sum2 = (a: Int) => (b: Int) => a + b sum: (Int) => (Int) => Int =