Skip to content

Instantly share code, notes, and snippets.

@rbrockerhoff
rbrockerhoff / bug.swift
Last active August 29, 2015 14:04
Must be a Swift bug
extension Dictionary {
mutating func merge <S: Sequence where S.GeneratorType.Element == Element> (seq: S) {
var gen = seq.generate()
while let (key: KeyType, value: ValueType) = gen.next() {
self[key] = value
}
}
}
var dict = [0:0]
@rbrockerhoff
rbrockerhoff / Decimal.md
Created March 18, 2016 22:35
Pre-proposal: Safer Decimal Calculations

Pre-proposal: Safer Decimal Calculations

Quoting the “The Swift Programming Language” book: “Swift adopts safe programming patterns…”; “Swift is friendly to new programmers”. The words “safe” and “safety” are found many times in the book and in online documentation. The usual rationale for safe features is, to quote a typical sentence, “…enables you to catch and fix errors as early as possible in the development process”.

One frequent stumbling point for both new and experienced programmers stems from the vagaries of binary floating-point arithmetic. This tentative pre-proposal suggests one possible way to make the dangers somewhat more clear.

@rbrockerhoff
rbrockerhoff / dictext.swift
Last active March 6, 2020 19:19
Some useful extensions to Dictionary
extension Dictionary {
init (_ array: Array<Element>) {
self = [ : ]
self.merge(array)
}
mutating func merge (array: Array<Element>) {
for (key: KeyType, value: ValueType) in array {
self[key] = value
}