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 ba52acb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 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.
51 changes: 51 additions & 0 deletions lib/livebook/helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule Livebook.Helpers do
@moduledoc """
Common helpers for these livebooks.
"""

defmacro __using__(_opts \\ []) do
quote do
import unquote(__MODULE__), only: [defmodule: 3]
Code.put_compiler_option(:ignore_module_conflict, true)
unquote(__MODULE__).start_patching_modules()
end
end

@doc """
Inspired by [this macro](https://github.com/brettbeatty/experiments_elixir/blob/master/module_patching.livemd).
"""
defmacro defmodule(alias, version, do_block) do
[do: block] = do_block
module = Macro.expand(alias, __CALLER__)
version = Macro.expand(version, __CALLER__)
store_block(module, version, block)

quote do
defmodule unquote(alias) do
(unquote_splicing(patches_so_far(module, version)))
end
end
end

defp store_block(module, version, block) do
:ets.insert(__MODULE__, {{module, version}, block})
end

defp patches_so_far(module, version) do
__MODULE__
|> :ets.select([{{{module, :"$1"}, :"$2"}, [{:"=<", :"$1", version}], [:"$2"]}])
|> Enum.flat_map(&unblock/1)
end

defp unblock(ast)
defp unblock({:__block__, _meta, block}), do: block
defp unblock(ast), do: [ast]

def start_patching_modules do
with :undefined <- :ets.whereis(__MODULE__) do
__MODULE__ = :ets.new(__MODULE__, [:ordered_set, :named_table])
end

:ok
end
end
3 changes: 0 additions & 3 deletions lib/livebooks.ex

This file was deleted.

34 changes: 24 additions & 10 deletions livebooks/surreal.livemd
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
# 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
```

## Caching

```elixir
require Logger

defmodule Surreal.Guards do
Expand Down Expand Up @@ -50,10 +68,6 @@ end
Surreal.Cache.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,7 +110,7 @@ five_eighths = {[one_half], [three_quarters]}
## Module

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

Expand All @@ -108,7 +122,7 @@ end
## Set Concatenation

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

Expand Down

0 comments on commit ba52acb

Please sign in to comment.