inputs = [1, 2, 3, 2.5]
weights = [0.2, 0.8, -0.5, 1.0]
bias = 2.0
This file contains 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
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) |
This file contains 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 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)}}" |
This file contains 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 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 |
This file contains 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
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) |
This file contains 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
# 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): |
This file contains 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
defp log_stacktrace do | |
{:current_stacktrace, stacktrace} = | |
Process.info(self(), :current_stacktrace) | |
Logger.info(Exception.format_stacktrace(stacktrace)) | |
end |
This file contains 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
# 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} | |
]) |
This file contains 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
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 | |
}, |
This file contains 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 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 |