Skip to content

Instantly share code, notes, and snippets.

View tkareine's full-sized avatar

Tuomas Kareinen tkareine

View GitHub Profile
#!/usr/bin/env ruby
# A toy program demonstrating consuming REST API and quering and
# inserting documents to MongoDB database with Ruby and its standard
# library without extra dependencies.
#
# Usage:
#
# $ mongod # leave it running
#
@tkareine
tkareine / comment-or-uncomment-region-or-line.el
Created January 9, 2015 08:35
Comment or uncomment region or current line
(defun tkareine/active-region-or-line ()
(if mark-active (list (region-beginning) (region-end))
(list (line-beginning-position)
(line-beginning-position 2))))
(defun tkareine/comment-or-uncomment-region-or-line ()
(interactive)
(let ((region (tkareine/active-region-or-line)))
(when region
(let ((rbegin (car region))
@tkareine
tkareine / LTOfficeHeuristicsTest.java
Last active August 29, 2015 14:23
Before and after refactoring method control flow with a custom monad, in Java 7. Method LTOfficeHeuristics#findOffices is the main entry point. LTOfficeHeuristics_before.java is the original implementation with explicit state handling and early returns. LTOfficeHeuristics_after.java is the refactored version with Narrowing monad. LTOfficeHeurist…
package org.tkareine.demo.service.csv.parser;
import com.google.inject.Inject;
import org.tkareine.demo.model.Address;
import org.tkareine.demo.model.Office;
import org.tkareine.demo.service.OfficeService;
import org.tkareine.demo.support.GuiceJUnit4ClassRunner;
import org.tkareine.demo.support.di.StubExternalServicesMongoModule;
import org.junit.Before;
import org.junit.Test;
@tkareine
tkareine / foo.rb
Created October 14, 2009 07:29
Test Ruby executable with code injection to have stubs/mocks
#!/usr/bin/env ruby
puts "from original executable, args: #{ARGV.join(" ")}"
require "pstore"
module Notes
class << self
attr_accessor :store_location
def put_note(id, note)
store.transaction do
store[:notes] ||= {}
store[:notes][id] = note
#!/usr/bin/env ruby
def exit_upon(*signals)
process_going_down = false
signals.map { |signal| signal.to_s.upcase }.each do |signal|
Signal.trap(signal) do
exit! if process_going_down # terminate if still executing the given block
process_going_down = true
yield(signal) if block_given?
exit! 0
@tkareine
tkareine / create_random_file.rb
Created January 27, 2011 11:35
Create random plain text file
# coding: utf-8
SIZE = ARGV.shift.to_i
FILENAME = ARGV.shift
if SIZE < 1 || FILENAME.nil?
puts "Usage: ruby #{File.basename(__FILE__)} <size> <file>"
exit 1
end
@tkareine
tkareine / fork_test_demo.rb
Created September 21, 2011 14:05
Test global state changing code with fork
# coding: utf-8
# Test global state changing code with fork
#
# * You should avoid having global state altogether
# * That said, if you cannot avoid it, for one reason or another,
# you can execute state changing code in a child process
# * Fork a child process, execute state changing code there
# * The forked child process has its own memory space, separate
# from the parent process
@tkareine
tkareine / jasmine.beforeAll.js
Created December 15, 2011 13:18
beforeAll decorator for Jasmine
function withBeforeAll(tests) {
var beforeAllCalled = false
var helpers = {
beforeAll: function (fun) {
beforeEach(function () {
if (beforeAllCalled) return
fun()
runs(function () { beforeAllCalled = true } )
})
(ns pi-estimation
"See Opinion 18 in <http://programmers.blogoverflow.com/2012/08/20-controversial-programming-opinions/>")
;; Given that Pi can be estimated using the function 4 * (1 – 1/3 + 1/5
;; – 1/7 + …) with more terms giving greater accuracy, write a function that
;; calculates Pi to an accuracy of 5 decimal places.)
;; with lazy-seq
(defn odds []