Skip to content

Instantly share code, notes, and snippets.

View jmilamwalters's full-sized avatar

Jonathan Walters jmilamwalters

  • Estée Lauder
  • The Americas
View GitHub Profile
@jmilamwalters
jmilamwalters / changeset_assertion_helpers.exs
Last active January 24, 2020 19:32
A catalog of helpers functions to make assertions against `%Ecto.Changeset{}`s
defmodule ChangesetAssertionHelpers do
alias Ecto.Changeset
import ExUnit.Assertions
@spec assert_changeset_error_count(Changeset.t(), non_neg_integer()) :: Changeset.t()
def assert_changeset_error_count(%Changeset{} = changeset, count) do
assert length(changeset.errors) == count
changeset
end
@jmilamwalters
jmilamwalters / new_year_chaos.ex
Created November 22, 2019 21:58
New Year Chaos, optimized
defmodule NewYearChaos do
@moduledoc """
https://www.hackerrank.com/challenges/new-year-chaos/problem
"""
def halt_if_too_chaotic([]), do: :ok
def halt_if_too_chaotic([{first, index} | rest]) do
if first - index > 2, do: :halt, else: halt_if_too_chaotic(rest)
end
@jmilamwalters
jmilamwalters / functions_function_clauses.md
Last active November 13, 2019 21:38
Functions: function clauses in Elixir

Functions: function clauses in Elixir

A compilation of my favorite functions and patterns in functional programming

Elixir is far too generous. I realize how cossetted I am when I go to use function clauses in JavaScript. Because you can't do that.

I personally detest if statements, along with case -- perhaps to a fault. Function clauses redeem me.

Why?

Keybase proof

I hereby claim:

  • I am jmilamwalters on github.
  • I am jmilamwalters (https://keybase.io/jmilamwalters) on keybase.
  • I have a public key ASDDyPSOvUasnQQG3HGK0pGxR_TYFSS5lPotTcTJA3iTtQo

To claim this, I am signing this object:

@jmilamwalters
jmilamwalters / functions_lens.md
Last active December 28, 2018 21:26
Functions: `lens`

Functions: lens

A compilation of my favorite functions and patterns in functional programming

Lenses are difficult to articulate. More challenging is presenting a strong use-case. Perhaps you've already read up on lenses a bit. Perhaps your tireless search brings you here.

How does one describe a lens?

  • A functional getter/setter pair
@jmilamwalters
jmilamwalters / functions_debounce.md
Last active December 11, 2018 19:24
Functions: `debouce`

Functions: debounce

A compilation of my favorite functions and patterns in functional programming

I first learned of debounce from something of a mentor, who explained that it originates from the early days of the Internet when it was important to preclude repeated clicks to Buy Now buttons.

Generally speaking, to debounce a function is to nullify subsequent calls -- for an allotted period of time -- following the function's initial invocation. Debouncing presides as something of a governor over your function, and can be made to consolidate sequences of function calls into one.

In many contexts, its use is straightforward:

@jmilamwalters
jmilamwalters / functions_group_by.md
Last active December 28, 2018 21:40
Functions: `group_by`

Functions: group_by

A compilation of my favorite functions and patterns in functional programming

I encountered this little gem while pair-programming with a colleague earlier this week. It affords much potential in the domain of complex sorting operations.

Say we've a large list of unsorted, unordered exam results:

def list_exam_results do
@jmilamwalters
jmilamwalters / ramda_powered_life.md
Last active May 11, 2021 00:02
An implementation of Conway's Game of Life using Ramda and functional JavaScript

Ramda-powered Life

I remember when I became a developer. I had written my own implementation of Conway's Game of Life, and in doing so, had crossed a threshold. I reasoned then that programming was something I could do. I feel there are few motives so pure as coding an implementation of Life. I wonder if others do not view it as something of a rite of passage.

I find Life useful as an exercise when attempting to learn a new language or library, and accordingly, I've coded variations on my initial implementation from years ago. Most recently, I sought to use Ramda, a functional library for JavaScript. In this article, I intend to unpack the results.

Let us begin with an overview. There are two factors that characterize the specific nature of our approach:

  1. Our step function, also commonly referred to as tick, evaluates as input only living cells. Here's why: there is no reason to evaluate all cells on our grid -- livin
@jmilamwalters
jmilamwalters / functions_curry.md
Last active December 28, 2018 21:43
Functions: `curry`

Functions: curry

A compilation of my favorite functions and patterns in functional programming

Haskell Curry is a giant. His first name denotes a textbook functional programming language, his last name a technique in functional programming. It's his surname that I shall address here.

While learning to program, currying to me seemed some esoteric concept in FP reserved for practitioners of Haskell and Clojure. But, in practical usage, it's remarkably simple.

As a general rule, any time I find myself calling the same function -- and passing it the same arguments -- it's a good time to curry the function. When a function is curried, we can preload it with certain arguments, and then pass it whatever its remaining arguments at will.

@jmilamwalters
jmilamwalters / functions_with_statement.md
Last active December 28, 2018 21:47
Functions: `with` statement

Functions: with statement

A compilation of my favorite functions and patterns in functional programming

The with statement may be my favorite feature of Elixir since the pipe operator. It enables the chaining of uncertainty. It affords elegance to the otherwise unsavory business of nested control structures.

There are few things in code more corrupting of beauty than:

case fetch_token() do