currying

Scala, currying and overloading

拜拜、爱过 提交于 2019-11-30 07:26:06
问题 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? 回答1: 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

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

本小妞迷上赌 提交于 2019-11-30 02:13:32
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() { int i = 0; Slot<int> someCallback = bind( fun_ptr(foo), i ); ++i; // added to show that late

Partially applying a function that has an implicit parameter

梦想的初衷 提交于 2019-11-30 01:38:44
问题 Can I turn a method which takes an implicit parameter into a function? trait Tx def foo(bar: Any)(implicit tx: Tx) {} foo _ // error: could not find implicit value for parameter tx: Tx I am trying to achieve the following, preferably if I can somehow make it work with the plain call withSelection(deleteObjects) : trait Test { def atomic[A](fun: Tx => A): A def selection: Iterable[Any] def withSelection(fun: Iterable[Any] => Tx => Unit) { val sel = selection if (sel.nonEmpty) atomic { implicit

Does Haskell have variadic functions/tuples?

泄露秘密 提交于 2019-11-30 01:37:13
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? Try uncurryN from the tuple package. Like all forms of overloading, it's implemented using type classes. In this case by manually spelling out

How do I write a function that returns another function?

a 夏天 提交于 2019-11-29 20:22:51
In Python, I'd like to write a function make_cylinder_volume(r) which returns another function. That returned function should be callable with a parameter h , and return the volume of a cylinder with height h and radius r . I know how to return values from functions in Python, but how do I return another function ? Try this, using Python: import math def make_cylinder_volume_func(r): def volume(h): return math.pi * r * r * h return volume Use it like this, for example with radius=10 and height=5 : volume_radius_10 = make_cylinder_volume_func(10) volume_radius_10(5) => 1570.7963267948967 Notice

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

风格不统一 提交于 2019-11-29 13:47:54
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): static void Main(string[] args) { // compiler error here var composed = Compose(Test, () => Console

Currying groovy CPS closure for parallel execution

瘦欲@ 提交于 2019-11-29 13:27:23
We do some dynamic creation of parallel steps in some of our jobs. Thanks to this thread I found how to dynamically create the map with parameters for use in the parallel step. However now I wanted to reuse parts of the code which is used to create those parallel steps. For this I feel that I would need to curry the closures. However currying seems not to work correctly. Referencing the loop variable (valueCopy) inside the closure does the right thing ( as mentioned here ) but currying does not do what I would expect. Am I doing something wrong, is this just not (yet) supported, are there any

Passing list elements as parameters to curried function

南楼画角 提交于 2019-11-29 13:27:23
问题 Still a Haskell newbie here. I know just enough to get myself into trouble with wrong assumptions. If I have the following function... quadsum w x y z = w+x+y+z I want a function that can take a list, use each element as a parameter in a specified function like quadsum , and return a curried function for later use. I've been trying something to the effect of... magicalFunctionMaker f [] = (f) magicalFunctionMaker f (x:xs) = magicalFunctionMaker (f x) xs With the hope of being able to do this.

Scala - Currying and default arguments

為{幸葍}努か 提交于 2019-11-29 13:02:10
问题 I have a function with two parameter lists that I am trying to partially apply and use with currying. The second parameter list contains arguments that all have default values (but not implicit). Something like this: def test(a: Int)(b: Int = 2, c: Int = 3) { println(a + ", " + b + ", " + c); } Now, the following is all fine: test(1)(2, 3); test(1)(2); test(1)(c=3); test(1)(); Now if I define: def partial = test(1) _; Then the following can be done: partial(2, 3); Can someone explain why I

JS Curry function with Recursion

回眸只為那壹抹淺笑 提交于 2019-11-29 09:54:54
问题 Kindly read before you mark it as duplicate. Im not asking for single curry call. This functions multiplies, multiplication(4,4,4) //64 function multiplication(...args) { return args.reduce((accum, val) => accum * val, 1) } But Im trying to achieve something else... This same function should multiply its curry function parenthesis as well. e.g. /* which return the multiplication of three numbers. The function can be called in any of the following forms: multiply(2, 3)(4) => 24 multiply(2)(3,