Skip to content

Instantly share code, notes, and snippets.

#!/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
#
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.()
@jdfrens
jdfrens / foo.rb
Last active April 27, 2018 14:43
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 }
@jdfrens
jdfrens / newline_bench.exs
Last active April 27, 2018 14:44
The results may surprise you!
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
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