Skip to content

Instantly share code, notes, and snippets.

list = Enum.to_list(1..10_000)
map_fun = fn(i) -> [i, i * i] end
Bunny.eat(%{
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten end
})
@pragtobgists
pragtobgists / my_user.rb
Created December 11, 2017 21:52
A not so coll rollback user
class User < ApplicationRecord
attr_accessor :rollback
after_save :potentially_rollback
def potentially_rollback
raise ActiveRecord::Rollback if rollback
end
end
@pragtobgists
pragtobgists / nested_transaction_requires_new.rb
Created December 11, 2017 21:41
nested transactions requires_new api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
User.transaction do
User.create(name: 'Kotori')
User.transaction(requires_new: true) do
User.create(name: 'Nemu')
raise ActiveRecord::Rollback
end
end
@pragtobgists
pragtobgists / nested_transactions.rb
Created December 11, 2017 21:29
Taken from the nested transaction section of api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
User.transaction do
User.create(name: 'Kotori')
User.transaction do
User.create(name: 'Nemu')
raise ActiveRecord::Rollback
end
end
@pragtobgists
pragtobgists / why_comment.rb
Last active November 13, 2017 20:08
WHY comment
def paint_control(event)
# some painting code
rescue => e
# Really important to rescue here. Failures that escape this method
# cause odd-ball hangs with no stacktraces. See #559 for an example.
puts "SWALLOWED PAINT EXCEPTION ON #{@obj} - go take care of it: " + e.to_s
puts 'Unfortunately we have to swallow it because it causes odd failures :('
end
@pragtobgists
pragtobgists / parameters.rb
Created November 12, 2017 20:38
Ridiculous parameters
# context, outlet, times, time per step, state, data
def pattern(c, o, t, l, s, d)
# ...
end
@pragtobgists
pragtobgists / separating_comments.rb
Created November 12, 2017 20:35
Comments separating a method
# do one thing
...
...
...
...
# do another thing
...
...
...
@pragtobgists
pragtobgists / benchee_hound.exs
Last active October 23, 2017 18:37
Benchee Hound
# ATTENTION: gotta start phantomjs via `phantomjs --wd` first..
Application.ensure_all_started(:hound)
{:ok, server} = SimpleServer.start
Application.put_env(:hound, :app_host, "http://localhost")
Application.put_env(:hound, :app_port, SimpleServer.port(server))
use Hound.Helpers
@pragtobgists
pragtobgists / benchee_scenario.ex
Created October 23, 2017 18:09
Benchee Scenario type
defmodule Benchee.Benchmark.Scenario do
@moduledoc """
A Scenario in Benchee is a particular case of a whole benchmarking suite. That
is the combination of a particular function to benchmark (`job_name` and
`function`) in combination with a specific input (`input_name` and `input`).
It then gathers all data measured for this particular combination during
`Benchee.Benchmark.measure/3` (`run_times` and `memory_usages`),
which are then used later in the process by `Benchee.Statistics` to compute
the relevant statistics (`run_time_statistics` and `memory_usage_statistics`).
"""
@pragtobgists
pragtobgists / my_map_bench.exs
Last active February 26, 2017 10:35
Benchmark of different map implementations
list = Enum.to_list(1..10_000)
map_fun = fn(i) -> i + 1 end
Benchee.run %{
"map tail-recursive with ++" =>
fn -> MyMap.map_tco_concat(list, map_fun) end,
"map with TCO reverse" =>
fn -> MyMap.map_tco(list, map_fun) end,
"stdlib map" =>
fn -> Enum.map(list, map_fun) end,