Skip to content

Instantly share code, notes, and snippets.

@pauljohanneskraft
pauljohanneskraft / Abstract - Locked.md
Last active August 4, 2017 09:55
Locked makes it possible to lock certain variables by a key

Locked

var myLocked = Locked(value: Int(5), key: 5)
try myLocked.access(key: 5) { $0 = 10 }
@pauljohanneskraft
pauljohanneskraft / Abstract - OutOfPlace.md
Last active July 4, 2016 00:20
Make all in-place-operations out-of-place by using this function

Out of place

This function makes it possible to use an out-of-place operation on some object where only in-place variants exist. simply use it as following

func outOfPlaceVariant(_ element: T) -> T {
  outOfPlace(with: element) { $0.inPlaceVariant() }
}
@pauljohanneskraft
pauljohanneskraft / Abstract - Safe.md
Last active July 1, 2016 00:02
Safe - a wrapper struct to safely access data

Safe

let myNumber = 5
var mySafe = Safe(myNumber)
try mySafe.access {
  $0 += 10
  if $0 > 14 { throw SomeError.isGreaterThanFourteen }
}
print( mySafe.value )
@pauljohanneskraft
pauljohanneskraft / Abstract - CommonFields.md
Last active July 3, 2016 19:10
Implementation of common mathematical fields, such as R, N, Z, Q, C

Common Fields

typealias R = Double 
typealias N = UInt 
typealias Z = Int
typealias Q = RationalNumber
typealias C = ComplexNumber<Double>
@pauljohanneskraft
pauljohanneskraft / Abstract - Function.md
Last active June 30, 2016 23:58
A way to describe a function using a wrapper struct

Function

let addition = Function(call: { (c: Int, d: Int) -> Int in return c + d }, description: "$0 + $1")

print( addition )
// Prints "$0 + $1"

print( addition.call((5,3)) )
// Prints "8"
@pauljohanneskraft
pauljohanneskraft / Abstract - Quantifiers.md
Last active July 1, 2016 00:05
Mathematical logical quantifiers implemented in Swift

Quantifiers

let b = [0,1,2]   { $0 <= 1 } // true
let c = [0,1,2]   { $0 < 10 } // true
let d = [0,1,2] ∃! { $0 == 1 } // true

Mathematical logical operators, like the universal (∀) and the existential (∃) quantifier, as well as ∃! meaning "exactly one exists".

@pauljohanneskraft
pauljohanneskraft / Abstract - ReferenceValueTypeWrappers.md
Last active June 15, 2016 13:27
Handle value types as if they were references or references as if they were value types in Swift

Reference / Value Type

The Reference wrapper class for any Type in Swift makes it possible to handle objects from structs, as if they were classes (e.g. imitates behavior of Java references.)

The ValueType wrapper on the other hand mimics the behavior of structs for class objects.

You can also wrap the same type into its wrapper, but this doesn't make sense, even though it is possible.

Matrix operators

Matrices: +, +=, -, -=, *, *=, /, /=, LaTeX
Vectors:  ^⟙

Collection operators

let isSubset = [0,1]  [0,1,2] // true
let intersection = [0,1]  [0,2] // [0]
let union = [0,1]  [0,2] // [0,1,2]

Object manipulation operators

swap <->