Skip to content

Instantly share code, notes, and snippets.

Avatar
🦅
Swifting

Harlan Haskins harlanhaskins

🦅
Swifting
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? {
@harlanhaskins
harlanhaskins / phoneformat.swift
Created Jul 8, 2015
String.stringByFormattingPhoneNumbers()
View phoneformat.swift
extension String {
static let phoneDetector = NSDataDetector(types: NSTextCheckingType.PhoneNumber.rawValue, error: nil)!
func stringByFormattingPhoneNumbers() -> String {
var mutableString = self
String.phoneDetector.enumerateMatchesInString(self, options: NSMatchingOptions.allZeros, range: NSRange(location: 0, length: count(self))) { result, flags, stop in
if let
range = self.rangeFromNSRange(result.range),
phoneNumber = result.phoneNumber.flatMap(PhoneManager.sanitizedPhoneNumber) {
mutableString.replaceRange(range, with: PhoneManager.formattedPhoneNumber(phoneNumber))
}
View kindaequatable.swift
protocol KindaEquatable {
func ~==(lhs: Self, rhs: Self) -> Bool
}
infix operator ~!= { associativity left precedence 140 }
func ~!=<T where T: KindaEquatable>(lhs: T, rhs: T) -> Bool {
return !(lhs ~== rhs)
}
// MARK: - String
You can’t perform that action at this time.