Skip to content

Instantly share code, notes, and snippets.

View RuiAAPeres's full-sized avatar
💭
🏔🏃‍♂️

Rui Peres RuiAAPeres

💭
🏔🏃‍♂️
View GitHub Profile
Task { @MainActor in
}
// let base = [1,2,4,6,5,8,6]
// let filtered = base.takeUntil{ $0.isMultiple(of: 2) } // [2,4,6]
//
// let base = [2,4,6,5,8,6]
// let filtered = base.takeUntil{ $0.isMultiple(of: 2) } // [2,4,6]
//
// It stops collecting when the predicate fails (5,8,6 are not evaluated)
//
// Idea by https://twitter.com/K0nserv
//
public typealias CompletionHandler = Function<Void, Void>
public typealias SideEffect<Input> = Function<Input, Void>
public struct Function<Input, Output>: Identifiable {
public let id: String
internal let f: (Input) -> Output
public init(
id: String,
@RuiAAPeres
RuiAAPeres / Assertion+ReactiveSwift.swift
Last active August 5, 2020 10:46
Unit Testing ReactiveSwift streams with Assertions (idea by @andersio)
import ReactiveSwift
import XCTest
private let counterFailure: (Int, Int) -> String = { count, maxCount in
return "Counter is currently at \(count) which higher than the max \(maxCount)"
}
private let timeoutFailure = "Expectation timeout"
private let infoTemplate: (String, String, Int) -> String = { fileName, functionName, lineNumber in
@RuiAAPeres
RuiAAPeres / reactiveswift+grdb.swift
Last active August 30, 2021 17:24
ReactiveSwift extensions for GRDB
/// This heavily based on the work done at RxGRDB
import GRDB
import ReactiveSwift
extension DatabasePool: ReactiveExtensionsProvider {}
extension DatabaseQueue: ReactiveExtensionsProvider {}
extension ValueObservation: ReactiveExtensionsProvider {}
extension DatabaseRegionObservation: ReactiveExtensionsProvider {}
import Foundation
@propertyWrapper struct Notifier<Value> {
private let identifier: String
var wrappedValue: Value? {
didSet {
NotificationCenter.default.post(name: Notification.Name(identifier), object: wrappedValue)
}
}
public typealias CompletionHandler = Function<Void, Void>
public typealias SideEffect<Input> = Function<Input, Void>
public struct Function<Input, Output>: Identifiable {
public let id: String
internal let f: (Input) -> Output
public init(
id: String,
/// |                    World                 |
/// |------------------------------------------|
/// | Module A | Module B | Module C | Module D|
  1. World is a module
  2. World is aware of all modules.
  3. Modules aren't aware of World.
@RuiAAPeres
RuiAAPeres / opaqueEquivalent.swift
Last active February 18, 2020 07:50
Extension to provide an equivalent opaque UIColor to a UIColor with alpha channel. So: f(r,g,b,a) == (r,g,b,1)
import UIKit
public extension UIColor {
var components: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
@RuiAAPeres
RuiAAPeres / covariance_contravariance.swift
Last active September 16, 2019 16:06
Covariance and Contravariance in Swift
import UIKit
// Based on https://www.stephanboyer.com/post/132/what-are-covariance-and-contravariance
// > Denotes "a subtype of"
// UIButton > UIView > UIResponder > NSObject
//
// e.g. `UIButton` is a subtype of `UIView`
//
// This means that any function that takes a `UIView`, can receive a `UIButton`:
//