Skip to content

Instantly share code, notes, and snippets.

@lukeledet
Last active June 3, 2020 13:35
Show Gist options
  • Save lukeledet/554ea994f71b9688179970669579efca to your computer and use it in GitHub Desktop.
Save lukeledet/554ea994f71b9688179970669579efca to your computer and use it in GitHub Desktop.
My iex.exs
IEx.configure(default_prompt: "%prefix>", alive_prompt: "%node>")
pbcopy = fn arg ->
port = Port.open({:spawn_executable, "/usr/bin/pbcopy"}, [])
Port.command(port, arg)
Port.close(port)
arg
end
pbpaste = fn ->
:os.cmd('pbpaste')
|> to_string
end
# iex> duplicates.([1,2,2,3,3,3,4,4,4,4])
# %{2 => 2, 3 => 3, 4 => 4}
duplicates = fn list ->
list
|> Enum.group_by(&(&1))
|> Enum.filter(fn {_k,v} -> Enum.count(v) > 1 end)
|> Enum.map(fn {k,v} -> {k, Enum.count(v)} end )
|> Enum.into(%{})
end
random_string = fn length ->
:crypto.strong_rand_bytes(length) |> Base.url_encode64 |> binary_part(0, length)
end
receive_if_available = fn ->
{_, queue_length} = :erlang.process_info(self(), :message_queue_len)
if queue_length > 0 do
receive do
x -> x
end
end
end
spawn_server = fn function, initial_state ->
spawn(fn ->
initial_state = if is_function(initial_state) do
initial_state.(self())
else
initial_state
end
Stream.unfold(initial_state, fn state ->
new_state = receive do
{:system, {_pid, _ref}, :get_state} -> IO.inspect(state, label: "State")
message -> function.(message, state)
end
{:ok, new_state}
end)
|> Stream.run()
end)
end
spawn_echo = fn ->
spawn_server.(fn message, _state -> IO.inspect(message, label: "Echo") end, nil)
end
spawn_echo_with_initial_state = fn initial_state ->
spawn_server.(fn message, _state -> IO.inspect(message, label: "Echo") end, initial_state)
end
spawn_counter = fn ->
spawn_server.(fn message, state ->
IO.inspect(message, label: state)
state + 1
end, 1)
end
spawn_blackhole = fn ->
spawn_server.(fn _, nil -> nil end, nil)
end
spawn_distributed_echo = fn node ->
Node.spawn_link node, fn ->
receive do
{module, function, arguments} -> apply(module, function, arguments)
function when is_function(function) -> apply(function, [])
end
end
end
get_state = fn
pid when is_pid(pid) -> :sys.get_state(pid)
other -> other |> Process.whereis() |> :sys.get_state()
end
to_sql = fn string ->
[query, params, _] = Regex.split(~r/(\[.*\])/, string, include_captures: true)
{params, _} = Code.eval_string(params)
Regex.replace(~r/\$(\d+)/, query, fn _, i ->
index = i |> String.to_integer() |> Kernel.-(1)
"#{Enum.at(params, index)}"
end)
end
defmodule Benchmark do
@doc """
Returns the number seconds it took to run the function
"""
def measure(function, times \\ 1) do
Enum.reduce(1..times, 0, fn _, acc ->
function
|> :timer.tc
|> elem(0)
|> Kernel./(1_000_000)
|> Kernel.+(acc)
end)
end
def compare(functions, times \\ 1) do
functions
|> Enum.with_index()
|> Enum.each(fn {f, i} ->
time = measure(f, times)
IO.puts "Function #{i} ran #{times} times in #{time} seconds"
end)
end
end
defmodule Runtime do
@moduledoc """
Helper functions related to the current runtime environment
"""
@doc """
Returns the top 10 processes using the most CPU
"""
def top() do
Process.list()
#|> Enum.map(fn pid -> Process.info(pid) end)
#|> Enum.sort_by(fn proc -> Keyword.fetch!(proc, :reductions) end)
|> Enum.sort_by(fn pid -> pid |> Process.info() |> Keyword.fetch!(:reductions) end)
|> Enum.reverse()
|> Enum.take(10)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment