Skip to content

Instantly share code, notes, and snippets.

@masakih
masakih / CodePiece.txt
Created March 4, 2021 12:58
これでSwiftのソースを作ってみたけどswiftcが帰ってこないw #CodePiece
%{
def commaSep(array):
return ', '.join(array)
def closure(n, param):
if n == 1:
return '{ p' + str(len(param) - n) + ' in f(' + commaSep(param) + ') } '
return '{ p' + str(len(param) - n) + ' in ' + closure(n - 1, param) + '} '
@masakih
masakih / CodePiece.swift
Created November 3, 2020 02:01
Indexへの制約を外したのでStringに直接使える #CodePiece
extension Collection {
func list3() -> [[Element]] {
zip(0..., self).flatMap { (i: Int, e0: Element) -> [[Element]] in
zip((i + 1)..., self[index(startIndex, offsetBy: (i + 1))...]).flatMap { (j: Int, e1: Element) -> [[Element]] in
self[index(startIndex, offsetBy: (j + 1))...].map { (e2: Element) -> [Element] in
[e0, e1, e2]
}
}
@masakih
masakih / CodePiece.swift
Created November 3, 2020 01:30
Collectionから取り出した3つの要素の全組み合わせを列挙するやつ #CodePiece
extension Collection {
func list3() -> [[Element]] where Index == Int {
zip(0..., self).flatMap { (i: Int, e0: Element) -> [[Element]] in
zip((i + 1)..., self[(i + 1)...]).flatMap { (j: Int, e1: Element) -> [[Element]] in
self[(j + 1)...].map { (e2: Element) -> [Element] in
[e0, e1, e2]
}
}
@masakih
masakih / CodePiece.swift
Created November 1, 2020 01:23
これ必要でしょ? #CodePiece
extension Combine.Future {
convenience init<S: Scheduler>(on scheduler: S, _ attemptToFulfill: @escaping (@escaping Future<Output, Failure>.Promise) -> Void) {
self.init { promise in
scheduler.schedule {
attemptToFulfill(promise)
}
}
}
@masakih
masakih / CodePiece.swift
Created October 18, 2020 04:21
できた #CodePiece
extension Sequence {
func traverse<T>(_ transform: @escaping (Element) -> AnyPublisher<T, Never>) -> AnyPublisher<[T], Never> {
var index = 0
return self
.publisher
.flatMap { i -> AnyPublisher<(Int, T), Never> in
@masakih
masakih / CodePiece.swift
Created October 18, 2020 02:20
これでもだめ #CodePiece
(0...3)
.publisher
.flatMap { i -> AnyPublisher<Int, Never> in
Future<Int, Never> { promise in
DispatchQueue.global().async {
print("start", i, "----", Thread.current)
sleep(UInt32([0, 2, 3, 5].randomElement()!))
print("end", i)
@masakih
masakih / CodePiece.swift
Created October 18, 2020 02:02
こうやると非同期で動くけど(当たり前) sinkが発動しない(意味不明) #CodePiece
(0...3)
.map { i -> AnyPublisher<Int, Never> in
Future<Int, Never> { promise in
DispatchQueue.global().async {
print("start", i, "----", Thread.current)
sleep(UInt32([0, 2, 3, 5].randomElement()!))
print("end", i)
@masakih
masakih / CodePiece.swift
Created October 18, 2020 01:52
だめ #CodePiece
(0...3)
.map { i -> AnyPublisher<Int, Never> in
Future<Int, Never> { promise in
print("start", i)
sleep(UInt32([0, 2, 3, 5].randomElement()!))
print("end", 1)
promise(.success(i * 2))
@masakih
masakih / CodePiece.swift
Created September 15, 2020 12:27
ArraySliceを上手に使ったバイナリサーチ #CodePiece
import Foundation
extension Array {
func binarySearch(_ f: (Element) -> ComparisonResult) -> Element? {
self[...].binarySearch(f)
}
}
extension ArraySlice {
func binarySearch(_ f: (Element) -> ComparisonResult) -> Element? {
@masakih
masakih / CodePiece.swift
Created April 12, 2020 12:12
こうだ! #CodePiece
private func contentImage(in rect: CGRect) -> UIImage {
UIGraphicsImageRenderer(bounds: rect)
.image { con in
textField.layer.render(in: con.cgContext)
}
}