This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Generates a very opinionated `tasks.json` for Elixir projects. | |
# | |
# Usage (from the root of your project) | |
# | |
# mkdir .vscode | |
# cp .vscode/tasks.json .vscode/tasks.backup.json | |
# task-maker > .vscode/tasks.json | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule GenericTransform do | |
# Example: transform(list, &list_to_set/1) | |
@spec transform(map | list | term, (term -> term)) :: map | list | term | |
def transform(map, fun) when is_map(map) do | |
Enum.reduce(map, %{}, fn {k, v}, acc -> | |
acc | |
|> Map.put(k, transform(v, fun)) | |
|> fun.() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
foo = [[8, nil], [8, 99]] * 10_000_000 | |
Benchmark.bm(7) do |x| | |
x.report("to_i") do | |
foo.each { |(a, b)| [a, b.to_i].max } | |
end | |
x.report("compact") do | |
foo.each { |(a, b)| [a, b].compact.max } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule ExperimentBench do | |
use Benchfella | |
@list Enum.to_list(1..500_000) |> Enum.map(&to_string/1) | |
bench "IO.puts" do | |
file = File.open!("/tmp/puts.txt", [:write]) | |
Enum.each(@list, &(IO.puts file, &1)) | |
File.close(file) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule SillyExperiment do | |
def strip_and_reverse(s), do: s |> String.strip |> String.reverse | |
def all_stream do | |
File.stream!("/usr/share/dict/words") | |
|> Stream.map(&strip_and_reverse/1) | |
|> Stream.each(&IO.puts/1) | |
|> Stream.run | |
end |