Skip to content

Instantly share code, notes, and snippets.

benchmark: reduce(), filter(), map(), transducers and for loops

reduce()        10            x 574,156 ops/sec ±1.34% (86 runs sampled)
reduce()       100            x 42,746 ops/sec ±1.99% (88 runs sampled)
reduce()     1,000            x 781 ops/sec ±1.61% (88 runs sampled)

filter() & map()        10    x 128,652 ops/sec ±1.21% (87 runs sampled)
filter() & map()       100    x 9,537 ops/sec ±1.22% (90 runs sampled)
filter() & map()     1,000    x 256 ops/sec ±1.07% (81 runs sampled)

filter() & map() 10,000 x 67.54 ops/sec ±2.56% (68 runs sampled)

def combos(items, k, s = 0, combo = [])
return [combo] if k == 0
(s .. items.size - k).flat_map do |i|
combos(items, k - 1, i + 1, combo + [items[i]])
end
end
combos([:a, :b, :c, :d, :e, :f], 3).each do |combo|
p combo
input_path = File.expand_path('../input.txt', __FILE__)
masses = File.read(input_path).lines.map(&:to_i)
def fuel(mass)
(mass / 3.0).floor - 2
end
p masses.reduce(0) { |s, m| s + fuel(m) }
def required_fuel(init)

Rust types reference

Prelude

  • std::borrow::ToOwned
  • std::boxed::Box
  • std::clone::Clone
  • std::cmp::{PartialEq, PartialOrd, Eq, Ord}
  • std::convert::{AsRef, AsMut, Into, From}
  • std::default::Default
@jcoglan
jcoglan / blame.rb
Last active February 13, 2019 15:20
require_relative "./base"
require_relative "../diff"
require_relative "../rev_list"
module Command
class Blame < Base
def run
@lines = []
@commits = []
require "set"
mods = Class.ancestors +
Method.ancestors +
UnboundMethod.ancestors
reserved = %i[
==
===
=~
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o---o o o---o o o o o o o o o o o o o o o o o o o o
| | | |
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o---o---
#!/bin/bash
cd ~/src
dir="git-long-uninteresting-branch"
git init $dir > /dev/null
cd $dir
export GIT_COMMITTER_DATE="1600000000"
.................................................................................................|...............................................................#....#..................
........#........................................................................................|...............................................................#....#..................
........#.............#..........................................................................|...............................................................#....#..................
........#.............#..........................................................................|...............................................................#....#..................
........#.............#..........................................................................|...#....#......................................................#....#..................
........#.............#.......................................................................
module Pack
module Numbers
module VarIntLE
def self.write(size, shift)
bytes = []
mask = 2 ** shift - 1
until size <= mask
bytes << (0x80 | size & mask)