Skip to content

Instantly share code, notes, and snippets.

@josevalim
josevalim / distributed-agent-demo.exs
Last active November 23, 2026 22:14
Distributed agent demo
# Usage: elixir demo.exs NAME
#
# elixir demo.exs alice
# elixir demo.exs bob
#
# Automatically connects to all other nodes registered in the local epmd.
Mix.install([{:req_llm, "~> 1.0"}])
defmodule Demo do
@josevalim
josevalim / bench.ex
Last active December 2, 2020 07:09
Mixed maps:intersect_with/3
small_int_list = Enum.to_list(1..10)
medium_int_list = Enum.to_list(1..1000)
large_int_list = Enum.to_list(1..100_000)
small_other_int_list = Enum.to_list(11..20)
medium_other_int_list = Enum.to_list(1001..2000)
large_other_int_list = Enum.to_list(100_001..200_000)
small_bin_list = Enum.map(1..10, fn _ -> :crypto.strong_rand_bytes(10) end)
medium_bin_list = Enum.map(1..1000, fn _ -> :crypto.strong_rand_bytes(10) end)
@josevalim
josevalim / authorize.ex
Last active April 29, 2018 09:16 — forked from jeremytregunna/authorize.ex
This looks horrible (formatter)
defmodule Api.Authorize do
alias Account.Models.User
def board?(board_id, %User{} = user, available_permissions, required_permissions) do
with(
{:ok, board} <- Tracker.Board.find(board_id),
true <- Tracker.OrganizationMembers.user_in_organization?(user.id, board.organization_id),
^required_permissions <-
Enum.to_list(
MapSet.intersection(
@josevalim
josevalim / keybase.md
Last active January 24, 2017 23:24
keybase.md

Keybase proof

I hereby claim:

  • I am josevalim on github.
  • I am josevalim (https://keybase.io/josevalim) on keybase.
  • I have a public key whose fingerprint is E140 099F 114F 4B2A 21FE 2E69 E337 0FF7 D467 E599

To claim this, I am signing this object:

@josevalim
josevalim / watcher.sh
Last active May 22, 2024 10:06
A 1LOC bash script for re-running tests whenever a lib/ or test/ file changes keeping the same VM instance
# You will need fswatch installed (available in homebrew and friends)
# The command below will run tests and wait until fswatch writes something.
# The --stale flag will only run stale entries, it requires Elixir v1.3.
fswatch lib/ test/ | mix test --stale --listen-on-stdin
$ git clone git@github.com:elixircnx/comeonin.git
$ cd comeonin/
$ git checkout develop
$ mix deps.get
$ mix test
@josevalim
josevalim / mix app.tree
Last active July 20, 2018 15:48
Sample output of the new "mix app.tree" and "mix deps.tree" tasks in a Phoenix project (using master). The first command shows the applications you will effectively need at runtime in production while "deps.tree" is your whole dependency tree, including compile time, dev and test dependencies.
$ mix app.tree
demo
├── elixir
├── phoenix
│ ├── elixir
│ ├── plug
│ │ ├── elixir
│ │ ├── crypto
│ │ └── logger
│ │ └── elixir
@josevalim
josevalim / phoenix showdown rackspace onmetal io.md
Last active October 31, 2022 13:59 — forked from omnibs/phoenix showdown rackspace onmetal io.md
Phoenix Showdown Comparative Benchmarks @ Rackspace

Comparative Benchmark Numbers @ Rackspace

I've taken the benchmarks from Matthew Rothenberg's phoenix-showdown, updated Phoenix to 0.13.1 and ran the tests on the most powerful machines available at Rackspace.

Results

Framework Throughput (req/s) Latency (ms) Consistency (σ ms)
defmodule SomeModule do
def start(host, options) do
# We are assuming SomeSup has been already started somewhere
# (ideally in a supervision tree).
{:ok, child_pid} = SomeSup.attach_to(SomeSup, host, options)
end
end
defmodule SomeSup do
use Supervisor
%% When compiled with: erlc +bin_opt_info foo.erl
%%
%% It prints:
%%
%% foo.erl:13: Warning: NOT OPTIMIZED: compiler limitation: instruction {get_map_elements,
%% {f,3},
%% {x,1},
%% {list,[{atom,state},{x,6}]}} prevents delayed sub binary optimization
-module(foo).