Skip to content

Instantly share code, notes, and snippets.

View jsborjesson's full-sized avatar

Jimmy Börjesson jsborjesson

View GitHub Profile
class Matrix
def initialize(rows = [])
@rows = rows
end
def [](x, y)
@rows.fetch(y, [])[x]
end
def []=(x, y, value)
# Measure startup time on a Ruby file
vim_startup_time=$((time vim Rakefile +q) 2>&1 >/dev/null | grep real | awk '{ print $2 }')
# Echo the results
echo $(git sha) " " $vim_startup_time
# Step back one commit without warnings
git checkout HEAD~ 2> /dev/null
# Recurse
@jsborjesson
jsborjesson / postgres_exclude_constraint.sql
Last active January 17, 2017 10:50
Demonstrate how to use EXCLUDE to prevent overlapping time sequences.
-- Create 2 tables to link together
CREATE TABLE customer (id SERIAL PRIMARY KEY);
CREATE TABLE article (id SERIAL PRIMARY KEY);
-- Insert a few rows
INSERT INTO customer DEFAULT VALUES;
INSERT INTO customer DEFAULT VALUES;
INSERT INTO article DEFAULT VALUES;
INSERT INTO article DEFAULT VALUES;
@jsborjesson
jsborjesson / moneywise_wallet_migrate.rb
Last active September 16, 2016 19:21
Migrate data from MoneyWise to Wallet
# This is a quick script migrating data from MoneyWise to Wallet
#
# MoneyWise: https://play.google.com/store/apps/details?id=com.handynorth.moneywise
# Wallet: https://play.google.com/store/apps/details?id=com.droid4you.application.wallet
#
# It uses MoneyWise's CSV export to upload via the Wallet API.
#
# Data loss/Things I did not care about.
# - Time. All records will be at 00:00 at the correct date.
# - Accounts. Everything will go into the same account.

Org-mode solution to this riddle.

More to learn about org-mode than to solve the riddle…

  1. The product of the 3 numbers is 36.
  2. The sum is the hallway number, which Zara knows.
  3. The largest number is unique.
  4. Zara needed the third clue.

Work out all the factors that produce 36:

@jsborjesson
jsborjesson / rake_remove_task.rb
Created June 16, 2016 12:15
Programatically remove a task by name in Rake
# Monkeypatch to remove tasks
module Rake
def self.remove_task(task_name)
Rake.application.instance_variable_get('@tasks').delete(task_name.to_s)
end
end
# Example: disable all of the tasks in the db namespace
namespace :db do |ns|
ns.tasks.each do |task|
import java.util.ArrayList;
import java.util.Arrays;
public class Primes {
public static void main(String[] args) {
int limit = 1000;
// Calculate primes under limit and measure the time it took
long startTime = System.currentTimeMillis();
ArrayList<Integer> answer = primes(limit);
# Reset
Color_Off='\[\e[0m\]'
# Regular Colors
Black='\[\e[0;30m\]'
Red='\[\e[0;31m\]'
Green='\[\e[0;32m\]'
Yellow='\[\e[0;33m\]'
Blue='\[\e[0;34m\]'
Purple='\[\e[0;35m\]'
@jsborjesson
jsborjesson / sequel_migrations.rb
Last active August 29, 2015 14:25
Rails-like rake db:migrate with sequel
# Rakefile
namespace :db do
desc "Run migrations"
task :migrate, [:version] do |t, args|
require "sequel"
Sequel.extension :migration
db = Sequel.connect(ENV.fetch("DATABASE_URL"))
if args[:version]
puts "Migrating to version #{args[:version]}"
Sequel::Migrator.run(db, "db/migrations", target: args[:version].to_i)
@jsborjesson
jsborjesson / tables.css
Created July 7, 2015 07:57
Makes tables look decent
table {
padding: 0;
border-collapse: collapse;
}
table tr {
border-top: 1px solid #cccccc;
background-color: white;
margin: 0;
padding: 0;