currying

Lambda as a combination of methods from the Predicate interface doesn't compile if it is written as one statement

非 Y 不嫁゛ 提交于 2019-12-01 02:04:54
问题 What is the difference between both these ways of lambda creation? Why doesn't the first one compile? Predicate<Integer> predicate = Predicate.isEqual(0).or(Predicate.isEqual(1)); Gives: error : incompatible types: Predicate<Object> cannot be converted to Predicate<Integer> = Predicate.isEqual(0).or(Predicate.isEqual(1)); Predicate<Integer> pred21 = Predicate.isEqual(0); Predicate<Integer> pred22 = pred21.or(Predicate.isEqual(1)); This one works. 回答1: Adding <Integer> before the isEqual

Partial function application prematurely runs codeblock when used with underscore

江枫思渺然 提交于 2019-12-01 00:11:12
问题 Given: def save(f: => Any)(run:Boolean) { if (run) { println("running f"); f } else println("not running f") } I can call it with: save("test")(true) -> running f save("test")(false) -> not running f save(throw new RuntimeException("boom!"))(false) -> not running f save(throw new RuntimeException("boom!"))(true) -> running f and then exception thrown Here's the curious behaviour with partial application: save(throw new RuntimeException("boom!"))(_) -> (Boolean) => Unit = <function1> //as

How does function application with the $ operator curry in Haskell?

╄→гoц情女王★ 提交于 2019-11-30 23:55:57
问题 I am learning haskell and am a little confused how the function application operator $ curry's. According to GHC the type of $ is *Main>:t ($) ($) :: (a->b) -> a -> b But I can type the following code *Main>map ($ 2) [(*2), (+2), (/2)] [4.0,4.0,1.0] According to the signature of $ though I would assume I would need to use the flip function because the first parameter to $ is (a->b). For example, I can't do the following curry_test :: Integer -> String -> String curry_test x y = (show x) ++ "

What's the difference between currying and multiple parameter lists?

陌路散爱 提交于 2019-11-30 22:02:46
问题 Everywhere I look, I see the terms multiple parameter lists and currying used interchangably. I see it in dozens of stackoverflow questions, and even on scala-lang.org. This page, for example, has the title "Currying". And the first sentence? "Methods may define multiple parameter lists." And yet some very knowledgeable people get annoyed when they see multiple parameter lists and currying equated. I posted an answer to this question but then deleted it when I saw Randall Schulz's comment

Partially applying a function that has an implicit parameter

拈花ヽ惹草 提交于 2019-11-30 17:35:35
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 tx => fun(sel)(tx) } } object deleteAction { def apply() { withSelection(deleteObjects) // ! } } def

Does curry function in javascript uses principle of closure? [closed]

自古美人都是妖i 提交于 2019-11-30 12:55:15
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . It would be very helpful, if someone explains the working of a curry function. I have read many examples, but not able to grasp it properly. Is it anyhow related to closure. 回答1: Currying is just technique, that can make use of any language feature (e.g. closures) to achieve the

Is it possible to “curry” higher-kinded types in Scala?

谁都会走 提交于 2019-11-30 11:58:41
问题 Let's suppose I have a trait with two type parameters, e.g. trait Qux[A, B] and another trait with a higher-kinded type parameter, e.g. trait Turkle[C[_]] I'd like to be able to substitute a fixed value for one of the type parameters for Qux , so that it can be used to parametrize Turkle . Here's an example (of code that doesn't make sense in Scala!): trait Baz[A] extends Turkle[Qux[A, _]] Anyone have any ideas how to achieve this effect? 回答1: Jason Zaugg came up with the most succinct way to

Currying in javascript for function with n parameters

人走茶凉 提交于 2019-11-30 09:52:20
问题 If f :: (a, b) -> c, we can define curry(f) as below: curry(f) :: ((a, b) -> c) -> a -> b -> c const curry = f => a => b => f(a, b); const sum = curry((num1, num2) => num1 + num2); console.log(sum(2)(3)); //5 How do we implement generic curry function that takes a function with n parameters? 回答1: If I understand correctly, I think this is the way to go using ES6: const curry = f => { const nargs = f.length; const vargs = []; const curried = (...args) => vargs.push(...args) >= nargs ? f(..

Passing list elements as parameters to curried function

谁说胖子不能爱 提交于 2019-11-30 09:19:29
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... magicalFunctionMaker (quadsum) [4,3,2] Getting a curried function like...: (((quadsum 4) 3) 2) Or,

Scala - Currying and default arguments

杀马特。学长 韩版系。学妹 提交于 2019-11-30 08:58:43
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 can't omit some/all arguments in 'partial' as follows: partial(2); partial(c=3); partial(); Shouldn't