partial-application

Using bind for partial application without affecting the receiver

强颜欢笑 提交于 2019-12-02 06:40:21
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 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, and optionally more arguments. If you only want to fix the first (and potentially more) parameters of your

Haskell recursive function example with foldr

拜拜、爱过 提交于 2019-12-01 17:46:36
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 of applying this function to some arguments: firstThat (>10) 2000 [10,20,30,40] --returns 20, but

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

Partially applying a function that has an implicit parameter

拈花ヽ惹草 提交于 2019-11-30 17:35:35
Can I turn a method which takes an implicit parameter into a function? trait Tx def foo(bar: Any)(implicit tx: Tx) {} foo _ // error: could not find implicit value for parameter tx: Tx I am trying to achieve the following, preferably if I can somehow make it work with the plain call withSelection(deleteObjects) : trait Test { def atomic[A](fun: Tx => A): A def selection: Iterable[Any] def withSelection(fun: Iterable[Any] => Tx => Unit) { val sel = selection if (sel.nonEmpty) atomic { implicit tx => fun(sel)(tx) } } object deleteAction { def apply() { withSelection(deleteObjects) // ! } } def

Partially applying a function that has an implicit parameter

梦想的初衷 提交于 2019-11-30 01:38:44
问题 Can I turn a method which takes an implicit parameter into a function? trait Tx def foo(bar: Any)(implicit tx: Tx) {} foo _ // error: could not find implicit value for parameter tx: Tx I am trying to achieve the following, preferably if I can somehow make it work with the plain call withSelection(deleteObjects) : trait Test { def atomic[A](fun: Tx => A): A def selection: Iterable[Any] def withSelection(fun: Iterable[Any] => Tx => Unit) { val sel = selection if (sel.nonEmpty) atomic { implicit

What are the rules to govern underscore to define anonymous function?

风流意气都作罢 提交于 2019-11-28 10:11:03
I am using _ as placeholder for creating anonymous function, and the problem is I cannot predict how Scala is going to transform my code. More precisely, it mistakenly determines how "large" the anonymous function I want. List(1,2,3) foreach println(_:Int) //error ! List(1,2,3) foreach (println(_:Int)) //work List(1,2,3) foreach(println(_:Int)) //work Using -Xprint:typer I can see Scala transforms the first one into "a big anonymous function": x$1 => List(1,2,3) foreach(println(x$1:Int)) the worked 2th 3th are right transformation into what I want. ... foreach (x$1 => println(x$1:Int)) Why

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

怎甘沉沦 提交于 2019-11-28 04:50:50
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 = db.find.bind(null, arguments); bound(function(err, docs) { if(err) { deferred.fail(err); } else {

When do I have to treat my methods as partially applied functions in Scala?

你离开我真会死。 提交于 2019-11-27 14:38:49
问题 I noticed that when I'm working with functions that expect other functions as parameters, I can sometimes do this: someFunction(firstParam,anotherFunction) But other times, the compiler is giving me an error, telling me that I should write a function like this, in order for it to treat it as a partially applied function: someFunction(firstParam,anotherFunction _) For example, if I have this: object Whatever { def meth1(params:Array[Int]) = ... def meth2(params:Array[Int]) = ... } import

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

别来无恙 提交于 2019-11-27 13:56:28
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 ? This has nothing to do with functools.partial , really. You are essentially calling your function like this: f(1, x=3) Python first fulfils the positional arguments, and your first argument

Why does Scala provide both multiple parameters lists and multiple parameters per list? [duplicate]

混江龙づ霸主 提交于 2019-11-27 06:07:24
This question already has an answer here: What's the difference between multiple parameters lists and multiple parameters per list in Scala? 4 answers Multiple parameters lists, e.g. def foo(a:Int)(b:Int) = {} and multiple parameters per list, e.g. def foo(a:Int, b:Int) = {} are semantically equivalent so far as I can tell, and most functional languages have only one way of declaring multiple parameters, e.g. F#. The only reason I can figure out for supporting both these styles of function definitions is to allow syntax-like language extensions using a parameter list that has only one