This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std/os | |
type | |
Shape = enum Rock, Paper, Scissors | |
GameResult = range[-1..1] | |
func toShape(shapeChr: char): Shape = | |
case shapeChr: | |
of 'A', 'X': Rock | |
of 'B', 'Y': Paper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std/os | |
import std/strutils | |
var max = 0 | |
var partial = 0 | |
for line in lines(paramStr(1)): | |
if len(line) != 0: | |
partial += parseInt(line) | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Simulation | |
# Clocks in a Linux system | |
# ======================== | |
# | |
# A Linux system has two clocks: The hardware clock, and the system clock. | |
# | |
# The hardware clock, also known as RTC or real-time clock, is a physical | |
# clock. This clock runs always, even when the system is shut down or | |
# unplugged the clock keeps running because it has an independent source of | |
# power, normally a lithium battery. See |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 3004953 | |
start = n/2 + 1 | |
play = [*start..n, *1...start] | |
while n > 1 | |
play.shift | |
play << play.shift if n.odd? | |
n -= 1 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We use a circular double-linked list, and two pointers: one to the current player, | |
// and another one to their target, the player across the circle. | |
// | |
// By using a cicular list, API is intuitive. | |
// | |
// By using a double-linked list, deletion is cheap. | |
// | |
// By maintaining both pointers in each turn, we avoid seeks. | |
const ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mandelbrot set generator from "Programming Rust", with I/O removed (the | |
// original code writes a PNG file). | |
use std::env; | |
use std::str::FromStr; | |
use num::Complex; | |
fn main() { | |
let args: Vec<String> = env::args().collect(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sidekiq.configure_server do |config| | |
config.on(:startup) do | |
BatchWriter.start_timer | |
at_exit do | |
BatchWriter.stop_timer | |
BatchWriter.flush | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "concurrent" | |
class BatchWriter | |
MAX_BATCH_SIZE = 100 | |
TIMER_OPTS = { execution_interval: 1, timeout_interval: 1 }.freeze | |
cattr_accessor :buffer | |
self.buffer = [] | |
cattr_accessor :mutex |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LocationsJob < ApplicationJob | |
queue_as :high_priority | |
def perform(location) | |
BatchWriter.push(location) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
queue = Queue.new | |
workers = Array.new(N) do | |
Thread.new do | |
while record = queue.pop | |
# insert record | |
end | |
end | |
end |