Skip to content

Instantly share code, notes, and snippets.

@JoelQ
JoelQ / MagicStrings.elm
Created August 10, 2018 15:32
Refactoring magic strings in Elm views
view : Model -> Html Msg
view model =
div []
[ div [ class "icon" ] [ span [class "icon-profile" ] [] ]
, div [ class "icon" ] [ span [ class "icon-clock" ] [] ]
]
-- REFACTOR TO
view : Model -> Html Msg
@JoelQ
JoelQ / non_empty_array.rb
Created August 9, 2018 17:30
Creating a non-empty array in Ruby
class NonEmptyArray
include Enumerable
def initialize(first, rest)
@first = first
@rest = rest
end
def first
@first
@JoelQ
JoelQ / inter-group-edges.md
Created June 20, 2018 17:15
Edges to different groups

Problem

You have a graph where the nodes are in groups. You want to create edges between all nodes that are in different groups.

For example, given the following groups:

[[1, 2], [3], [4, 5]]
@JoelQ
JoelQ / hamming_benchmark_styles.rb
Created June 29, 2018 21:16
Hamming benchmarks - Style comparisons
class Hamming
def self.zip_count(s1, s2)
s1.chars.
zip(s2.chars).
count { |c1, c2| c1 != c2 }
end
def self.for_loop(s1, s2)
distance = 0
@JoelQ
JoelQ / hamming_benchmark_classic.rb
Created June 29, 2018 21:09
Hamming Benchmark - Classic
class Hamming
def self.eager(s1, s2)
s1.chars.
zip(s2.chars).
count { |c1, c2| c1 != c2 }
end
def self.half_lazy(s1, s2)
s1.each_char.
zip(s2.each_char).
@JoelQ
JoelQ / hamming_benchmark_take.rb
Created June 29, 2018 19:53
Hamming Lazy/Eager benchmarks
class Hamming
def self.eager(s1, s2)
s1.chars.take(10).
zip(s2.chars.take(10)).
count { |c1, c2| c1 != c2 }
end
def self.half_lazy_args(s1, s2)
s1.each_char.take(10).
zip(s2.each_char.take(10)).
@JoelQ
JoelQ / elm-decoder-challenge.md
Created June 9, 2018 23:37
Elm Decoder Challenges
@JoelQ
JoelQ / MapZero.elm
Created May 30, 2018 18:08
A look at mapN functions in Elm. The `map0` function is just the constructor function! Haskell folks would call this pure/return.
-- Maybe
map2 f v1 v2 = Just f |> andMap v1 |> andMap v2
map f v1 = Just f |> andMap v1
map0 f = Just f
-- List
map2 f v1 v2 = singleton f |> andMap v1 |> andMap v2
map f v1 = singleton f |> andMap v1
@JoelQ
JoelQ / Folding.elm
Created March 20, 2018 18:44
Folding a list is like replacing the cons and empty constructors
-- GIVEN
type MyList a
= Cons a (MyList a)
| Empty
add : Int -> Int -> Int
add a b =
a + b

A Darwinian Nightmare

It seemed to have stepped right out of the pages of a horror novel. The mutant shape-shifter struck without warning, defying a mere mortal's attempts to detect it. It seemed to distort the fabric of reality itself. And it was going to ruin my job interview.

We've all encountered this monster at some point. Like many classic monsters, it is our own creation. Every year it devastate projects worldwide. The fearsome mutation bug is an expert at hiding and altering your programs in subtle and