Skip to content

Instantly share code, notes, and snippets.

View dkuku's full-sized avatar

Daniel Kukula dkuku

View GitHub Profile
@dkuku
dkuku / struct_access.ex
Created April 14, 2023 15:08
Elixir struct Access helper
get_in_struct(User, [:addresses, Access.at(0), :street])
def get_in_struct(nil, _fields), do: nil
def get_in_struct(model, []), do: model
def get_in_struct(model, [h | t]) when is_atom(h), do: get_in_struct(Map.get(model, h), t)
def get_in_struct(model, [h | t]), do: get_in_struct(h.(:get, model, & &1), t)
@dkuku
dkuku / latexify.ex
Created January 27, 2023 18:59
generate latex charts from elixir code in livebook
defmodule Latexify do
@leftb "\\mathopen{}\\left("
@rightb "\\mathclose{}\\right)"
def run(int) when is_integer(int), do: int
def run(float) when is_float(float), do: float
def run({atom, [], Elixir}) when is_atom(atom), do: "#{atom}"
def run({:-, _, [a]}), do: "{-#{run(a)}}"
def run({:-, _, [a, b]}), do: "{#{run(a)} - #{run(b)}}"
def run({:+, _, [a, b]}), do: "#{run(a)} + #{run(b)}"
def run({:*, _, [a, b]}), do: "{#{run(a)}*#{run(b)}}"
@dkuku
dkuku / underscore.ex
Created January 8, 2023 11:25
convert sql from camel case to underscore
defmodule Underscore do
@special_char [?\t, ?\s, ?:, ?;, ?", ?=, ?', ?., ?-, ?/, ?_, ?(, ?), ?,, ?|]
@spec underscore(module() | atom() | String.t()) :: String.t()
def underscore(atom_or_string)
def underscore(atom) when is_atom(atom) do
"Elixir." <> rest = Atom.to_string(atom)
underscore(rest)
end
@dkuku
dkuku / tree.ex
Last active December 6, 2022 20:43
list elixir submodules and functions
tree = fn module_name ->
:code.all_loaded()
|> Enum.map(&(elem(&1, 0) |> to_string))
|> Enum.filter(&String.starts_with?(&1, to_string(module_name)))
|> Enum.sort()
|> Enum.each(fn module ->
module_name = String.replace_leading(module, "Elixir.", "")
dots_count = module_name |> String.split(".") |> Enum.count() |> Kernel.-(1)
pad = " "
padding = String.duplicate(pad, dots_count)
# coding: utf-8
# pip install wheel torch transformers sacremoses sentencepiece
import sys, os
from transformers import MarianTokenizer, MarianMTModel
from struct import unpack, pack
class Translator:
def __init__(self, model):
defp log_stacktrace do
{:current_stacktrace, stacktrace} =
Process.info(self(), :current_stacktrace)
Logger.info(Exception.format_stacktrace(stacktrace))
end
# Neural network from scratch in ... Elixir
## Pure Elixir initial version test
## First layer using NX
```elixir
Mix.install([
{:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true}
])

Neural network from scratch in ... Elixir

Pure Elixir initial version test

inputs = [1, 2, 3, 2.5]
weights = [0.2, 0.8, -0.5, 1.0]
bias = 2.0
data = for num <- 1..map_size, reduce: %{} do
acc -> Map.put(acc, num, num)
end
enum = fn -> data |> Enum.map(fn {k, v} -> {k, to_string(v)} end) |> Map.new() end
map = fn -> :maps.map(fn k, v -> to_string(v) end, data) end
Benchee.run(%{
"map" => map,
"enum" => enum
},
defmodule RegexPmBenchmark do
@moduledoc """
Documentation for `RegexPmBenchmark`.
"""
@uppercase ?A..?Z
@lowercase ?a..?z
def valid_language_re?(language) do
Regex.match?(~r/^([a-zA-Z]{2}|[a-zA-Z]{2}-[a-zA-Z]{2})$/, language)
end
def valid_re_list(language) do