Skip to content

Instantly share code, notes, and snippets.

View mattfield's full-sized avatar

Matt Field mattfield

View GitHub Profile
@mattfield
mattfield / ec2_volume_check.sh
Created July 19, 2017 09:56
Bash snippet to poll attachment of EC2 volume
EC2_INSTANCE_ID=$(curl -s http://instance-data/latest/meta-data/instance-id)
DEVICE=/dev/sda
DATA_STATE="unknown"
until [ "$DATA_STATE" == "attached" ]; do
DATA_STATE=$(aws ec2 describe-volumes \
--region $REGION \
--filters \
Name=attachment.instance-id,Values=$EC2_INSTANCE_ID \
Name=attachment.device,Values=$DEVICE \
### Keybase proof
I hereby claim:
* I am mattfield on github.
* I am mattfield (https://keybase.io/mattfield) on keybase.
* I have a public key ASBlGnVvRZbBp5og48qSJ8ZribR59UsVZowxFWhIPKfajwo
To claim this, I am signing this object:
# Functional JS - Event Handler
## What is FP: 15 mins [tim]
## Defining features: 5
### Vs OOP: 5
- flat
@mattfield
mattfield / member.js
Created January 27, 2014 10:45
small, recursive function to detect if item is member of a collection
function car(list){
return list[0];
}
function cdr(list){
return list.slice(1);
}
function memberp(a, list){
if (!list.length) {
@mattfield
mattfield / walker.clj
Created January 27, 2014 09:41
Clojure tree walker
(defn tree-searcher
"Parse through every node in a tree and check against
give matcher function. If a match succeeds, return the node.
If no match is found after all recursions, just return the tree.
Also see clojure.walk"
[zipper matcher]
(loop [loc zipper]
(if (zip/end? loc)
(zip/root loc)
(if-let [matcher-result (matcher (zip/node loc))]
@mattfield
mattfield / expression_problem.md
Last active December 26, 2015 07:59
The Expression Problem

The Problem

We have data coming from one system and we want to use a library in another system to deal with it. However, the data is coming in in a format that doesn't match what the library expects. Square peg, round hole. In the OO world, we wrap the data (wrappers/adapters) that allow us to move the data into the library. There are a few problems with this, however. For example, you don't have a reference to the original data object you're actually using. In the functional world, where we don't tie behaviour to data, we alter the library itself to be able to accept the data we're being given.

Say we have a library that already supports the summation of lists such that:

    sum([1,2,3]) //=> 6
    sum(["a", "b", "c"]) //=> "abc"

These means that we have two different concepts of summation: an Addable kind for integers and a Foldable kind for strings; for integers it's just normal addition, and with strings it's a process of appending. There are a couple of basic requirements here: the library

#! /usr/bin/env sh
RUBY_VERSION="1.9.1"
SERVER="${HOME}/.gem/ruby/${RUBY_VERSION}/bin/camper_van"
PIDFILE="/tmp/camper_van.pid"
if [ ! -e ${SERVER} ]; then
echo "CamperVan server not found at '${SERVER}'."
fi

Everything David Nolen Mentioned

When watching David Nolen's excellent Lambda Jam keynote on InfoQ, the references to interesting reading came a little too fast and furious for me to jot down, so I went through the slides and put this gist together.

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 in JavaScript, discuss what it has to offer and provide some "real-world" examples to show how it can help you write cleaner, more disciplined code that's easier to reason about, easier to test and easier to maintain.

@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