This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
RED="\033[31m" | |
GREEN="\033[32m" | |
RESET="\033[0m" | |
MAGENTA="\033[35m" | |
CYAN="\033[36m" | |
YELLOW="\033[33m" | |
BLUE="\033[34m" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A simple Lisp parser. You could copy paste this into iex (the elixir repl) | |
# or use a livebook, and you can run the parser like this: | |
# | |
# Elexer.parse("(+ 1 -2)") | |
# A more fully featured prject can be found here: https://github.com/Adzz/elexer | |
defmodule Elexer do | |
@moduledoc """ | |
A simple lisp parser. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Shape do | |
@callback area(map()) :: integer() | |
@callback perimeter(map()) :: integer() | |
end | |
defmodule Square do | |
defstruct [:side] | |
@behaviour Shape | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Third round of feature requests | |
# Extend your design to support one of the following two features: | |
# Ability to look up, for all users, how many have a todo item with the same name as one of yours. | |
# Also, explain how to extend your user interface to display the total number of todo items in the | |
# list currently being viewed. This feature is simple, but there are some easy ways to get it wrong. | |
# Ability to go back into the past, and see what a user's todo list looked like at any point in time. | |
# Explain how this would work for todo lists that already exist. | |
# ================================================================================================= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// As you might expect | |
func add(x int, y int) int { | |
return x + y | |
} | |
// Go functions can return multiple values though, so you | |
// Can type each of the return values | |
func swap(x, y string) (string, string) { | |
return y, x | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map_1 = %{first: 1, second: 2} | |
map_2 = %{first: 3, second: 4} | |
# These just work! 🤩 | |
Zip.apply(map_1, map_2, Add) #=> %{first: 4, second: 6} | |
Zip.apply(map_1, map_2, Subtract) #=> %{first: 4, second: 6} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defimpl Zip, for: Map do | |
def apply(a, b, operation) do | |
# We take the all the keys from b that are also in a | |
intersection = Map.take(b, Map.keys(a)) | |
Enum.reduce(a, %{}, fn {key, value}, acc -> | |
Map.put(acc, key, operation.calculate(value, Map.fetch!(intersection, key))) | |
end) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defprotocol Subtract do | |
def calculate(a, b) | |
end | |
# And implement it | |
defimpl Subtract, for: Integer do | |
def calculate(a, b) when is_integer(b), do: a - b | |
end | |
defimpl Subtract, for: Decimal do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defimpl Add, for: Decimal do | |
def calculate(decimal, decimal_2 = %Decimal{}), do: Decimal.add(decimal, decimal_2) | |
end | |
list_1 = [Decimal.new(1), Decimal.new(2)] | |
list_2 = [Decimal.new(3), Decimal.new(4)] | |
Zip.apply(list_1, list_2, Add) #=> [Decimal.new(4), Decimal.new(6)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defimpl Zip, for: List do | |
def apply(a, b, operation) do | |
Enum.zip(a, b) | |
|> Enum.map(fn {a, b} -> operation.calculate(a, b) end) | |
end | |
end | |
# Now we can call it like this: | |
Zip.apply([1, 2], [3, 4], Add) #=> [4, 6] |
NewerOlder