Last active
December 13, 2015 21:38
-
-
Save quaat/4978425 to your computer and use it in GitHub Desktop.
zip for javscript
This file contains hidden or 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
// Examples inspired by http://learnyouahaskell.com/syntax-in-functions | |
var fibobj = { | |
0: function(){return 1}, | |
1: function(){return 1}, | |
undefined: function(n){return fibs(n-1) + fibs(n-2)} | |
} | |
var fibs = (function(o){ | |
return function() { | |
var args = [].slice.call(arguments); | |
var arg = args[0]; | |
return (o[arg] instanceof Function) ? o[arg](args) : o[undefined](args); | |
} | |
})(fibobj); | |
var charNameObj = { | |
'a': function(){return "Albert"}, | |
'b': function(){return "Broseph"}, | |
'c': function(){return "Cecil"}, | |
undefined: function(){throw("Non-exhaustive patterns in function charName")} | |
} | |
var charName = (function(o){ | |
return function(arg) { | |
var args = [].slice.call(arguments); | |
if( args.length != 1) throw("Illegal number of arguments"); | |
if( typeof arg != 'string') throw("Illegal type"); | |
return (o[arg] instanceof Function) ? o[arg](arg) : o[undefined](arg); | |
} | |
})(charNameObj); | |
This file contains hidden or 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
function zip() { | |
var args = [].slice.call(arguments); | |
var shortest = args.length == 0 ? [] : args.reduce(function(a,b) { | |
return a.length < b.length ? a : b; | |
}); | |
return shortest.map( function(_,i) { | |
return args.map(function(array) { | |
return array[i] | |
}) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment