Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am marksands on github.
  • I am marksands (https://keybase.io/marksands) on keybase.
  • I have a public key ASCiHMudz2diWLv9Nu36oOAK8MPoacliJ3oPi89JfulWngo

To claim this, I am signing this object:

@marksands
marksands / OptionalDateValue.swift
Created September 11, 2020 15:53
An Optional DateValue for BetterCodable, by duplicating existing types :P
public protocol OptionalDateValueCodableStrategy {
associatedtype RawValue: Codable
static func decode(_ value: RawValue?) throws -> Date?
static func encode(_ date: Date?) -> RawValue?
}
@propertyWrapper
public struct OptionalDateValue<Formatter: OptionalDateValueCodableStrategy>: Codable {
struct PerformActionSideEffect<Output, Failure: Error> {
private let publisher: AnyPublisher<Output, Failure>
init<P: Publisher>(_ publisher: P) where P.Output == Output, P.Failure == Failure {
self.publisher = publisher.eraseToAnyPublisher()
}
@discardableResult
func performSink(_ action: @escaping (Output) -> Void) -> PerformActionSideEffect {
publisher.subscribe(
import UIKit
extension CGRect {
public static var unbounded: CGRect {
return CGRect(origin: .zero, size: .unbounded)
}
public var center: CGPoint {
return CGPoint(x: midX, y: midY)
}
@marksands
marksands / 1_playground.swift
Last active December 9, 2019 03:06
Advent of Code 2019 Day 8 Vision Detection
import PlaygroundSupport
import UIKit
import Foundation
import Vision
func part2layer() -> [String] {
let layers = Array(input).chunks(ofSize: 25 * 6)
var picture = Array(repeating: "2", count: 25 * 6)
layers.forEach { layer in
enum Direction {
case up, down, left, right
}
protocol PositionType {
var x: Int { get }
var y: Int { get }
}
func euclideanDistance(_ lhs: PositionType, _ rhs: PositionType) -> Double {
@marksands
marksands / find_by_dynamic.swift
Last active July 2, 2018 17:42
playing with dynamicMemberLookup
class Database {
private static let users = [User(id: 1, name: "Jane"), User(id: 2, name: "Clara"), User(id: 3, name: "Sandy")]
static func find(id: Int) -> User? {
return users.first(where: { $0.id == id })
}
static func find(name: String) -> User? {
return users.first(where: { $0.name == name })
}
func =(_ lhs: UInt8, _ rhs: UInt8) -> UInt8 { return 0 }
struct WHelper {
subscript(_ a: (UInt8, UInt8) -> (UInt8)) -> () -> Void {
get { return { } }
}
}
func w(_ a: () -> Void) -> WHelper {
return WHelper()
@marksands
marksands / Menu.swift
Last active April 21, 2022 18:13
AppKit NSMenu Helper
import AppKit
public struct MenuHotKeyPress: ExpressibleByStringLiteral {
public let character: String
public let modifierKeys: NSEvent.ModifierFlags?
public init(stringLiteral value: String) {
character = value
modifierKeys = nil
}
@marksands
marksands / Example.swift
Created January 9, 2018 01:24 — forked from IanKeen/Driver+Create.swift
RxSwift subject to provide public triggering but private subscription
class API {
func obtainIds() -> Observable<[Int]> {
return .just([1,2,3,4])
}
}
class ViewModel {
let update: AnyObserver<Void>
let dataUpdated: Observable<[String]>