Last active
September 26, 2023 19:59
-
-
Save Youenn-Bouglouan/0a689f6b8828a3424ab74b0d8bd7ac2a to your computer and use it in GitHub Desktop.
Some useful Elixir iex console stuff
This file contains hidden or 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 Utils do | |
def logw(content) do | |
write("iex_output.exs", content, [:write]) | |
content | |
end | |
def loga(content) do | |
write("iex_output.exs", content, [:append], 1) | |
content | |
end | |
def write_text(content, filename) do | |
File.write(filename, content, [:write]) | |
end | |
defp write(filename, content, options, padding_lines \\ 0) do | |
padding = | |
Enum.map(0..padding_lines, fn _x -> "\n" end) | |
|> Enum.reduce("", fn x, acc -> acc <> x end) | |
File.write( | |
filename, | |
Kernel.inspect(content, pretty: true, limit: :infinity) <> padding, | |
options | |
) | |
end | |
end | |
defmodule Benchmark do | |
def measure(function) do | |
function | |
|> :timer.tc() | |
|> elem(0) | |
|> Kernel./(1_000_000) | |
end | |
end | |
import Utils | |
import Benchmark |
This file contains hidden or 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
:observer.start() | |
Process.whereis(MyElixirModule) | |
Process.info( | |
Process.whereis(MyElixirModule), | |
:message_queue_len | |
) | |
:sys.get_state(MyElixirAgentModule) |
This file contains hidden or 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 TestErrors do | |
def test_catch_1_param(mode) do | |
case mode do | |
"throw" -> throw("uh oh") | |
"raise" -> raise ArgumentError, message: "uh oh" | |
"match_error" -> 1 = 2 | |
end | |
catch | |
error -> | |
IO.inspect(error, label: "ERROR CAUGHT OK") | |
IO.inspect(mode, label: "MODE") | |
end | |
def test_catch_2_params(mode) do | |
case mode do | |
"throw" -> throw("uh oh") | |
"raise" -> raise ArgumentError, message: "uh oh" | |
"match_error" -> 1 = 2 | |
end | |
catch | |
kind, error -> | |
IO.inspect(kind, label: "KIND CAUGHT OK") | |
IO.inspect(error, label: "ERROR CAUGHT OK") | |
IO.inspect(mode, label: "MODE") | |
end | |
def test_raise(mode) do | |
case mode do | |
"throw" -> throw("uh oh") | |
"raise" -> raise ArgumentError, message: "uh oh" | |
"match_error" -> 1 = 2 | |
end | |
rescue | |
# rescue will only accept 1 param (compilation error otherwise). | |
error -> | |
IO.inspect(error, label: "ERROR CAUGHT OK") | |
IO.inspect(mode, label: "MODE") | |
end | |
end | |
TestErrors.test_catch_1_param("throw") # OK, catches the error thrown. | |
TestErrors.test_catch_1_param("raise") # NOK, does NOT catch the error raised. | |
TestErrors.test_catch_1_param("match_error") # NOK, does NOT catch the match error. | |
TestErrors.test_catch_2_params("throw") # OK, catches the error thrown. | |
TestErrors.test_catch_2_params("raise") # OK, catches the error raised. | |
TestErrors.test_catch_2_params("match_error") # OK, catches the match error (returns {:badmatch, 2}). | |
TestErrors.test_raise("throw") # NOK, does NOT rescue the error thrown. | |
TestErrors.test_raise("raise") # OK, rescues the error raised. | |
TestErrors.test_raise("match_error") # OK, catches the match error (returns MatchError). |
This file contains hidden or 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 SqlHelpers do | |
def sql_results_to_map(%Postgrex.Result{columns: _, rows: nil}, _id_fields), do: [] | |
def sql_results_to_map(%Postgrex.Result{columns: col_nms, rows: rows}, id_fields) when is_list(id_fields) do | |
Enum.map(rows, fn row -> | |
col_nms | |
|> sql_row_to_map(row) | |
|> sql_binary_ids_to_string(id_fields) | |
end) | |
end | |
defp sql_row_to_map(col_nms, vals) do | |
Stream.zip(col_nms, vals) | |
|> Enum.into(Map.new(), fn {k, v} -> {String.to_atom(k), v} end) | |
end | |
defp sql_binary_ids_to_string(map, id_fields) when is_map(map) and is_list(id_fields) do | |
Enum.reduce(id_fields, map, fn id_field, acc -> | |
{:ok, string_id} = Ecto.UUID.cast(Map.get(map, id_field)) | |
Map.put(acc, id_field, string_id) | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment