Skip to content

Instantly share code, notes, and snippets.

View itarato's full-sized avatar

Peter Arato itarato

  • Montreal, Canada
  • 02:55 (UTC -04:00)
View GitHub Profile
@itarato
itarato / Screenshot 2024-09-02 212704.png
Last active September 3, 2024 01:28
Slider puzzle generator (crap)
Screenshot 2024-09-02 212704.png
@itarato
itarato / parallel_topo_batch_runner.rs
Created July 2, 2024 00:43
Multithreaded topological batch runner example
use std::{
collections::{HashMap, HashSet, VecDeque},
sync::{Arc, Mutex},
thread,
time::Duration,
};
#[derive(Debug)]
struct TopologicalBatchProvider {
unavailable: HashSet<usize>,
@itarato
itarato / poorman_ruby_spy.rb
Last active April 24, 2024 19:57
Poorman's Ruby Spy
class Foo
def bar(val, key:)
puts("BAR #{val} #{key}")
end
end
def spy_on(object, method)
did_call = false
aliased = "#{method}_spied".to_sym
@itarato
itarato / sorbet_compatible_test_init.md
Last active April 8, 2024 20:09
Use Sorbet compatible setup-initializations in tests

Use Sorbet compatible setup-initializations in tests

Sorbet can help with type inference in tests if the setup is right. Otherwise types will be reduced to T.untyped.

Don't do

class Test < ActiveSupport::TestCase
  def setup # Don't use `dev setup`.
 @user = make_fixture(User, :alex) # Don't forget `T.let`
@itarato
itarato / denormalized_data_mock_builder_tip.md
Last active April 5, 2024 17:33
Tip on how to make a test mock builder for composite denormalized structures

Tip for test mock builders for denormalized attributes

(This is just one recommendation - and doesn't talk about error-free data structure design.)

Let's say there is a Project class that needs a convenience builder for testing. This Project consist of a company who owns the project and a list of tickets - where a ticket is just a name and the same company denormalized (for reasons). Since this company attribute is just a denormalization - it is/must-be consistent.

Don't do

@itarato
itarato / 1brc.rs
Created March 29, 2024 21:11
1 Billion Record Challenge
/*
* The task is to write a ~Java~ Rust program which reads the file, calculates the
* min, mean, and max temperature value per weather station, and emits the results
* on stdout like this (i.e. sorted alphabetically by station name, and the result
* values per station in the format <min>/<mean>/<max>, rounded to one fractional digit):
*
* {
* Abha=-23.0/18.0/59.2,
* Abidjan=-16.2/26.0/67.3,
* Abéché=-10.0/29.4/69.0,
# Terminal Tetris
#
# Usage:
#
# ```bash
# ruby tetris.rb <WIDTH> <HEIGHT> <SPEED>
# ```
#
# Control:
# - a: left
@itarato
itarato / note_to_dot.rb
Last active October 4, 2023 20:16
Notes to Dot converter
note = File.read(ARGV[0])
gid = 1
prev_indent = 0
stack = [0]
labels = ["Root"]
edges = []
note.lines.map { _1.delete!("\n") }.map do |line|
@itarato
itarato / io_reopen.md
Last active June 17, 2023 17:34
Ruby's IO#reopen - thoughts

IO#reopen and its surprising side effect

This is a short debugging story that led to a few learnings. Our team is working on making TruffleRuby support one of our larger internal Ruby application. Running tests in CI we've noticed a flaky test failure reporting:

undefined method 'write_without_cli_ui' for #<IO:<STDOUT>> (NoMethodError)

Debugging

@itarato
itarato / pipe_write_full.c
Created June 3, 2023 15:23
Demonstrating a Posix pipe writer being full and not being emptied
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#define PIPE_MAX (1 << 16)
int main() {
int pipefd[2];