Skip to content

Instantly share code, notes, and snippets.

@wstucco
wstucco / build.sh
Last active October 12, 2015 00:55
Writing Ruby Extensions in go
go build -buildmode=c-shared -o hello.so hello.go
# if no error is returned you can check that the shared library is exporting
# the right symbols by executing
# $ nm -gU ./hello.so | grep hello
# 0000000000002050 T __cgoexp_d4a435ec6890_hello_world
# 0000000000001ae0 T _hello_world <--- ALL SYSTEMS ARE GO
@wstucco
wstucco / find_similar_ip.ex
Last active July 21, 2017 09:51
most similar ip Elixir
{:ok, ips} = :inet.getif()
ips
|> Enum.map(fn {ip, _broadaddr, _mask} -> ip |> Tuple.to_list |> Enum.map_join(".", &to_string/1) end)
|> Enum.map(fn ip -> {ip, String.jaro_distance(ip, "192.168.99.100")} end)
|> Enum.sort(fn {_, score1}, {_, score2} -> score1 > score2 end)
|> List.first
|> elem(0)
@wstucco
wstucco / one_million_processes.exs
Last active September 27, 2017 15:18
One million threads VS one million Erlang processes
defmodule Processes do
@compile :native
def create_and_forget do
1..1_000_000
|> Enum.each(fn x ->
Task.async(fn -> x + 1 end) |> Task.await
end)
IO.puts("Maximum number of threads per process is = 1000000")
defmodule A do
use GenServer
def start do
GenServer.start_link(__MODULE__, [])
end
def send_after(pid) do
1..10
|> Enum.each(fn _ -> Process.send_after(pid, :message, 1_000) end)
defmodule App do
def bench do
:timer.tc(fn -> start() end)
end
defp start do
run()
wait()
end
@wstucco
wstucco / NumberParser.ex
Created March 19, 2020 16:34
NimbleParser excercise: parse numbers in "it_IT" locale format
defmodule NumberParser do
import NimbleParsec
@moduledoc """
In most European countries decimal separator is the , (comma)
This is a simple parser for numbers formatted that way
"""
nat = integer(min: 1)
sep = string(",")