currying

Partial application of operators

孤人 提交于 2019-12-01 13:58:57
If I want to add a space at the end of a character to return a list, how would I accomplish this with partial application if I am passing no arguments? Also would the type be? space :: Char -> [Char] I'm having trouble adding a space at the end due to a 'parse error' by using the ++ and the : operators. What I have so far is: space :: Char -> [Char] space = ++ ' ' Any help would be much appreciated! Thanks Doing what you want is so common in Haskell it's got its own syntax, but being Haskell, it's extraordinarily lightweight. For example, this works: space :: Char -> [Char] space = (:" ") so

What is a function composition algorithm that will work for multiple arguments, such as h(x,y) . f(x) . g(x) = h(f(x),g(x))?

泪湿孤枕 提交于 2019-12-01 13:26:35
For example, suppose we had the functions double(x) = 2 * x , square(x) = x ^ 2 and sum(x,y) = x + y . What is a function compose such as compose(compose(sum, square), double) = x^2 + 2*x ? Notice that I'm asking a function that can be used for functions of any arity. For example, you could compose f(x,y,z) with g(x) , h(x) , i(x) into f(g(x), h(x), i(x)) . This is a common Haskell idiom, applicative functors : composed = f <$> g1 <*> g2 <*> ... <*> gn (A nicer introduction can be found here ). This looks very clean because of automatic partial application, and works like this: (<*>) f g x = f

Confused about javascript function returning multiple functions with many fat arrow

萝らか妹 提交于 2019-12-01 12:44:30
I have problem with my cs homework. I need to access the x value of the function, but my codes is returning me an empty function instead of with the values I have googled all the currying and closures, but none of them are advanced enough to help me solve my problem const pair = (x, y) => f => f(x, y); // Do not edit this function const head = p => //Answer here console.log(head(pair(1,2))) // Do not edit this my console keeps returning me functions instead when I try all the combinations function(a,b){return a;} You could change head function like this: const pair = (x, y) => f => f(x, y);

Confused about javascript function returning multiple functions with many fat arrow

北城余情 提交于 2019-12-01 11:56:54
问题 I have problem with my cs homework. I need to access the x value of the function, but my codes is returning me an empty function instead of with the values I have googled all the currying and closures, but none of them are advanced enough to help me solve my problem const pair = (x, y) => f => f(x, y); // Do not edit this function const head = p => //Answer here console.log(head(pair(1,2))) // Do not edit this my console keeps returning me functions instead when I try all the combinations

Currying Expressions in C#

拈花ヽ惹草 提交于 2019-12-01 06:49:09
I am trying to build up an expression tree that I can feed into Linq2SQL so that it will generate a nice clean query. My purpose is to build a filter that takes an arbitrary set of words to AND and NOT (or OR and NOT) together. Because I want to vary the fields that I search on I preferably want to compose a list of Expresssion<Func<T, string, bool>> 's together (where T is the entity I am operating on) by calling a variety of helper functions. Then I would receive an array of words and loop though them and build an Expresssion<Func<T, bool>> up (negating certain expressions where necessary)

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

廉价感情. 提交于 2019-12-01 05:39:46
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. Adding <Integer> before the isEqual method call should help : Predicate<Integer> predicate = Predicate.<Integer>isEqual(0).or(Predicate.isEqual(1));

Returning a lambda capturing a local variable

会有一股神秘感。 提交于 2019-12-01 04:26:21
问题 Today I encountered a very unintuitive behavior (for me, at least) in C++11 lambdas. The code in question is the following: #include <stdio.h> auto sum(int x) { return [&x](int y) { return x + y; }; } int main() { int a = sum(2)(3); printf("%d\n",a); } Instead of printing 5, this prints gibberish. Actually, at least in my version of GCC, if I turn on the -O2 optimization flag, it actually prints 5. Since the output depends on the optimization level of the compiler, it is undefined behavior.

Writing a curried javascript function that can be called an arbitrary number of times that returns a value on the very last function call

旧城冷巷雨未停 提交于 2019-12-01 03:37:02
问题 I'm currently working on a programming problem in my personal time that asks that I make a javascript function that can be called in this manner. add(1) // 1 add(1)(2) // 3 add(1)(2)(3); // 6 add(1)(2)(3)(4); // 10 add(1)(2)(3)(4)(5); // 15 What I'm having trouble figuring out is how to make it return a value on the very last call. For example, in order for add(1)(2) to work, then add(1) has to return a function, but according to the instructions add(1) when called by itself will return 1 . I

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

橙三吉。 提交于 2019-12-01 03:29:13
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) ++ " " ++ y *Main> let x = curry_test "123" Couldn't match expected type `Integer' with actual type `[Char]'

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

那年仲夏 提交于 2019-12-01 02:25:35
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 because I was afraid I might be inadvertently spreading misinformation. My understanding was that a