Skip to content

Instantly share code, notes, and snippets.

View onmyway133's full-sized avatar
๐Ÿ˜‡
What you don't know is what you haven't learned

Khoa onmyway133

๐Ÿ˜‡
What you don't know is what you haven't learned
View GitHub Profile
@onmyway133
onmyway133 / exercise.md
Created December 12, 2015 04:56 — forked from wo0dyn/exercise.md
Find a 9 letter string of characters that contains only letters from โ€œacdegilmnoprstuwโ€ such that the hash(the_string) is โ€œ910897038977002โ€.

Find a 9 letter string of characters that contains only letters from

acdegilmnoprstuw

such that the hash(the_string) is

910897038977002
@onmyway133
onmyway133 / json.swift
Created December 7, 2015 03:46 — forked from chriseidhof/json.swift
Reflection
import Cocoa
struct Person {
var name: String = "John"
var age: Int = 50
var dutch: Bool = false
var address: Address? = Address(street: "Market St.")
}
struct Address {
import Foundation
protocol Serializable {
static func deserializeInto(bytePtr: UnsafeMutablePointer<UInt8>, bytes: ArraySlice<UInt8>) -> ArraySlice<UInt8>
}
extension Serializable {
typealias WorkaroundSelf = Self
@onmyway133
onmyway133 / shuffle.swift
Created November 27, 2015 02:04 — forked from natecook1000/shuffle.swift
Swift 2.0 shuffle / shuffleInPlace
// (c) 2015 Nate Cook, licensed under the MIT license
//
// Fisher-Yates shuffle as protocol extensions
extension CollectionType {
/// Return a copy of `self` with its elements shuffled
func shuffle() -> [Generator.Element] {
var list = Array(self)
list.shuffleInPlace()
return list
import Foundation
// Generic Error
public struct Error: ErrorType {let reason: String}
/**
Printing version of try? Call either with standard or autoclosure approach
```
let contents = attempt{try NSFileManager.defaultManager().contentsOfDirectoryAtPath(fakePath)}
let contents = attempt{try NSFileManager.defaultManager().contentsOfDirectoryAtPath(XCPlaygroundSharedDataDirectoryURL.path!)}
@onmyway133
onmyway133 / gist:5360b88171541a7109f5
Created November 15, 2015 08:13 — forked from ccgus/gist:6324222
Custom SQLite Functions
[db makeFunctionNamed:@"UTTypeConformsTo" maximumArguments:2 withBlock:^(sqlite3_context *context, int argc, sqlite3_value **argv) {
if (sqlite3_value_type(argv[0]) == SQLITE_TEXT) {
const unsigned char *a = sqlite3_value_text(argv[0]);
const unsigned char *b = sqlite3_value_text(argv[1]);
CFStringRef as = CFStringCreateWithCString(nil, (const char*)a, kCFStringEncodingUTF8);
CFStringRef bs = CFStringCreateWithCString(nil, (const char*)b, kCFStringEncodingUTF8);
sqlite3_result_int(context, UTTypeConformsTo(as, bs));
import Cocoa
enum CoroutineState {
case Fresh, Running, Blocked, Canceled, Done
}
struct CoroutineCancellation: ErrorType {}
class CoroutineImpl<InputType, YieldType> {
let body: (yield: YieldType throws -> InputType) throws -> Void
@onmyway133
onmyway133 / NSHashTable+SequenceType.swift
Created September 28, 2015 09:07 — forked from almostintuitive/NSHashTable+SequenceType.swift
Use NSHashTable with for .. in .. style enumeration with Swift2 (safely)
extension NSHashTable: SequenceType {
public func generate() -> AnyGenerator<AnyObject> {
var array = self.allObjects
var nextIndex = array.count-1
return anyGenerator {
if (nextIndex < 0) {
return nil

Copy and paste the swift code below into a playground to experiment.

This is a very close emulation of Functor and Monad typeclasses in swift. As of Swift 1.2 and Xcode 6.3, this is no longer very fragile.

Unfortunately, the compiler cannot verify the types when passing a function to (>>=). We have to wrap the function in a closure and call it with an explicit argument to compile.

optionalDoubles >>= squareRoot // doesn't compile
optionalDoubles >>= { squareRoot($0) } // compiles