Skip to content

Instantly share code, notes, and snippets.

View kazjote's full-sized avatar

Kacper Bielecki kazjote

View GitHub Profile
import scala.util.control.TailCalls._
object Main extends App {
def findSumProduct(factors: Int): Option[Long] = {
def recurseStartNumber(level: Int): Int = if (level < factors - 2) 1 else 2
def inner(level: Int, currentNumber: Int, sum: Long, product: Long, knownMin: Option[Long]): TailRec[Option[Long]] = {
@kazjote
kazjote / resque_zombie_killer.rb
Created January 2, 2015 10:53
Script to kill resque processes running > 1 day
#!/usr/bin/env ruby
# Kills resque workers that are there for longer than 24 hours
processes = `ps ax -o pid,cmd`.split("\n")
processes_by_pid = processes.inject({}) do |hash, line|
pid, cmd = line.split(" resque-1.25.2: ")
hash unless pid && cmd
# http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/
# "In computer science, a closure is a first-class function with free variables that are bound in the lexical environment."
#
# "A closure is a function that is said to be "closed over" it’s free variables"
#
#
# * You can pass it around like an object (to be called later)
#
# * It remembers the values of all the variables that were in scope when the function was created. It is then able to
@kazjote
kazjote / restart_gs
Created December 15, 2013 22:08
Because of some strange bug on ubuntu 13.10 terminal becomes sometimes unusable after restarting of gnome-shell. This script restarts gnome-shell, gnome-terminal and reattaches tmux session. It is supposed of be run from inside of tmux session.
#!/bin/bash
gnome-shell --replace &
killall gnome-terminal
sleep 2
gnome-terminal --hide-menubar --maximize -e "bash -l -c 'unset TMUX; tmux attach'" &>/dev/null &
@kazjote
kazjote / missing_translations.rb
Last active December 17, 2015 19:39
Script for finding missing translations in rails projects.
#!/usr/bin/ruby
require 'yaml'
files = Dir["**/*.rb"] + Dir["**/*.haml"]
patterns = [
/ t\('([^']+)'\)/, # t('')
/ t\("([^"]+)"\)/, # t("")
/I18n.translate\("([^"]+)"\)/, # I18n.translate("")
@kazjote
kazjote / my.cnf
Created May 17, 2013 14:04
mysql configuration optimized for running tests
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
@kazjote
kazjote / benchmark
Created May 13, 2013 11:35
Ruby threads Threads are executed in parallel while waiting for external command to finish.
Using /home/kazjote/.rvm/gems/ruby-1.9.3-p327
➜ threads time ruby threads.rb 1
ruby threads.rb 1 0.02s user 0.01s system 0% cpu 10.054 total
➜ threads time ruby threads.rb 2
ruby threads.rb 2 0.02s user 0.02s system 0% cpu 6.049 total
➜ threads time ruby threads.rb 4
ruby threads.rb 4 0.01s user 0.02s system 0% cpu 4.040 total
➜ threads time ruby threads.rb 8
ruby threads.rb 8 0.02s user 0.02s system 1% cpu 3.037 total
➜ threads time ruby threads.rb 16
const Gio = imports.gi.Gio;
let file = Gio.File.new_for_path('file.txt');
stream = file.replace(null, false, null, null, null);
let str = 'hello Kacper :)';
stream.write(str, null, null, null);
stream.close(null);
require 'httparty'
Array.new(32) {|i| i}.map do |worker|
fork do
file = File.open("worker_#{worker}.log", "w")
1000.times do |i|
url = "http://localhost:9290/tests/test1.txt"
page = HTTParty.get(url)
file.puts("==============")
file.puts("url: #{url}")