Translating a fixed-point operator to Haskell language - javascript

I am trying to translate this JS fixed-point operator into Haskell.
JS:
const fix = F => {
const D = X => F(
t => X(X)(t)
)
return D(D)
};
My attempt is (Haskell):
fix' f = d d
where
d x = f (\t -> x x t)
However, I get the following error:
Couldn't match expected type ‘(t2 -> t3) -> t4’
with actual type ‘p’
because type variables ‘t2’, ‘t3’, ‘t4’ would escape their scope
These (rigid, skolem) type variables are bound by the inferred type of d :: (t1 -> t2 -> t3) -> t4
Does someone know what's happening here ?

In the self-application d d, d is both a function, of some type a -> r, and its argument, of type a. Thus the two types must be one and the same, a ~ (a -> r).
Haskell wants to know its types in full upfront so it keeps substituting one for another ending up with an infinite type.
Infinite types aren't allowed in Haskell, but recursive types are allowed.
All we need to do here is to name that recursive type:
newtype T r = D { app :: T r -> r }
Now T r is both a type of a function and its argument, for some result type r.
Here T is a type constructor, and D its data constructor, D :: (T r -> r) -> T r.
The above record syntax defines a new data type (here though with the keyword newtype, not data) and names its single field as app. It also defines app as an accessor function, app :: T r -> (T r -> r). (It's kind of an inverse of D, and oftentimes one sees such functions named with the prefix of "un", like app could have been named unD. But here app makes sense, as we will see later.)
For a value x of type T r, x :: T r, this means that x is / matches up with / some value D g where (g = app x) :: T r -> r, i.e. app simply unwraps the data constructor D to get to the underlying value (a function) g: x = D g ; app x = app (D g) = g. That's how the record syntax works in Haskell.
Now we can write
{- fix' f = d d
where
d x = f (\t -> x x t) -- applying x to x can't be typed!
-}
fix1 :: ((t1 -> t) -> t1 -> t) -> t1 -> t
fix1 f = d (D d)
where
d x = f (\t -> app x x t) -- `app`ing x to x is well-typed!
fix2 :: ((t1 -> t) -> t1 -> t) -> t1 -> t
fix2 f = d (D d)
where
d (D y) = f (\t -> y (D y) t)
fix3 :: ((t1 -> t) -> t1 -> t) -> t1 -> t
fix3 f = f (\t -> d (D d) t)
where
d (D y) = f (\t -> y (D y) t)
fix4 :: (t -> t) -> t
fix4 f = f (d (D d))
where
d (D y) = f (y (D y))
all work. The last one even has the same type as the built-in fix.
But Haskell doesn't only have recursive types. It also has recursion itself. An entity is allowed to refer to itself in its own definition.
Thus as the comments say we don't really need to emulate recursion by self-application of a value passed as an argument. We can just use recursively the function being defined, itself:
fix0 :: (t -> t) -> t
fix0 f = f (fix0 f)
Or we can use a recursively defined value:
y :: (t -> t) -> t
y f = x where { x = f x }
Regarding the error, the second type error you get,
prog.hs:3:22: error:
• Occurs check: cannot construct the infinite type:
t1 ~ t1 -> t2 -> t3
• In the first argument of ‘x’, namely ‘x’
In the expression: x x t
In the first argument of ‘f’, namely ‘(\ t -> x x t)’
• Relevant bindings include
t :: t2 (bound at prog.hs:3:15)
x :: t1 -> t2 -> t3 (bound at prog.hs:3:7)
d :: (t1 -> t2 -> t3) -> t4 (bound at prog.hs:3:5)
|
3 | d x = f (\t -> x x t)
| ^
seems more to-the-point / helpful / than the one you included.

Related

how to demonstrate functor's MAP method associative property

I read that a functor's map is assocative and that it implies the following :
Functor.map(f).map(g) == Functor.map(x => g(f(x))
Shouldn't the above equation be written like this to demonstrate associativity?
Functor.map(f).map(g) == Functor.map(f.map(g))
(but it seams impossible because funcitons dont have a map method).
There is something that I am not grasping here.
This is an example in Haskell.
You can create two functions.
The first composeAndMap which will first compose f and g and then apply the map function.
composeAndMap :: Functor f => (b -> c) -> (a -> b) -> f a -> f c
composeAndMap f g = fmap (f . g)
The second will be called mapAndCompose which will compose two partial application functions using the map function. The first map will use the f and the second one will use the g like this:
mapAndCompose :: Functor f => (b -> c) -> (a -> b) -> f a -> f c
mapAndCompose f g = fmap f . fmap g
Eventually you can compare between the results of each function using a specific functor instance, the most obvious is the list functor.
list :: [Int]
list = [1..5]
main :: IO ()
main = print $ mapAndCompose (+1) (+2) list == composeAndMap (+1) (+2) list
You should not forget the identity law also.
identity :: Functor f => f a -> f a
identity = fmap id
composeAndMap :: Functor f => (b -> c) -> (a -> b) -> f a -> f c
composeAndMap f g = fmap (f . g)
mapAndCompose :: Functor f => (b -> c) -> (a -> b) -> f a -> f c
mapAndCompose f g = fmap f . fmap g
list :: [Int]
list = [1..5]
main :: IO ()
main = do
print $ identity list == list
print $ mapAndCompose (+1) (+2) list == composeAndMap (+1) (+2) list
This is an example in JavaScript:
Create two functions plusOne and plusTwo
function plusOne(val) {
return val + 1;
}
function plusTwo(val) {
return val + 2;
}
Create a compose function which composes two functions
function compose(f, g) {
return (val) => f(g(val));
}
Create a utility function sequenceEquals
function sequenceEquals(arr1, arr2) {
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}
Now you can compare between the two implementations:
let list = [1,2,3,4,5];
let composed = compose(plusOne, plusTwo);
let composeAndMap = list.map(composed);
let mappedTwice = list.map(plusTwo).map(plusOne);
console.log(sequenceEquals(composeAndMap, mappedTwice));
And the identity law.
Create an identity function:
function identity(value) { return value; }
And compare the results:
console.log(sequenceEquals(list.map(identity), list));

How to implement f(g) == g(f)

I answered a question yesterday and it got my mind thinking about an interesting (to me) puzzle
With the restriction of using lambdas, numbers, and + only (no if, ?:, or other language features), the goal is to implement some f and some g such that
// contract
f(x) => f'
g(y) => g'
f'(g') == g'(f')
// or more simply:
m(n) == n(m)
Here's what I came up with so far - this code is in JavaScript for the purpose of being able to demonstrate the code in the browser but answers in any functional language are acceptable (racket, clojure, ocaml, lambda calc, etc)
// f
const f = x => k =>
k(y => y + x)
// g
const g = y => k =>
k(x => x + y)
// make instance of each
const a = f(1)
const b = g(2)
console.log(a(b))
// x => x + y1
// should be 3
console.log(b(a))
// y => y + x2
// should be 3
I was able to fix one half of relationship, but the other side remains broken due f and g now being asymmetrical
// f
const f = x => k =>
k(y => y(x))
// g
const g = y => k =>
k(x => x + y)
// make instance of each
const a = f(1)
const b = g(2)
console.log(a(b))
// 3
// should be 3 (OK)
console.log(b(a))
// y => y + x2
// should be 3
I know why it's not working but I'm having trouble trying to fix it. Most importantly, if it's not possible, I'd be interested in learning why.
If you come up with a solution that breaks the restrictions, I'm still interested in seeing it ^_^
This answer assumes a strong non-unit type system (eg. Haskell, but I try to stick to JS-like syntax here).
If we stay in the realm of parametricity, we don't need (and cannot even use) numbers or conditions. The constant functions don't change anything, so I'll leave them out and deal with f and g directly.
First, observe that the equation
f(g) == g(f)
implies that both f and g have function types. Assuming both have different inputs we get that f: A -> X and g: B -> X == (A -> X) -> X == ((B -> X) -> X) -> X == ..., ie., you get an infinite type. I remember having read a paper about this exact construction (one can represent it as a pair of types, and I think it forms a category), but unfortunately forgot its name -- maybe there's more to say here.
A simpler solution would be to require A == B. Then f, g: A -> X, but since X == A by the symmetry equation, it follows that f, g: A -> A -- for arbitrary A, that is. One possibilitity fulfilling this is the identity function:
id(id) == id(id)
The other solutions arise when we specialize A to A -> A; then we search for functions of type (A -> A) -> (A -> A). These are, for one, the (specialized) identity function, which has already been found, but also all functions of the shape h => h o ... o h -- compositions ((o) = h => x => h(h(x))) of a function for a number of types. These "add their repetitions" on application, e.g.
(h => h o h)(h => h o h) == (h => h o h) o (h => h o h)
== h => h o h o h o h.
From this we see that we can choose
f == g == h => h,
f == g == h => h o h,
f == g == h => h o h o h,
f == g == ...
which are, I think, all functions of type forall A. (A -> A) -> (A -> A) (excluding nontermination).
There also seems to be a relation of the limit of this construction (infinite composition) to the infinite case mentined above (now in real Haskell):
Prelude> let g = \h -> h . g
<interactive>:11:19:
Occurs check: cannot construct the infinite type: b ~ (b -> c) -> c
This is the closest I've been able to get but it does use a ternary (?:) expression
const f = x => g =>
g === undefined ? x : g() + x
const g = y => f =>
f === undefined ? y : f() + y
const a = f(1)
const b = g(2)
console.log(a(b)) // 3
console.log(b(a)) // 3
Here f is completely equivalent to g and we could easily just use one or the other
const f = x => g =>
g === undefined ? x : g() + x
const a = f(1)
const b = f(2)
console.log(a(b)) // 3
console.log(b(a)) // 3
For this to happen obviously both f and g has to accept a function as an argument and return a same type value. However after tinkering a while it quickly strike me that i am dealing with infinite types.
Well unless your functions are Haskell's id like JS's x => x. So in Haskell i would do;
f :: a -> a
g :: a -> a
f = id
g = id
*Main> f(g)(+3) 2
5
*Main> g(f)(+3) 2
5
Can simply be implemented in JS as well.

