Skip to content

Instantly share code, notes, and snippets.

View elm4ward's full-sized avatar

Elmar Kretzer elm4ward

View GitHub Profile
// ----------------------------------------------------------------------------------------------------
// MARK: - Lazy comonadic array zipper
//
// http://elm4ward.github.io/swift/functional/comonad/2016/04/06/da-lazy-one.html
//
// Stacking functions lazily can speed up your code.
//
// 1. The struct
// 2. Lazy in action
// ----------------------------------------------------------------------------------------------------
import UIKit
import PlaygroundSupport
import Foundation
import CoreGraphics
// ----------------------------------------------------------------------------------------------------
// MARK: - Interpolations
//
// http://elm4ward.github.io/swift/generator/protocol/2016/04/08/Interpolations.html
//
// ----------------------------------------------------------------------------------------------------
// Iterators for Naysayer
//
// Generating understandable, reusable, simplified, ... code with Iterators
//
// http://elm4ward.github.io/swift/Iterator/protocol/2016/04/14/Iterators-for-the-naysayers.html
//
// 1. Base Protocols
// 2. ZipWithSum
// 3. ZipWithSumFrom
let mark = "####################################"
let chapter: (String) -> Void = { print("\n", mark, "# \($0)", mark, separator: "\n")}
// ----------------------------------------------------------------------------------------------------
// MARK: - Look ma, no protocols
//
// http://elm4ward.github.io/swift/generics/type/erasure/2016/04/21/erase-em-all.html
//
// Erasing generics to life in freedom
// This is a follow up to the article about Protocols with Typealias
let mark = Array(repeating: "-", count: 40).joined(separator: "")
func chapter(_ s: String, line: Int = #line) { print("\n", "// " + mark, "// \(s) - line: \(line)", "// " + mark, separator: "\n")}
func section(_ s: String, line: Int = #line) { print("\n", "# \(s)", mark, separator: "\n")}
import struct CoreGraphics.CGFloat
// ----------------------------------------------------------------------------------------------------
// MARK: - Bi-directional Type Inference
//
// http://elm4ward.github.io/swift/generics/type/inference/2016/05/01/bidirectional.html
import Foundation
let mark = Array(repeating: "-", count: 40).joined(separator: "")
func chapter(s: String, line: Int = #line) { print("\n", "// " + mark, "// \(s) - line: \(line)", "// " + mark, separator: "\n")}
func section(s: String, line: Int = #line) { print("", "~~ \(s)", separator: "\n")}
func assertEqual<A: Equatable>(_ a: A, _ b: A) { assert(a == b); print(a, "==", b) }
// ----------------------------------------------------------------------------------------------
// It`s a generic type of world
//
// http://elm4ward.github.io/swift/generics/mocking/dependency/injection/2016/05/21/generic-dependency-mocking.html
@elm4ward
elm4ward / dsltest.swift
Last active September 1, 2016 10:47 — forked from marcpalmer/dsltest.swift
Example of using Swift property change observation to bind models to arguments passed to DSL closures
import UIKit
enum GraphicsCommand {
case setColour(colour: UInt32)
case moveTo(x: Float, y: Float)
case lineTo(x: Float, y: Float)
}
@elm4ward
elm4ward / font.swift
Created March 1, 2017 08:08
Using iOS FontStyles with different weights and scales
import UIKit
import PlaygroundSupport
extension UIFont {
static func font(for style: UIFontTextStyle,
atScale scale: CGFloat = 1,
usingFont fontInSize: ((CGFloat) -> UIFont)? = nil) -> UIFont {
switch (fontInSize, scale) {
@elm4ward
elm4ward / ProofofProducts.swift
Last active April 25, 2017 13:28
Proofing Products / Tuples ...
precedencegroup Apply {
higherThan: ComparisonPrecedence
associativity: right
}
precedencegroup Compose {
higherThan: Apply
}