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 / pattern_matching.md
Created November 24, 2019 09:45
Current WIP of pattern matching documentation. Still needs more work.

Pattern Matching

Pattern match statements are an extension of the case expression. They introduce a new keyword to case statements: in.

The in expression

in is used in place of when to use pattern matching behaviors. These two syntaxes cannot be mixed.

An in expression is different from when in that it allows one to match against data structures:

@baweaver
baweaver / symbol_ap.rb
Created October 19, 2019 07:09
Stealing some vague ideas from Applicatives in Haskell, probably wrongly but it amuses me so...
class Symbol
def ap(*applied_args)
-> *block_args {
if applied_args.empty?
receiver, *new_args = block_args[0]
receiver.send(self, *new_args)
else
self.to_proc.call(*block_args, *applied_args)
end
}
@baweaver
baweaver / favorite_things_ruby_edition
Last active September 21, 2019 07:49
A Few of my Favorite Things translated to Ruby (WIP)
Ruby programming
And Gems pushed to GitHub
Cloud based deployments and CI / CD
Tests that will pass on the first try
These are a few of my favorite things
Just Rails new app name
and omakase thinking
convention o'er configuration
and sane default settings
class Hash
alias_method :+, :merge
def -(other)
other_keys = Set.new(other.keys)
# Irrelevant value method
# reject { |k, _v| other_keys.include?(k) }
# Relevant value method
def double(n) n * 2 end
increment = -> n { n + 1 }
5
|> double # Method
|> increment # Proc
|> to_s(2) # self.to_s
@baweaver
baweaver / tales_of_the_ruby_grimoire.md
Created June 4, 2019 04:03
Tales of the Ruby Grimoire

Tales of the Ruby Grimoire

Introduction

The council of 3.0 had been called, and Scarlet was readying to join them to discuss the future of the land of Ruby. With her was her student, Red, carrying out his latest lesson.

"Now be sure to finish the remaining problems by the time I return, Red. I expect I'll have many fun things to tell you of when I return." said Scarlet.

Red nodded, still writing away on his scrolls, but as Scarlet exited the castle a sly sort of grin grew on his face.

class Proc
def contramap(&fn) self << fn end
end
adds = -> a, b { a + b }.curry
[1, 2, 3].map(&adds[2])
# => [3, 4, 5]
# What if the type on the left doesn't work with `adds`?
@baweaver
baweaver / async_helpers.js
Created April 12, 2019 17:50
Helpers for working with ActionCable async on the client side
// Helpers
const MAX_WAIT = 10000;
const POLLING_RATE = 500;
export function timedOut (maxWait) {
const startTime = Date.now();
return () => Date.now() - startTime > maxWait;
}
@baweaver
baweaver / tracepoint_notes_raw.md
Created January 26, 2019 20:56
Raw notes from my TracePoint session

TracePoint

What is TracePoint?

A class that provides the functionality of Kernel#set_trace_func in a nice Object-Oriented API.

Well what is set_trace_func?

Establishes proc as the handler for tracing, or disables tracing if the parameter is nil.

user_id_tp = tutor_profiles.index_by(&:user_id)
tutor_subjects = TutorSubject
.where(tutor_id: user_id_tp.keys)
.group_by(&:tutor_id)
.to_h { |uid, subjects| [user_id_tp[uid], subjects.map(&:subject_id)] }
# Faster
user_id_tp = tutor_profiles.index_by(&:user_id)
hash_of_arrays = Hash.new { |h,k| [] }