Skip to content

Instantly share code, notes, and snippets.

View Qata's full-sized avatar

Charles Maria Tor Qata

View GitHub Profile
@Qata
Qata / Bool.swift
Last active February 28, 2020 03:58
public extension Bool {
func `if`<T>(true truth: @autoclosure () -> T, false falsity: @autoclosure () -> T) -> T {
switch self {
case true:
return truth()
case false:
return falsity()
}
}
@Qata
Qata / Either.swift
Last active February 28, 2020 03:57
import Foundation
public enum Either<Left, Right> {
case left(Left)
case right(Right)
public var left: Left? {
switch self {
case let .left(value):
return value
public extension Collection {
/// Allows lensing properties before comparison by the std lib `firstIndex(where:)` function, using the `where` closure.
func firstIndex<T>(lensing path: KeyPath<Element, T>, where predicate: (T) -> Bool) -> Index? {
return firstIndex {
predicate($0[keyPath: path])
}
}
}
public extension Collection {
public struct RingBuffer<Element> {
public let capacity: Int
private var underlying: [Element] = []
private var index: Int = 0
public init(capacity: Int, initial: [Element] = []) {
precondition(capacity > 0, "A positive capacity is required")
underlying = Array(initial.suffix(capacity))
self.capacity = capacity
index = underlying.count % capacity
prefix operator ==
public prefix func == <T: Equatable>(value: T) -> (T) -> Bool {
return {
value == $0
}
}
@Qata
Qata / Actor.swift
Last active August 28, 2019 07:03
Actors using Combine
import Combine
import Foundation
class Actor<Message, State> {
@Published public private(set) var state: State
private var cancellables: Set<AnyCancellable>
let inbox = PassthroughSubject<Message, Never>()
init(initialState: State) {
state = initialState
@Qata
Qata / Middleware.swift
Last active August 6, 2019 05:27
Recombine
/**
Middleware is a structure that allows you to modify, filter out and dispatch more
actions, before the action being handled reaches the store.
*/
public struct Middleware<State, Action> {
typealias Transform = (State, Action) -> [Action]
internal let transform: (State, Action) -> [Action]
/// Create a blank slate Middleware.
public init() {
import Foundation
indirect enum LinkedList<Element> {
case node(element: Element, next: LinkedList)
case end
init<S: Sequence>(values: S, last: LinkedList = .end) where S.Element == Element {
self = values.reversed().reduce(last, { .node(element: $1, next: $0) })
}
public extension LazySequence {
/// Create an infinitely repeating sequence of values identical to `value`.
///
/// let names = ["Jane", "Jack", "Gwynne", "Micky"]
/// let hellos = LazySequence(repeating: "Hello")
/// let g = zip(hellos, names).map { "\($0), \($1)" }
/// g == ["Hello, Jane", "Hello, Jack", "Hello, Gwynne", "Hello, Micky"]
///
/// - Parameter repeating: `repeating` takes the element to be repeated.
/// - Returns: An infinite lazy sequence of `value`.
extension Optional {
func filter(_ isIncluded: (Wrapped) throws -> Bool) rethrows -> Optional<Wrapped> {
switch self {
case let value? where try isIncluded(value):
return value
default:
return nil
}
}
}