partial-application

bind first argument of function without knowing its arity

て烟熏妆下的殇ゞ 提交于 2019-12-21 04:18:58
问题 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()

Haskell recursive function example with foldr

纵饮孤独 提交于 2019-12-19 17:35:32
问题 I've taken up learning Haskell again, after a short hiatus and I am currently trying to get a better understanding of how recursion and lambda expressions work in Haskell. In this: YouTube video, there is a function example that puzzles me far more than it probably should, in terms of how it actually works: firstThat :: (a -> Bool) -> a -> [a] -> a firstThat f = foldr (\x acc -> if f x then x else acc) For the sake of clarity and since it wasn't immediately obvious to me, I'll give an example

JavaScript curry function

落花浮王杯 提交于 2019-12-17 23:38:49
问题 I have implemented a curry function this way: function curry (fn) { var slice = Array.prototype.slice, args = slice.apply(arguments, [1]); return function () { fn.apply(null, args.concat(slice.apply(arguments))); }; } When I use the above function to do the following function add (x, y) { return x + y; } var inc = curry(add, 1); console.log(inc(10)); it logs undefined . Isn't 11 the expected output? What is wrong with my code? Note: Using console.log(x, y) inside the add function logs 1 10 .

Using Function.prototype.bind with an array of arguments?

两盒软妹~` 提交于 2019-12-17 17:38:44
问题 How can I call Function.prototype.bind with an array of arguments, as opposed to hardcoded arguments? (Not using ECMA6, so no spread operator). I'm trying to put a promises wrapper around a module that uses callbacks and I want to bind all of the arguments passed in to my wrapper method and bind them. Then I want to call the partially applied bound function with my own callback, which will resolve or reject a promise. var find = function() { var deferred, bound; deferred = Q.defer(); bound =

functools.partial wants to use a positional argument as a keyword argument

青春壹個敷衍的年華 提交于 2019-12-17 10:58:31
问题 So I am trying to understand partial : import functools def f(x,y) : print x+y g0 = functools.partial( f, 3 ) g0(1) 4 # Works as expected In: g1 = functools.partial( f, y=3 ) g1(1) 4 # Works as expected In: g2 = functools.partial( f, x=3 ) g2(1) TypeError: f() got multiple values for keyword argument 'x' The TypeError disappears if I use y as a keyword argument: In: g2( y=1 ) 4 What causes the TypeError ? 回答1: This has nothing to do with functools.partial , really. You are essentially calling

Does Java support Currying?

╄→гoц情女王★ 提交于 2019-12-17 08:04:08
问题 I was wondering if there is any way to pull that in Java. I think it is not possible without native support for closures. 回答1: Java 8 (released March 18th 2014) does support currying. The example Java code posted in the answer by missingfaktor can be rewritten as: import java.util.function.*; import static java.lang.System.out; // Tested with JDK 1.8.0-ea-b75 public class CurryingAndPartialFunctionApplication { public static void main(String[] args) { IntBinaryOperator simpleAdd = (a, b) -> a

Can one partially apply the second argument of a function that takes no keyword arguments?

徘徊边缘 提交于 2019-12-17 07:14:44
问题 Take for example the python built in pow() function. xs = [1,2,3,4,5,6,7,8] from functools import partial list(map(partial(pow,2),xs)) >>> [2, 4, 8, 16, 32, 128, 256] but how would I raise the xs to the power of 2? to get [1, 4, 9, 16, 25, 49, 64] list(map(partial(pow,y=2),xs)) TypeError: pow() takes no keyword arguments I know list comprehensions would be easier. 回答1: No According to the documentation, partial cannot do this (emphasis my own): partial.args The leftmost positional arguments

Why does this point-free F# function behave differently from the non-point-free version?

扶醉桌前 提交于 2019-12-12 16:24:54
问题 Consider the following F#:- type TestClass() = let getValFromMap m k = Map.find k m let addToMap map k i = map |> Map.add k i let mutable someMap : Map<string,int> = Map.empty let getValFromMapPartial key = getValFromMap someMap key let getValFromMapPartialAndTacit = getValFromMap someMap member this.AddThenGet() = someMap <- addToMap someMap "A" 10 let value = getValFromMapPartial "A" printfn "Value from partial = %i" value // prints out let value = getValFromMapPartialAndTacit "A" // throws

How does the outermost evaluation strategy evaluate partial application of a function and application of a curried function

元气小坏坏 提交于 2019-12-11 06:47:33
问题 Programming in Haskell by Hutton says When evaluating an expression, in what order should the reductions be performed? One common strategy, known as innermost evaluation , is to always choose a redex that is innermost, in the sense that it contains no other redex. If there is more than one innermost redex, by convention we choose the one that begins at the leftmost position in the expression. Another common strategy for evaluating an expression, dual to innermost evaluation, is to always

Can I rewrite this code to avoid the F# error

谁说胖子不能爱 提交于 2019-12-11 04:18:55
问题 I have the following code in F# live version at https://repl.it/repls/CarefulGiganticExtraction let inline tryParse text = let mutable r = Unchecked.defaultof<_> (^a : (static member TryParse: string * ^a byref -> bool) (text, &r)), r let inline tryParseWithDefault (defaultVal:'a) text = match tryParse text with | true, v -> v | _ -> defaultVal type TextBox(text:string) = member this.Text = text type View() = member this.Name = "View" type State = { count: int } module Binder = let inline