Created
August 19, 2014 23:29
-
-
Save webbower/d20c7f639de1776597cb to your computer and use it in GitHub Desktop.
Functional list utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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