Skip to content

Instantly share code, notes, and snippets.

View Sajjon's full-sized avatar
💭
Core Developer @ Parity 12ktCaPgc745AwxALoU4E2NAhAc8aW3ytSM5Qigi3SwaNodc

Alexander Cyon Sajjon

💭
Core Developer @ Parity 12ktCaPgc745AwxALoU4E2NAhAc8aW3ytSM5Qigi3SwaNodc
View GitHub Profile
/// Returns a binary predicate using the given key path to create an ascending order
/// for elements of type `Root`.
func ascending<Root, Value: Comparable>(_ path: KeyPath<Root, Value>) -> (Root, Root) -> Bool {
return { $0[keyPath: path] < $1[keyPath: path] }
}
/// Returns a binary predicate using the given key path to create a descending order
/// for elements of type `Root`.
func descending<Root, Value: Comparable>(_ path: KeyPath<Root, Value>) -> (Root, Root) -> Bool {
return { $0[keyPath: path] > $1[keyPath: path] }
@Sajjon
Sajjon / ValidIdentifierHeadsInSwift.swift
Last active September 4, 2018 09:31
Swift Valid Identifier Heads
import UIKit
import Foundation
// Written in Swift 4.2, Xcode 10 beta 6
// Valid Identifier Heads found at: https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html
// Code inspired by Nate Cooks gist: https://gist.github.com/natecook1000/c5fb2b8cd0967f53770e
extension UnicodeScalar : Strideable {
@Sajjon
Sajjon / Weakify.swift
Last active September 9, 2018 20:33
Weakify / Unownify Methods
typealias NoArgMethod<StrongObject, ReturnType> = (StrongObject) -> () -> ReturnType
typealias SingleArgumentMethod<Argument, StrongObject> = (StrongObject) -> (Argument) -> Void
func unown<O, R>(
_ object: O, // This is strong
_ method: @escaping NoArgMethod<O, R>
) -> () -> R where O: AnyObject, R: Any
{
return { [unowned object] in
@Sajjon
Sajjon / AnyNavigator.swift
Created September 9, 2018 20:33
Typeerasure for AnyNavigator
struct AnyNavigator<Destination_>: Navigator {
typealias Destination = Destination_
private let _navigateTo: (Destination_) -> Void
private let _start: () -> Void
init<Concrete>(_ concrete: Concrete) where Concrete: Navigator, Concrete.Destination == Destination_ {
_navigateTo = concrete.navigate
_start = concrete.start
}
@Sajjon
Sajjon / ViewModelType.swift
Last active September 20, 2018 17:24
ViewModelType.swift - credits to: Sergey Shulga @sergdort
protocol ViewModelType {
associatedtype Input
associatedtype Output
func transform(input: Input) -> Output
}
@Sajjon
Sajjon / Settings.swift
Last active October 19, 2018 08:10
Single-Line Controller Pattern
/* This ⬇ single line ⬇ constitutes a fully working ViewController. Clean code 👌🏽 */
typealias Settings = Scene<SettingsView>
@Sajjon
Sajjon / SettingsView.swift
Created October 19, 2018 09:12
Simple Settings View
import UIKit
import RxSwift
import RxCocoa
// MARK: - SettingsView
final class SettingsView: ScrollingStackView {
private lazy var signOutButton: UIButton = "Sign out"
// Two `UILabel`s grouped together
@Sajjon
Sajjon / ViewModelled.swift
Last active October 19, 2018 12:03
Bridge between a controller's View and its ViewModel
import RxCocoa
protocol EmptyInitializable {
init()
}
protocol ViewModelled: EmptyInitializable {
associatedtype ViewModel: ViewModelType
var inputFromView: ViewModel.Input { get }
@Sajjon
Sajjon / AuthenticateView.swift
Last active November 26, 2018 13:52
Medium article: SLC part 1 - View
import UIKit
import RxSwift
import RxCocoa
final class AuthenticateView: UIView {
private lazy var signUpButton = UIButton(type: .custom)
private lazy var signInButton = UIButton(type: .custom)
private lazy var stackView = UIStackView(arrangedSubviews: [signUpButton, signInButton])
@Sajjon
Sajjon / ViewModelled.swift
Last active November 26, 2018 15:26
Medium article: SLC part 1 - ViewModelled
import RxSwift
protocol EmptyInitializable {
init()
}
protocol ViewModelled: EmptyInitializable {
associatedtype ViewModel: ViewModelType