Skip to content

Instantly share code, notes, and snippets.

View tkareine's full-sized avatar

Tuomas Kareinen tkareine

View GitHub Profile
@tkareine
tkareine / BuilderExample.java
Created December 5, 2016 23:43
An example of user input validation, handling legacy code that loves to use nulls and exceptions.
package org.tkareine.validation_example;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
/**
* Design considerations:

Connecting Visual VM to Tomcat 7

Adapted from Mark Shead's blog.

  1. Login to remote host with SSH:

    local $ ssh remote`
@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 / 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 / nodelike.swift
Last active June 20, 2018 08:59
Node.js like event-loop with libdispatch and Swift
#!/usr/bin/env xcrun swift
import Dispatch
let appQueue = dispatch_queue_create("org.tkareine.NodeLike.appQueue", DISPATCH_QUEUE_SERIAL)
let appGroup = dispatch_group_create()
func delay(delayInMS: Int, block: () -> Void) {
let delayInNS = Int64(delayInMS) * Int64(NSEC_PER_MSEC)
let scheduleAt = dispatch_time(DISPATCH_TIME_NOW, delayInNS)
#!/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
#
require 'benchmark'
def with_greet(name)
yield "hi, #{name}"
end
def with_greet_jack
with_greet("jack") { |greet| yield greet }
end
$ ruby test_pty.rb ./ex19
[child] You enter the The great hall.
[child]
[child] >
--(interaction)--
[child] You can go:
[child] NORTH
[child]
[child] >
--(interaction)--
@tkareine
tkareine / surprise.coffee
Last active December 24, 2015 21:49
Using CoffeeScript 1.6.3 (rev 581af4540ac).
Q = require 'q'
value = if this.require 'aa' else 'bb' # missing `then` in `if` expr; should be compile error
incorrectChain = Q.nfcall(fun).then(
(result) -> console.log 'Result', result
, (error) -> console.error 'Error', error # a small indentation error
)
fun = (callback) -> callback(new Error('intentional error'))
@tkareine
tkareine / missing-then.coffee
Created October 7, 2013 13:36
Missing `then` in `if` expr should cause parsing error in CoffeeScript. Using CoffeeScript 1.6.3 (rev 581af4540ac).
value = if this.require 'aa' else 'bb' # missing `then` in `if` expr