partial-application

Partial application in Haskell with multiple arguments

*爱你&永不变心* 提交于 2019-12-04 15:41:54
Given some function f(x1,x2,x3,..,xN) it is often useful to apply it partially in several places. For example, for N=3 we could define g(x)=f(1,x,3). However, the standard partial application in Haskell does not work this way and only allows us to partially apply a function by fixing its first arguments (because all functions actually only take one argument). Is there any simple way to do something like this: g = f _ 2 _ g 1 3 with output the value of f 1 2 3 ? Of course we could do a lambda-function g=(\x1 x3 -> f x1 2 x3) but I find this quite unreadable. For example, in Mathematica it works

How to efficiently partially apply a function in R?

情到浓时终转凉″ 提交于 2019-12-04 09:57:30
问题 Suppose I have a function in R that takes multiple arguments, and I'd like to reduce it to a function of fewer arguments by setting some of the arguments to pre-specified values. I'm trying to figure out what is the best way to do this is in R. For example, suppose I have a function f <- function(a,b,c,d){a+b+c+d} I'd like to create or find a function partial that would do the following partial <- function(f, ...){ #fill in code here } new_f <- partial(f, a=1, c= 2) new_f would be a function

Is a section the result of currying?

ε祈祈猫儿з 提交于 2019-12-04 05:47:11
问题 In Programming in Haskell by Hutton In general, if # is an operator, then expressions of the form (#) , (x #) , and (# y) for arguments x and y are called sections, whose meaning as functions can be formalised using lambda expressions as follows: (#) = \x -> (\y -> x # y) (x #) = \y -> x # y (# y) = \x -> x # y What are the difference and relation between "section" and "currying"? Is a section the result of applying the currying operation to a multi-argument function? Thanks. 回答1: Left

Using bind for partial application without affecting the receiver

南笙酒味 提交于 2019-12-04 05:28:52
问题 If I want to partially apply a function I can use bind , but it seems I have to affect the receiver of the function (the first argument to bind ). Is this correct? I want to perform partial application using bind without affecting the receiver. myFunction.bind(iDontWantThis, arg1); // I dont want to affect the receiver 回答1: partial application using bind without affecting the receiver That's not possible. bind was explicitly designed to partially apply the "zeroth argument" - the this value,

Why is a built-in function applied to too few arguments considered to be in weak head normal form?

会有一股神秘感。 提交于 2019-12-04 02:50:27
问题 The Haskell definition says: An expression is in weak head normal form (WHNF), if it is either: a constructor (eventually applied to arguments) like True, Just (square 42) or (:) 1 a built-in function applied to too few arguments (perhaps none) like (+) 2 or sqrt. or a lambda abstraction \x -> expression. Why do built-in functions receive special treatment? According to lambda calculus, there is no difference between a partially applied function and any other function, because at the end we

Partial application of operators

一个人想着一个人 提交于 2019-12-04 02:32:08
问题 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 回答1: Doing what you want is so common in Haskell it's got its own syntax, but being

Why and when do I need to follow a method name with _?

耗尽温柔 提交于 2019-12-04 01:12:46
I'm a bit shaky on the rules as to when you need a _ after a method to use it as a function. For example, why is there a difference between Foo 's and Nil 's :: in the following? def square(n: Int) = n * n object Foo { def ::(f: Int => Int) = f(42) } // ... scala> Foo.::(square) res2: Int = 1764 scala> Nil.::(square) <console>:6: error: missing arguments for method square in object $iw; follow this method with `_' if you want to treat it as a partially applied function Nil.::(square) ^ scala> Nil.::(square _) res3: List[(Int) => Int] = List(<function1>) When you omit all parameters in a

bind first argument of function without knowing its arity

北战南征 提交于 2019-12-03 12:50:47
I'd like to have a function BindFirst that binds the first argument of a function without me having to explicitly know/state the arity of the function by using std::placeholders. I'd like the client code to look something like that. #include <functional> #include <iostream> void print2(int a, int b) { std::cout << a << std::endl; std::cout << b << std::endl; } void print3(int a, int b, int c) { std::cout << a << std::endl; std::cout << b << std::endl; std::cout << c << std::endl; } int main() { auto f = BindFirst(print2, 1); // std::bind(print2, 1, std::placeholders::_1); auto g = BindFirst

How to efficiently partially apply a function in R?

北战南征 提交于 2019-12-03 05:31:16
Suppose I have a function in R that takes multiple arguments, and I'd like to reduce it to a function of fewer arguments by setting some of the arguments to pre-specified values. I'm trying to figure out what is the best way to do this is in R. For example, suppose I have a function f <- function(a,b,c,d){a+b+c+d} I'd like to create or find a function partial that would do the following partial <- function(f, ...){ #fill in code here } new_f <- partial(f, a=1, c= 2) new_f would be a function of b and d and would return 1+b+2+d In python I would do from functools import partial def f(a,b,c,d):

Is there a reason why `this` is nullified in Crockford's `curry` method?

那年仲夏 提交于 2019-12-03 05:04:27
问题 In Douglas Crockford's book "Javascript: The Good Parts" he provides code for a curry method which takes a function and arguments and returns that function with the arguments already added (apparently, this is not really what "curry" means, but is an example of "partial application"). Here's the code, which I have modified so that it works without some other custom code he made: Function.prototype.curry = function(){ var slice = Array.prototype.slice, args = slice.apply(arguments), that =