Skip to content

Instantly share code, notes, and snippets.

@mharju
mharju / gist:1300745
Created October 20, 2011 09:21
Playing around with functional features of C# to get concise code. This style does not feel so idiomatic, though.
OpenReadCompletedEventHandler GetGenericOpenHandler<T>(ObservableCollection<T> destination, Func<XElement, IEnumerable<T>> elementQuery)
{
return (object sender, OpenReadCompletedEventArgs e) =>
{
XElement xmlResult = this.GetXmlResult(e);
foreach (T result in elementQuery(xmlResult))
{
destination.Add(result);
}
};
@mharju
mharju / gist:1342709
Created November 6, 2011 09:54
Just a quick test to make fancy images out of b/w triangles. I fully agree on the lack of mathematical elegance. Maybe I'll clean this up sometime. Or then not.
// Patchwork
void setup()
{
size(640, 640, P3D);
noStroke();
initialize();
}
void initialize()
@mharju
mharju / gist:1504903
Created December 21, 2011 06:27
8h 12m 23s to hours representation. Used as python -c '<script here>'
import re
import sys
i = sys.stdin.readline()
print "%.2f" % sum( [ x*y for x, y in zip([int(a) for a in re.match(r'(\d+)h (\d+)m (\d+)s', i).groups()], (1, 1/60., 1/(60.*60.))) ])
@mharju
mharju / Makefile
Created December 30, 2011 14:20
Makefile for Ibniz (http://pelulamu.net/ibniz/) for use with OS X
# Copy SDLmain.m and SDLmain.h from the SDL DMG to the same directory
# To get decent framerates edit line 171 on ui_sdl.c and enter
# return SDL_GetTicks() / 3;
# ( source for that trick: http://lgo900.wordpress.com/2011/12/25/running-ibniz-on-mac-os-x/ )
CC=gcc
EXE=ibniz
LFLAGS=-framework SDL -framework Cocoa
FLAGS=-I/Library/Frameworks/SDL.Framework/Headers
all: ibniz
@mharju
mharju / gist:1593894
Created January 11, 2012 09:26
Invoke timeout after there has been no events in a given timeout
Observable.FromEvent<MapDragEventArgs>(Map, "MapPan")
.Publish(mapPan_ =>
{
mapPan_.Subscribe( _ =>
Observable.Timer(TimeSpan.FromMilliseconds(500))
.TakeUntil(mapPan_)
.Subscribe(__ => Debug.WriteLine("Timeout"))
);
return mapPan_;
@mharju
mharju / gist:1605373
Created January 13, 2012 10:04
tokenize input and subscribe. Instant profit when combined with JSON object async response stuff.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Subjects;
namespace ReactiveTest
# wat am i doing wrong here?
def setUp(self):
cursor = connection.cursor()
script = open('epic/sql/player.sql').read()
cursor.execute(script)
transaction.commit_unless_managed()
def tearDown(self):
cursor = connection.cursor()
# -*- coding: utf-8
import random
class APIHandler(object):
def __init__(self):
self.queue = list()
def do_api_request(self, *args, **kwargs):
# tässä koko kupletin juoni. työnnetään paikallisesti määritelty
from functools import wraps
def db(selector):
def _funcall(function):
@wraps(function)
def _wrapper(row):
got_args = {
'primary': { '1': ('baba', 15), '2': ('gaga', 16) },
'secondary': { '1': ('foo', 17), '2': ('bar', 18) },
}
@mharju
mharju / gist:3181751
Created July 26, 2012 12:26
Prime number sieve
(defn multiple-of [number divisor] (and (not (= number divisor))
(= (mod number divisor) 0)))
(defn not-multiples-or-1 [numbers divisor]
(filter (fn [n]
(and
(not (= n 1))
(not (multiple-of n divisor)))) numbers))
(defn primes-in-decreasing-seq [n] (reduce not-multiples-or-1 n (range 2 (Math/sqrt (first n)))))