Is there a way to write this Javascript function without listing the arguments?

I'm trying to write a function that compares two items using another function, then checks if the result is greater than some other value also provided to the function. I can write it like this:
const compareDifference = function(t1, t2, threshold) {
return getDifference(t1, t2) > threshold;
};
... but this doesn't seem very functional. Every example I find for classical composition assumes that I'll know the value to be compared against before the function is called, in which case I could write it functionally like so:
const greaterThan = (x, y) => x > y;
const greaterThan10 = _.partial(greaterThan, _, 10);
const compareDifference = _.compose(greaterThan10, getDifference);
Since I'm relatively new to functional programming, I feel like I'm missing something easy or fundamental here. Is there a way to write the function so that it incorporates the parameter to be passed to greaterThan without me having to mention it explicitly? Ideally it would be something like:
const compareDifference = _.compose(_.partial(greaterThan, _), getDifference);
I think LUH3417's answer is great for beginners. It touches on some basics but I think there's room for some other info
First, if you wanted the exact same API in your original question, you could break it down into parts like this.
const comp = f=> g=> x=> f (g (x))
const comp2 = comp (comp) (comp)
const flip = f=> x=> y=> f (y) (x)
const sub = x=> y=> y - x
const abs = x=> Math.abs
const diff = comp2 (Math.abs) (sub)
const gt = x=> y=> y > x
// your function ...
// compose greaterThan with difference
// compareDifference :: Number -> Number -> Number -> bool
const compareDifference = comp2 (flip(gt)) (diff)
console.log(compareDifference (3) (1) (10))
// = gt (10) (abs (sub (1) (3)))
// = Math.abs(1 - 3) > 10
// => false
console.log(compareDifference (5) (17) (10))
// = gt (10) (abs (sub (5) (17)))
// = Math.abs(17 - 5) > 10
// => true
But, you are right to have suspicion that your original code doesn't feel that functional. The code I gave you here works, but it still feels... off, right ? I think something that would greatly improve your function is if you made it a higher-order function, that is, a function that accepts a function as an argument (and/or returns a function).
The Yellow Brick Road
We could then make a generic function called testDifference that takes a threshold function t as input and 2 numbers to base the threshold computation on
// testDifference :: (Number -> bool) -> Number -> Number -> bool
const testDifference = t=> comp2 (t) (diff)
Looking at the implementation, it makes sense. To test the difference, we need a test (some function) and we need two numbers that compute a difference.
Here's an example using it
testDifference (gt(10)) (1) (3)
// = gt (10) (abs (sub (1) (3)))
// = Math.abs(1 - 3) > 10
// = Math.abs(-2) > 10
// = 2 > 10
// => false
This is a big improvement because > (or gt) is no longer hard-coded in your function. That makes it a lot more versatile. See, we can just as easily use it with lt
const lt = x=> y=> y < x
testDifference (lt(4)) (6) (5)
// = lt (4) (abs (sub (6) (5)))
// = Math.abs(5 - 6) < 4
// = Math.abs(-1) < 4
// = 1 < 4
// => true
Or let's define a really strict threshold that enforces the numbers have a exact difference of 1
const eq = x=> y=> y === x
const mustBeOne = eq(1)
testDifference (mustBeOne) (6) (5)
// = eq (1) (abs (sub (6) (5)))
// = Math.abs(5 - 6) === 1
// = Math.abs(-1) === 1
// = 1 === 1
// => true
testDifference (mustBeOne) (5) (8)
// = eq (1) (abs (sub (5) (8)))
// = Math.abs(8 - 5) === 1
// = Math.abs(3) === 1
// = 3 === 1
// => false
Because testDifference is curried, you can also use it as a partially applied function too
// return true if two numbers are almost the same
let almostSame = testDifference (lt(0.01))
almostSame (5.04) (5.06)
// = lt (0.01) (abs (sub (5.04) (5.06)))
// = Math.abs(5.06 - 5.04) < 0.01
// = Math.abs(0.02) < 0.01
// = 0.02 < 0.01
// => false
almostSame (3.141) (3.14)
// = lt (0.01) (abs (sub (3.141) (3.14)))
// = Math.abs(3.14 - 3.141) < 0.01
// = Math.abs(-0.001) < 0.01
// = 0.001 < 0.01
// => true
All together now
Here's a code snippet with testDifference implemented that you can run in your browser to see it work
// comp :: (b -> c) -> (a -> b) -> (a -> c)
const comp = f=> g=> x=> f (g (x))
// comp2 :: (c -> d) -> (a -> b -> c) -> (a -> b -> d)
const comp2 = comp (comp) (comp)
// sub :: Number -> Number -> Number
const sub = x=> y=> y - x
// abs :: Number -> Number
const abs = x=> Math.abs
// diff :: Number -> Number -> Number
const diff = comp2 (Math.abs) (sub)
// gt :: Number -> Number -> bool
const gt = x=> y=> y > x
// lt :: Number -> Number -> bool
const lt = x=> y=> y < x
// eq :: a -> a -> bool
const eq = x=> y=> y === x
// (Number -> bool) -> Number -> Number -> bool
const testDifference = f=> comp2 (f) (diff)
console.log('testDifference gt', testDifference (gt(10)) (1) (3))
console.log('testDifference lt', testDifference (lt(4)) (6) (5))
console.log('testDifference eq', testDifference (eq(1)) (6) (5))
// almostSame :: Number -> Number -> bool
let almostSame = testDifference (lt(0.01))
console.log('almostSame', almostSame (5.04) (5.06))
console.log('almostSame', almostSame (3.141) (3.14))
If I'm barking up the wrong tree with this, then tell me and I'll edit, but if I wanted to do something like this that was 'more functional' I would do the following:
let greaterThan = _.curry((x, y) => y > x); // notice the args are flipped
let difference = _.curry((x, y) => Math.abs(x - y));
let greaterThan5 = greaterThan(5); // this naming is why we ordered the args backwards
let differenceBetweenGreaterThan5 = _.compose(greaterThan5, difference);
differenceBetweenGreaterThan5(10, 34); // true
differenceBetweenGreaterThan5(10, 6); // false
We could then rewrite your original function like so:
let compareDiff = (threshold, x, y) => {
return _.compose(greaterThan(threshold), difference)(x)(y);
};
Although I'd probably just use something like differenceBetweenGreaterThan5
Also I apologize for the comically long variable names, but I wanted it to be quite clear what I was naming. There's a few other things to note: I reordered the arguments to greaterThan to make the naming of the partial application more sensible and avoid the need for the _ placeholder. Although I curried difference and consider it good for something that generic, its not strictly necessary for this example.
As for what I think you were missing, the functional approach in this case (per my understanding of what 'functional approach' means) is that we break the complicated case of getting the difference between two numbers and seeing if a third falls in that range, broken it down into its atomic constituents, and structured it as a composition of those atomic elements like greaterThan and difference.
Its the breaking and rebuilding that's difficult: doing so cleanly requires reordering the arguments for flexibility, convenience, and clarity (even relative to the English version I spelt out in the paragraph above as I give the 'third' number first). The argument and piece reordering part seems to me to be what you were missing.

