Skip to content

Instantly share code, notes, and snippets.

ken@Nepheli /tmp> clang -Wunused -c x.c
x.c:2:19: warning: unused variable 'x' [-Wunused-variable]
void f(void) {int x, y;}
^
x.c:2:22: warning: unused variable 'y' [-Wunused-variable]
void f(void) {int x, y;}
^
2 warnings generated.
ken@Nepheli /tmp> clang -Wunused -Werror -c x.c
x.c:2:19: error: unused variable 'x' [-Werror,-Wunused-variable]
@kongtomorrow
kongtomorrow / gist:0526c14dacf155b572c3
Created June 9, 2014 22:23
Lazy vs computed property
let bricks : Array<UIView!> = {
var bricks = Array<UIView!>(count:brickCount, repeatedValue: nil)
for i in 0..brickCount {
let brick = UIView()
brick.backgroundColor = UIColor.blueColor()
brick.layer.borderColor = UIColor.greenColor().CGColor
brick.layer.borderWidth = 1.0
bricks[i] = brick
}
return bricks
@kongtomorrow
kongtomorrow / gist:e153a95907d188fb078a
Created June 10, 2014 22:52
@auto_closure for control over evaluation
func myAnd(lhs : Bool, rhs : @auto_closure () -> Bool) -> Bool {
if lhs {
return rhs()
} else {
return false
}
}
func logAndBool(val : Bool) -> Bool {
println("evaluating and returning \(val)!")
func forAllCombinations<T>(r1 : Range<T>, r2 : Range<T>, body : (T, T) -> ()) {
for i in r1 {
for j in r2 {
body(i, j)
}
}
}
forAllCombinations(0..3, 0..5) {
(i, j) in
func takeThree(i : Int, j : Int, k : Int) {
println("yus")
}
let arg = (1,2,3)
takeThree(arg)
#!/usr/bin/xcrun swift -i
func takeThree(i : Int, j : Int, k : Int) {
println("yus")
}
let arg = (1,2,3)
takeThree(arg)
@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)
@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?>()
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: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)