Skip to content

Instantly share code, notes, and snippets.

@hsavit1
hsavit1 / String.swift
Created December 16, 2015 21:48 — forked from kharrison/String.swift
Swift String Playground Examples
// Swift Standard Librray - String
// Keith Harrison http://useyourloaf.com
// Import Foundation if you want to bridge to NSString
import Foundation
// ====
// Initializing a String
// ====
@hsavit1
hsavit1 / gist:53e98a28423981906e9b
Created December 16, 2015 02:32 — forked from ikesyo/gist:8877f7c6d0cfb9ffab35
Mapping with filtering nil
let values = [ "1", "foo", "3" ]
// Swift 1.2
extension Array {
func filterMap(@noescape transform: T -> U?) -> [U] {
var results = [U]()
for x in self {
if let mapped = transform(x) {
results.append(mapped)
// Similar to `enumerate` but provides the collection's index type
// rather than an Int for the position
public func iterate<C: CollectionType>(collection: C) -> SequenceOf<(C.Index, C.Generator.Element)> {
var index = collection.startIndex
// type-inference doesn't want to work without this
return SequenceOf { _ -> GeneratorOf<(C.Index, C.Generator.Element)> in
return GeneratorOf {
if index == collection.endIndex {
return nil
/*
Erica Sadun, http://ericasadun.com
Basic Errors
*/
import Foundation
/// A basic utility error type that stores the reason for
@hsavit1
hsavit1 / minReduce.swift
Created December 15, 2015 23:22 — forked from kristopherjohnson/minReduce.swift
Using Swift reduce() and min() to find minimum value in an array
struct Value {
let num: Int
init(_ n: Int) { num = n }
}
let a = [Value(3), Value(2), Value(1), Value(4), Value(5)]
let min1: Value = a.reduce(Value(Int.max)) {
($0.num < $1.num) ? $0 : $1
@hsavit1
hsavit1 / JSONLens.swift
Created December 15, 2015 22:31 — forked from tLewisII/JSONLens.swift
JSON Lens traversal in Swift
let userjs: NSData = "{\"name\": \"max\", \"age\": 10, \"tweets\": [\"hello\"], \"attrs\": {\"one\": \"1\", \"more\": {\"stuff\": \"again\"}}}"
.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let val = JSValue.decode(userjs)
let traversal = JSValue.key("attrs") • JSValue.key("more") • JSValue.key("stuff")
let getIt = traversal.get(val)
let setIt = traversal.set(val, JSValue.JSString("bob"))
@hsavit1
hsavit1 / Writer.swift
Created December 15, 2015 22:30 — forked from tLewisII/Writer.swift
Basic Writer [String] A Writer monad
//bind
operator infix >>= {
associativity left
}
struct Writer<A> {
let a:A
let s:String[]
init(_ a:A, _ s:String[]) {
@hsavit1
hsavit1 / Reader.swift
Created December 15, 2015 22:28 — forked from tLewisII/Reader.swift
Try at a Reader Monad in Swift
//this is a playground
struct Reader<R,A> {
let f:R -> A
init(_ fun:R -> A) {
f = fun
}
static func wrap(val:A) -> Reader<R,A> {
return Reader({_ in val})
@hsavit1
hsavit1 / fibs.swift
Created December 14, 2015 21:31
Fibonacci sequence in Swift
/// Generator for Fibonacci sequence
func fibonacciGenerator() -> AnyGenerator<Int> {
var a = -1
var b = 1
return anyGenerator {
let next = a + b
a = b
b = next
return next
}
@hsavit1
hsavit1 / zipCombine.swift
Created December 14, 2015 21:30 — forked from kristopherjohnson/zipCombine.swift
zipWith-like function for Swift
/// Given two sequences and a function, return a sequence of results of applying
/// the function to sucessive elements of the sequences.
///
/// Like Haskell's zipWith.
func zipCombine<S1: SequenceType, S2: SequenceType, T>(
sequence1: S1,
_ sequence2: S2,
combine: (S1.Generator.Element, S2.Generator.Element) -> T
) -> AnySequence<T> {
return AnySequence { () -> AnyGenerator<T> in