Skip to content

Instantly share code, notes, and snippets.

View polvalente's full-sized avatar

Paulo Valente polvalente

View GitHub Profile
@polvalente
polvalente / einsum_to_tensordot.py
Created March 1, 2024 23:35
Equivalência de tensordot para einsum
In [1]: import torch
In [2]: a = torch.arange(3, 4, 5)
In [3]: a = torch.arange(60).reshape(3, 4, 5)
In [4]: b = torch.arange(24).reshape(1, 4, 3, 2)
# Aqui, (junto com o output da linha Out[7]) a gente vê que
# que o tensordot usando contraction axes específicas tem
# esse resultado.
@polvalente
polvalente / main.cpp
Created December 7, 2023 13:08
First Fibonnaci index with 1000 digits
#include <cmath>
#include <iostream>
#include <vector>
const uint64_t MAX_UINT64 = std::numeric_limits<uint64_t>::max();
uint64_t numDigits(std::vector<uint64_t> num) {
// num is described by num[0] + num[1] * MAX_UINT64 + num[2] * MAX_UINT64^2 + ...
if (num.empty()) return 0;
@polvalente
polvalente / code.exs
Created June 21, 2023 08:59
Nx (?!) solution for to-do app
# To-Do app
Mix.install [:exla, {:nx, github: "elixir-nx/nx", sparse: "nx", override: true}]
max_todo_length = 5
max_todos = 10
run = fn run_fn, state ->
case IO.gets("Choose one of the following options:\n1. List TO-DOs\n2. Add a new TO-DO\n3. Mark TO-DO as completed\n4. Delete TO-DO.\n5. Exit\n>> ") |> String.trim() do
"1" ->
todos = if state.count > 0, do: Nx.to_list(state.to_dos[0..(state.count - 1)]), else: []
@polvalente
polvalente / code.exs
Created June 21, 2023 00:21
code-golf with error handling solution
# To-Do app
run = fn run_fn, state ->
case IO.gets("Choose one of the following options:\n1. List TO-DOs\n2. Add a new TO-DO\n3. Mark TO-DO as completed\n4. Delete TO-DO.\n5. Exit\n>> ") |> String.trim() do
"1" ->
state.to_dos
|> Enum.reverse()
|> Enum.with_index()
|> Enum.map_join("\n", fn {%{todo: todo, completed: completed}, idx} ->
"#{idx}. #{todo} [#{if(completed, do: "✔", else: " ")}]"
@polvalente
polvalente / kmeans.livemd
Created November 27, 2021 01:41
K-means livebook example

K-means

Initialization

Mix.install([
  {:torchx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "torchx"},
  {:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true}
])
@polvalente
polvalente / custom_test.exs
Created January 13, 2021 22:41
Exemplos de setup e argumentos
defmodule TesteTest do
use ExUnit.Case
setup_all do
# we want all tests to use the same admin id
admin_id = Ecto.UUID.generate()
%{x: 1, admin_id: admin_id}
end
@polvalente
polvalente / effect_handling.exs
Created November 27, 2020 03:51
Effect handling with strategy + observer patterns
defmodule Effect do
defstruct subscribers: [], payload: nil
end
defmodule EffectHandler do
@callback handle(effect :: Effect.t()) :: :ok | {:error, reason :: term()}
defmacro __using__(effectful_function: [{effectful_function, 0}]) do
quote do
def main do
@polvalente
polvalente / observable.ex
Created November 22, 2020 23:39
Observable pattern implemented with metaprogramming
defmodule Observable do
defmacro __using__(opts) do
subscriber_modules = opts[:subscriber_modules]
function = opts[:observable_function]
tag = opts[:tag]
{f, _} = Code.eval_quoted(function)
arity = f |> :erlang.fun_info() |> Keyword.get(:arity)
@polvalente
polvalente / counter_generator.ex
Created November 8, 2020 03:51
Closures in Elixir (almost)
defmodule Incrementer do
@moduledoc """
We need to define this macro separately so we have a fully-qualified name
to call it in a stable manner. Otherwise, the module would have to recursively
(and infinitely) define this macro, which would in turn define the module itself.
"""
defmacro gen_module(value, state_holder_module_name) do
quote bind_quoted: [value: value, state_holder_module_name: state_holder_module_name] do
defmodule state_holder_module_name do
require Incrementer
@polvalente
polvalente / multi_bool_comp.ex
Created August 16, 2020 21:38
Multi-comparator expression parser
defmodule MultiBoolComp do
@moduledoc """
Defines an `sequential_bool` macro that parses a sequence of boolean comparisons in-line, such as:
`sequential_bool(a < b >= c == d <= e)` which would be equivalent to `a < b and b >= c and c == d and d <= e`
Only the native comparators are accepted (<, >, <=, >=, ==, !=, ===, !==)
### Examples:
iex> MultiBoolComp.sequential_bool(1 <= 4)
true