Skip to content

Instantly share code, notes, and snippets.

View ha1f's full-sized avatar

はるふ ha1f

View GitHub Profile
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))
@ha1f
ha1f / Zip3Sequence.swift
Last active July 30, 2018 09:09
Implementation of zip3
//
// Zip3Sequence.swift
//
// Created by はるふ on 2018/05/28.
// Copyright © 2018年 ha1f. All rights reserved.
//
import Foundation
/// An iterator for `Zip3Sequence`.
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()
}
protocol Drinkable {
}
struct 🍶: Drinkable {}
struct 🍺: Drinkable {}
struct 🍸: Drinkable {}
struct Person {
}
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?
//
// UserDefaultsStoreItem.swift
//
// Created by ha1f on 2019/02/26.
// Copyright © 2019年 ha1f. All rights reserved.
//
import Foundation
struct UserDefaultsStoreItem<T> {
//
// KeyboardSizeObserver.swift
// KeyboardFrameObserver
//
// Created by ha1f on 2019/02/08.
// Copyright © ha1f 2019年 ha1f. All rights reserved.
//
import UIKit
final class DeallocNotifier {
private var _observers: [() -> Void] = []
func observe(_ observer: @escaping () -> Void) {
_observers.append(observer)
}
deinit {
_observers.forEach { observer in
observer()
//
// 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.
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)