Skip to content

Commit

Permalink
Try module patching helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
christhekeele committed Apr 19, 2024
1 parent a03e72a commit 9b51996
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Welcome to the home for [my personal livebooks](https://github.com/christhekeele/livebooks/tree/latest/livebooks): Elixir experiments, guides, and accompanying source code.

Their history can be accessed [on GitHub](https://github.com/christhekeele/livebooks) by the clicking the "view source"
`</>` icon next to the title of every page. Corrections, proposals, and discussions can be made in the GitHub [issues](https://github.com/christhekeele/livebooks/issues), [pull requests](https://github.com/christhekeele/livebooks/pulls), and [discussions](https://github.com/christhekeele/livebooks/discussions) pages respectively.
`</>` icon next to the title of every page. Corrections, proposals, and discussions can be made in the GitHub [pull requests](https://github.com/christhekeele/livebooks/pulls), [issues](https://github.com/christhekeele/livebooks/issues), and [discussions](https://github.com/christhekeele/livebooks/discussions) pages respectively.
11 changes: 11 additions & 0 deletions lib/livebook/helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Livebook.Helpers do
@moduledoc """
Common helpers for these livebooks.
"""

defmacro __using__(_opts \\ []) do
quote do
import Livebook.Modules, only: [defmodule: 3]
end
end
end
3 changes: 0 additions & 3 deletions lib/livebooks.ex

This file was deleted.

69 changes: 42 additions & 27 deletions livebooks/surreal.livemd
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
# Surreal Numbers

```elixir
Mix.install([
{:ets, "~> 0.9.0"},
{:eternal, "~> 1.2"}
])
Mix.install(
[
{:ets, "~> 0.9.0"},
{:eternal, "~> 1.2"},
{:livebooks, github: "christhekeele/livebooks", tag: "latest"}
],
force: true
)
```

## Video Format

[Watch the ElixirConf 2022 lightning talk here!](https://www.youtube.com/watch?v=f1lNK5gDlwA&t=235s)

## Helpers

```elixir
use Livebook.Helpers
Application.started_applications()
```

## Caching

```elixir
require Logger

defmodule Surreal.Guards do
defmodule SurrealGuards do
defguard is_set(surreals) when is_list(surreals)

defguard is_surreal(surreal) when is_tuple(surreal) and tuple_size(surreal) == 2
end

defmodule Surreal.Cache do
defmodule SurrealCache do
@name __MODULE__

def init() do
Expand Down Expand Up @@ -47,13 +66,9 @@ defmodule Surreal.Cache do
end
end

Surreal.Cache.init()
SurrealCache.init()
```

## Video Format

[Watch the ElixirConf 2022 lightning talk here!](https://www.youtube.com/watch?v=f1lNK5gDlwA&t=235s)

## Axioms

* Zero
Expand Down Expand Up @@ -96,9 +111,9 @@ five_eighths = {[one_half], [three_quarters]}
## Module

```elixir
defmodule Surreal do
defmodule Surreal, v: 1 do
import Kernel, except: [-: 1, +: 2, -: 2, *: 2, /: 2, <>: 2]
import Surreal.Guards
import SurrealGuards

@zero {[], []}
@one {[{[], []}], []}
Expand All @@ -108,9 +123,9 @@ end
## Set Concatenation

```elixir
defmodule Surreal do
defmodule Surreal, v: 2 do
import Kernel, except: [-: 1, +: 2, -: 2, *: 2, /: 2, <>: 2]
import Surreal.Guards
import SurrealGuards

@empty_set []
@zero {@empty_set, @empty_set}
Expand All @@ -135,9 +150,9 @@ end
## Surreal Math

```elixir
defmodule Surreal do
defmodule Surreal, v: 3 do
import Kernel, except: [-: 1, +: 2, -: 2, *: 2, /: 2, <>: 2]
import Surreal.Guards
import SurrealGuards

@empty_set []
@zero {@empty_set, @empty_set}
Expand Down Expand Up @@ -290,9 +305,9 @@ neg_two |> IO.inspect()
```elixir
require Logger

defmodule Surreal do
defmodule Surreal, v: 4 do
import Kernel, except: [-: 1, +: 2, -: 2, *: 2, /: 2, <>: 2]
import Surreal.Guards
import SurrealGuards

@zero {[], []}
@one {[{[], []}], []}
Expand Down Expand Up @@ -340,19 +355,19 @@ defmodule Surreal do
def to_number({[left_surreal], []} = surreal) do
Logger.debug("to_number: `#{inspect(surreal)}`")

Surreal.Cache.try({:to_number, surreal}, fn ->
SurrealCache.try({:to_number, surreal}, fn ->
Kernel.+(to_number(left_surreal), 1)
end)
end

def to_number({[], [right_surreal]} = surreal) do
Surreal.Cache.try({:to_number, surreal}, fn ->
SurrealCache.try({:to_number, surreal}, fn ->
Kernel.-(to_number(right_surreal), 1)
end)
end

def to_number({[left_surreal], [right_surreal]} = surreal) do
Surreal.Cache.try({:to_number, surreal}, fn ->
SurrealCache.try({:to_number, surreal}, fn ->
Kernel./(Kernel.+(to_number(left_surreal), to_number(right_surreal)), 2)
end)
end
Expand All @@ -364,7 +379,7 @@ defmodule Surreal do
if length(left_numbers) > 1 or length(right_numbers) > 1 do
raise "Multi-surreals { #{inspect(left_numbers)} | #{inspect(right_numbers)} }:\n#{inspect(surreal)}"
else
Surreal.Cache.try({:to_number, surreal}, fn ->
SurrealCache.try({:to_number, surreal}, fn ->
case {length(left_numbers), length(right_numbers)} do
{0, 0} -> 0
{0, 1} -> Kernel.-(List.first(right_numbers), 1)
Expand Down Expand Up @@ -402,7 +417,7 @@ defmodule Surreal do
def -surreal when is_surreal(surreal) do
Logger.debug("negating: `#{inspect(surreal)}`")

Surreal.Cache.try({:-, surreal}, fn ->
SurrealCache.try({:-, surreal}, fn ->
{left, right} = surreal
{-right, -left}
end)
Expand All @@ -423,7 +438,7 @@ defmodule Surreal do
def surreal1 + surreal2 when is_surreal(surreal1) and is_surreal(surreal2) do
Logger.debug("adding: `#{inspect(surreal1)} + #{inspect(surreal2)}`")

Surreal.Cache.try({:+, surreal1, surreal2}, fn ->
SurrealCache.try({:+, surreal1, surreal2}, fn ->
{left1, right1} = surreal1
{left2, right2} = surreal2

Expand Down Expand Up @@ -452,7 +467,7 @@ defmodule Surreal do
def surreal1 - surreal2 when is_surreal(surreal1) and is_surreal(surreal2) do
Logger.debug("subtracting: `#{inspect(surreal1)} - #{inspect(surreal2)}`")

Surreal.Cache.try({:-, surreal1, surreal2}, fn ->
SurrealCache.try({:-, surreal1, surreal2}, fn ->
surreal1 + -surreal2
end)
end
Expand Down Expand Up @@ -492,7 +507,7 @@ defmodule Surreal do
def surreal1 * surreal2 when is_surreal(surreal1) and is_surreal(surreal2) do
Logger.debug("multiplying: `#{inspect(surreal1)} * #{inspect(surreal2)}`")

Surreal.Cache.try({:*, surreal1, surreal2}, fn ->
SurrealCache.try({:*, surreal1, surreal2}, fn ->
{left1, right1} = surreal1
{left2, right2} = surreal2

Expand Down
26 changes: 24 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,31 @@ defmodule Livebooks.MixProject do
]

def cli do
test_by_default = aliases() |> Keyword.keys() |> Map.new(&{&1, :test})
dev_by_default = aliases() |> Keyword.keys() |> Map.new(&{&1, :dev})

test_overrides =
[
:check,
:lint,
:"lint.compile",
:"lint.deps",
:"lint.format",
:"lint.style",
:typecheck,
:"typecheck.build-cache",
:"typecheck.clean",
:"typecheck.explain",
:"typecheck.run",
:"test.coverage",
:"test.coverage.report"
]
|> Map.new(&{&1, :test})

doc_overrides = [:build, :docs, :static] |> Map.new(&{&1, :docs})

preferred_envs =
test_by_default
dev_by_default
|> Map.merge(test_overrides)
|> Map.merge(doc_overrides)
|> Map.to_list()

Expand Down Expand Up @@ -151,6 +171,8 @@ defmodule Livebooks.MixProject do

defp deps(),
do: [
{:matcha, "~> 0.1", github: "christhekeele/matcha", branch: "latest"},
# Site generation
{:ex_doc, "~> 0.32", only: @doc_envs, runtime: false},
# Static analysis
{:credo, "~> 1.7", only: @dev_envs, runtime: false},
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
"makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.5", "e0ff5a7c708dda34311f7522a8758e23bfcd7d8d8068dc312b5eb41c6fd76eba", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "94d2e986428585a21516d7d7149781480013c56e30c6a233534bedf38867a59a"},
"matcha": {:git, "https://github.com/christhekeele/matcha.git", "cf65ed351be8b05427deb1d0c82af8f58ee12169", [branch: "latest"]},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"recon": {:hex, :recon, "2.5.5", "c108a4c406fa301a529151a3bb53158cadc4064ec0c5f99b03ddb8c0e4281bdf", [:mix, :rebar3], [], "hexpm", "632a6f447df7ccc1a4a10bdcfce71514412b16660fe59deca0fcf0aa3c054404"},
}

0 comments on commit 9b51996

Please sign in to comment.