Skip to content

Instantly share code, notes, and snippets.

@kongtomorrow
kongtomorrow / sneaky
Created July 17, 2014 02:04
sekret protocol requirements!
class FooColl : Swift.Collection {
/* Secret protocol conformance requirements! Apparently Collection
inherits from a "_Collection" protocol that has more stuff.
error: type 'FooColl' does not conform to protocol '_Collection'
class FooColl : Swift.Collection {
^
Swift._Collection:3:19: note: protocol requires property 'startIndex' with type 'FooColl.IndexType'
/*@public*/ var startIndex: IndexType { get }
func triangleNums(count:Int)->GeneratorOf<Int> {
var i = 0
return GeneratorOf<Int> {
++i < count ? (1+i)*i/2 : nil
}
}
for i in triangleNums(10) {
println(i)
}
@kongtomorrow
kongtomorrow / gist:b2c13644b772c371d72e
Created July 13, 2014 03:43
looping ω combinator
// trying to implement http://math.stackexchange.com/questions/96122/looping-ω-combinator
// want ω: ω ↦ ω(ω)
class SelfToSelf {
let value:SelfToSelf->SelfToSelf
init(value:SelfToSelf->SelfToSelf) {
self.value = value
}
}
@kongtomorrow
kongtomorrow / gist:ee9d14208659555ea578
Last active August 29, 2015 14:03
Y combinator in ObjC
/* This is an exercise, basically. See http://www.ece.uc.edu/~franco/C511/html/Scheme/ycomb.html
for why you might care, or https://gist.github.com/kongtomorrow/e95bea13162ca0e29d4b for the
same in Swift.
*/
#define Y(Domain,Range) ({ \
typedef Range(^DtoR)(Domain); \
\
^(DtoR (^almost)(DtoR)){ \
@kongtomorrow
kongtomorrow / gist:e95bea13162ca0e29d4b
Last active August 31, 2022 16:20
Y combinator in Swift!
/* The Y combinator in Swift!
For a discussion of what the heck this is all about, see http://www.ece.uc.edu/~franco/C511/html/Scheme/ycomb.html
The nifty thing is that it allows us to implement recursion without the ability for a function to refer to itself from within its own definition.
Note how we manage a recursive definition of factorial without any function referring to its own name.
Thanks to @eridius for help with the SelfToUnderlying<T> type.
*/
@kongtomorrow
kongtomorrow / gist:cbe2d73075e879f7d73d
Created July 9, 2014 18:30
recursion with an unnamed lambda via fancy pants fixed point combinator
func fixedPoint<S,T>(f:(S->T) -> (S->T)) -> (S->T) {
return {
(x : S) -> T in
return f(fixedPoint(f))(x)
}
}
let fact:(Int)->Int = fixedPoint {
(almostFactorial:(Int)->Int) -> ((Int)->Int) in
return {
@kongtomorrow
kongtomorrow / gist:513747f7a105768719d8
Created July 2, 2014 01:39
Cons using environments
(define (cons a b)
(define (dispatch msg)
(cond ((= msg 0) a)
((= msg 1) b)))
dispatch)
(define (car cell)
(cell 0))
(define (cdr cell)
NSImage *pdfImage = [[NSImage alloc] initWithContentsOfFile:@"/Applications/Utilities/Disk Utility.app/Contents/Frameworks/DUSupport.framework/Versions/A/Resources/key.pdf"];
CGSize desiredSize = CGSizeMake(300,300);
NSColorSpace *desiredColorspace = [NSColorSpace sRGBColorSpace];
CGRect bounds = { CGPointZero, desiredSize };
// if you'd rather give size as a scale factor from the PDF's logical "size" (which isn't in pixels), could use NSImageHintCTM instead and pass NULL for proposedRect
CGImageRef rasterizedIm = [pdfImage CGImageForProposedRect:&bounds context:nil hints:@{ kCIContextOutputColorSpace : (id)[desiredColorspace CGColorSpace], NSImageHintCTM : [NSAffineTransform transform] }];
CGImageDestinationRef idst = CGImageDestinationCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:@"/tmp/output.png"], kUTTypePNG, 1, NULL);
@kongtomorrow
kongtomorrow / gist:385239877f976e34e8fd
Last active August 29, 2015 14:02
wrapped nils, oy
import Foundation
var d1 = Dictionary<String, String>()
d1["foo"] = "hi"
d1["foo"] = nil // this removes the entry from the dict
println("\(d1)") // prints [:]
var d2 = Dictionary<String, String?>()
@kongtomorrow
kongtomorrow / gist:35897a6a2442b1ad4650
Created June 18, 2014 23:45
ambiguous varargs vs call with tuple
#!/usr/bin/xcrun swift -i
func ambiguous( f : Any...) {
for i in f {
println("\(i)")
}
}
let arg = (1,2)
ambiguous(arg)