Skip to content

Instantly share code, notes, and snippets.

View baweaver's full-sized avatar
📝
Documenting things

Brandon Weaver baweaver

📝
Documenting things
View GitHub Profile
@baweaver
baweaver / ruby_galaxy_poker_pattern_matching.rb
Created January 28, 2021 19:53
Variant of pattern matching example from RubyGalaxy talk
class Card
SUITS = %w(S H D C).freeze
RANKS = %w(2 3 4 5 6 7 8 9 10 J Q K A).freeze
RANKS_SCORES = RANKS.each_with_index.to_h
include Comparable
attr_reader :suit, :rank
def initialize(suit, rank)
@baweaver
baweaver / pattern_array_deconstruct_benchmark.rb
Created January 18, 2021 05:11
Benchmark to measure speed difference between Array for matching and implied matching through `to_a` on Enumerable
require 'benchmark/ips'
module Enumerable
def deconstruct() = to_a
end
class Collection
attr_reader :items
include Enumerable
@baweaver
baweaver / dio_quick_example.rb
Created January 17, 2021 05:53
Quick example of the new Dio gem, https://www.github.com/baweaver/dio
require 'dio'
# Dive into arbitrary objects using their methods
Dio[1] in { succ: { succ: { succ: 4 } } }
# => true
Node = Struct.new(:value, :children)
tree = Node[1,
Node[2, Node[3, Node[4]]],
@baweaver
baweaver / properly_refactored_poker_pattern_match.rb
Created January 15, 2021 06:45
This time with a bit more gusto in the refactoring and care to remembering classes exist and things related to them should go in them
class Card
SUITS = %w(S H D C).freeze
SUITS_SCORES = SUITS.each_with_index.to_h
RANKS = %w(2 3 4 5 6 7 8 9 10 J Q K A).freeze
RANKS_SCORES = RANKS.each_with_index.to_h
include Comparable
attr_reader :suit, :rank
Card = Struct.new(:suit, :rank) do
include Comparable
def precedence() = [SUITS_SCORES[self.suit], RANKS_SCORES[self.rank]]
def rank_precedence() = RANKS_SCORES[self.rank]
def suit_precedence() = SUITS_SCORES[self.rank]
def <=>(other) = self.precedence <=> other.precedence
def to_s() = "#{self.suit}#{self.rank}"
@baweaver
baweaver / pattern_match_string_interpolation.rb
Created January 14, 2021 04:13
Forgive me, for I have done bad things to Ruby again. Credits to @philferne on Twitter for pointing out this might work
RANKS = [*2..10, *%w(J Q K A)].map(&:to_s).freeze
RANKS_SCORES = RANKS.each_with_index.to_h
Card = Struct.new(:suit, :rank)
straight = [Card['H', RANKS.first], *RANKS[1..4].map { Card['S', _1] }]
not_straight = [Card['H', RANKS.last], *RANKS[1..4].map { Card['S', _1] }]
def add_rank(r, n) = RANKS[RANKS_SCORES[r] + n]
@baweaver
baweaver / pattern_match_tic_tac_toe.rb
Created January 14, 2021 00:25
Using Pattern Matching to solve Tic Tac Toe
MOVE = /[XO]/.freeze
def board(*rows) = rows.map(&:chars)
def winner(board)
case board
in [
[MOVE => move, ^move, ^move],
[_, _, _],
[_, _, _]
@baweaver
baweaver / poker_hands.rb
Last active February 3, 2021 12:14
Computing poker hand scores using Ruby
Card = Struct.new(:suite, :rank) do
include Comparable
def precedence() = [SUITES_SCORES[self.suite], RANKS_SCORES[self.rank]]
def rank_precedence() = RANKS_SCORES[self.rank]
def suite_precedence() = SUITES_SCORES[self.rank]
def <=>(other) = self.precedence <=> other.precedence
def to_s() = "#{self.suite}#{self.rank}"
class Either
def self.deconstruct_keys(*)
{ Right: Right, Left: Left }
end
class Right < Struct.new(:value); end
class Left < Struct.new(:value); end
end
Either => { Right:, Left: }
# Ideal syntax for pattern matched methods:
def handle_response(body)
in 200, response
# Ok response
in 500, error
# Handle failure
else
# No idea?
end