Skip to content

Instantly share code, notes, and snippets.

View gfontenot's full-sized avatar
football

Gordon Fontenot gfontenot

football
View GitHub Profile
@gfontenot
gfontenot / WeView.swift
Last active April 17, 2020 01:12
Demonstration of weird WKWebView behavior when paired with SwiftUI
import SwiftUI
struct ContentView: View {
@State var showModal: Bool = false
var body: some View {
NavigationView {
VStack {
WebView(url: URL(string: "https://www.example.com")!)
Button("Fixme") { self.showModal = true }
@gfontenot
gfontenot / Sample.swift
Created February 15, 2020 17:28
Swift UI sample of layout bug
import SwiftUI
struct Sample: View {
var body: some View {
NavigationView {
WebView(url: URL(string: "https://example.com")!)
.navigationBarItems(
trailing: Button(
action: { },
label: { Text("Done") }
@gfontenot
gfontenot / ElmSpike.swift
Created August 3, 2018 17:27
Quick spike on an elm-ish architecture in Swift
// Dumb value types representing UIViews. Comperable to our current models like Button.Model
struct StackView<Message> {
let children: [View<Message>]
}
struct Label {
let text: String
}
func assert(_ cond: @autoclosure () -> Bool) -> String {
if cond() {
return "✅"
} else {
return "❌"
}
}
#if !swift(>=4.2)
protocol KnownEnum: RawRepresentable, Hashable where RawValue == String {
@gfontenot
gfontenot / goroutines.swift
Created February 16, 2018 15:02 — forked from chriseidhof/goroutines.swift
goroutines.swift
import Foundation
enum Message<T> {
case value(T)
case finished
}
protocol Channel: IteratorProtocol {
func send(_ value: Message<Element>)
}
@gfontenot
gfontenot / Decoding.swift
Created July 22, 2015 15:40
Example of using partial application as a way to solve side loaded JSON with immutable value objects
// Returning a function here, instead of a fully realized object
extension Company: Decodable {
static func decode(j: JSON) -> Decoded<[People] -> Company> {
return create
<^> j <| "id"
<*> j <| ["attributes", "name"]
}
}
// stored property, no default value
let foo: String
// stored property, default value
let foo: String = "foo"
// or
let foo = "foo"
// computed property, no custom setter
var foo: String {
// Original implementation with multiple returns:
class func fromId(id: String) -> Office? {
let officesData = JSONData.load("offices") as? [[String: String]] ?? []
let officeData = officesData.filter { $0["id"] == id }.first
if let office = officeData {
return decode(JSONValue.parse(office))
}
@gfontenot
gfontenot / decodeDict.swift
Created February 19, 2015 15:38
example of decoding dictionary objects with Argo/FP
import Argo
import Runes
func +<T, V>(lhs: [T: V], rhs: [T: V]) -> [T: V] {
var dict = lhs
for (key, val) in rhs {
dict[key] = val
}
return dict
import Foundation
infix operator >>- { associativity left precedence 150 }
public func >>-<T, U>(a: T?, f: T -> U?) -> U? {
return a.flatMap(f)
}
extension Optional {
func flatMap<U>(f: T -> U?) -> U? {
switch self {