Skip to content

Instantly share code, notes, and snippets.

@kimhunter
kimhunter / 2048.c
Last active August 29, 2015 13:58 — forked from justecorruptio/2048.c
M[16], X = 16, W, k;
main()
{
T(system("stty cbreak"));
puts(W & 1 ? "WIN" : "LOSE");
}
K[] = { 2, 3, 1 };
s(f, d, i, j, l, P)
{
for (i = 4; i--;)
@kimhunter
kimhunter / NSNumber+FastEnum.m
Created May 29, 2014 23:21
Add FastEnumeration to NSNumber
/*
inspired by ruby's 4.times {} method, created becasue I can and it was fun to do.
Usage:
for (NSNumber *i in @100)
{
NSLog(@"%@", i);
}
*/
@interface NSNumber (FastEnum)<NSFastEnumeration>
import Cocoa
var nums: Int[] = []
for var i = 0; i < 10; i++ {
nums.append(Int(arc4random()) % 200)
}
nums
func qSort(var arr: Int[]) -> Int[] {
import Cocoa
extension Array {
func myMap<A,B>(items: Array<A>, f:((A) -> B)) -> Array<B> {
var xs: Array<A> = items
return xs.count == 0 ? [] : [f(xs.removeAtIndex(0))] + myMap(xs,f)
}
func myMap<B>(f:((T) -> B)) -> Array<B> {
return myMap(self,f)
@kimhunter
kimhunter / scanner_test.m
Last active August 29, 2015 14:02
timed comparison of nsscanner to an array split parsing task.
//
// main.m
// Scratch
//
// Created by Kim Hunter on 12/06/2014.
// Copyright (c) 2014 Kim Hunter. All rights reserved.
//
// Here I do similar test while the code does some actual task in the loops
// The results: the nsscanner approach takes less than a tenth of the time
@kimhunter
kimhunter / MakeImageTemplate.m
Last active August 29, 2015 14:03
Turn an Image into a single colour template, for use with iOS 7
//
// MakeImageTemplate
// Turn those badly coloured template images into a normalized color
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Githubs syntax highlighter doesn't see _ spaced numbers correctly
println(1_000_000)
println(0xFF_FF_FF_FF)
@kimhunter
kimhunter / export_stashes.rb
Created August 17, 2011 23:24
Export Git stashes
require "yaml"
stash_list = `git stash list`.chomp.split("\n").map {|i| i.chomp }
stash_list.map!{|s|
b = s.match(/stash@\{([0-9]+)\}: (.*)/)
[b[1], b[2]]
}
stash_list.each do |s|
puts "{#{s[0]}}"
@kimhunter
kimhunter / kmprofile.m
Created October 1, 2011 06:10
objective-c code inline code block profiling
#define KMProfile(MSG, CODE_TO_PROFILE) { \
NSDate *KMStartDate = [NSDate date]; \
NSTimeInterval KMduration; \
CODE_TO_PROFILE \
KMduration = [[NSDate date] timeIntervalSinceDate:KMStartDate]; \
NSLog(@"KMProfile: %@ TOOK %.0lfms (%.1lfs) ", MSG,(KMduration*1000) , KMduration); \
}
// Usage: (works well with something a bit more intensive but you get the gist)
@kimhunter
kimhunter / merge_sort.rb
Created October 13, 2011 15:08
Merge sort ruby
#!/usr/bin/env ruby
# 2011-10-10 20:57:53 +1000
def merge_sort a
return a if a.size <= 1
l,r = split_array a
result = combine merge_sort(l), merge_sort(r)
end
def split_array a