display all possible paths between the 2 nodes

I am learning javascript and data structures at the same time and struggling with my output for a graph. Can anyone help me fix my code to output all available paths between start and end node?
If I enter F to C I get the following:
F -> B C :3
F -> A D G :4
F -> A D E :3
F -> C B E F G :7
F -> C D :3
F -> D :1
F -> B D :3
F to C
F D B
F D C
F D
F D E
F
F D G
I'd like the first output to show all available paths from F to C. All its doing now is showing all available paths. The second output is showing all available paths but not correctly.
I'm trying to make the first output show the following when I enter F and C:
F -> D C: 2
F -> D E C: 4
F -> D B A C: 5
F -> D G B A C: 7
I've looked at these:
https://en.wikipedia.org/wiki/Depth-first_search
All the paths between 2 nodes in graph
http://www.quora.com/What-are-good-ways-to-find-all-the-possible-paths-between-two-nodes-in-a-directed-graph
Graph Algorithm To Find All Connections Between Two Arbitrary Vertices
https://mathematica.stackexchange.com/questions/25779/finding-all-simple-paths-between-two-vertices-in-a-graph
https://mathematica.stackexchange.com/questions/4128/finding-all-shortest-paths-between-two-vertices
and many others and I am still stuck.
My code is here:
https://jsfiddle.net/jtlindsey/584hh0vf/3/
My two outputs so far start at line number 116 and 209 in the code.

