Skip to content

Instantly share code, notes, and snippets.

View rizumita's full-sized avatar
🏠
Working from home

Ryoichi Izumita rizumita

🏠
Working from home
View GitHub Profile
private let state: BehaviorRelay<String> = .init(value: "!!!")
...
public func doSomething() -> Observable<String> {
...
@rizumita
rizumita / CodePiece.swift
Created February 9, 2020 12:47
Combineのasync/awaitをライブラリ化して自作フレームワークで使ってみた。かなり良い感じ。 https://github.com/rizumita/CombineAsync #CodePiece
static func update(event: Event, context: Context) -> Async<Model> {
async { yield in
switch event {
case .increase:
context.countRepository.increase()
case .increaseAutomatically:
yield(context.countRepository.increaseAutomatically(interval: 2.0, until: context.until)
.flatMap { self.makeModelAsync(context: context) })
@rizumita
rizumita / CodePiece.swift
Created February 8, 2020 23:40
Swiftにasync/awaitが無いのでCombineでなんちゃってasync/awaitを書いた。Swift5.2のcallAsFunctionを使うことでyieldが書きやすくなった。 #CodePiece
import Foundation
import Combine
public func async<T>(_ body: @escaping (Yield<T>) throws -> ()) -> Async<T> {
Async(body: body)
}
public func await<P>(_ publisher: P) throws -> P.Output where P: Publisher {
try publisher.await()
}
@propertyWrapper
class YORO<T> {
private var value: T?
var wrappedValue: T? {
get {
let result = value
value = .none
return result
}
set {
public protocol OptionalType {
associatedtype Wrapped
var optional: Wrapped? { get }
}
extension Optional: OptionalType {
public var optional: Wrapped? { self }
}
import UIKit
public typealias Send<A> = (A) -> ()
public typealias Receive<A> = (A) -> ()
public typealias Observe<A> = (@escaping Receive<A>) -> (ObserveToken)
public typealias OPipe<A> = (send: Send<A>, observe: Observe<A>)
public class ObserveToken: Hashable {
let uuid = UUID()
let receive: Any
@rizumita
rizumita / CodePiece.swift
Created December 13, 2018 01:51
計算結果に名前を付けてifで判定する場合、Swiftでこういう書き方はありなのか、してる人いるのかって思ったんですがどうなんでしょう。 #swift #CodePiece
let i = 1
let ii = 2
// let flag = i < ii
// if flag {}
if (flag: i < ii).0 {}
@rizumita
rizumita / debounce.swift
Created November 21, 2018 00:09
SwiftでDispatchQueueでdebounce
extension DispatchQueue {
func debounce(interval: DispatchTimeInterval) -> (_ exec: @escaping () -> ()) -> DispatchSourceTimer? {
var timer: DispatchSourceTimer?
return { [weak self] exec in
timer?.cancel()
timer = DispatchSource.makeTimerSource(queue: self)
timer?.setEventHandler(handler: exec)
timer?.schedule(deadline: .now() + interval, repeating: .infinity)
@rizumita
rizumita / for_in_vs_reduce_into.swift
Created February 1, 2018 12:20
for in vs reduce into
let range = 0..<10000000
var i = false
test("for in") {
for n in range {
i = i || (n % 12345 == 0)
}
}
print(String(i))
@rizumita
rizumita / for_in_vs_map.swift
Created February 1, 2018 11:51
for in vs map
let range = 0..<10000000
var i = [Int]()
test("for in") {
for n in range {
i.append(n)
}
}
print(String(i.count))