This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Array { | |
/// Returns unique pairs from array | |
/// Following is result of `[1, 2, 3, 4].pairs()` | |
/// [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] | |
func pairs() -> [(Element, Element)] { | |
return enumerated() | |
.flatMap { index, element in | |
dropFirst(index.advanced(by: 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Zip3Sequence.swift | |
// | |
// Created by はるふ on 2018/05/28. | |
// Copyright © 2018年 ha1f. All rights reserved. | |
// | |
import Foundation | |
/// An iterator for `Zip3Sequence`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OperationGroup<T1, T2> { | |
private var item1: T1? | |
private var item2: T2? | |
private var observers: [(T1, T2) -> Void] = [] | |
func notify1(_ item: T1) { | |
item1 = item | |
_notifyIfNeeded() | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Drinkable { | |
} | |
struct 🍶: Drinkable {} | |
struct 🍺: Drinkable {} | |
struct 🍸: Drinkable {} | |
struct Person { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/// Debouncer is useful for situations like following. | |
/// If we want to execute action n sec after the last trigger. | |
public final class Debouncer<T> { | |
let dispatchQueue: DispatchQueue | |
let interval: TimeInterval | |
let action: (T) -> Void | |
private var _lastDispatchTime: DispatchTime? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UserDefaultsStoreItem.swift | |
// | |
// Created by ha1f on 2019/02/26. | |
// Copyright © 2019年 ha1f. All rights reserved. | |
// | |
import Foundation | |
struct UserDefaultsStoreItem<T> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// KeyboardSizeObserver.swift | |
// KeyboardFrameObserver | |
// | |
// Created by ha1f on 2019/02/08. | |
// Copyright © ha1f 2019年 ha1f. All rights reserved. | |
// | |
import UIKit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class DeallocNotifier { | |
private var _observers: [() -> Void] = [] | |
func observe(_ observer: @escaping () -> Void) { | |
_observers.append(observer) | |
} | |
deinit { | |
_observers.forEach { observer in | |
observer() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CircleView.swift | |
// | |
// Created by ha1f on 2019/02/27. | |
// Copyright © 2019年 ha1f. All rights reserved. | |
// | |
import UIKit | |
/// I guess this is faster than using cornerRadius. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct YearMonthDay { | |
var year: Int | |
var month: Int | |
var day: Int | |
/// Extracts yearMonthDay from Date, using calendar. | |
static func from(_ date: Date, calendar: Calendar = Calendar(identifier: .gregorian)) -> YearMonthDay { | |
let components = calendar.dateComponents([.year, .month, .day], from: date) |