How to correctly curry a function in JavaScript?

I wrote a simple curry function in JavaScript which works correctly for most cases:
const curry = (f, ...a) => a.length < f.length
? (...b) => curry(f, ...a, ...b)
: f(...a);
const add = curry((a, b, c) => a + b + c);
const add2 = add(2);
const add5 = add2(3);
console.log(add5(5));
However, it doesn't work for the following case:
// length :: [a] -> Number
const length = a => a.length;
// filter :: (a -> Bool) -> [a] -> [a]
const filter = curry((f, a) => a.filter(f));
// compose :: (b -> c) -> (a -> b) -> a -> c
const compose = curry((f, g, x) => f(g(x)));
// countWhere :: (a -> Bool) -> [a] -> Number
const countWhere = compose(compose(length), filter);
According to the following question countWhere is defined as (length .) . filter:
What does (f .) . g mean in Haskell?
Hence I should be able to use countWhere as follows:
const odd = n => n % 2 === 1;
countWhere(odd, [1,2,3,4,5]);
However, instead of returning 3 (the length of the array [1,3,5]), it returns a function. What am I doing wrong?
#Aadit,
I'm posting this because you shared a comment on my answer to To “combine” functions in javascript in a functional way? I didn't specifically cover currying in that post because it's a very contentious topic and not really a can of worms I wanted to open there.
I'd be wary using the phrasing "how to correctly curry" when you seem to be adding your own sugar and conveniences into your implementation.
Anyway, all of that aside, I truly don't intend for this to be an argumentative/combative post. I'd like to be able to have an open, friendly discussion about currying in JavaScript while emphasizing some of the differences between our approaches.
Without further ado...
To clarify:
Given f is a function and f.length is n. Let curry(f) be g. We call g with m arguments. What should happen? You say:
If m === 0 then just return g.
If m < n then partially apply f to the m new arguments, and return a new curried function which accepts the remaining n - m arguments.
If m === n then apply f to the m arguments. If the result is a function then curry the result. Finally, return the result.
If m > n then apply f to the first n arguments. If the result is a function then curry the result. Finally, apply the result to the remaining m - n arguments and return the new result.
Let's see a code example of what #Aadit M Shah's code actually does
var add = curry(function(x, y) {
return function(a, b) {
return x + y + a + b;
}
});
var z = add(1, 2, 3);
console.log(z(4)); // 10
There are two things happening here:
You're attempting to support calling curried functions with variadic arguments.
You're automatically currying returned functions
I don't believe there's a lot of room for debate here, but people seem to miss what currying actually is
via: Wikipedia
In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument...
I'm bolding that last bit, because it's so important; each function in the sequence only takes a single argument; not variadic (0, 1, or more) arguments like you suggest.
You mention haskell in your post, too, so I assume you know that Haskell has no such thing as functions that take more than one argument. (Note: a function that takes a tuple is still just a function that takes one argument, a single tuple). The reasons for this are profound and afford you a flexibility in expressiveness not afforded to you by functions with variadic arguments.
So let's re-ask that original question: What should happen?
Well, it's simple when each function only accepts 1 argument. At any time, if more than 1 argument is given, they're just dropped.
function id(x) {
return x;
}
What happens when we call id(1,2,3,4)? Of course we only get the 1 back and 2,3,4 are completely disregarded. This is:
how JavaScript works
how Wikipedia says currying should work
how we should implement our own curry solution
Before we go further, I'm going to use ES6-style arrow functions but I will also include the ES5 equivalent at the bottom of this post. (Probably later tonight.)
another currying technique
In this approach, we write a curry function that continuously returns single-parameter functions until all arguments have been specified
As a result of this implementation we have 6 multi-purpose functions.
// no nonsense curry
const curry = f => {
const aux = (n, xs) =>
n === 0 ? f (...xs) : x => aux (n - 1, [...xs, x])
return aux (f.length, [])
}
// demo
let sum3 = curry(function(x,y,z) {
return x + y + z;
});
console.log (sum3 (3) (5) (-1)); // 7
OK, so we've seen a curry technique that is implemented using a simple auxiliary loop. It has no dependencies and a declarative definition that is under 5 lines of code. It allows functions to be partially applied, 1 argument at a time, just as a curried function is supposed to work.
No magic, no unforeseen auto-currying, no other unforeseen consequences.
But what really is the point of currying anyway?
Well, as it turns out, I don't really curry functions that I write. As you can see below, I generally define all of my reusable functions in curried form. So really, you only need curry when you want to interface with some functions that you don't have control over, perhaps coming from a lib or something; some of which might have variadic interfaces!
I present curryN
// the more versatile, curryN
const curryN = n => f => {
const aux = (n, xs) =>
n === 0 ? f (...xs) : x => aux (n - 1, [...xs, x])
return aux (n, [])
};
// curry derived from curryN
const curry = f => curryN (f.length) (f);
// some caveman function
let sumN = function() {
return [].slice.call(arguments).reduce(function(a, b) {
return a + b;
});
};
// curry a fixed number of arguments
let g = curryN (5) (sumN);
console.log (g (1) (2) (3) (4) (5)); // 15
To curry or not to curry? That is the question
We'll write some examples where our functions are all in curried form. Functions will be kept extremely simple. Each with 1 parameter, and each with a single return expression.
// composing two functions
const comp = f => g => x => f (g (x))
const mod = y => x => x % y
const eq = y => x => x === y
const odd = comp (eq (1)) (mod (2))
console.log (odd(1)) // true
console.log (odd(2)) // false
Your countWhere function
// comp :: (b -> c) -> (a -> b) -> (a -> c)
const comp = f => g => x =>
f(g(x))
// mod :: Int -> Int -> Int
const mod = x => y =>
y % x
// type Comparable = Number | String
// eq :: Comparable -> Comparable -> Boolean
const eq = x => y =>
y === x
// odd :: Int -> Boolean
const odd =
comp (eq(1)) (mod(2))
// reduce :: (b -> a -> b) -> b -> ([a]) -> b
const reduce = f => y => ([x,...xs]) =>
x === undefined ? y : reduce (f) (f(y)(x)) (xs)
// filter :: (a -> Boolean) -> [a] -> [a]
const filter = f =>
reduce (acc => x => f (x) ? [...acc,x] : acc) ([])
// length :: [a] -> Int
const length = x =>
x.length
// countWhere :: (a -> Boolean) -> [a] -> Int
const countWhere = f =>
comp (length) (filter(f));
console.log (countWhere (odd) ([1,2,3,4,5]))
// 3
Remarks
So to curry or not to curry?
// to curry
const add3 = curry((a, b, c) =>
a + b + c
)
// not to curry
const add3 = a => b => c =>
a + b + c
With ES6 arrow functions being the go-to choice for today's JavaScripter, I think the choice to manually curry your functions is a no-brainer. It's actually shorter and has less overhead to just write it out in curried form.
That said, you're still going to be interfacing with libs that do not offer curried forms of the functions they expose. For this situation, I'd recommend
curry and curryN (defined above)
partial (as defined here)
#Iven,
Your curryN implementation is very nice. This section exists solely for you.
const U = f=> f (f)
const Y = U (h=> f=> f(x=> h (h) (f) (x)))
const curryN = Y (h=> xs=> n=> f=>
n === 0 ? f(...xs) : x=> h ([...xs, x]) (n-1) (f)
) ([])
const curry = f=> curryN (f.length) (f)
const add3 = curry ((x,y,z)=> x + y + z)
console .log (add3 (3) (6) (9))
The problem with your curry function (and for most curry functions that people write in JavaScript) is that it doesn't handle extra arguments correctly.
What curry does
Suppose f is a function and f.length is n. Let curry(f) be g. We call g with m arguments. What should happen?
If m === 0 then just return g.
If m < n then partially apply f to the m new arguments, and return a new curried function which accepts the remaining n - m arguments.
Otherwise apply f to the m arguments and return the result.
This is what most curry functions do, and this is wrong. The first two cases are right, but the third case is wrong. Instead, it should be:
If m === 0 then just return g.
If m < n then partially apply f to the m new arguments, and return a new curried function which accepts the remaining n - m arguments.
If m === n then apply f to the m arguments. If the result is a function then curry the result. Finally, return the result.
If m > n then apply f to the first n arguments. If the result is a function then curry the result. Finally, apply the result to the remaining m - n arguments and return the new result.
The problem with most curry functions
Consider the following code:
const countWhere = compose(compose(length), filter);
countWhere(odd, [1,2,3,4,5]);
If we use the incorrect curry functions, then this is equivalent to:
compose(compose(length), filter, odd, [1,2,3,4,5]);
However, compose only accepts three arguments. The last argument is dropped:
const compose = curry((f, g, x) =>f(g(x)));
Hence, the above expression evaluates to:
compose(length)(filter(odd));
This further evaluates to:
compose(length, filter(odd));
The compose function expects one more argument which is why it returns a function instead of returning 3. To get the correct output you need to write:
countWhere(odd)([1,2,3,4,5]);
This is the reason why most curry functions are wrong.
The solution using the correct curry function
Consider the following code again:
const countWhere = compose(compose(length), filter);
countWhere(odd, [1,2,3,4,5]);
If we use the correct curry function, then this is equivalent to:
compose(compose(length), filter, odd)([1,2,3,4,5]);
Which evaluates to:
compose(length)(filter(odd))([1,2,3,4,5]);
Which further evaluates to (skipping an intermediate step):
compose(length, filter(odd), [1,2,3,4,5]);
Which results in:
length(filter(odd, [1,2,3,4,5]));
Producing the correct result 3.
The implementation of the correct curry function
Implementing the correct curry function in ES6 is straightforward:
const curry = (f, ...a) => {
const n = f.length, m = a.length;
if (n === 0) return m > n ? f(...a) : f;
if (m === n) return autocurry(f(...a));
if (m < n) return (...b) => curry(f, ...a, ...b);
return curry(f(...a.slice(0, n)), ...a.slice(n));
};
const autocurry = (x) => typeof x === "function" ? curry(x) : x;
Note that if the length of the input function is 0 then it's assumed to be curried.
Implications of using the correct curry function
Using the correct curry function allows you to directly translate Haskell code into JavaScript. For example:
const id = curry(a => a);
const flip = curry((f, x, y) => f(y, x));
The id function is useful because it allows you to partially apply a non-curried function easily:
const add = (a, b) => a + b;
const add2 = id(add, 2);
The flip function is useful because it allows you to easily create right sections in JavaScript:
const sub = (a, b) => a - b;
const sub2 = flip(sub, 2); // equivalent to (x - 2)
It also means that you don't need hacks like this extended compose function:
What's a Good Name for this extended `compose` function?
You can simply write:
const project = compose(map, pick);
As mentioned in the question, if you want to compose length and filter then you use the (f .) . g pattern:
What does (f .) . g mean in Haskell?
Another solution is to create higher order compose functions:
const compose2 = compose(compose, compose);
const countWhere = compose2(length, fitler);
This is all possible because of the correct implementation of the curry function.
Extra food for thought
I usually use the following chain function when I want to compose a chain of functions:
const chain = compose((a, x) => {
var length = a.length;
while (length > 0) x = a[--length](x);
return x;
});
This allows you to write code like:
const inc = add(1);
const foo = chain([map(inc), filter(odd), take(5)]);
foo([1,2,3,4,5,6,7,8,9,10]); // [2,4,6]
Which is equivalent to the following Haskell code:
let foo = map (+1) . filter odd . take 5
foo [1,2,3,4,5,6,7,8,9,10]
It also allows you to write code like:
chain([map(inc), filter(odd), take(5)], [1,2,3,4,5,6,7,8,9,10]); // [2,4,6]
Which is equivalent to the following Haskell code:
map (+1) . filter odd . take 5 $ [1,2,3,4,5,6,7,8,9,10]
Hope that helps.
Apart from its mathematical definition
currying is the transformation of a function with n parameters into a sequence of n functions, which each accept a single parameter. The arity is thus transformed from n-ary to n * 1-ary
what impact has currying on programming? Abstraction over arity!
const comp = f => g => x => f(g(x));
const inc = x => x + 1;
const mul = y => x => x * y;
const sqr = x => mul(x)(x);
comp(sqr)(inc)(1); // 4
comp(mul)(inc)(1)(2); // 4
comp expects two functions f and g and a single arbitrary argument x. Consequently g must be an unary function (a function with exactly one formal parameter) and f too, since it is fed with the return value of g. It won't surprise anyone that comp(sqr)(inc)(1) works. sqr and inc are both unary.
But mul is obviously a binary function. How on earth is that going to work? Because currying abstracted the arity of mul. You can now probably imagine what a powerful feature currying is.
In ES2015 we can pre-curry our functions with arrow functions succinctly:
const map = (f, acc = []) => xs => xs.length > 0
? map(f, [...acc, f(xs[0])])(xs.slice(1))
: acc;
map(x => x + 1)([1,2,3]); // [2,3,4]
Nevertheless, we need a programmatic curry function for all functions out of our control. Since we learned that currying primarily means abstraction over arity, our implementation must not depend on Function.length:
const curryN = (n, acc = []) => f => x => n > 1
? curryN(n - 1, [...acc, x])(f)
: f(...acc, x);
const map = (f, xs) => xs.map(x => f(x));
curryN(2)(map)(x => x + 1)([1,2,3]); // [2,3,4]
Passing the arity explicitly to curryN has the nice side effect that we can curry variadic functions as well:
const sum = (...args) => args.reduce((acc, x) => acc + x, 0);
curryN(3)(sum)(1)(2)(3); // 6
One problem remains: Our curry solution can't deal with methods. OK, we can easily redefine methods that we need:
const concat = ys => xs => xs.concat(ys);
const append = x => concat([x]);
concat([4])([1,2,3]); // [1,2,3,4]
append([4])([1,2,3]); // [1,2,3,[4]]
An alternative is to adapt curryN in a manner that it can handle both multi-argument functions and methods:
const curryN = (n, acc = []) => f => x => n > 1
? curryN(n - 1, [...acc, x])(f)
: typeof f === "function"
? f(...acc, x)
: x[f](...acc);
curryN(2)("concat")(4)([1,2,3]); // [1,2,3,4]
I don't know if this is the correct way to curry functions (and methods) in Javascript though. It is rather one possible way.
EDIT:
naomik pointed out that by using a default value the internal API of the curry function is partially exposed. The achieved simplification of the curry function comes thus at the expense of its stability. To avoid API leaking we need a wrapper function. We can utilize the U combinator (similar to naomik's solution with Y):
const U = f => f(f);
const curryN = U(h => acc => n => f => x => n > 1
? h(h)([...acc, x])(n-1)(f)
: f(...acc, x))([]);
Drawback: The implementation is harder to read and has a performance penalty.
//---Currying refers to copying a function but with preset parameters
function multiply(a,b){return a*b};
var productOfSixNFiveSix = multiply.bind(this,6,5);
console.log(productOfSixNFive());
//The same can be done using apply() and call()
var productOfSixNFiveSix = multiply.call(this,6,5);
console.log(productOfSixNFive);
var productOfSixNFiveSix = multiply.apply(this,[6,5]);
console.log(productOfSixNFive);

Categories