Skip to content

Instantly share code, notes, and snippets.

@kean
kean / DFCoreDataMigration.h
Last active June 18, 2016 20:30
DFCoreDataMigration.h
// The MIT License (MIT)
//
// Copyright (c) 2016 Alexander Grebenyuk (github.com/kean).
#import <CoreData/CoreData.h>
extern NSString *__nonnull const DFMigrationErrorDomain;
typedef NS_ENUM(NSInteger, DFMigrationError) {
DFMigrationErrorSourceModelNotFound = 1,
@kean
kean / DFCoreDataMigration.m
Last active June 20, 2016 20:50
DFCoreDataMigration.m
// The MIT License (MIT)
//
// Copyright (c) 2016 Alexander Grebenyuk (github.com/kean).
#import "DFCoreDataMigration.h"
NSString *const DFMigrationErrorDomain = @"DFMigrationErrorDomain";
@implementation DFMigrationManager
@kean
kean / cd_progressive_migration.m
Last active June 19, 2016 09:42
Tricking Core Data Into Performing Progressive Migrations
+ (BOOL)migrateStoreAtURL:(NSURL *)storeURL
models:(NSArray<NSManagedObjectModel *> *)moms // @[ mom_v1, mom_v2, mom_v3 ... ];
error:(NSError **)error {
// Find an index of the current store's mom
NSDictionary *meta = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:error];
if (!meta) {
return NO;
}
NSInteger idx = [moms indexOfObjectPassingTest:^BOOL(NSManagedObjectModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
return [obj isConfiguration:nil compatibleWithStoreMetadata:meta];
@kean
kean / Migration.swift
Created June 20, 2016 20:05
Core Data Progressive Migration
// The MIT License (MIT)
//
// Copyright (c) 2016 Alexander Grebenyuk (github.com/kean).
import Foundation
import CoreData
enum MigrationError: ErrorProtocol {
case IncompatibleModels
}
@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")
@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
@kean
kean / CoreImageHelpers.swift
Created August 22, 2016 14:57
CoreImageHelpers
// MARK - CoreImage
private let sharedContext = CIContext(options: [kCIContextPriorityRequestLow: true])
/// Core Image helper methods.
public extension UIImage {
/// Applies closure with a filter to the image.
///
// Performance considerations. Chaining multiple CIFilter objects is much
/// more efficient then using ProcessorComposition to combine multiple
@kean
kean / Nuke.podspec
Last active September 16, 2016 08:01
Nuke 4.0-beta3 iOS 8
Pod::Spec.new do |s|
s.name = 'Nuke'
s.version = '4.0-beta3'
s.summary = 'A powerful image loading and caching framework'
s.description = <<-EOS
A powerful image loading and caching framework.
Has full featured UI extensions, support for image filters, optional Alamofire and FLAnimatedImage plugins and [more](https://github.com/kean/Nuke).
EOS
@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:
///
@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