Skip to content

Instantly share code, notes, and snippets.

@webbower
Created August 19, 2014 23:29
Show Gist options
  • Save webbower/d20c7f639de1776597cb to your computer and use it in GitHub Desktop.
Save webbower/d20c7f639de1776597cb to your computer and use it in GitHub Desktop.
Functional list utilities
// Requires utils.misc.js for unbind() and testers.js for existy()
var slice = unbind(Array.prototype.slice);
function toList(arr) {
return existy(arr) ? slice(arr) : [];
}
function nth(index, list) {
return list[index];
}
function first(list) {
return nth(0, list);
}
function last(list) {
return nth(list.length - 1, list);
}
function initial(list) {
return slice(list, 0, -1);
}
function rest(list) {
return slice(list, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment