Skip to content

Instantly share code, notes, and snippets.

@pauljohanneskraft
pauljohanneskraft / Abstract - Memoizer.md
Last active June 10, 2016 08:54
Memoizer for e.g. recursive mathematical functions like fibonacci, factorial, etc, inspired by "Advanced Swift" - WWDC 15

Memoizer

Memoizer is a struct, that can be used to calculate different recursive functions with repeatingly the same values.

To do that it saves every single calculated result in a Dictionary and can then, if repeatedly called, just take a already calcualted result from the dictionnary instead of recalculating it.

Use cases

Fibonacci numbers, factorial, see MemoizerExamples.swift

@pauljohanneskraft
pauljohanneskraft / Abstract - Atomic.md
Last active July 1, 2016 00:23
Atomic - locking a value using a NSLock-wrapper-struct

Atomic

var myAtomic = Atomic<Int>(5)
myAtomic.synced { $0 = 2 }
print( myAtomic.value )
// Prints "2"

Atomic is a generic wrapper-struct for values that need to be accessible safely, when working concurrently.

@pauljohanneskraft
pauljohanneskraft / Abstract - CatchLoop.md
Last active June 9, 2016 19:01
CatchLoop - catches all Errors being thrown and safes either the return value or the thrown error in a Dictionary

CatchLoop

The function can be used to iterate over a sequence (implementing Sequence-protocol).

It returns a dictionary containing either the returned value or a thrown error.

@pauljohanneskraft
pauljohanneskraft / Abstract - StringSubscripts.md
Last active June 25, 2019 10:35
A Swift Int-subscript for Strings

String subscripts

Implementation of string subscripts using Int.

var str = "😂😅😊😜😝"
str[0..<2] = ""
print(str)
// Prints "😊😜😝"
@pauljohanneskraft
pauljohanneskraft / Abstract - Dijkstra.md
Last active March 24, 2017 09:58
Dijkstra-algorithm implemented in Swift, heavily using Dictionaries (e.g. for finding edge, finding vertex, etc)
@pauljohanneskraft
pauljohanneskraft / Abstract - Numeric.md
Last active June 11, 2016 17:24
A try to combine numeric types in Swift to enable easy implementation of mathematical functions, ...

Numeric

Numeric is a protocol which unites numeric values.

inherited operators

!=, ==, <, >, <=, >=

+, -, *, /, %

@pauljohanneskraft
pauljohanneskraft / Abstract - Constants.md
Last active June 9, 2016 18:59
an enum containing some important constants in mathematics and physics, as well as a function returning a set of integers up to a given one (exclusive)

Constants

constant values

pi, tau, e, y_0, h, c

often used sets

Z(_: Int) for sets of integer up to (excluding) a given number.

@pauljohanneskraft
pauljohanneskraft / Abstract - GroupLikeAlgebraicStructures.md
Last active June 9, 2016 20:33
A try to model group-like algebraic structures

Modelling group-like algebraic structures

Magma

Requirements: The given operation is closed under the given set.

Semigroup

Requirements: is a Magma, operation is associative. (a • b) • c == a • (b • c)

String manipulation operators

let fiveTimesHello = 5 * "hello" // "hellohellohellohellohello"
let helloPlus5 = "hello" + 5 // "hello5"
@pauljohanneskraft
pauljohanneskraft / Abstract - BoolExtensions.md
Last active June 9, 2016 18:58
extension Bool : Comparable

Bool extensions

Comparable