Skip to content

Instantly share code, notes, and snippets.

View RocketPuppy's full-sized avatar

Daniel Wilson-Thomas RocketPuppy

View GitHub Profile
/*
* The Maybe monad type. All monads must respond to both `bind` and `return`
* functions. Here return is called `ret` as return is already a keyword. I've
* defined `bind` and `return` on the Maybe prototype so they can be used by
* the two subclasses `Just` and `Nothing`.
*/
function Maybe(){}
/*
* The bind function takes a single function as an argument. That function must
* take a value of the same type as is inside the Just and return a new
/*
* Currying is the process of taking a function that takes multiple arguments
* and turning it into a function that takes a single argument and returns
* another function. For example:
* plus(x, y, z) takes three arguments
* curry(plus) becomes a function I can call three times with a single argument.
*
* The big advantage to currying is that we can partially apply functions.
*/
function curry(fn, args){

Math and Programming

Functions

In algebra functions are a way to map from one set of objects (the domain) to another set of objects (the codomain). In traditional imperative programs functions are a way to sequence operations to achieve a desired state. In some sense traditional imperative programs encourage reasoning about implementation details. Instead if we think about our functions in the same sense as in algebra, as mappings from some set to another, the implementation becomes almost incidental. Traditional imperative programming doesn't encourage this sort of thinking. It's a style of thinking that encourages reasoning about behavior over implementation. While there can be differences between implementations of the same function, the differences between implementations are almost always performance related and do not alter the domain and codomain.

Knowing the domain and codomain gives us information about how a function behaves in isolation. It clearly identifies potential edge cases, usually th

~$ irb
irb(main):001:0> FOO = 'baz'
=> "baz"
irb(main):002:0> FOO = 'bar'
(irb):2: warning: already initialized constant FOO
(irb):1: warning: previous definition of FOO was here
=> "bar"
irb(main):003:0> FOO
=> "bar"
irb(main):004:0> FOO.upcase

As developers it seems like we're in a land of plenty when it comes to code quality tools. There are many options, some more controversial than others. One such option I'd like to talk about today is the use of ESLint in Javascript with the consistent-return rule.

When you enable the consistent-return rule in ESLint the tool will automatically check that all code paths in a function either explicitly return a value or don't return a value. In functions that branch one might forget to return a value in one of the branches, and the consistent-return rule will warn you when that happens.

{ stdenv ? (import <nixpkgs> {}).stdenv,
go ? (import <nixpkgs> {}).go_1_6,
goPackages ? (import <nixpkgs> {}).go16Packages,
git ? (import <nixpkgs> {}).git,
curl ? (import <nixpkgs> {}).curl,
cacert ? (import <nixpkgs> {}).cacert,
cowsay ? (import <nixpkgs> {}).cowsay,
jq ? (import <nixpkgs> {}).jq,
openssl ? (import <nixpkgs> {}).openssl,
nodejs ? (import <nixpkgs> {}).nodejs_5_x,
{ stdenv, go, goPackages, git, curl, cacert, cowsay, jq,
openssl, nodejs, python, fetchFromGitHub }:
stdenv.mkDerivation rec {
rev = "v5.1.6";
name = "heroku-cli-${rev}";
buildInputs = [
cacert go nodejs curl git python cowsay jq openssl
goPackages.osext goPackages.binarydist goPackages.merry
goPackages.speakeasy goPackages.netrc goPackages.golock
{ goPackages, fetchFromGitHub }:
with goPackages; rec {
errors = buildGoPackage rec {
rev = "a41850380601eeb43f4350f7d17c6bbd8944aaf8";
name = "errors-${rev}";
goPackagePath = "github.com/go-errors/errors";
src = fetchFromGitHub {
inherit rev;
owner = "go-errors";
repo = "errors";
# My NixOS channel is behind the regular nix pkgs channel, so I didn't have go 1.6 or node 5 available in <nix_pkgs>
nix-build default.nix --arg go '(import <custom_pkgs> {}).go_1_6' --arg goPackages '(import <custom_pkgs> {}).go16Packages' --arg nodejs '(import <custom_pkgs> {}).nodejs-5_x'
nix-env -i -f default.nix --arg go '(import <custom_pkgs> {}).go_1_6' --arg goPackages '(import <custom_pkgs> {}).go16Packages' --arg nodejs '(import <custom_pkgs> {}).nodejs-5_x'
function deleteTodo(text) {
return {
type: DELETE_TODO,
text: text
};
}
const initialState = []
function todoReducer(state = initialState, action) {