Skip to content

Instantly share code, notes, and snippets.

@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:
///
struct SearchView: View {
@ObservedObject var viewModel: SearchViewModel
var body: some View {
VStack {
TextField("Search", text: $viewModel.query)
List(viewModel.songs) {
Text($0.name)
}
}
import Future
import Combine
public extension Future {
var publisher: Combine.Future<Value, Error> {
Combine.Future { fulfill in
self.on(success: {
fulfill(.success($0))
}, failure: {
fulfill(.failure($0))
@kean
kean / _DynamicProperty.swift
Last active February 23, 2020 22:47
_DynamicProperty
// A speculative _ViewRendererHost implementation which uses Mirror (Swift reflection) to find all
// of the dynamic properties (`DynamicProperty`) associated with a given view (`View`), observe
// the changes to these properties and automatically refresh the view whenever any of these property change.
protocol _DynamicProperty {
var objectWillChange: AnyPublisher<Void, Never> { get }
}
extension ObservedObject: _DynamicProperty {
var objectWillChange: AnyPublisher<Void, Never> {
// The MIT License (MIT)
//
// Copyright (c) 2020 Alexander Grebenyuk (github.com/kean).
import CoreData
final class FetchedEntities<Element: NSManagedObject>: NSObject, NSFetchedResultsControllerDelegate, RandomAccessCollection, ObservableObject {
private let controller: NSFetchedResultsController<Element>
init(context: NSManagedObjectContext, request: NSFetchRequest<Element>) {
@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
@kean
kean / Promise.swift
Last active February 15, 2019 05:15
Micro Promise under 100 sloc in Swift
// The MIT License (MIT)
//
// Copyright (c) 2016 Alexander Grebenyuk (github.com/kean).
import Foundation
public class Promise<T> {
private var state: State<T> = .pending(Handlers<T>())
private var queue = DispatchQueue(label: "com.github.kean.Promise")
// 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 / books.md
Last active November 19, 2018 11:47
recommend_to_a_friend

books that I would recommend to a friend (not in any particular order, they are all on different topics):

  • Pro Git by Scott Chacon
  • The Art of Concurrency by Clay Breshears
  • Algorithm Design by Jon Kleinberg , Eva Tardos
  • Structured Computer Organization by Andrew S. Tanenbaum
  • Structure and Interpretation of Computer Programs by H. Abelson and G.J. Sussman
  • The C Programming Language by Brian Kernighan, Dennis Ritchie
  • Code: The Hidden Language of Computer Hardware and Software by Charles Petzold
  • The Pragmatic Programmer: From Journeyman to Master by Andrew Hunt