Skip to content

Instantly share code, notes, and snippets.

@JoelQ
JoelQ / tree.rb
Last active January 27, 2022 15:11
A Ruby implementation of a binary tree and its catamorphism
class Tree
attr_reader :value, :left, :right
def initialize(value, left, right)
@value = value
@left = left
@right = right
end
def reduce(initial, &block)
@JoelQ
JoelQ / list-pattern-matching-elm.md
Last active December 5, 2021 16:39
What does `x :: xs` pattern matching mean in Elm?

What does x :: xs pattern matching mean in Elm?

:: is the list prepend operator in Elm.

3 :: 2 :: 1 :: []

is equivalent to

@JoelQ
JoelQ / RandomToTask.elm
Last active September 30, 2021 07:16
Turn an Elm random generator into task, allowing it to be chained with other side effects.
-- 0.19
randomToTask : Generator a -> Task x a
randomToTask generator =
Time.now
|> Task.map (Tuple.first << Random.step generator << Random.initialSeed << Time.posixToMillis)
-- 0.18
@JoelQ
JoelQ / ellie_examples.md
Last active July 28, 2021 14:12
List of helpful Ellie's I've written
@JoelQ
JoelQ / math.rb
Last active June 30, 2021 20:41
Derive addition, multiplication, and exponentiation from from `Integer#next`
def add(number1, number2)
number2.times.reduce(number1) { |total| total.next }
end
add(2,3)
# => 5
def subtract(number1, number2)
number2.times.reduce(number1) { |total| total.pred }
end
@JoelQ
JoelQ / knuth_arrows.rb
Last active June 30, 2021 20:40
Exploration of Knuth Arrows as monoids
class Add
def self.mempty
new(0)
end
def initialize(number)
@number = number
end
attr_reader :number
@JoelQ
JoelQ / build-deploy
Created March 21, 2019 18:55
Netlify + Parcel deployment
#!/bin/sh
set -e
echo "== BUILDING THE APP =="
yarn parcel build src/index.html
echo "== CONFIGURING REDIRECTS =="
if [ "$CONTEXT" = "production" ]; then
cp production_redirects dist/_redirects
@JoelQ
JoelQ / Ball.elm
Last active October 23, 2019 18:10
Gravity and Ball
import Graphics.Collage exposing (..)
import Graphics.Element exposing (..)
import Color exposing (..)
import Keyboard
import Time
import Debug
-- Model
type alias Model = { x : Float, y : Float, radius: Float }
@JoelQ
JoelQ / conditionals_to_booleans_ruby.md
Created December 5, 2018 22:03
Common Ruby conditionals refactored to boolean expressions

Redundant if/else - identity

def can_edit?
  if admin?
    true
  else
    false
  end
end
@JoelQ
JoelQ / four_line_repl.rb
Last active November 10, 2018 19:54
4-line Ruby REPL (read eval print loop)
puts "4 line REPL (read eval print loop)"
$stdin.each_line do |line| # READ
return_value = eval(line.chomp) # EVAL
puts return_value # PRINT
end # LOOP