currying

Converting between Func with different # of type args

烂漫一生 提交于 2019-12-23 03:38:08
问题 Are there built in methods for converting between the various types of Func delegates? That is, suppose you need a Func, but you have a Func (and you have the value that should be passed in for the T parameter). For example: static TREsult Foo<TResult>(Func<TResult> f) { // ... TResult result = f(); // ... return result; } static int MyFunc(int i) { return i; } void CallFoo() { Func<int> func = ConvertFunc(MyFunc, 1); // Does this family of methods exist? int j = Foo(func); } I've written my

Scala, Currying on multi parameter-group method including implicit params?

柔情痞子 提交于 2019-12-22 04:45:23
问题 After having discovered that currying multi parameter-groups method is possible, I am trying to get a partially applied function which requires implicit parameters. It seams not possible to do so. If not could you explain me why ? scala> def sum(a: Int)(implicit b: Int): Int = { a+b } sum: (a: Int)(implicit b: Int)Int scala> sum(3)(4) res12: Int = 7 scala> val partFunc2 = sum _ <console>:8: error: could not find implicit value for parameter b: Int val partFunc2 = sum _ ^ I use a singleton

Confusion about currying and point free style in Haskell

时光怂恿深爱的人放手 提交于 2019-12-22 04:07:38
问题 I was trying to implement the function every :: (a -> IO Bool) -> [a] -> IO Bool which was the topic for this question. I tried to do this without explicit recursion . I came up with the following code every f xs = liftM (all id) $ sequence $ map f xs My function didn't work since it wasn't lazy (which was required in the question), so no upvotes there :-). However, I did not stop there. I tried to make the function point-free so that it would be shorter (and perhaps even cooler). Since the

Currying Example in Scala

邮差的信 提交于 2019-12-21 05:32:06
问题 Is the following a good example of currying? def sum(a: Int, b: Int) : (Int => Int) = { def go(a: Int) : Int = { a + b; } go } I half understand the below results, but how could I write (or maybe how I should've written) sum() in a curried way? scala> sum(3,4) res0: Int => Int = <function1> scala> sum(3,4).apply(2) res1: Int = 6 scala> sum(3,4).apply(3) res2: Int = 7 回答1: Currying mechanism was introduced in Scala to support type inference. For example foldLeft function in the standard lib:

how to implement curry(partial function) in ruby

↘锁芯ラ 提交于 2019-12-21 05:02:36
问题 I need some examples of implementing curry function in ruby(1.8.6 or 1.8.7 not 1.9). 回答1: So here's how to do currying with blocks, rather than methods: def curry(&block) arity = (block.arity >= 0) ? block.arity : -(block.arity + 1) # return an immediate value if the block has one return block[] if arity == 0 # otherwise, curry it argument by argument args = [] innermost = lambda do |last,*extra| args[arity-1] = last block[*(args+extra)] end (0...(arity-1)).to_a.reverse.inject(innermost) do

“int -> int -> int” What does this mean in F#?

匆匆过客 提交于 2019-12-21 03:49:13
问题 I wonder what this means in F#. “a function taking an integer, which returns a function which takes an integer and returns an integer.” But I don't understand this well. Can anyone explain this so clear ? [Update]: > let f1 x y = x+y ;; val f1 : int -> int -> int What this mean ? 回答1: F# types Let's begin from the beginning. F# uses the colon ( : ) notation to indicate types of things. Let's say you define a value of type int : let myNumber = 5 F# Interactive will understand that myNumber is

Partial Binding of Function Arguments

…衆ロ難τιáo~ 提交于 2019-12-20 18:39:23
问题 Is there a way to partially bind the first/last n arguments of a callable object (e.g. function) without explicitly specifying the rest of the arguments? std::bind() seems to require that all the arguments are be bound, those that are to be left should be bound to std::placeholders::_1 , _2 , _3 etc. Is it possible to write a bind_first() / bind_last() for partial binding starting from the first/last argument and that automagically inserts the placeholders for any remaining unbound arguments

C# Linq vs. Currying

谁说我不能喝 提交于 2019-12-20 09:13:29
问题 I am playing a little bit with functional programming and the various concepts of it. All this stuff is very interesting. Several times I have read about Currying and what an advantage it has. But I do not get the point with this. The following source demonstrates the using of the curry concept and the solution with linq. Actually, I do not see any advatages of using the currying concept. So, what is the advantage of using currying? static bool IsPrime(int value) { int max = (value / 2) + 1;

Understanding Currying in Scala

瘦欲@ 提交于 2019-12-20 05:30:23
问题 I'm getting problems to understand the currying concept, or at least the SCALA currying notation. wikipedia says that currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. Following this explanation, are the two next lines the same for scala? def addCurr(a: String)(b: String): String = {a + " " + b} def add(a:String): String => String = {b => a + " " + b} I've run both lines

What is currying in F#? [duplicate]

喜夏-厌秋 提交于 2019-12-20 03:31:43
问题 This question already exists : Closed 9 years ago . Possible Duplicate: Functional programming: currying I'm reading the free F# Wikibook here: http://en.wikibooks.org/wiki/F_Sharp_Programming There's a section explaining what Partial Functions are. It says that using F# you can partially use a function, but I just can't understand what's going on. Consider the following code snippet that is used an example: #light open System let addTwoNumbers x y = x + y let add5ToNumber = addTwoNumbers 5