Skip to content

Instantly share code, notes, and snippets.

View Ben-G's full-sized avatar
💭
💻

Benjamin Encz Ben-G

💭
💻
View GitHub Profile
struct Car: StringLiteralConvertible {
enum CarType: String {
case Mercedes, Porsche, Ford, Unknown
}
var type: CarType
typealias ExtendedGraphemeClusterLiteralType = StringLiteralType
@Ben-G
Ben-G / findUnusedPngs.sh
Created May 23, 2012 13:28 — forked from floriankrueger/findUnusedPngs.sh
finds and prints unreferenced png files from xcode projects
#!/bin/sh
PROJ=`find . -name '*.xib' -o -name '*.[mh]'`
for png in `find . -name '*.png'`
do
filename=`basename $png`
name=${filename%.*}
if ! grep -q $name $PROJ; then
echo "$png is not referenced"
fi
@Ben-G
Ben-G / RandomNumberGenerator.m
Last active December 25, 2015 01:29
Generates a set of unique random numbers. Useful when you want to pick multiple random elements without duplicates.
- (NSArray *)generate:(int)n randomUniqueNumbersBetween:(int)lowerLimit upperLimit:(int)upperLimit
{
NSMutableArray *randomNumberArray = [NSMutableArray arrayWithCapacity:upperLimit-lowerLimit];
// add all numbers to array
for (int i = lowerLimit; i < upperLimit; i++)
{
[randomNumberArray addObject:@(i)];
}
class Parent {
var child: Child? {
didSet {
child.callback = { [weak self] in
delegateCallback()
}
}
}
@Ben-G
Ben-G / NotCrash.swift
Last active January 24, 2016 04:21
Does not crash compiler
public protocol X {
func _ok(a: Any)
}
public protocol StoreSubscriber: X {
typealias StoreSubscriberStateType
typealias AppStateType
func newState(state: StoreSubscriberStateType)
@Ben-G
Ben-G / ForwardIndexType.swift
Created February 3, 2016 16:55
"Infinite" loop with ForwardIndexType and `distanceTo`
struct AIndex: ForwardIndexType {
let index: Int
func successor() -> AIndex {
return AIndex(index: self.index + 1)
}
}
func == (lhs: AIndex, rhs: AIndex) -> Bool {
return lhs.index == rhs.index
@Ben-G
Ben-G / UnwrapAll.swift
Last active February 11, 2016 04:37
Generic Function That Unwraps Optional of Arbitrary Depth
// Benjamin Encz
// Note: Proof of Concept, you should definitely not use this!
import Cocoa
public protocol OptionalType {
init()
func unwrap() -> Any
}
@Ben-G
Ben-G / NonEmptyCollection.swift
Last active February 25, 2016 01:07
Type that forces a collection to not be empty
public struct NonEmptyCollection<T: CollectionType> {
let collection: T
init?(collection: T) {
if !collection.isEmpty {
self.collection = collection
} else {
return nil
}
@Ben-G
Ben-G / ComposeTypes.Swift
Last active March 5, 2016 17:49
Compose Types
// Example of a thing I cannot do with types in Swift
struct Operation<Input, Output> {
// would also be nice to have a list of operations that yield different types (and be able to dynamically inspect the generic type)
var operations: [Operation<>] = []
// This can be accomplished by using type erause
// would be nice to be able to leave a type hole here
var nextOperation: Operation<Output, _> // second generic argument can be any type from the perspective of this specific type
// only important aspect is that input of next operation matches output of this operation
@Ben-G
Ben-G / IsUnique.swift
Created May 30, 2015 15:48
isUniquelyReferencedNonObjC, simple example
import Foundation
class A { var x = "a" }
var a:A = A()
isUniquelyReferencedNonObjC(&a)
var b = a