Skip to content

Instantly share code, notes, and snippets.

View RSNara's full-sized avatar

Ramanpreet Nara RSNara

  • Facebook
View GitHub Profile
@RSNara
RSNara / functional.js
Last active October 4, 2015 07:43
Just a short implementation of curry and compose.
var _ = (function() {
var _ = {
curry: curry,
compose: compose
};
return _;
function realArgumentCount(unreal, args) {
@RSNara
RSNara / run.js
Last active October 14, 2015 03:34
Use generators with promises for better error handling. :)
/*
Let `a` be a Promise. To make the generator wait for a to resolve,
simply `yield a`. By default, all rejected Promises throw an error
in our generator. Yield an iteratable to get an array of resolved
values.
*/
function isIterable(object) {
return Symbol.iterator in Object(object);
}
@RSNara
RSNara / decorators.js
Last active July 7, 2019 00:24
An example of using decorators in ES5.
function memoize(object, name, descriptor) {
var fn = descriptor.value;
var memoized = function() {
memoized.cache = memoized.cache || {};
var key = JSON.stringify(arguments);
return memoized.cache[key] = memoized.cache[key]
? memoized.cache[key]
: fn.apply(this, arguments);
}
descriptor.value = memoized;
export const _ = Symbol();
export function curry(fn) {
return function func(...args) {
if (countRealArguments(_, args) < fn.length) {
return function inner(...innerArgs) {
return this::func(...merge(_, args, innerArgs))
}
} else {
return this::fn(...args);
@RSNara
RSNara / option-curry.js
Created October 10, 2015 00:58
Currying but with an options object!
var curry = (function() {
return curry;
function isObject(thing) {
return Object(thing) === thing;
}
function isEmpty(object) {
return isObject(object) && ! Object.keys(object).length;
@RSNara
RSNara / fibonacci.js
Last active December 31, 2015 00:18
Tail-recursive Fibonacci!
function fibonacci(n) {
return (function loop(i, f0, f1) {
return i === 0 ? f0 : loop(i - 1, f1, f0 + f1);
})(n, 0, 1);
}
@RSNara
RSNara / promise-tree.js
Last active December 30, 2015 06:04
Promise.tree
/**
* A Tree is an Object that may contain other Trees, Objects, Promises,
* or primitives. By definition, it contains no cycles.
* @typedef [Object] Tree
*/
/**
* @function tree
* @memberof Promise
* @param [Tree] tree
@RSNara
RSNara / sorta-tail-recursive-quick-sort.js
Last active December 30, 2015 06:46
An flavour of quick-sort I found while reading "The joy of Clojure". In the book, the Clojure implementation was tail-recursive and lazy; this translation has neither of those qualities. It was written to help me understand the Clojure implementation.
console.log(quickSort([2,1,4,3]));
console.log(quickSort(randomInts(20)));
function randomInts(n) {
return Array.apply(null, Array(n)).map(() => Math.floor(Math.random() * n));
}
function quickSort(array) {
return quickSortLists([array]);
}
@RSNara
RSNara / litr-quick-sort.clj
Last active December 31, 2015 04:15
A lazy, incremental, and tail-recursive implementation of quick-sort. Algorithm originally found in "The Joy of Clojure", but re-implemented and uploaded here for future reference.
(defn quick-sort-lists [lists]
(lazy-seq
(loop [[selected & other-lists] lists]
(cond (seq selected)
(let [[pivot & selected-others] selected
smaller-than-pivot? #(< %1 pivot)]
(recur (list* (filter smaller-than-pivot? selected-others)
pivot
(remove smaller-than-pivot? selected-others)
other-lists)))
@RSNara
RSNara / filter.clj
Created December 31, 2015 04:12
A lazy implementation of filter.
(defn my-filter [predicate? collection]
(lazy-seq
(loop [[head & tail] collection]
(if (predicate? head)
(cons head (my-filter predicate? tail))
(if (seq tail)
(recur tail)
())))))