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
(defmacro ∑ [m-expr] | |
(loop [e (->(list) (conj (nth m-expr 2)) (conj (nth m-expr 0)) (conj (nth m-expr 1))) | |
op 3 | |
arg 4] | |
(if (< arg (count m-expr)) | |
(recur | |
(->(list)(conj (nth m-expr arg))(conj e)(conj (nth m-expr op))) | |
(+ op 2) | |
(+ arg 2) | |
) |
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
// A function decorator that returns a curryable version of the original function with an arbitrary number of arguments | |
// This is an introspective helper function that allows us get the parameter names of our original function and return it as an array | |
function getParamNames(func) { | |
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | |
var ARGUMENT_NAMES = /([^\s,]+)/g; | |
var fnStr = func.toString().replace(STRIP_COMMENTS, ''); | |
var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES); | |
if (result === null) | |
result = []; |
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
// a JavaScript implementation of Clojure MultiMethods. Depends on Lodash but you could easily write the helpers yourself | |
function MultiMethod(dispatcherMethod) { | |
function fn() { | |
var args = _.toArray(arguments); | |
var coll = _.head(args); | |
var additions = _.tail(args); | |
var dispatchingValue = fn.dispatcher(coll); | |
var method = fn._methods[dispatchingValue]; | |
return method(coll, additions); |
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
var nums = [1, 2, 3, 4, 5]; | |
function sumList(numList) { | |
return numList.reduce(function(memo, num) { | |
return memo + num | |
}, 0) | |
} | |
function double(num) { | |
return num * 2 |
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
var nums = [1, 2, 3, 4, 5]; | |
function sum(numList) { | |
return numList.reduce(function(memo, num) { | |
return memo + num | |
}, 0) | |
} | |
function double(num) { | |
return num * 2 |
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
// Second version of partializer that takes a simplified and more flexible implementation of making a function curryable. | |
// The original function created a new function from a string that made every argument a curry. That version forced a curry for every argument | |
// This function allows you to pass in any number of arguments at a time and as soon as all the arguments have been passed it will call the function | |
// and return the result. | |
// Rather than attempting to explicitly nest functions, this implementation is only concerned with whether all the arguments have been met or not | |
// If not, then it returns a function with the arguments currenty supplied stored for later use. | |
// You can include or exclude as many arguments as you want untill all of them have been fulfilled. |
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
; This function decorator returns an auto currying version of the function passed to it | |
; a helper function that looks at a functions metadata to get the number of arguments required | |
(defn arities | |
[v] | |
(-> v meta :arglists first count)) | |
; curry takes a function quote (ex. #'map) and any initial arguments you may want to pass to it (or none) and if all the arguments aren't | |
; fulfilled it returns a curry. You can continue to call each curry and it will return another one until all args are fulfilled | |
(defn curry |