Skip to content

Instantly share code, notes, and snippets.

🦅
Swifting

Harlan Haskins harlanhaskins

🦅
Swifting
Block or report user

Report or block harlanhaskins

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View -
harlan@durant ~/B/C> valgrind ./BogoSort -n 10 -s
==29299== Memcheck, a memory error detector
==29299== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==29299== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==29299== Command: ./BogoSort -n 10 -s
==29299==
[38, 89, 25, 84, 30, 4, 71, 84, 7, 46]
[4, 7, 25, 30, 38, 46, 71, 84, 84, 89]
It took me 00:00:12.591712 to BogoSort this list.
I shuffled it 790,612 times.
View ColorManager.swift
class ColorManager {
lazy var thing = ColorManager.loadColors()
class func loadColors() -> [String: UIColor] {
// Go grab the stuff from the plist
return ["Hello": UIColor.whiteColor()]
}
}
View -
harlanhaskins@nietzsche ~/D/C/P/B/C> ./BogoSort -n 10 -s
[98, 93, 95, 60, 100, 37, 21, 97, 60, 94]
[21, 37, 60, 60, 93, 94, 95, 97, 98, 100]
It took me 00:00:0.458424 to BogoSort this list.
I shuffled it 2,844,195 times.
That's 6204289.042459 shuffles per second.
harlanhaskins@nietzsche ~/D/C/P/B/C> ./BogoSort-Ofast -n 10 -s
[98, 32, 21, 98, 60, 3, 35, 0, 64, 74]
[0, 3, 21, 32, 35, 60, 64, 74, 98, 98]
It took me 00:00:0.221931 to BogoSort this list.
View apidoc.md

Quotes

A web API for quotes.

Dependencies

This project requires PostgreSQL and these python dependencies:

  • flask
  • peewee
View wordcount.hs
import System.IO (getContents)
import Data.Map (fromListWith, toList)
import Data.List (sortBy)
import Data.Ord (comparing)
import Data.Char (toLower, isLetter)
countedWords = toList . fromListWith (+) . map (flip (,) 1)
showTuple (item, count) = "word: " ++ item ++ " count: " ++ show count
normalized = map toLower . filter isLetter
View vcardstub.py
@app.route("/<username>", methods=["GET"])
def vcard(username):
vcard = create_vcard_from_username(username)
if not vcard:
return Response("Whoops. Sorry, br0.", status=500)
return Response(vcard, headers={"Content-Type":"text/vcard"})
View gist:6dd2ac99d5867de4d7ea
harlanhaskins@nietzsche ~> swiftc /Users/harlanhaskins/Documents/Code/Apple/iOS/Personal/sdfadf/sdfadf/main.swift -v
Apple Swift version 1.2 (swiftlang-602.0.47.4 clang-602.0.48)
Target: x86_64-apple-darwin14.3.0
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file /Users/harlanhaskins/Documents/Code/Apple/iOS/Personal/sdfadf/sdfadf/main.swift -target x86_64-apple-darwin14.3.0 -enable-objc-interop -color-diagnostics -module-name main -o /var/folders/dy/xfvh7x4j5y35vx4pfjjjx8440000gn/T/main-35261c.o
<unknown>:0: error: cannot load underlying module for 'CoreGraphics'
<unknown>:0: note: did you forget to set an SDK using -sdk or SDKROOT?
<unknown>:0: note: use "xcrun -sdk macosx swift" to select the default OS X SDK installed with Xcode
0 swift 0x000000010bb3da08 llvm::sys::PrintStackTrace(__sFILE*) + 40
1 swift 0x000000010bb3dee4 SignalHandler(int) + 452
2 libsystem_platform.dylib 0x00007fff83dadf1a
@harlanhaskins
harlanhaskins / SafeArray.swift
Last active Aug 29, 2015
SafeArray -- a safe Swifty Array abstraction
View SafeArray.swift
import Foundation
struct SafeArray<T> {
private var values: [T]
init(_ values: [T]) {
self.values = values
}
subscript(index: Int) -> T? {
View 1-Functor-and-Monad.md

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
View mapMaybe.swift
// Transform a list of values into a list of transformed values where the transformation might fail and produce an optional.
func mapMaybe<C : CollectionType, T>(array: C, transform: (C.Generator.Element -> T?)) -> [T] {
var result = [T]()
for element in array {
if let transformed = transform(element) {
result.append(transformed)
}
}
return result
}
You can’t perform that action at this time.