Skip to content

Instantly share code, notes, and snippets.

@pauljohanneskraft
pauljohanneskraft / Using async functions in Combine.md
Last active September 8, 2023 09:40
Using async/await from Combine

With the introduction of async/await, you might wonder how you can use the new feature in combination with Combine.

The code posted below allows you to write the following constructs:

let somePublisher = [1, 2, 3].publisher

func someAsyncFunction(_ int: Int) async -> Int {
    return int
}
struct KeychainItem {
// MARK: Nested Types
enum KeychainError: Error {
case noPassword
case unexpectedPasswordData
case unexpectedItemData
case unhandledError(status: OSStatus)
}

ScreenMonitoringNotifier

ScreenMonitoringNotifier uses a timer with a specific timeInterval - default is 1 second - to check whether any screen is potentially being monitored. Potentially being monitored includes active screen recordings and the use of external displays.

Use ScreenMonitoringNotifier.shared to send out notifications or instantiate your own ScreenMonitoringNotifier to use callbacks.

Either

Use Either to allow multiple result types to be combined into one. You can then switch over the specific types to evaluate the result. Either can be used as described below.

let _b = false

func b() -> Either<String, Int> {
    if _b {
 return *""

(Sparse) Memoizer

Initialization

let fibonacci = memoize { x, op in x < 2 ? x : op(x-1) + op(x-2) }
let fibonacciSparse = sparseMemoize(when: { $0 % 3 != 0 }) { x, op in x < 2 ? x : op(x-1) + op(x-2) }

Use

ALU in VHDL

This is an implementation of a 16-bit ALU in VHDL.

Inputs

Name Purpose Size
R, S operands 16-bit integer
I operation 3-bit
@pauljohanneskraft
pauljohanneskraft / Abstract - DayOfTheWeek.md
Last active September 11, 2019 02:06
returns the day of the week of any date between 01.01.1700 to 31.12.2199

DayOfTheWeek

This function returns the weekday of any date between 1700 and 2199.

print(try dayOfTheWeek("28.09.1964")) // Prints "Monday"
print(try dayOfTheWeek("12.03.2014")) // Prints "Wednesday"
print(try dayOfTheWeek("31.12.1980")) // Prints "Wednesday"
@pauljohanneskraft
pauljohanneskraft / Abstract - StringReplacer.md
Created July 23, 2016 14:58
enables fast switching between certain letters in Strings

String Replacer

enables fast switching between certain letters in Strings

var s = StringReplacer(("E", "3"), ("g", "9"), ("O", "0"), ("o", "0"), ("l", "1"))

s["1"] = "l"

var str = "hallo"
@pauljohanneskraft
pauljohanneskraft / Abstract - CachedProperties.md
Last active February 13, 2024 16:11
cache a complicated computed property using the following pattern

Cached Properties

Complicated, time expensive computed properties can be cached using the following pattern.

var a = Cached(0) {
    (a: Int) -> Int in
    print("did calculate")
    return a + 100
}
@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 }