Skip to content

Instantly share code, notes, and snippets.

@jrsonline
jrsonline / codesplit.sh
Last active August 12, 2020 15:08
Split a file into named files
#!/bin/bash
# Given a text file like this:
# /* HEADER */
# import x
# import y
# /* END HEADER */
# ...code...
# /* TO: codeFile.swift */
# ...more code...
@jrsonline
jrsonline / gist:3a2346c423edf0649182fe6211673d08
Created June 15, 2019 14:33
Sample using toBlockingArray ... and a way to publish debug output!
struct NSLoggerStream : TextOutputStream {
mutating func write(_ string: String) {
guard string.count > 0 && string != "\n" else { return }
NSLog(string)
}
}
// Test
var logger = NSLoggerStream()
@jrsonline
jrsonline / gist:ccd2338f6e39a2f0c7e7640063a0182c
Created June 15, 2019 14:30
Simple FutureScheduler for Apple's combine, like ImmediateScheduler
struct FutureScheduler : Scheduler {
var now: DispatchTime { get { return DispatchTime.now() }}
var minimumTolerance: Int64 = 1
func schedule(after date: DispatchTime, tolerance: Int64, options: Never?, _ action: @escaping () -> Void) {
_ = self.schedule(after: date, interval: .seconds(0), tolerance: tolerance, options: nil, action)
}
func schedule(after date: DispatchTime, interval: Int64, tolerance: Int64, options: Never?, _ action: @escaping () -> Void) -> Cancellable {
FutureScheduledAction( action, at: date)
@jrsonline
jrsonline / gist:dd9799929e1aceb5d99e83fc6ac2b43b
Last active December 19, 2019 09:19
Simple implementation of RxSwift's "toBlocking" for Apple's Combine, now using the DispatchQueue
extension Publisher {
func toBlockingResult(timeout: Int) -> Result<[Self.Output],BlockingError> {
var result : Result<[Self.Output],BlockingError>?
let semaphore = DispatchSemaphore(value: 0)
let sub = self
.collect()
.mapError { error in BlockingError.otherError(error) }
.timeout(
.seconds(timeout),
protocol Functor {
typealias F = Self
static func fmap<A,B>(_ transform: (A) -> B ) -> (F<A>) -> F<B>
static func map<A,B>(_ transform: (A) -> B ) -> (F<A>) -> [B]
}
// suppose we have some class
class AClass<A> { required init() {} }
// we can certainly create a generic type using that class
// note that we are "hard coding" creating an AClass object
struct GenericType<A> { var a = AClass<A>() }
let ta = Type<Int>()
// ...but we can't tell Swift what sort of object to create
// eg we cannot be generic in the 'AClass'
extension Tree: Constructible {
typealias TypeParameter = Element
}
extension TreeTag: FunctorTag {
static func fmap<A, B>(_ transform: @escaping (A) -> B) -> (Construct<TreeTag, A>) -> Construct<TreeTag, B> {
return { applyA in
return Tree<A>.lower(applyA).tmap(transform)^
}
}
extension LinkedList : Constructible {
typealias TypeParameter = T
}
extension LinkedListTag: FunctorTag {
static func fmap<A, B>(_ transform: @escaping (A) -> B) -> (Construct<LinkedListTag, A>) -> Construct<LinkedListTag, B> {
return { applyA in
return LinkedList<A>.lower(applyA).lmap(transform)^
}
}
extension Array : Constructible {
typealias TypeParameter = Element
}
extension ArrayTag : FunctorTag {
static func fmap<A, B>(_ transform: @escaping (A) -> B) -> (Construct<ArrayTag, A>) -> Construct<ArrayTag, B> {
return { applyA in
return [A].lower(applyA).map( transform )^
}
}
@jrsonline
jrsonline / lift_and_lower.swift
Last active January 31, 2018 04:13
Magic Objects: Lifts and Lowers
let aas = [1,2,3]^ // "lifts" a to Construct<ArrayTag,Int>
/// the below are all equivalent ways to lower, pick the one that looks best to you
let array1 = aas.toArray
let array2 = Array.lower(aas)
let array3 = aas >>>¬ toArray // "continuation-passing" style. ¬ is ALT-L. >>>¬ is "lower"
let array4 = aas.lower
let array5 = aas¬
let bbs = LinkedList(array: ["a","b","c"])^ // lifts to Construct<LinkedListTag,String>
/// the below are all equivalent ways to lower, pick the one that looks best to you