Skip to content

Instantly share code, notes, and snippets.

View hollance's full-sized avatar

Matthijs Hollemans hollance

View GitHub Profile
extension String {
subscript(i: Int) -> Character {
return Array(self)[i]
}
}
var s = "Hello world"
println(s[0])
@hollance
hollance / Set.swift
Created June 23, 2014 16:05
Very basic set in Swift
class Set<T: Hashable>: Sequence, Printable {
var dictionary = Dictionary<T, Bool>() // private
func addElement(newElement: T) {
dictionary[newElement] = true
}
func removeElement(element: T) {
dictionary[element] = nil
}
import Foundation
import CoreGraphics
@infix func * (left: CGPoint, right: (a: CGFloat, b: CGFloat)) -> CGPoint {
return CGPoint(x: left.x * right.a, y: left.y * right.b)
}
let pt1 = CGPointMake(100, 50)
let pt2 = pt1 * (2, 3)
@hollance
hollance / PrimeFactorSequence.swift
Last active August 29, 2015 14:05
Solution to Explore Swift Challenge #1
/*
The code from https://gist.github.com/GarthSnyder/c8efe80675cd00322b51
rewritten as a Swift sequence and generator.
*/
import Foundation
struct PrimeFactorGenerator: GeneratorType {
var n: Int
var divisor = 2
@hollance
hollance / TakeWhile.swift
Created August 15, 2014 07:11
Explore Swift Challenge #2
struct TakeWhile<S: SequenceType>: SequenceType {
private let seq: S
private let block: (S.Generator.Element) -> Bool
init(_ seq: S, block: (S.Generator.Element) -> Bool) {
self.seq = seq
self.block = block
}
func generate() -> GeneratorOf<S.Generator.Element> {
var gen = seq.generate()
return GeneratorOf<S.Generator.Element> {
@hollance
hollance / gist:c0e1f84d2932ce39fed8
Created August 22, 2014 17:55
Calculating height for a UILabel
var rect = CGRect(x: 100, y: 10, width: 205, height: 10000)
addressLabel.frame = rect
addressLabel.sizeToFit()
rect.size.height = addressLabel.frame.size.height
addressLabel.frame = rect
@hollance
hollance / Boyer-Moore.swift
Last active December 27, 2018 04:50
Boyer-Moore string search algorithm in Swift 2.0
/*
Boyer-Moore string search
This code is based on the article "Faster String Searches" by Costas Menico
from Dr Dobb's magazine, July 1989. (Yes, 1989!)
http://www.drdobbs.com/database/faster-string-searches/184408171
*/
extension String {
func indexOf(pattern: String) -> String.Index? {
@hollance
hollance / Barfdown.markdown
Last active March 25, 2023 18:16
A markdown-ish parser written in Swift 2.0

Barfdown: a Markdown-ish Parser Written in Swift

Goals for this project:

  • Parse a simplified version of Markdown that is good enough for writing my blog posts.
  • Be reasonably efficient. This means the parser shouldn't copy substrings around if not necessary. This is done by storing all the elements as indexes into the original text.
  • Be small and therefore be easy to understand.
  • No regular expressions. They are the lazy person's solution to parsing. ;-)

This is just a toy project for me to experiment with writing parsers in Swift. Because why not?

@hollance
hollance / BoundedPriorityQueue.swift
Created March 19, 2016 17:56
BoundedPriorityQueue
public class LinkedListNode<T: Comparable> {
var value: T
var next: LinkedListNode?
var previous: LinkedListNode?
public init(value: T) {
self.value = value
}
}
@hollance
hollance / fmincg.swift
Created March 31, 2016 18:46
fmingc: minimizes a continuous differentiable multivariate function. Needs my Matrix type.
import Foundation
/**
Minimizes a continuous differentiable multivariate function.
Usage:
let (X, fX, i) = fmincg(f, X, length)
The starting point is given by `X` (a row vector).