Skip to content

Instantly share code, notes, and snippets.

View tkareine's full-sized avatar

Tuomas Kareinen tkareine

View GitHub Profile

Connecting Visual VM to Tomcat 7

Adapted from Mark Shead's blog.

  1. Login to remote host with SSH:

    local $ ssh remote`
@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)
@tkareine
tkareine / pfork.rb
Created May 31, 2011 22:35
Capture input and output and get the exit status of arbitrary Ruby code, variant of Open4::popen4
# coding: utf-8
module PFork
extend self
# Call function +fun+ in a child process, giving access to the child process
# id and capturing STDIN, STDOUT, and STDERR of the function call.
#
# Adapted from stdlib's Open3::popen3 and
# {Open4::popen4}[https://github.com/ahoward/open4] by Ara T. Howard, but
@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:
@tkareine
tkareine / count_down_latch.rb
Created December 13, 2010 21:56
A simple count down latch in Ruby
#!/usr/bin/env ruby
# coding: utf-8
require 'thread'
class CountDownLatch
attr_reader :count
def initialize(to)
@count = to.to_i
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
var subj0 = new Rx.Subject()
var obs0 = subj0.doAction(function(e) { console.log("obs0 doAction", e) })
var subs0_1 = obs0.subscribe(function(e) { console.log("subs0_1", e) })
subj0.onNext('a')
//> obs0 doAction a
//> subs0_1 a
var subs0_2 = obs0.subscribe(function(e) { console.log("subs0_2", e) })
subj0.onNext('b')