Skip to content

Instantly share code, notes, and snippets.

View kaqu's full-sized avatar

Kacper kaqu

View GitHub Profile
protocol Buildable {
init(dict: [AnyKeyPath: Any]) throws
}
struct BuilderError : Error {
let reason: String
}
struct SomeBuildable : Buildable {
let field1: String
@kaqu
kaqu / RawHTTP.swift
Created January 26, 2019 17:20
Swift HTTP over Network framework
import Network
let host: NWEndpoint.Host = .init("httpbin.org")
let port: NWEndpoint.Port = .https
let tlsConfig: NWProtocolTLS.Options = .init()
let parameters: NWParameters = .init(tls: tlsConfig)
let connection: NWConnection = .init(host: host, port: port, using: parameters)
connection.stateUpdateHandler = { state in
print("State Update: \(state)")
}
@kaqu
kaqu / Future.swift
Last active March 9, 2019 18:34
PasteIn Future implementation
#if os(Linux)
import Glibc
#else
import Darwin
#endif
public final class Future<Value> {
public typealias Handler = (Value) -> Void
public typealias Executor = (@escaping () -> Void) -> Void
@kaqu
kaqu / Hex.swift
Created April 17, 2019 18:02
Hex Swift
// from: https://github.com/raywenderlich/swift-algorithm-club
public struct Heap<T> {
/** The array that stores the heap's nodes. */
var nodes = [T]()
/**
* Determines how to compare two nodes in the heap.
* Use '>' for a max-heap or '<' for a min-heap,
@kaqu
kaqu / Coconut.swift
Created May 11, 2019 10:55
CoconutUIKit
import UIKit
// MARK: Corner rounding
public enum CornerRoundings {
case none
case all(CGFloat)
case topLeft(CGFloat)
@kaqu
kaqu / FluidLayout.swift
Created May 11, 2019 22:06
FluidLayout
public protocol FluidView: UIView {
var id: UUID { get }
}
public protocol FluidViewLayoutModel {
var id: UUID { get }
var prefferedSize: CGSize? { get }
var prefferedInsets: UIEdgeInsets? { get }
var minimumSize: CGSize? { get }
var maximumSize: CGSize? { get }
func prepareView() -> FluidView
@kaqu
kaqu / Actor.swift
Last active May 15, 2019 20:06
Swift Actor
////////////////////////////////////////////////////////
//// From futura: https://github.com/miquido/futura ////
////////////////////////////////////////////////////////
import Darwin
import libkern
public enum Mutex {
public typealias Pointer = UnsafeMutablePointer<pthread_mutex_t>
@kaqu
kaqu / Executor.swift
Last active May 25, 2019 21:09
Futures 3.0
import Foundation
public protocol Executor {
func execute(_ task: @escaping () -> Void)
}
extension DispatchQueue: Executor {
@inlinable public func execute(_ task: @escaping () -> Void) {
async(execute: task)
@kaqu
kaqu / EntityDecoder.swift
Created September 7, 2019 17:47
CodeData - CoreData from Code
import Foundation
import CoreData
// TODO: FIXME: minimal, naive implementation
internal final class EntityDecoder<EntityType: StorageEntity>: Decoder {
private let managed: NSManagedObject
public var codingPath: [CodingKey] = []
@kaqu
kaqu / ByteParser.swift
Created September 14, 2019 18:01
BytesParser based on https://pointfree.co parser video series
import Foundation
public extension Data {
func parse<T>(with parser: Parser<T>) -> T? {
self.withUnsafeBytes { (bufferPointer) -> T? in
var slice: Slice<Array<Byte>> = .init(bufferPointer)
return parser.parse(&slice)
}
}
}