Skip to content

Instantly share code, notes, and snippets.

@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 / 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
@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 / 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 / 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 / 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]]