Skip to content

Instantly share code, notes, and snippets.

var randomNumbers = [42, 12, 88, 62, 63, 56, 1, 77, 88, 97, 97, 20, 45, 91, 62, 2, 15, 31, 59, 5]
func partition(v: Int[], left: Int, right: Int) -> Int {
var i = left
for j in (left + 1)..(right + 1) {
if v[j] < v[left] {
i += 1
(v[i], v[j]) = (v[j], v[i])
}
}
@hsavit1
hsavit1 / avl.swift
Created November 3, 2015 16:48 — forked from pocketkk/avl.swift
AVL Tree
import UIKit
class Thing {
var value : Int
init(v: Int) {
value = v
}
}
class ThingBST {
@hsavit1
hsavit1 / Lens.swift
Created November 11, 2015 21:05 — forked from robb/Lens.swift
import Prelude
public struct Lens<A, B> {
private let get: A -> B
private let set: (A, B) -> A
public init(get: A -> B, set: (A, B) -> A) {
self.get = get
self.set = set
}
@hsavit1
hsavit1 / dijsktra.py
Created November 16, 2015 04:46 — forked from 57uff3r/dijsktra.py
Modified Python implementation of Dijkstra's Algorithm (https://gist.github.com/econchick/4666413)
class Graph:
def __init__(self):
self.nodes = set()
self.edges = defaultdict(list)
self.distances = {}
return self
def add_node(self, value):
self.nodes.add(value)
return self
@hsavit1
hsavit1 / NullObjectReferenceTypeProtocol.swift
Created November 16, 2015 16:15 — forked from eofster/NullObjectReferenceTypeProtocol.swift
An example implementation of Null Object pattern for reference types using protocol
protocol PhoneCall {
var identifier: Int { get }
var isNil: Bool { get }
func hangUp()
}
class RealPhoneCall: PhoneCall {
let identifier: Int
let isNil = false
@hsavit1
hsavit1 / FizzBuzz.swift
Created November 27, 2015 21:50 — forked from obecker/FizzBuzz.swift
FizzBuzz implementation in Swift
let numbers = AnySequence { () -> AnyGenerator<Int> in
var i = 1
return anyGenerator {
return i++
}
}.lazy
let fizzes = numbers.map { $0 % 3 == 0 ? "Fizz" : "" }
let buzzes = numbers.map { $0 % 5 == 0 ? "Buzz" : "" }
@hsavit1
hsavit1 / list.swift
Created November 28, 2015 05:03 — forked from oisdk/list.swift
enum List<Element> {
case Nil
indirect case Cons(head: Element, tail: List<Element>)
}
struct ListGenerator<Element> : GeneratorType {
private var list: List<Element>
mutating func next() -> Element? {
switch list {
case .Nil: return nil
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
@hsavit1
hsavit1 / CollectionViewDataSource.swift
Created November 30, 2015 05:51 — forked from andymatuschak/CollectionViewDataSource.swift
Type-safe value-oriented collection view data source
//
// CollectionViewDataSource.swift
// Khan Academy
//
// Created by Andy Matuschak on 10/14/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
@hsavit1
hsavit1 / gist:5ad303f1351e2678359a
Created November 30, 2015 05:52 — forked from andymatuschak/gist:2b311461caf740f5726f
A pragmatic and intentionally non-abstract solution to JSON decoding / initialization that doesn't require learning about five new operators.
struct User {
let id: Int
let name: String
let email: String?
}
extension User: JSONDecodable {
static func create(id: Int, name: String, email: String?) -> User {
return User(id: id, name: name, email: email)
}