Skip to content

Instantly share code, notes, and snippets.

@TGOlson
TGOlson / func_validations.js
Last active September 26, 2017 16:10
Functional validations
var Validator = require('simple-validate');
var validateTenantArgs = Validator.validateOrThrow({
title: 'somePred',
etc: '...'
});
// current style
function postTenantToSystemTenant(systemTenant, tenantArgs) {
validateTenantArgs(tenantArgs);
@TGOlson
TGOlson / proxy.js
Created March 9, 2015 06:38
Proxy JS
// `proxy` is a function to allow for point-free recursion in JavaScrpipt
// it takes in a function name as a string, and returns a proxy function to the original function
// examples
// explicit proxy - save proxy after declaration
// var length = ifElse(isEmpty, always(0), compose(inc, proxy('length'), tail));
// proxy('length', length);
// use short had proxy chaining
@TGOlson
TGOlson / factorial.js
Last active March 22, 2016 19:30
Point free factorial kata
var R = require('ramda');
// `proxy` is a function to allow for point-free recursion in JavaScrpipt
// it takes in a function name as a string, and returns a proxy function to the original function
var factorial = R.ifElse(
R.eq(0), R.always(1),
R.converge(R.multiply, R.I, R.compose(proxy('factorial'), R.dec))
);
factorial(0);
@TGOlson
TGOlson / length.js
Created March 8, 2015 23:04
Recursive point-free array length one-liner
// functional helpers assumed to be included
// `proxy` is a function to allow for point-free recursion in JavaScrpipt
// it takes in a function name as a string, and returns a proxy function to the original function
var length = ifElse(isEmpty, always(0), compose(inc, proxy('length'), tail));
length([]);
// => 0
length([1, 2, 3, 4]);
@TGOlson
TGOlson / fizzbuzz.js
Created March 8, 2015 08:28
Point-free JavaScript FizzBuzz Kata
var R = require('ramda');
var factorOf = R.curryN(2, R.compose(R.eq(0), R.flip(R.modulo)));
var getFizzBuzz = R.cond(
[R.and(factorOf(3), factorOf(5)), R.always('FizzBuzz')],
[factorOf(3), R.always('Fizz')],
[factorOf(5), R.always('Buzz')],
[R.T, R.identity]
);
@TGOlson
TGOlson / fizzbuzz.hs
Created March 8, 2015 08:14
Haskell FizzBuzz Kata
getFizzBuzz :: (Int, Int, Int) -> String
getFizzBuzz (_, 0, 0) = "FizzBuzz"
getFizzBuzz (_, 0, _) = "Fizz"
getFizzBuzz (_, _, 0) = "Buzz"
getFizzBuzz (x, _, _) = show x
fizzBuzz :: Int -> [String]
fizzBuzz n = [getFizzBuzz (x, x `mod` 3, x `mod` 5) | x <- [1..n]]
fizzBuzz 15
@TGOlson
TGOlson / list_comp.js
Created March 8, 2015 04:29
Single set list comprehension in JavaScript
var R = require('ramda');
// flipped filter to work with folds -> (acc, fn)
// [a] -> [(a -> Boolean)] -> [a]
var reduceWithFilter = R.reduce(R.flip(R.filter));
// (a -> b) -> [a] -> [(a -> Boolean)] -> [a]
function listComp(outputFn, inputSet, predicates) {
return R.compose(R.map(outputFn), reduceWithFilter(inputSet))(predicates);
}
@TGOlson
TGOlson / assertions.js
Last active August 29, 2015 14:16
Point free assertion dream
var R = require('ramda');
// * -> IO
var report = R.bind(console.log, console);
// * -> IO
var reportSuccess = R.compose(report, R.always('Success'));
// * -> * -> IO
var reportFailure = function(expected, actual) {
@TGOlson
TGOlson / funcify.js
Created February 10, 2015 04:03
Funcify OO functions
var _ = require('ramda'),
curry = _.curry,
compose = _.compose;
var fnames = [
'map',
'forEach',
'concat'
// TODO: add more
@TGOlson
TGOlson / functional_fizzbuzz_kata.js
Last active August 29, 2015 14:14
Functional FizzBuzz Kata
/*
* General Functional Helpers
*/
function compose(a, b) {
return function() {
a(b.apply(null, arguments));
};
}
function is(v1, v2) {