Skip to content

Instantly share code, notes, and snippets.

@masakih
masakih / CodePiece.swift
Created March 12, 2020 14:22
テスト用に同一要素がいっぱい入ったSequenceを生成コスト0で使える奴書いた #CodePiece
struct YesIterator<Value>: IteratorProtocol {
private var value: Value
private var count: Int
init(value: Value, count: Int) {
self.value = value
self.count = count
}
mutating func next() -> Value? {
guard count > 1 else { return nil }
@masakih
masakih / CodePiece.swift
Created March 2, 2020 12:25
できました。NSAttributedStringから直接NSStringをとり出すメソッドです #CodePiece
private typealias StringIMP = @convention(c) (AnyObject, Selector) -> NSString
private let stringSelector = #selector(getter: NSAttributedString.string)
private var stringIMP: StringIMP?
extension NSAttributedString {
private var stringImp: StringIMP {
if let i = stringIMP { return i }
@masakih
masakih / CodePiece.swift
Created February 18, 2020 11:14
これでそれなりに動くものになった #CodePiece
@dynamicMemberLookup
struct Point {
var cgPoint: CGPoint
subscript(dynamicMember keyPath: WritableKeyPath<CGPoint, CGFloat>) -> Float {
get { Float(cgPoint[keyPath: keyPath]) }
set(value) { cgPoint[keyPath: keyPath] = CGFloat(value) }
}
}
@masakih
masakih / ActionPublisher.swift
Last active January 10, 2020 16:05
Target-ActionをCombineで処理出来るようにする
import Cocoa
import Combine
public protocol ActionPerfomer: AnyObject {
var target: AnyObject? { get set }
var action: Selector? { get set }
}
extension NSControl: ActionPerfomer {}
@masakih
masakih / pasteboardFilter.swift
Created January 8, 2020 13:03
クリップボードからスタイル付きテキストなどを削除する
func ommit(_ pType: NSPasteboard.PasteboardType) -> Bool {
let a = [
"rtf",
"NeXT",
"HTML",
"Web"
]
let r = pType.rawValue.lowercased()
@masakih
masakih / CodePiece.swift
Created November 25, 2019 13:45
if文のメソッドチェーン出来た #CodePiece
enum IfResult<T> {
case `false`
case value(T)
@discardableResult
func elseIf(_ condition: @autoclosure () -> Bool, _ f: () -> T) -> IfResult {
switch self {
case .false: return condition() ? .value(f()) : .false
default: return self
}
@masakih
masakih / ArraySplit.swift
Created September 5, 2019 09:28
ArrayをN個づつのArraySliceにする
func split<Element>(_ array: [Element], _ n: Int) -> [ArraySlice<Element>] {
func g(_ array: ArraySlice<Element>, _ n: Int) -> [ArraySlice<Element>] {
let a = array.prefix(n)
guard a.count != 0 else { return [] }
return [a] + g(array.dropFirst(n), n)
}
@masakih
masakih / awaji.md
Last active August 29, 2019 04:48
海防艦「淡路」

海防艦 淡路

  • 1943年

    • 6月1日 起工
    • 8月31日 命名
    • 10月10日 進水
  • 1944年

    • 1月25日 竣工(本籍:呉鎮守府
  • 呉鎮守府警備海防艦

@masakih
masakih / fizzBuzz.swift
Created August 16, 2019 09:04
FizzBuzz with IteratorProtocol
func fizzBuzz1(_ n: Int) -> String {
switch (n.isMultiple(of: 3), n.isMultiple(of: 5)) {
case (true, true): return "FizzBuzz"
case (true, false): return "Fizz"
case (false, true): return "Buzz"
default: return "\(n)"
}
}
@masakih
masakih / Result+extensions.swift
Last active August 7, 2019 09:07
Resultで複数のエラーに同時に対処する
extension Result {
@discardableResult
func ifFailure(_ f: (Failure) -> Void) -> Result {
if case let .failure(e) = self { f(e) }
return self
}
}
extension Result where Failure: OptionSet, Failure == Failure.Element {