Skip to content

Instantly share code, notes, and snippets.

defmodule LogLevel do
def to_label(level, legacy?) do
cond do
level == 0 and not legacy? -> :trace
level == 1 -> :debug
level == 2 -> :info
level == 3 -> :warning
level == 4 -> :error
level == 5 and not legacy? -> :fatal
true -> :unknown
@vKxni
vKxni / rate.ex
Created September 25, 2022 19:52
Returns a FL rate based on the time
defmodule Rates do
def daily_rate(hourly_rate) do
hourly_rate * 8.0
end
def apply_discount(before_discount, discount) do
before_discount * (100 - discount) / 100
end
def monthly_rate(hourly_rate, discount) do
@vKxni
vKxni / pac_man.ex
Created September 25, 2022 19:53
Rules of the PacMan Game
defmodule Rules do
def eat_ghost?(power_pellet_active, touching_ghost) do
power_pellet_active and touching_ghost
end
def score?(touching_power_pellet, touching_dot) do
touching_power_pellet or touching_dot
end
def lose?(power_pellet_active, touching_ghost) do
@vKxni
vKxni / a-reverse-bench.exs
Created September 25, 2022 19:56 — forked from evadne/a-reverse-bench.exs
Reversing Binaries in Elixir
defmodule Benchmarker do
def run(title, module, function, size \\ 1024, iterations \\ 100) do
times = for (_ <- 1 .. iterations) do
data = :crypto.strong_rand_bytes(size)
{duration, _value} = :timer.tc fn ->
apply(module, function, [data])
end
duration
end
@vKxni
vKxni / getAllFiles.ts
Created October 16, 2022 17:46
Recursively read all files in a directory
import fs from "fs";
import path from "path";
/**
* Recursively read all files in a directory.
* @param {fs.PathLike} dirPath The path to the directory that will be recursively traversed.
* @param {Array} arrayOfFiles The array that all files will be recursively pushed to.
* @returns Returns an array of files.
*/
@vKxni
vKxni / spam_pids.erl
Created November 18, 2022 21:08
A funny way to create 1000 processes
-module(spam_pids).
-export([main/0]).
main() ->
Pids = lists:map(fun(_) -> spawn(fun() -> ok end) end, lists:seq(1, 1000)),
lists:foreach(fun(Pid) -> exit(Pid, normal) end, Pids).
@vKxni
vKxni / list_sort.erl
Created November 18, 2022 21:09
Sorting Alg Vol. 1
-module(list_sort).
-export([main/0]).
main() ->
L = lists:map(fun(_) -> rand:uniform(1000) end, lists:seq(1, 1000)),
Sorted = bubble_sort(L),
io:format("~p~n", [Sorted]).
bubble_sort(L) ->
bubble_sort(L, []).
@vKxni
vKxni / ids.erl
Created November 18, 2022 21:09
Creates sorted IDs based on seq
-module(ids).
-export([main/0]).
main() ->
Pid = spawn(fun() ->
Ids = lists:map(fun(_) -> rand:uniform(1000) end, lists:seq(1, 1000)),
io:format("~p~n", [lists:sort(Ids)])
end),
exit(Pid, normal).
@vKxni
vKxni / generate.sql
Created December 8, 2022 19:08
MD5 PostgreSQL generation
select
left(md5(i::text), 10),
md5(random()::text),
md5(random()::text),
left(md5(random()::text), 4)
from generate_series(1, 1000000) s(i);
@vKxni
vKxni / controller.ex
Created December 10, 2022 11:43
Phoenix auto redirect to URL
defmodule Web.MainController do
use YourWeb, :controller
@moduledoc """
Triggered once a user enters the basic url, without endpoint.
Automatically redirects to the given URL.
"""
@github_url "https://github.com/vKxni"