Skip to content

Instantly share code, notes, and snippets.

View Bclayson's full-sized avatar

Bobby Clayson Bclayson

View GitHub Profile
@Bclayson
Bclayson / curry.clj
Last active April 23, 2016 18:17
Make your Clojure functions auto-cury
; 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
@Bclayson
Bclayson / partializer2.js
Last active April 23, 2016 07:19
Simpler and more flexible implementation of previous partialize function. Requires Underscore
// 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.
@Bclayson
Bclayson / compose-left.js
Created April 7, 2016 21:52
Compose function decorator that takes data and subsequent functions and returns a function that composes callbacks right to left. Uses "pipe" function from pipe.js (my gist) and Underscore.js
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
@Bclayson
Bclayson / pipe.js
Created April 7, 2016 21:30
A piping function based on Clojure's Thread First Macro. Pipe functions takes data as first argument and subsequent functions to "pipe" the data through. Pipes left to right and returns final result. Uses Underscore library or Lodash alternatively.
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
// 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);
// 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 = [];
(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)
)