Skip to content

Instantly share code, notes, and snippets.

View Arkham's full-sized avatar
🏠
Working from home

Ju Liu Arkham

🏠
Working from home
View GitHub Profile
defmodule Enchainer.Command do
@callback do_call(term) :: {:ok, term} | {:error, term}
@callback on_success(term, term) :: term
@callback on_failure(term, term) :: term
@callback on_revert(term) :: term
defmacro __using__(_params) do
quote do
@behaviour Enchainer.Command
defmodule Taskie do
def pbump(n) do
chunk_size = div(n, 10)
1..n
|> Enum.chunk(chunk_size, chunk_size, [])
|> Enum.map(fn(chunk) ->
chunk
|> Enum.map(fn(v) -> Task.async(fn() -> Process.sleep(1000); bump(v) end)end)
|> Enum.map(fn(t) -> Task.await(t) end)
end)
defmodule CoffeeFsm do
@name :coffee_fsm
@timeout 30_000
def start_link do
:gen_fsm.start_link({:local, @name}, __MODULE__, [], [])
end
def init([]) do
Hw.reboot()
defmodule CoffeeMachine do
@name :coffee_machine
def start_link do
spawn_link(__MODULE__, :init, [])
end
def init do
Process.register(self, @name)
Hw.reboot()
defmodule MyDb do
## Public API
def start do
pid = spawn(__MODULE__, :init, [])
Process.register(pid, :my_db)
{:ok, pid}
end
def stop, do: cast(:stop)
defmodule MutexTest do
use ExUnit.Case, async: false
doctest Mutex
setup do
Mutex.start
on_exit fn ->
Mutex.stop
end
end
defmodule Db do
def new, do: []
def destroy(_db), do: :ok
def write([], key, element) do
[{key, element}]
end
def write([{key, _value} | db], key, element) do
[{key, element} | db]
defmodule Frequency do
def start() do
pid = spawn(Frequency, :init, [])
Process.register(pid, :frequency)
end
def init() do
frequencies = {get_frequencies(), []}
loop(frequencies)
defmodule ProcessRing do
@moduledoc """
Documentation for ProcessRing.
"""
def start(name) do
spawn(ProcessRing, :init, [name])
end
def init(name) do
@Arkham
Arkham / 1 - intro.rb
Last active April 26, 2019 14:51
Sonic PI experiments
# HI! I'm Ju. You can find me online as @arkh4m
# Download Sonic PI here: http://sonic-pi.net
def p(note, s: 1)
play note, release: s, cutoff: rrand(80,110)
sleep s
end
def fra_martino