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
Math trick: works with any number
(Source: https://twitter.com/Perspective_pic/status/399718664540520448)
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 | |
// (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!)} |
[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 |
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