Skip to content

Instantly share code, notes, and snippets.

View eat.exs
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 Dec 11, 2017
A not so coll rollback user
View my_user.rb
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 Dec 11, 2017
nested transactions requires_new api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
View nested_transaction_requires_new.rb
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 Dec 11, 2017
Taken from the nested transaction section of api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
View nested_transactions.rb
User.transaction do
User.create(name: 'Kotori')
User.transaction do
User.create(name: 'Nemu')
raise ActiveRecord::Rollback
end
end
View why_comment.rb
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 Nov 12, 2017
Ridiculous parameters
View parameters.rb
# context, outlet, times, time per step, state, data
def pattern(c, o, t, l, s, d)
# ...
end
@pragtobgists
pragtobgists / separating_comments.rb
Created Nov 12, 2017
Comments separating a method
View separating_comments.rb
# do one thing
...
...
...
...
# do another thing
...
...
...
View benchee_hound.exs
# 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
View benchee_scenario.ex
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`).
"""
View multiple_inputs.exs
map_fun = fn(i) -> [i, i * i] end
Benchee.run(%{
"flat_map" => fn(input) -> Enum.flat_map(input, map_fun) end,
"map.flatten" => fn(input) -> input |> Enum.map(map_fun) |> List.flatten end
},
inputs: %{
"Small" => Enum.to_list(1..1000),
"Bigger" => Enum.to_list(1..100_000)
})