Skip to content

Instantly share code, notes, and snippets.

View Koshimizu-Takehito's full-sized avatar
🏝️

takehito Koshimizu-Takehito

🏝️
View GitHub Profile
@Koshimizu-Takehito
Koshimizu-Takehito / StreamReader.swift
Created February 17, 2019 02:24 — forked from sooop/StreamReader.swift
Read a large text file line by line - Swift 3
import Foundation
class StreamReader {
let encoding: String.Encoding
let chunkSize: Int
let fileHandle: FileHandle
var buffer: Data
let delimPattern : Data
var isAtEOF: Bool = false
@Koshimizu-Takehito
Koshimizu-Takehito / Signal+split.swift
Last active March 31, 2019 08:34
split Signal/SignalProducer
import ReactiveCocoa
import ReactiveSwift
extension SignalProducer {
func result() -> SignalProducer<Result<Value, Error>, Never> {
return map(Swift.Result<Value, Error>.success)
.flatMapError { SignalProducer<Result<Value, Error>, Never>(value: .failure($0)) }
}
}
public protocol EitherProtocol {
associatedtype Left
associatedtype Right
var either: Either<Left, Right> { get }
}
public enum Either<Left, Right> {
case left(Left)
case right(Right)
public protocol ResultProtocol {
associatedtype Success
associatedtype Failure: Error
var result: Result<Success, Failure> { get }
}
extension Result: ResultProtocol {
public var result: Result<Success, Failure> {
return self
#if canImport(Foundation) && canImport(Accelerate)
import Foundation
import Accelerate
extension la_object_t {
public var rows: UInt { la_matrix_rows(self) }
public var cols: UInt { la_matrix_cols(self) }
public func array1D() -> [Double] {
///
/// ```swift
/// let fibonacci = memoize { (function: (Int) -> Int, n: Int) -> Int in
/// if n < 2 {
/// return n
/// }
/// return function(n - 1) + function(n - 2)
/// }
/// ```
///
import SwiftUI
import enum Accelerate.vDSP
public struct AnimatableVector<V: BinaryFloatingPoint>: VectorArithmetic, Hashable {
public var values: [Double]
public init<V: BinaryFloatingPoint>(values: [V]) {
self.values = values.map(Double.init)
}
public final class ViewController: UIViewController {
private lazy var myView = MyView()
public override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myView)
myView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(myView, view) { content, parent in
@Koshimizu-Takehito
Koshimizu-Takehito / FlowLayoutHorizontalLeft.swift
Created February 23, 2021 03:06
UICollectionViewでタグが左寄せに並んでいるようなレイアウトを実現する
class FlowLayoutHorizontalLeft: UICollectionViewFlowLayout {
private var cache: [IndexPath: UICollectionViewLayoutAttributes] = [:]
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
super.layoutAttributesForElements(in: rect)?
.lazy
.filter { $0.representedElementCategory == .cell }
.map { self.layoutAttributesForItem(at: $0.indexPath) }
.compactMap { $0 }
@Koshimizu-Takehito
Koshimizu-Takehito / ArrayDataSource.swift
Last active March 9, 2021 20:29
DataSource, CellProvider をビューコントローラから委譲するサンプルコード
import UIKit
extension UITableView {
func register(_ type: UITableViewCell.Type, identifier: String? = nil) {
self.register(type, forCellReuseIdentifier: identifier ?? String(describing: type))
}
func register(@ArrayBuilder<UITableViewCell.Type> builder: () -> [UITableViewCell.Type]) {
builder().forEach { type in
register(type)