currying

Why can't I implicitly cast a Delegate with Extension methods?

倖福魔咒の 提交于 2019-12-05 05:53:40
I'm trying to figure out a way to automatically cast something to an Action or Func and the best I can come up with is something like this: [TestFixture] public class ExecutionTest { public void BadMethod() { throw new Exception("Something bad happened"); } [Test] public void TestBadMethod() { // Want this, but it won't work!! // BadMethod.Execute().IgnoreExceptions(); // Ick ((Action)BadMethod).Exec().IgnoreExceptions(); // Still ick ((Action)BadMethod).IgnoreExceptions(); // Do not want ExtensionMethods.Exec(BadMethod).IgnoreExceptions(); // Better but still meh this.Exec(BadMethod)

How do you curry any javascript function of arbitrary arity?

我是研究僧i 提交于 2019-12-05 04:06:56
Let's say I have some function: function g(a,b,c){ return a + b + c } And I'd like to turn it into its "curried" form (in quotations since it's not exactly curried per se): function h(a,b,c){ switch(true){ case (a !== undefined && b !== undefined && c !== undefined): return a + b + c case (a !== undefined && b !== undefined && c === undefined): return function(c){ return a + b + c } case (a !== undefined && b == undefined && c === undefined ): return function(b,c){ return (c === undefined) ? function(c){ return a + b + c } : a + b + c } default: return h } } The above form has the partial

Confusion about currying and point free style in Haskell

不想你离开。 提交于 2019-12-05 02:48:27
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 arguments f and xs are the last ones in the expression I just dropped them: every = liftM (all id) $

Function composition in Haskell with tuple arguments [duplicate]

南楼画角 提交于 2019-12-05 01:13:25
This question already has an answer here: Feed elements of a tuple to a function as arguments in Haskell? 3 answers Sometimes I have two functions of the form: f :: a -> (b1,b2) h :: b1 -> b2 -> c and I need the composition g. I solve this by changing h to h': h' :: (b1,b2) -> c Can you please show me (if possible) a function m, so that: (h . m . f) == (h' . f) Or another way to deal with such situations. Thanks. What you're looking to do is to take a function that operates on curried arguments, h , and apply it to the result of f , which is a tuple. This process, turning a function of two

When to use currying and partial functions in JavaScript

纵饮孤独 提交于 2019-12-05 01:09:46
问题 I read this post on Dr. Dobb's about currying and partial functions in JavaScript. It looks useful, but I'm wondering (as an occasional developer in JavaScript) if there are standard situations where this is regularly used? 回答1: For starters, I personally wouldn't recommend currying in 99% cases. It can easily make code unreadable if used improperly. However, some of the applications that I could name would be associated with setting the function context. For example when you first execute

Partial Application with Infix Functions

戏子无情 提交于 2019-12-05 00:31:50
While I understand a little about currying in the mathematical sense, partially applying an infix function was a new concept which I discovered after diving into the book Learn You a Haskell for Great Good . Given this function: applyTwice :: (a -> a) -> a -> a applyTwice f x = f (f x) The author uses it in a interesting way: ghci> applyTwice (++ [0]) [1] [1,0,0] ghci> applyTwice ([0] ++) [1] [0,0,1] Here I can see clearly that the resulting function had different parameters passed, which would not happen by normal means considering it is a curried function (would it?). So, is there any

Number of arguments and point-free in Haskell [duplicate]

一曲冷凌霜 提交于 2019-12-04 23:15:11
This question already has answers here : Defining a function by equations with different number of arguments (3 answers) Closed 2 years ago . With multiple pattern-matching, different numbers of arguments are impossible, even with point-free! foo True b = b + 2 foo _ = id doesn't work for example. But foo True = (+2) foo _ = id does. Sometimes we can use point-free only in one part of the function, so... Why? Is it too hard for GHC? :'( Why? Is it too hard for GHC? No . It is not at all too hard for GHC. Actually, this is the fault of the Haskell Report. See: Haskell Report 2010 > Declarations

Does .Net support curried generics?

六月ゝ 毕业季﹏ 提交于 2019-12-04 16:56:17
问题 Suppose we have a nested generic class: public class A<T> { public class B<U> { } } Here, typeof(A<int>.B<>) is in essence a generic class with two parameters where only the first is bound. If I have a single class with two parameters public class AB<T, U> { } Is there a way to refer to " AB with T=int and U staying open"? If not, is this a C# limitation, or a CLR limitation? 回答1: Apparently it can't be done in C#, you have to specify either both type parameters, or none. And it doesn't seem

Memoize a currified function

天大地大妈咪最大 提交于 2019-12-04 08:56:12
问题 const f = (arg1) => (arg2) => { /* returns something */ } Is it possible to memoize f with regard to the 2 arguments, namely: f(1)(2); f(1)(3); // Cache not hit f(4)(2); // Cache not hit f(1)(2); // Cache hit 回答1: You could take a Map as cache and take nested maps for all following arguments. This cache works for arbitrary count of arguments and reuses the values from the former calls. It works by taking a curried function and an optional Map . If the map is not supplied, a new map is created

Haskell Monad bind operator confusion

萝らか妹 提交于 2019-12-04 07:51:34
问题 Okay, so I am not a Haskell programmer, but I am absolutely intrigued by a lot of the ideas behind Haskell and am looking into learning it. But I'm stuck at square one: I can't seem to wrap my head around Monads, which seem to be fairly fundamental. I know there are a million questions on SO asking to explain Monads, so I'm going to be a little more specific about what's bugging me: I read this excellent article (an introduction in Javascript), and thought that I understood Monads completely.