currying

Scala: curried constructors

☆樱花仙子☆ 提交于 2019-11-29 05:32:07
I have the following Scala class: class Person(var name : String, var age : Int, var email : String) I would like to use the Person constructor as a curried function: def mkPerson = (n : String) => (a : Int) => (e : String) => new Person(n,a,e) This works, but is there another way to accomplish this? This approach seems a bit tedious and error-prone. I could imagine something like Function.curried, but then for constructors. This will work: def mkPerson = (new Person(_, _, _)).curried A bit late to this party, but if you make Person a case class: scala> case class Person(name: String, age: Int

Scala, currying and overloading

你离开我真会死。 提交于 2019-11-29 03:28:16
Say you have the following: foo(x: String)(y: Int): Int foo(x: String)(y: Double): Int Scala does not allow such expression. As far as I can see, the reason for this is that foo("asdf") does not have a well defined type (it's either Int => Int or Double => Int). Is there a reason why such "polytyped" functions should not be allowed? Overloading resolution in Scala takes only the first parameter list into account. That's why alternatives must differ already in this list. There's a good reason for this: We can then use the resolved function's type to infer the type of subsequent arguments. This

What does uncurry ($) do?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 01:35:22
I'm doing some excersises where I have to add a function's type and explain what it does. I'm stuck with this: phy = uncurry ($) The type, according to GHCi is phy :: (a -> b, a) -> b . My haskell knowledge is basic so I really have no idea what it does. Let's spell out the type part systematically. We'll start with the types of uncurry and ($) : uncurry :: (a -> b -> c) -> (a, b) -> c ($) :: (a -> b) -> a -> b Since the target expression has ($) as the argument of uncurry , let's line up their types to reflect this: uncurry :: (a -> b -> c) -> (a, b) -> c ($) :: (a -> b) -> a -> b The whole

Feed elements of a tuple to a function as arguments in Haskell?

老子叫甜甜 提交于 2019-11-29 01:15:11
In my Haskell program, I want to use printf to format a list of tuples. I can map printf over a list to print out the values one at a time like this: mapM_ (printf "Value: %d\n") [1,2,3,4] Value: 1 Value: 2 Value: 3 Value: 4 I want to be able to do something like this: mapM_ (printf "Values: %d %d\n") [(1,100),(2,350),(3,600),(4,200)] Values: 1 100 Values: 2 350 Values: 3 600 Values: 4 200 But this passes a tuple to printf, not two separate values. How can I turn the tuple into two arguments for printf? Function uncurry converts a two-argument (curried) function into a function on pairs. Here

Does Haskell have variadic functions/tuples?

会有一股神秘感。 提交于 2019-11-29 00:07:01
问题 The uncurry function only works for functions taking two arguments: uncurry :: (a -> b -> c) -> (a, b) -> c If I want to uncurry functions with an arbitrary number of arguments, I could just write separate functions: uncurry2 f (a, b) = f a b uncurry3 f (a, b, c) = f a b c uncurry4 f (a, b, c, d) = f a b c d uncurry5 f (a, b, c, d, e) = f a b c d e But this gets tedious quickly. Is there any way to generalize this, so I only have to write one function? 回答1: Try uncurryN from the tuple package

(How) is it possible to bind/rebind a method to work with a delegate of a different signature?

梦想与她 提交于 2019-11-28 23:10:51
问题 I'm a c++ developer having used signals & slots in c++ which to me seems to be analogous to delegates in c#. I've found myself at a loss in searching for the functionality provided by "bind", and feel I must be missing something. I feel like that something like the following, which is possible in c++ should be possible in c# with delegates. Here is some psudo-code for what I would do in c++: Slot<void> someCallback; int foo(int i) { std::cout << "Value: " << i << "\n"; return i; } int main()

Practical use of curried functions?

ⅰ亾dé卋堺 提交于 2019-11-28 20:15:04
There are tons of tutorials on how to curry functions, and as many questions here at stackoverflow. However, after reading The Little Schemer, several books, tutorials, blog posts, and stackoverflow threads I still don't know the answer to the simple question: "What's the point of currying?" I do understand how to curry a function, just not the "why?" behind it. Could someone please explain to me the practical uses of curried functions (outside of languages that only allow one argument per function, where the necessity of using currying is of course quite evident.) edit: Taking into account

Currying with Mathematica

╄→гoц情女王★ 提交于 2019-11-28 15:49:37
问题 One may implement a limited form of Currying in Mathematica, using this construct: f[a_][b_][c_] := (a^2 + b^2)/c^2 Allowing one to do, for example: f[4][3] /@ Range@5 {25, 25/4, 25/9, 25/16, 1} There is a problem: Attributes only apply to the first (set of) argument(s). Consider: ClearAll[f] SetAttributes[f, HoldAllComplete] f[a_][b_][c_] := {ToString@Unevaluated@a, ToString@Unevaluated@b, ToString@Unevaluated@c} f[2 + 2][ 8/4 ][3 + 5] {"2 + 2", "2", "8"} My intent was to return "8 / 4" and

What does lambda with 2 arrows mean in Java 8?

℡╲_俬逩灬. 提交于 2019-11-28 15:45:35
I have read several Java 8 tutorials before. Right now I encountered following topic: Does java support Currying? Here, I see following code: IntFunction<IntUnaryOperator> curriedAdd = a -> b -> a + b; System.out.println(curriedAdd.apply(1).applyAsInt(12)); I understand that this example sum 2 elements but I cannot understand the construction: a -> b -> a + b; According to the left part of expression, this row should implement following function: R apply(int value); Before this, I only met lambdas only with one arrow. If you express this as non-shorthand lambda syntax or pre-lambda Java

How to curry a function across an unknown number of parameters

百般思念 提交于 2019-11-28 13:44:22
Say I have a function called multiplyDivide If I were to call multiplyDivide(2)(3)(4)(6) it would be equivalent to 2 * 3 / 4 * 6 . Update: Is it possible to write a function like this if I don't know in advance how many parameters I will be taking? For example, I could have multiplyDivide(1)(2) or multiplyDivide(1)(2)(3)(4)...(n-1)(n) Why does it have to be curried? Wouldn't this be much easier to just "overload" a function by supplying n arguments? function multiplyDivide() { var result = arguments[0]; for (var i = 1; i < arguments.length; i++) { result = (i % 2) ? (result * arguments[i]) :