Skip to content

Instantly share code, notes, and snippets.

protocol Value {
}
protocol Smashable {
typealias V : Value
func valueBySmashing​OtherValue​(value: V) -> V
}
struct Bar : Value {
}
@kongtomorrow
kongtomorrow / gist:f2f7aa2c92d61eadcb72
Created May 1, 2015 01:00
why does this compile?
import Foundation
var backing = NSMutableArray()
func pop()->Int? {
return backing.firstObject.map { $0.integerValue }
}
@kongtomorrow
kongtomorrow / gist:4f6d86bea37db7124a84
Last active August 29, 2015 14:16
templateImageByMappingWhiteToClear:
- (NSImage *)templateImageByMappingWhiteToClear:(NSImage *)image {
NSImage *output = [NSImage imageWithSize:[image size] flipped:NO drawingHandler:^BOOL(NSRect dstRect) {
CGImageRef srcCGImage = [image CGImageForProposedRect:&dstRect context:[NSGraphicsContext currentContext] hints:nil];
CGRect bounds = CGRectMake(0, 0, CGImageGetWidth(srcCGImage), CGImageGetHeight(srcCGImage));
CGContextRef maskCtx = CGBitmapContextCreate(NULL, bounds.size.width, bounds.size.height, 8, 0, CGColorSpaceCreateDeviceGray(), 0);
CGContextDrawImage(maskCtx, bounds, srcCGImage);
CGImageRef mask = CGBitmapContextCreateImage(maskCtx);
CFRelease(maskCtx);
CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
@kongtomorrow
kongtomorrow / gist:43f4b319a2f14c5fe48f
Created January 11, 2015 00:39
protocol and class
import Cocoa
protocol MyProtocol {
func myFunc()
}
class MyView : NSView, MyProtocol {
func myFunc() {
}
}
var s : NSString = String(count: n, repeatedValue: Character("."))
let r = NSRegularExpression(pattern: "^(..+?)\\1+$", options: nil, error: nil)!
var l = s.length
var re = [Int]()
while let m = r.firstMatchInString(s, options:nil, range:NSRange(0..<l)) {
let p = m.rangeAtIndex(1).length
re += [p]
l /= p
s = s.substringToIndex(l)
}
@kongtomorrow
kongtomorrow / gist:c5e3494452e419cbf016
Created December 4, 2014 04:08
Address sanitizer squawkage
ken@Nepheli-20 /tmp>
~/bin/clang/bin/clang -O3 -g -fsanitize=address -isysroot /Volumes/Data/Users/ken/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -framework Foundation /tmp/asanBarf.m
ken@Nepheli-20 /tmp> ./a.out
=================================================================
==91531==ERROR: AddressSanitizer: global-buffer-overflow on address 0x00010caf8f1f at pc 0x00010caf8dc2 bp 0x7fff531077a0 sp 0x7fff53107798
READ of size 32 at 0x00010caf8f1f thread T0
#0 0x10caf8dc1 (/private/tmp/./a.out+0x100000dc1)
#1 0x10caf8de4 (/private/tmp/./a.out+0x100000de4)
#2 0x7fff8cdc25c8 (/usr/lib/system/libdyld.dylib+0x35c8)
#3 0x0 (<unknown module>)
#import <Foundation/Foundation.h>
#import <mach/mach_time.h>
#import <simd/simd.h>
NSTimeInterval SecondsFromMachTimeInterval(uint64_t machTimeInterval) {
static double timeScaleSeconds = 0.0;
if (timeScaleSeconds == 0.0) {
@kongtomorrow
kongtomorrow / gist:57da9fd8b0fa72cd33cc
Last active August 29, 2015 14:10
playing with autovectorization and __CFBytesInASCII
Compiled with:
~/bin/clang/bin/clang -O3 -Rpass=loop-vectorize -Rpass-missed=loop-vectorize -Rpass-analysis=loop-vectorize -isysroot /Volumes/Data/Users/ken/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -framework Foundation /tmp/testcomp/testcomp/main.m
Interesting part of diagnostic output:
/tmp/testcomp/testcomp/main.m:57:13: remark: vectorized loop (vectorization factor: 16, unrolling interleave factor: 1) [-Rpass=loop-vectorize]
testBuffer[i] = (bytes[i] >= 128);
^
output:
2014-12-03 11:33:33.070 a.out[48363:2396624] shortLen: 6 longLen:2493109
@kongtomorrow
kongtomorrow / gist:80ff47743d2c63355c42
Last active August 29, 2015 14:09
Swift + Unicode evilness :-D
let é = "precomposed character!"
let é = "decomposed characters!"
println(é) // prints "precomposed character!"
println(é) // prints "decomposed characters!"
@kongtomorrow
kongtomorrow / gist:9d4eb16b02bc8993a162
Created October 22, 2014 01:03
processing a func with arbitrary parameters
func logWrap<Domain,Range>(f : Domain->Range) -> Domain->Range {
return { (args:Domain) -> Range in
println("called me!")
return f(args)
}
}
func foo(a:Int, b:Int)->Int {
return a + b
}