This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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)){ \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (cons a b) | |
(define (dispatch msg) | |
(cond ((= msg 0) a) | |
((= msg 1) b))) | |
dispatch) | |
(define (car cell) | |
(cell 0)) | |
(define (cdr cell) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/xcrun swift -i | |
func ambiguous( f : Any...) { | |
for i in f { | |
println("\(i)") | |
} | |
} | |
let arg = (1,2) | |
ambiguous(arg) |