currying

Partial application with a C++ lambda?

对着背影说爱祢 提交于 2019-12-02 22:41:54
EDIT: I use curry below, but have been informed this is instead partial application. I've been trying to figure out how one would write a curry function in C++, and i actually figured it out! #include <stdio.h> #include <functional> template< class Ret, class Arg1, class ...Args > auto curry( Ret f(Arg1,Args...), Arg1 arg ) -> std::function< Ret(Args...) > { return [=]( Args ...args ) { return f( arg, args... ); }; } And i wrote a version for lambdas, too. template< class Ret, class Arg1, class ...Args > auto curry( const std::function<Ret(Arg1,Args...)>& f, Arg1 arg ) -> std::function< Ret

Using ListPair.foldr to implement zipWith in SML

倖福魔咒の 提交于 2019-12-02 21:54:22
问题 Background: Beginner level at SML My assignment requires me to use ListPair.foldr and only this function to implement the zipWith function. ListPair.foldr : ('a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c zipWith : ('a * 'b -> 'c) -> 'a list -> 'b list -> 'c list ListPair.foldr returns a single 'c element, while zipWith returns a 'c list, so naturally my approach would use ListPair.foldr repeatedly to spill out individual 'c elements which I then put in my 'c list. ListPair.foldr takes

FoldLeft using FoldRight in scala

核能气质少年 提交于 2019-12-02 15:48:37
While going through Functional Programming in Scala , I came across this question: Can you right foldLeft in terms of foldRight? How about the other way around? In solution provided by the authors they have provided an implementation as follows: def foldRightViaFoldLeft_1[A,B](l: List[A], z: B)(f: (A,B) => B): B = foldLeft(l, (b:B) => b)((g,a) => b => g(f(a,b)))(z) def foldLeftViaFoldRight[A,B](l: List[A], z: B)(f: (B,A) => B): B = foldRight(l, (b:B) => b)((a,g) => b => g(f(b,a)))(z) Can somebody help me trace through this solution and make me understand how this actually gets the foldl

Haskell function application and currying

微笑、不失礼 提交于 2019-12-02 14:49:30
I am always interested in learning new languages, a fact that keeps me on my toes and makes me (I believe) a better programmer. My attempts at conquering Haskell come and go - twice so far - and I decided it was time to try again. 3rd time's the charm, right? Nope. I re-read my old notes... and get disappointed :-( The problem that made me lose faith last time, was an easy one: permutations of integers. i.e. from a list of integers, to a list of lists - a list of their permutations: [int] -> [[int]] This is in fact a generic problem, so replacing 'int' above with 'a', would still apply. From

Can I make this function defenition even shorter?

血红的双手。 提交于 2019-12-02 01:02:08
问题 I am trying move more towards functional programming in my javascript applications. I currently use the library ramda as a base lib for this. My desire: Create a function findWithId(id, list) which returns the item in the list with the property _id matching the input id. Make the implementation of it as short as possible, relying on existing code as much as possible. Acheived so far: My base is R.find which has this defenition find :: (a -> Boolean) -> [a] -> a | undefined I tried some

Currying syntax in scala

有些话、适合烂在心里 提交于 2019-12-02 00:51:09
The syntax of currying in scala is for example def f(x: Int, b: Int) = x + y is def f(x: Int)(b: Int) = x + y And currying for sum to sum for given range a and b is def sum(f: Int => Int, a: Int, b: Int) = { ... } sum(x=>x, 3, 6) // outcome is 18 (3+4+5+6) is def sum(f: Int => Int): (Int, Int) => Int = { def sumF(a: Int, b: Int): Int = if (a > b) 0 else f(a) + sumF(a + 1, b) sumF } sum(x=>x)(3, 6) // outcome is 18 (3+4+5+6) But I don't understand why colon(:) exists between (f: Int => Int) and (Int, Int) in def sum(f: Int => Int): (Int, Int) => Int = { instead of def sum(f: Int => Int)(Int,

Currying a function n times in Scheme

China☆狼群 提交于 2019-12-01 21:10:39
I'm having trouble figuring out a way to curry a function a specified number of times. That is, I give the function a natural number n and a function fun, and it curries the function n times. For example: (curry n fun) Is the function and a possible application would be: (((((curry 4 +) 1) 2) 3) 4) Which would produce 10. I'm really not sure how to implement it properly. Could someone please give me a hand? Thanks :) You can write your own n-curry procedure by repeatedly calling curry : (define (n-curry n func) (let loop ([i 1] [acc func]) (if (= i n) acc (loop (add1 i) (curry acc))))) If you

Currying a proc with keyword arguments in Ruby

帅比萌擦擦* 提交于 2019-12-01 17:31:13
Say I have a generic Proc , Lambda or method which takes an optional second argument: pow = -> (base, exp: 2) { base**exp } Now I want to curry this function, giving it an exp of 3 . cube = pow.curry.call(exp: 3) There's an ambiguity here, arising from the keyword arguments and the new hash syntax, where Ruby interprets exp: 3 as a hash being passed as the first argument, base . This results in the function immediately being invoked, rendering a NoMethodError when #** is sent to the hash. Setting a default value for the first argument will similarly result in the function being immediately

Currying a proc with keyword arguments in Ruby

放肆的年华 提交于 2019-12-01 17:01:09
问题 Say I have a generic Proc , Lambda or method which takes an optional second argument: pow = -> (base, exp: 2) { base**exp } Now I want to curry this function, giving it an exp of 3 . cube = pow.curry.call(exp: 3) There's an ambiguity here, arising from the keyword arguments and the new hash syntax, where Ruby interprets exp: 3 as a hash being passed as the first argument, base . This results in the function immediately being invoked, rendering a NoMethodError when #** is sent to the hash.

C++ Function bind repeating arguments to curried function

别说谁变了你拦得住时间么 提交于 2019-12-01 16:58:19
I am trying to understand the concept of currying and calling a function which concats three strings but by passing only two strings and using the second argument twice. However when I do this, the second argument is not getting sent to the function at all and it prints out an empty string. Is it some really obvious mistake? string concatthreestrings(string a,string b,string c){ cout<<"Value of A: "<<a<<endl; cout<<"Value of B: "<<b<<endl; cout<<"Value of C: "<<c<<endl; return a+b+c; } int main() { typedef std::function< string( string,string) > fun_t ; using namespace std::placeholders; fun_t