Skip to content

Instantly share code, notes, and snippets.

@JoelQ
JoelQ / tally.rb
Last active April 13, 2024 16:27
Demonstration of using a rich object to represent a composite count and how it makes the math easier to deal with
class Tally
def self.empty
new({})
end
def initialize(raw)
@raw = empty_hash.merge raw.slice(:small, :medium, :large)
end
# Accessors

How not to implement a multi-step form

"That ticket touches the wizard code? Why don't YOU take that one". The wizard in question is so gnarly my colleagues are afraid to touch it.

Looking at the code through the lens of conditional cardinality reveals what went wrong and how we can make things better. This mental model will change the way you think about structuring logic and help you take your conditional code from scary to delightful.

Description

This talk is aimed at an intermediate to advanced audience. While it will driven by a practical example (refactoring a multi-step form), it will mainly be about some more theoretical principles and mental models for structuring conditional code.

The Math Every Programmer Needs

Abstract

Leave your fear of math behind along with the equations and symbols, we're headed to Ruby-land! On this whirlwind tour, we’ll explore how to write better conditionals with Boolean algebra, figure out how many test cases we need with combinatorics, a mental model for breaking down large tasks with graph theory, and more.

You’ll leave this talk equipped to better do your job with some ideas, techniques, and mental models from the wonderfully practical world of discrete math.

Details

@JoelQ
JoelQ / tree_enumerator.rb
Created August 21, 2022 22:15
Demonstration of how using `Enumerator` makes it nicer to work with a data structure that has multiple valid traversals such as a binary tree
class Tree
attr_reader :val, :left, :right
def self.empty
EmptyTree.new
end
def self.leaf(val)
new(val, empty, empty)
end
@JoelQ
JoelQ / dollar.rb
Last active September 23, 2024 03:02
Implementing value object semantics as an RSpec shared_example.
class Dollar
attr_reader :cents
def initialize(cents:)
@cents = cents
end
def hash
[self.class, cents].hash
end
@JoelQ
JoelQ / month.rb
Last active September 2, 2024 18:44
require "date"
class Month
include Comparable
MONTHS_PER_YEAR = 12
def self.from_date(date)
self.from_parts(date.year, date.month)
end
@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 / 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 / elm-generic-types-exercises.md
Last active June 14, 2022 21:22
Short exercises to learn how generic types and type variables work in Elm.

Generic Elm types exercises

A series of exercises to get a feel for how generic types and type variables work in Elm. For each of these, you are given some code that is not compiling. Your task is to get it compiling. You can change the body of the function but not the signature. No cheating with Debug.todo or infinite recursions either 😉

@JoelQ
JoelQ / elm-types-glossary.md
Last active June 26, 2024 03:05
Elm Type Glossary

Elm Types Glossary

There's a lot of type terminology and jargon going around when discussing types in Elm. This glossary attempts to list some of the most common type terms along with synonyms, terms from other language communities, examples, and links to more detailed articles on each topic.