Skip to content

Instantly share code, notes, and snippets.

View Qata's full-sized avatar

Charles Maria Tor Qata

View GitHub Profile
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() {
@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()
}
}
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
}
}
}
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
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 {
@Qata
Qata / JSON.swift
Last active April 18, 2019 00:27
A JSONDecoder that wraps a JSONParser that isomorphically preserves the values of floating point numbers by treating all numbers as strings until instantiation of the exact type.
import Foundation
public indirect enum JSON: Equatable {
case null
case string(String)
case number(String)
case bool(Bool)
case dictionary([String: JSON])
case array([JSON])
}
@Qata
Qata / CTJSON.c
Last active January 30, 2019 23:12
//
// CTJSON.c
// CTObject
//
// Created by Charlotte Tortorella on 24/10/13.
// Copyright (c) 2013 Charlotte Tortorella. All rights reserved.
//
#include "CTJSON.h"
#include "CTFunctions.h"