Skip to content

Instantly share code, notes, and snippets.

View werediver's full-sized avatar
💭
🦀

Raman Fedaseyeu werediver

💭
🦀
View GitHub Profile
/// Cast the argument to the infered function return type.
func autocast<T>(some: Any) -> T? {
return some as? T
}
protocol Foo {
static func foo() -> Self
}
class Vehicle: Foo {
@werediver
werediver / BasicServiceLocator.swift
Last active January 14, 2019 06:13
Basic Service Locator pattern implementation in Swift 2.
protocol ServiceLocator {
func getService<T>() -> T?
}
final class BasicServiceLocator: ServiceLocator {
// Service registry
private lazy var reg: Dictionary<String, Any> = [:]
private func typeName(some: Any) -> String {
import Foundation
protocol ServiceLocator {
func getService<T>(type: T.Type) -> T?
func getService<T>() -> T?
}
extension ServiceLocator {
enum AntGrammar: Grammar {
enum Failure: Error {
case invalidCodon
}
static func generate(_ rule: GenotypeIterating) throws -> String {
return try prog(rule)
}
@werediver
werediver / concurrency.swift
Last active July 3, 2017 09:06
Limiting concurrent tasks.
import Foundation
func dispatch_async_batch(tasks: [() -> ()], limit: Int, completion: (() -> ())?) {
if tasks.count > 0 || completion != nil {
let q = dispatch_queue_create("dispatch_async_batch", DISPATCH_QUEUE_CONCURRENT);
let sema = dispatch_semaphore_create(limit);
dispatch_async(q, {
for task in tasks {
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
@werediver
werediver / FatalError+Ext.swift
Last active July 3, 2017 09:05
Better `fatalError()`.
import Foundation
func fatalError<T>(@autoclosure message: () -> String = "", file: StaticString = #file, line: UInt = #line) -> T {
fatalError(message(), file: file, line: line)
}
// Demo
protocol ResultType {

Keybase proof

I hereby claim:

  • I am werediver on github.
  • I am werediver (https://keybase.io/werediver) on keybase.
  • I have a public key ASDVl2sxFUxEvo7PVGoMPO5s152IjIIoS-f3PkJ284jUJAo

To claim this, I am signing this object:

@werediver
werediver / CurryBench.swift
Created January 25, 2017 09:20
Type inference impact on Swift 2.2 code compile time.
// CurryLight.swift
func curry<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, U>(f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25) -> U) -> (T1) -> (T2) -> (T3) -> (T4) -> (T5) -> (T6) -> (T7) -> (T8) -> (T9) -> (T10) -> (T11) -> (T12) -> (T13) -> (T14) -> (T15) -> (T16) -> (T17) -> (T18) -> (T19) -> (T20) -> (T21) -> (T22) -> (T23) -> (T24) -> (T25) -> U {
return { (x1: T1) -> (T2) -> (T3) -> (T4) -> (T5) -> (T6) -> (T7) -> (T8) -> (T9) -> (T10) -> (T11) -> (T12) -> (T13) -> (T14) -> (T15) -> (T16) -> (T17) -> (T18) -> (T19) -> (T20) -> (T21) -> (T22) -> (T23) -> (T24) -> (T25) -> U in { (x2: T2) -> (T3) -> (T4) -> (T5) -> (T6) -> (T7) -> (T8) -> (T9) -> (T10) -> (T11) -> (T12) -> (T13) -> (T14) -> (T15) -> (T16) -> (T17) -> (T18) -> (T19) -> (T20) -> (T21) -> (T22) -> (T23) -> (T24) -> (T25) -> U in { (x3: T3) -> (T4) -> (T5) -> (T6) -> (T7) -> (T8) -> (T9) -> (T10)
@werediver
werediver / GenerateCurry.swift
Last active January 24, 2017 08:59
Generating `curry` function in Swift 2.2
// Swift 2.2
enum AccessLevel {
case Default
case Private
case Internal
case Public
var asPrefix: String {
switch self {
@werediver
werediver / StylingConcept2.swift
Last active October 19, 2016 16:14
A concept of UI styling for iOS
//: Playground - noun: a place where people can play
import UIKit
// MARK: - Skeleton
protocol StyleProtocol {
func apply(to some: Any)
}