Skip to content

Instantly share code, notes, and snippets.

View ostinelli's full-sized avatar

Roberto Ostinelli ostinelli

View GitHub Profile
@ostinelli
ostinelli / 1_searchbar_live.ex
Created May 2, 2024 12:40 — forked from caspg/1_searchbar_live.ex
Example of real-time search bar implementation in Phoenix LiveView and Tailwind. Working example on https://travelermap.net/parks/usa
defmodule TravelerWeb.SearchbarLive do
use TravelerWeb, :live_view
alias Phoenix.LiveView.JS
alias Traveler.Places
def mount(_params, _session, socket) do
socket = assign(socket, places: [])
{:ok, socket, layout: false}
end
@ostinelli
ostinelli / block.ex
Created February 28, 2023 10:41
Calling a block passed in a macro in Elixir
defmodule Foo do
defmacro foo(do: block) do
quote do
res = unquote(block)
IO.inspect(res)
end
end
end
defmodule Use do
@ostinelli
ostinelli / worker.ex
Last active August 11, 2022 17:43
Elixir DynamicSupervisor with registered worker ids (local registry, use :syn for global workers instead)
defmodule MyApp.Worker do
use GenServer
# api
@spec start_link(worker_id :: String.t()) :: GenServer.on_start()
def start_link(worker_id) do
GenServer.start_link(__MODULE__, [worker_id], name: via_tuple(worker_id))
end
@ostinelli
ostinelli / dvvset_test.erl
Last active December 17, 2021 12:12
Dotted Version Vector Sets example on conflict detection
-module(dvvset_test).
-export([main/0]).
%% NOTE: we always generate a new client ID to enforce consistency.
main() ->
%% client 1 write
Dot0 = dvvset:update(dvvset:new("value-0"), generate_id()),
%% server
@ostinelli
ostinelli / kv_bench.erl
Last active October 23, 2021 11:20
Erlang take values from KV stores (dict vs orddict vs maps vs gb_sets vs ets set vs ets bag benchmark)
-module(kv_bench).
-compile([export_all]).
main(Count) ->
Values = lists:foldl(fun(C, Acc) ->
[{C, {v, C}} | Acc]
end, [], lists:seq(1, Count)),
{TimeDict, ResultDict} = timer:tc(?MODULE, get_last_dict, [Count, Values]),
@ostinelli
ostinelli / list_bench.erl
Last active October 23, 2021 11:20
Erlang delete double values in arrays (dict vs orddict vs sets vs ordsets vs maps vs gb_sets benchmark)
-module(list_bench).
-compile([export_all]).
main(Count) ->
Values = lists:foldl(fun(C, Acc) ->
[C, C, C, C, C, C, C, C, C, C | Acc]
end, [], lists:seq(1, Count)),
{TimeDict0, ResultDict} = timer:tc(?MODULE, count_dict, [Values]),
@ostinelli
ostinelli / rsa_openssl_for_python.md
Last active April 16, 2024 07:05
RSA Private / Public key pair with openssl & Python

RSA Private / Pubic key pair

To generate a private / public RSA key pair, you can either use openssl, like so:

$ openssl genrsa -out private.pem 4096  
$ openssl rsa -in private.pem -outform PEM -pubout -out public.pem  

Or, you can use the following python script:

@ostinelli
ostinelli / [email protected]
Last active December 2, 2019 13:46
How to install erlang 19 on OSX
# download erlang 19: <http://erlang.org/download/otp_src_19.3.tar.gz> & untar
# download patch: <https://github.com/erlang/otp/commit/a64c4d806fa54848c35632114585ad82b98712e8.diff>
# apply patch
patch < a64c4d806fa54848c35632114585ad82b98712e8.diff
# ensure that openssl is installed in [email protected]/{version}
brew install openssl
# compile
@ostinelli
ostinelli / intellij
Last active November 7, 2019 15:21
[Intellij] Run mix tests in open terminal
Program: /Users/roberto/workspace/intellij_run_cmd
Arguments: "mix test $FilePathRelativeToProjectRoot$:$SelectionEndLine$"
@ostinelli
ostinelli / biggest_file_by_ext.rb
Last active September 5, 2019 19:17
List biggest file size by extension on a directory.
require 'find'
biggest_file_by_ext = {}
Find.find('.') do |path|
next if File.directory?(path)
# get file info
ext = File.extname(path)
next unless ext.length > 0
# get file size
size = File.size(path)