Skip to content

Instantly share code, notes, and snippets.

View tsubasahiroe's full-sized avatar
🛴
Working from home

Tsubasa Hiroe tsubasahiroe

🛴
Working from home
View GitHub Profile
@mono0926
mono0926 / 1Database.swift
Last active June 2, 2022 08:15
FirestoreのRxSwiftとの組み合わせ。Codableも活用。 [追記] FirestoreのデータはCodable非対応のプロパティを持ててしまいそれが含まれると対応難しいのでCodableは捨てた方が良いかもしれない
//
// Database.swift
// Model
//
// Created by mono on 2017/10/14.
// Copyright © 2017 Masayuki Ono All rights reserved.
//
import Foundation
import FirebaseCore
// Modified from: https://github.com/katleta3000/CancelBlocks/blob/master/CancelBlocks.swift
typealias dispatch_cancelable_block_t = (cancel: Bool) -> (Void)
private func dispatch_after_delay(delay: Double, queue: dispatch_queue_t, block: dispatch_block_t?) -> dispatch_cancelable_block_t? {
guard let block = block else { return nil }
var originalBlock: dispatch_block_t? = block
var cancelableBlock: dispatch_cancelable_block_t? = nil
let delayBlock: dispatch_cancelable_block_t = {(cancel: Bool) -> Void in
if let originalBlock = originalBlock where !cancel {
@airspeedswift
airspeedswift / mergesort.swift
Last active January 20, 2017 12:29
Stable Swift merge sort
extension Array where Element: Comparable
{
mutating func mergesortInPlace() {
var tmp: [Generator.Element] = []
tmp.reserveCapacity(numericCast(self.count))
func merge(lo: Int, _ mi: Int, _ hi: Int) {
tmp.removeAll(keepCapacity: true)
tmp.extend(self[lo..<hi])
@davidpdrsn
davidpdrsn / mergesort.swift
Last active August 24, 2016 13:11
Merge sort in Swift
import Foundation
// Build 100 random numbers between 0 and 100
var numbers = Int[]()
for i in 1..100 {
let n = Int(arc4random() % 101)
numbers.append(n)
}
func elementsInRange<T>(a: T[], start: Int, end: Int) -> (T[]) {
@landonf
landonf / 1-README.md
Last active June 2, 2016 06:49
Swift Result Type

A result type (based on swiftz) that can represent either an error or success:

enum Result<X, T> {
  case Err(() -> X)
  case Ok(() -> T)
}

Now we need a way to chain multiple results together without lots of nesting of if statements -- or exceptions. To do so, we can define a new bind (result, next) operator (implementation borrowed from swiftz) that operates on Result types (a.k.a flatMap or >>=):

  • If the result is Err, the result is immediately returned.