Skip to content

Instantly share code, notes, and snippets.

@drewolson

drewolson/curry3.js

Last active Dec 18, 2015
Embed
What would you like to do?
Functional Javascript curry3 example
// The code below refers to examples found on page 120 of the PDF
// required functions
var _ = require("underscore")._;
var plays = [{artist: "Burial", track: "Archangel"},
{artist: "Ben Frost", track: "Stomp"},
{artist: "Ben Frost", track: "Stomp"},
{artist: "Burial", track: "Archangel"},
{artist: "Emeralds", track: "Snores"},
{artist: "Burial", track: "Archangel"}];
function songToString(song) {
return [song.artist, song.track].join(" - ");
}
function curry3(fun) {
return function(last) {
return function(middle) {
return function(first) {
return fun(first, middle, last);
};
};
};
};
// songsPlayed using curry3
var songsPlayed = curry3(_.uniq)(false)(songToString);
console.log(songsPlayed(plays));
// output:
// [{artist: 'Burial', track: 'Archangel'},
// {artist: 'Ben Frost', track: 'Stomp'},
// {artist: 'Emeralds', track: 'Snores'}]
// You then compare the call to curry3 with the call to _.uniq with the following lines:
_.uniq(plays, false, songToString);
curry3(_.uniq) (false) (songToString);
// The argument ordering above doesn't make sense to me. Shouldn't false be the last argument to _.uniq
// for the comparison to hold true?
// Aren't the following lines equivalent?
_.uniq(plays, songToString, false);
curry3(_.uniq)(false)(songToString)(plays);
// Interestingly, _.uniq appears to allow the boolean and interator arguments to be switched
// positionally, so both of these work:
_.uniq(plays, songToString, false);
_.uniq(plays, false, songToString);
// Perhaps this allowed the code samples in the book to run correctly even though the comparison of the two styles (_.uniq and curry3) seems incorrect.
@asdfzt

This comment has been minimized.

Copy link

@asdfzt asdfzt commented Jul 5, 2015

Thanks. Glad to know I'm not going crazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment