currying

Scala currying vs partially applied functions

做~自己de王妃 提交于 2019-12-27 12:39:06
问题 I realize that there are several questions on here about what currying and partially applied functions are, but I'm asking about how they are different. As a simple example, here is a curried function for finding even numbers: def filter(xs: List[Int], p: Int => Boolean): List[Int] = if (xs.isEmpty) xs else if (p(xs.head)) xs.head :: filter(xs.tail, p) else filter(xs.tail, p) def modN(n: Int)(x: Int) = ((x % n) == 0) So you could write the following to use this: val nums = List(1,2,3,4,5,6,7

Curried constructor of generic class

时光怂恿深爱的人放手 提交于 2019-12-25 08:26:19
问题 Let me preface this by saying that I am not sure if this is possible. I am trying to obtain a constructor function that can be invoked with new that takes no parameters that calls a generic class's constructor that does take parameters. Like so: class SimpleFoo { public Key: String = null; } class GenericFoo<T> { public Key: T = null; constructor(private type: { new (): T }) { this.Key = new this.type(); } } let simpleCtor: { new (): SimpleFoo } = SimpleFoo; // works let simpleObj = new

standard ml make bst out of a list

半腔热情 提交于 2019-12-25 03:32:55
问题 I want to make a function standard ml that takes a list and function and makes a BST out of it. The function's type is: 'a list -> ('a * 'a -> bool) -> 'a tree , but I'm having some problems with it, here are the code I wrote: datatype 'data tree = EMPTY | NODE of 'data tree * 'data * "data tree; fun makeBST [] f = EMPTY | makeBST (x::xs) f = let fun insert EMPTY x = NODE(EMPTY, x, EMPTY) | insert (NODE(left, root, right)) x = if f(x, root) then insert left x else insert right x in makeBST xs

How does outermost evaluation work on an application of a curried function?

只愿长相守 提交于 2019-12-24 08:58:34
问题 mult is defined as a curried function: mult :: Int -> Int -> Int mult x = \y -> x * y In mult (1+2) (2+3) , what are the redex's. and are they mult(1+2) , 1+2 and 2+3 ? What is the outermost redex, and is it 2+3 ? Innermost evaluation works on the expression as following, according to Programming in Haskell by Hutton: mult (1+2) (2+3) = { applying the first + } mult 3 (2+3) = { applying mult } (\y -> 3 * y) (2+3) = { applying + } (\y -> 3 * y) 5 = { applying the lambda } 3 * 5 = { applying *

How curry function should really work?

这一生的挚爱 提交于 2019-12-24 08:55:20
问题 I have function that look like this: function curry(fn) { var args = [].slice.call(arguments, 1); return function() { return fn.call(this, args.concat([].slice.call(arguments))); }; } I always thought that's how the function should look like and should work as: function add(a, b, c, d) { return a+b+c+d; } curry(add, 1, 2)(3, 4); but Wikipedia Article says that it can be called as a chain of functions, each with a single argument so the curry should look like this: function curry(fn) { var

Example of deep understanding of currying

北战南征 提交于 2019-12-24 08:17:21
问题 Reading https://wiki.haskell.org/Currying it states : Much of the time, currying can be ignored by the new programmer. The major advantage of considering all functions as curried is theoretical: formal proofs are easier when all functions are treated uniformly (one argument in, one result out). Having said that, there are Haskell idioms and techniques for which you need to understand currying. What is a Haskell technique/idiom that a deeper understanding of currying is required ? 回答1: Partial

Scala, Currying on multi parameter-group method?

▼魔方 西西 提交于 2019-12-24 04:30:24
问题 I am wondering if it is possible to use currying on multi parameter-group functions : scala> def sum(a: Int)(b: Int): Int = { a+b } sum: (a: Int)(b: Int)Int scala> sum(3)(4) res2: Int = 7 scala> val partFunc = sum(3) _ partFunc: Int => Int = <function1> scala> partFunc(4) res3: Int = 7 scala> val partFunc2 = sum _ _ <console>:1: error: ';' expected but '_' found. val partFunc2 = sum _ _ ^ scala> val partFunc2 = (sum _) _ <console>:8: error: _ must follow method; cannot follow Int => (Int =>

zip function requires also a second list, how can it work with only one argument list

六眼飞鱼酱① 提交于 2019-12-24 02:13:45
问题 I started learning Haskell and found a nice exercise. It's the following: grouping: Int -> [Student]->[(Team, Student)] grouping teamNumber = zip ys where ... So, the exercise wants that i try to fill the rest. The function should do the following: Example : grouping 2 ['Mark','Hanna','Robert','Mike','Jimmy'] = [(1,'Mark'),(2,'Hanna'),(1,'Robert'),(2,'Mike'),(1,'Jimmy')] . So, we are building teams which consists of two Students, and the last Student 'Jimmy' has no teammates. Then, I also

Why doesn't functools.partial return a real function (and how to create one that does)?

*爱你&永不变心* 提交于 2019-12-23 07:28:39
问题 So I was playing around with currying functions in Python and one of the things that I noticed was that functools.partial returns a partial object rather than an actual function. One of the things that annoyed me about this was that if I did something along the lines of: five = partial(len, 'hello') five('something') then we get TypeError: len() takes exactly 1 argument (2 given) but what I want to happen is TypeError: five() takes no arguments (1 given) Is there a clean way to make it work

js currying function example

戏子无情 提交于 2019-12-23 04:21:24
问题 How to understand the currying function? How the newSum and newFind works? var currying = function(fn) { var args = []; return function() { if (!!arguments.length) { [].push.apply(args, arguments); // What's the meaning of this writing? return arguments.callee; } else { return fn.apply(this, args); } } } // accumulation currying var sum = (function(num){ var ret = 0; return function(){ for(var i = 0, len = arguments.length; i < len; i++) { ret += arguments[i]; } return ret; } })(); var newSum