Skip to content

Instantly share code, notes, and snippets.

@kerryb
kerryb / blocks.rb
Created December 10, 2013 22:28
Ruby blocks, procs, map and reduce
# A proc is just a block of code that can be called later:
say_hello = -> { 2 + 2 }
say_hello.call # => 4
# Procs can take parameters:
double = ->(x) { x * 2 }
@kerryb
kerryb / space.exs
Created October 7, 2022 18:27
Who’s in space?
#!/usr/bin/env elixir
Mix.install([
{:httpoison, "~> 1.8"},
{:jason, "~> 1.3"}
])
defmodule Space do
def run do
fetch_data()
@kerryb
kerryb / parser.ex
Created October 12, 2022 14:46
Horrible code to convert some nested text data to json
text
|> String.split("\n")
|> Enum.reduce({["{"], " "}, fn line, {output, current_indent} ->
case Regex.run(~r/^( +)(.*): ?(.*)/, line, capture: :all_but_first) do
[indent, key, ""] ->
close_braces =
if indent < current_indent do
Enum.map_join(
(String.length(current_indent) - 4)..String.length(indent)//-4,
"",
@kerryb
kerryb / stack.rb
Created October 9, 2023 13:04
A simple stack, TDDed without using a testing framework. Unfortunately the individual steps weren’t recorded.
class Stack
class Empty < RuntimeError; end
def initialize
@items = []
end
def empty?
@items.count == 0
@kerryb
kerryb / changeset-form-issue.livemd
Last active December 13, 2023 14:38
LiveBook demonstrating an Ecto changeset/Phoenix form issue

Changeset/Form issue

Mix.install([
  {:phoenix_live_view, "~> 0.20.1"},
  {:phoenix_ecto, "~> 4.4"}
])

Create a struct