Skip to content

Instantly share code, notes, and snippets.

@kongtomorrow
kongtomorrow / gist:974350
Created May 16, 2011 12:23
Get debug descriptions for enums in Objective-C via terrible terrible macro hackery.
/* Get strings descriptions for enum values for debugging.
Usage:
With enum
typedef enum {
AppleFruit = 0,
BlueberryFruit = 1,
BlackberryFruit = -1
} Fruit;
@kongtomorrow
kongtomorrow / gist:6803313
Created October 3, 2013 01:37
compile-time generation of compile-time checked KVC key paths
#import <Foundation/Foundation.h>
#define KVCKeyPath_1(a) @"" a
#define KVCKeyPath_2(a, ...) a @"." KVCKeyPath_1(__VA_ARGS__)
#define KVCKeyPath_3(a, ...) a @"." KVCKeyPath_2(__VA_ARGS__)
#define KVCKeyPath_4(a, ...) a @"." KVCKeyPath_3(__VA_ARGS__)
#define KVCKeyPath_5(a, ...) a @"." KVCKeyPath_4(__VA_ARGS__)
#define KVCKeyPath_6(a, ...) a @"." KVCKeyPath_5(__VA_ARGS__)
#define KVCKeyPath_7(a, ...) a @"." KVCKeyPath_6(__VA_ARGS__)
JButton button;
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);
c.fill = GridBagConstraints.HORIZONTAL;
button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
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)