Skip to content

Instantly share code, notes, and snippets.

@kean
kean / FutureCompatible.swift
Created November 20, 2019 12:22
FutureCompatible.swift
// The MIT License (MIT)
//
// Copyright (c) 2016-2019 Alexander Grebenyuk (github.com/kean).
import Foundation
/// Future extenions.
public struct FutureExtension<Base> {
/// Base object to extend.
public let base: Base
// Configuration settings file format documentation can be found at:
// https://help.apple.com/xcode/#/dev745c5c974
SUPPORTED_PLATFORMS = iphoneos macosx appletvos watchos iphonesimulator appletvsimulator watchsimulator
SWIFT_VERSION = 4.2
IPHONEOS_DEPLOYMENT_TARGET = 9.0
MACOSX_DEPLOYMENT_TARGET = 10.11
@kean
kean / AsyncAwaitTest.swift
Last active November 24, 2018 22:47
Fake (blocking) Async/Await for https://github.com/kean/FutureX
class AsyncAwaitTests: XCTestCase {
func testAsyncAwait() {
XCTAssertEqual(fakeAsyncAwait().wait().value, 3)
}
}
func fakeAsyncAwait() -> Future<Int, Error> {
return Future.async {
// Add delay to demonstrate that `await` for `Future<_, Never>` don't
@kean
kean / CancellationToken.swift
Last active November 15, 2018 19:23
CancellationToken
// MARK: - CancellationTokenSource
/// Manages cancellation tokens and signals them when cancellation is requested.
///
/// All `CancellationTokenSource` methods are thread safe.
final class CancellationTokenSource {
/// Returns `true` if cancellation has been requested.
var isCancelling: Bool {
_lock.lock(); defer { _lock.unlock() }
return _observers == nil
@kean
kean / spacers.swift
Last active January 10, 2018 18:12
Stacks and Spacers
public typealias Stack = UIStackView
public extension Stack {
@nonobjc public convenience init(_ views: UIView..., with: (UIStackView) -> Void = { _ in }) {
self.init(arrangedSubviews: views)
with(self)
}
@nonobjc public convenience init(_ views: [UIView], axis: UILayoutConstraintAxis = .horizontal, spacing: CGFloat = 0, alignment: UIStackViewAlignment = .fill, distribution: UIStackViewDistribution = .fill) {
self.init(arrangedSubviews: views)
@kean
kean / AutoRetry.swift
Last active September 20, 2023 20:21
Smart Auto Retry using RxSwift
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
import RxSwift
import RxCocoa
extension ObservableType {
@kean
kean / CancellationToken.swift
Last active December 17, 2017 14:52
Cancellation Token in Swift
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
/// Manages cancellation tokens and signals them when cancellation is requested.
///
/// All `CancellationTokenSource` methods are thread safe.
public final class CancellationTokenSource {
@kean
kean / Client.swift
Last active September 16, 2022 03:41
API Client (Archived)
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
import Alamofire
import RxSwift
import RxCocoa
// This post is **archived**. For a modern version that uses Async/Await and Actors, see the new article
@kean
kean / CtCI-6h-problem-4.9.markdown
Last active February 24, 2023 14:53
CtCI 6h Edition, Problem 4.9: BST Sequences.

Problem 4.9. BST Sequences: A binary search tree was created by traversing through an array from left to right and inserting each element. Given a binary search tree with distinct elements, print all possible arrays that could have led to this tree.

Solution.

Let's start with an example.

    4
   / \
  2   5 
@kean
kean / List.swift
Last active March 7, 2020 10:01
Proof-of-concept List as Swift Enum
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
/// Proof-of-concept List implementation, not optimized in any way.
///
/// Usage:
///