Skip to content

Instantly share code, notes, and snippets.

Dmitry Zakharov comfly

Block or report user

Report or block comfly

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View ContextProvider.swift
class ContextProvider {
struct _$ {
static let model = NSManagedObjectModel.mergedModelFromBundles(nil)
static let coordinator: NSPersistentStoreCoordinator = {
let result = NSPersistentStoreCoordinator(managedObjectModel: model)
var error: NSError?
let store = result.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: URL, options: nil, error: &error)
if !store {
NSLog("Error creating store coordinator: %@", error!)
View FRC for Dependent entities.m
extern NSManagedObjectContext *GetContext(void);
@interface TableViewController () <NSFetchedResultsControllerDelegate>
@property (nonatomic) NSFetchedResultsController *controller;
@end
@implementation TableViewController
@comfly
comfly / Summands.hs
Last active Jun 8, 2016
All summands combinations
View Summands.hs
summands 0 = []
summands n = [n]:concat [summandsOfTwo k (n - k) | k <- [1..n `quot` 2]]
where summandsOfTwo l r =
let le = map (r:) (summands l)
re = map (l:) (summands r)
in if l == r then le else le ++ re
View SpinLock.swift
public final class SpinLock {
private var lock: OSSpinLock = OS_SPINLOCK_INIT
public func locked<A>(@noescape f: () throws -> A) rethrows -> A {
OSSpinLockLock(&lock)
defer { OSSpinLockUnlock(&lock) }
return try f()
}
}
View Using queues.swift
let queue = dispatch_queue_create("QUEUE", DISPATCH_QUEUE_CONCURRENT)
var holder: Int = 10
func readSafe() -> Int {
var read: Int!
dispatch_sync(queue) {
read = holder
}
return read
View Atomic.swift
protocol Lock {
func lock() throws
func unlock()
}
protocol Atomic {
associatedtype Protected
associatedtype Primitive: Lock
var lock: Primitive { get }
View synchronized.swift
func synchronized(object: AnyObject, @noescape closure: () -> Void) {
objc_sync_enter(object)
closure()
objc_sync_exit(object)
}
View shit.swift
import Foundation
infix operator <| { associativity right precedence 95 }
func <|<A, R>(@noescape f: A -> R, a: A) -> R {
return f(a)
}
func <|<A, B, R>(@noescape f: (A, B) -> R, a: (A, B)) -> R {
return f(a.0, a.1)
}
View ProveBST.hs
module Main where
data Tree a = Leaf | Node a (Tree a) (Tree a)
isBST :: Tree Double -> Bool
isBST = fst . isBST'
where isBST' Leaf = (True, Nothing)
isBST' (Node n left right) =
let (l, lminmax) = isBST' left
~(r, rminmax) = isBST' right
@comfly
comfly / rle.hs
Created Jun 8, 2016
Run length encoding
View rle.hs
module Main where
import Data.Foldable
rle = (concatMap (\(sym, num) -> sym:show num)) . foldr' collect []
where collect s [] = [(s, 1)]
collect s all@((sym, num):others)
| sym == s = (sym, num + 1):others
| otherwise = (s, 1):all
You can’t perform that action at this time.