This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Given a text file like this: | |
# /* HEADER */ | |
# import x | |
# import y | |
# /* END HEADER */ | |
# ...code... | |
# /* TO: codeFile.swift */ | |
# ...more code... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct NSLoggerStream : TextOutputStream { | |
mutating func write(_ string: String) { | |
guard string.count > 0 && string != "\n" else { return } | |
NSLog(string) | |
} | |
} | |
// Test | |
var logger = NSLoggerStream() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)^ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)^ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 )^ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder