Skip to content

Instantly share code, notes, and snippets.

enum Result<A,B> {
case Left(A)
case Right(B)
}
extension Result {
func reduce<R>(f: A -> R, g: B -> R) -> R {
switch self {
case .Left(let a): return f(a)
case .Right(let b): return g(b)
// Here we provide the structure of our bitmap context
struct BitmapContextConfig {
var bitsPerComponent: Int
var bytesPerPixel: Int
var space: CGColorSpace
var bitmapInfo: CGBitmapInfo
var data: UnsafeMutablePointer<Void> = nil
}

Literate Swift

Last year, when we started writing Functional Programming in Swift, we realized that the language would change every few weeks. We didn't anticipate the pace, but we were ready for change. Rather than manually updating every article, we used a technique called literate programming. In literate programming, you write the code and text at the same time. The code in the text can be executed. To do this, we wrote a small script called Literate Swift.

For our two new books, Core Data and Advanced Swift, we are using the same technique, except with a newer version of the tool. Literate Swift is a library (and comes with a GUI or command-line tool) that takes a Markdown file, extracts all the code blocks marked with swift, and compiles those. The awesome thing about it is that you can compile your Markdown: you can statica

#!/usr/bin/env xcrun swift -F Rome
import Foundation
import CommonMark
import LiterateSwift
func stripPrelude(var blocks: [Block]) -> [Block] {
guard blocks.count > 0 else { return blocks }
guard case let Block.Header(text, _) = blocks[0] where text.count == 1 else {
return blocks
// TODO should be an extension
func all<T> (xs : [T], predicate : T -> Bool) -> Bool {
for x in xs {
if !predicate(x) {
return false
}
}
return true
}
SIL verification failed: instruction isn't dominated by its bb argument operand: Dominance->dominates(valueBBA->getParent(), I->getParent())
Verifying instruction:
%110 = argument of bb6 : $Int // users: %111, %115, %649, %671, %703
-> debug_value %110 : $Int // let $match // id: %649
In function:
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<UnsafeMutablePointer<Int8>>):
// function_ref Swift._didEnterMain (Swift.Int32, argv : Swift.UnsafeMutablePointer<Swift.UnsafeMutablePointer<Swift.Int8>>) -> ()
%2 = function_ref @_TFSs13_didEnterMainFTVSs5Int324argvGVSs20UnsafeMutablePointerGS0_VSs4Int8___T_ : $@convention(thin) (Int32, UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) -> () // user: %3
import Foundation
var generator = [1,2,3].generate()
while let i = generator.next() {
print(i)
}
let i = 5
if case 0..<10 = i {
print("\(i) within range")
import Foundation
extension Comparable {
static func compare(l: UnsafePointer<Void>, _ r: UnsafePointer<Void>) -> Int32 {
let left: Self = UnsafePointer(l).memory
let right: Self = UnsafePointer(r).memory
if left < right { return -1 }
if left == right { return 0 }
return 1
}
enum Request {
case ProductsList // "/products"
case Product(productID: Int) // "/products/:pid"
case CustomersList // "/customers"
case Customer(customerID: Int) // "/customers/:cid"
case OrdersList(customerID: Int) // "/customers/:cid/orders"
case Order(customerID: Int, orderID: Int) // "/customers/:cid/orders/:oid"
}
extension Array {
> {-# LANGUAGE GADTs #-}
> module Example where
> import qualified Data.Map as M
Let's assume that requests are simple |String|s.
> type Request = String
When running a web continuation, either the computation is completely finished,