Skip to content

Instantly share code, notes, and snippets.

View mattfield's full-sized avatar
🫠

Matt Field mattfield

🫠
View GitHub Profile
@mattfield
mattfield / real-world-functional-javascript.md
Last active December 23, 2015 20:29
"Real-world" Functional Javascript

As software becomes more and more complex, it is more and more important to structure it well. Well-structured software is easy to write, easy to debug, and provides a collection of modules that can be re-used to reduce future programming costs. Functional programming is a paradigm shift away from how we normally think about constructing our programs and writing our abstractions. In this talk, I'd like to introduce the style of functional programming, discuss what it has to offer versus other programming metholodies (and how functional programming can complement paradigms such as OOP) and provide "real-world" examples of functional techniques such as:

  • Programming with functions as the primary construct
  • Moving from thinking imperatively to thinking declaritively in terms of the way data flows through our applications
  • Higher-order functions and applicative programming and how they remove complexity from imperative-style programming e.g. for-loops
  • Functions as building blocks and how to compose functio
@mattfield
mattfield / splat.js
Last active December 23, 2015 06:49
Explanation of a splat function for linking purposes
// splat is an abstraction that wraps around _.map,
// turning a function into a mapping
// NB: we're using Underscore/Lodash here because we
// want our mapping to work everywhere. You could
// easily replace this implementation with one
// that uses the native Array#map
function splat(fn){
return function(list){
_.map(list, fn);
}
@mattfield
mattfield / either.js
Created September 16, 2013 09:36
Either functor
var Either = function(left, right){
this.left = left;
this.right = right;
};
Either.prototype.map = function(f){
return this.right ?
Either(this.left, f(this.right)) :
Either(f(this.left), this.left);
}
/*
turns a binary function into a variadic function via reduction
*/
var reducify = function(fn){
return function(){
var args = [].slice.call(arguments)
return args.reduce(fn)
}
@mattfield
mattfield / fizz_buzz.hs
Last active December 21, 2015 01:58 — forked from adtaylor/fizz_buzz.rb
module Main where
import Data.Monoid (mappend)
import Data.Maybe (fromMaybe, listToMaybe, maybe)
import System.Environment (getArgs)
fizzbuzz i = fromMaybe (show i) $ mappend ["fizz" | i `rem` 3 == 0]
["buzz" | i `rem` 5 == 0]
main = mapM_ putStrLn [ fizzbuzz i | i <- [1..100] ]
@mattfield
mattfield / maybe.js
Last active December 20, 2015 05:29
Maybe functor
var Maybe = function(val){
this.val = val;
};
Maybe.prototype.map = function(f){
return this.val ? Maybe(f(this.val)) : Maybe(null);
};