Skip to content

Instantly share code, notes, and snippets.

Smiley Rob rjchatfield

Block or report user

Report or block rjchatfield

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
rjchatfield / 1Playground.swift
Created Jan 21, 2020
Sudoku solver Pt.2: reference types
View 1Playground.swift
import PlaygroundSupport
import SwiftUI
// Times: "copy+solve solve (val solve)"
Grid.easy // 6ms 0ms ( 8ms)
rjchatfield / 1Playground.swift
Last active Jan 21, 2020
Sudoku solver Pt.1: Value types
View 1Playground.swift
import PlaygroundSupport
import SwiftUI
Grid.easy // 8ms
Grid.medium // 356ms
Grid.hard // 103ms // 54,426ms
Grid.empty // 101ms
Grid.solved // 0ms
Grid.unsolvable // 0ms
rjchatfield / either2.swift
Last active Nov 19, 2019
SwiftUI either type many different (non-scalable) approaches
View either2.swift
// MARK: - Either Attempt 1 - FAIL!
struct EitherAttempt1 {
@State var state: ThreewayState
func either<V1: View, V2: View>(block: () -> (V1?, V2?)) -> some View {
TupleView<(V1?, V2?)>(block())
func either<V1: View, V2: View, V3: View>(block: () -> (V1?, V2?, V3?)) -> some View {
TupleView<(V1?, V2?, V3?)>(block())
rjchatfield / contrast.swift
Created Oct 15, 2019
UIColour contrast and luminance
View contrast.swift
/// Rip off of the JavaScript implementation of:
extension UIColor {
typealias Contrast = (ratio: CGFloat, error: CGFloat, min: CGFloat, max: CGFloat)
/// Range: 1 ... 21
func contrast(_ other: UIColor) -> Contrast {
var other = other
View inoutMutation.swift
func doNothing(_: inout String) { /* do nothing!! */ }
var value = "foo" {
didSet { print("didSet") }
doNothing(&value) // Unfortunately, triggers didSet!
var proxy: String {
get { value }
rjchatfield / CombineExtensions.swift
Last active Oct 1, 2019
EffectsHandler: (Publisher<Effect>) -> Publisher<ReAction>
View CombineExtensions.swift
import Combine
extension Publisher {
public func flatMap<T, P, UP>(
maxPublishers: Subscribers.Demand = .unlimited,
transform: @escaping (Self.Output) -> P,
until: @autoclosure @escaping () -> UP
) -> Publishers.FlatMap<Publishers.PrefixUntilOutput<P, UP>, Self>
where T == P.Output, P: Publisher, Self.Failure == P.Failure, UP: Publisher {
View ComponentsWithRenderProps.jsx
const ComponentsWithRenderProps = () => (
<ComponentA value={'Value'}>
{({ data, doSomethingWithComponentA }) => (
<ComponentB value={'B-value'}>
{something => (
rjchatfield / FluxDescription.swift
Created Sep 8, 2015
Explaining Flux data flow in Swift
View FluxDescription.swift
// Line 109 is where the magic is!
import Foundation
import XCPlayground
func setInterval(seconds interval: Int, f: ()->()) {
let delta = Int64(1) * Int64(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, delta)
dispatch_after(time, dispatch_get_main_queue(), {
rjchatfield /
Last active Dec 13, 2015
Types are Coming

Types are coming

Last week I had a crash course in PureScript - a Haskell-like language that compiles to JavaScript. I’m not about to preach that you should write your web apps in Haskell (though I strongly recommend learning Haskell), however, I do wish to explore an important part of any Functional Programming language that has eluded Javascript...


There is a lot to be said about Javascript and its lack of static typing. We’ve built the whole web with it! But this post will explore the benefits of a compile-time strong static type system.

“But Javascript does have types!?”

rjchatfield / phone.swift
Last active Jul 8, 2016
Validating PhoneNumber type in Swift with a failable initialiser and an enum (for type safety)
View phone.swift
import Foundation
typealias RegexPattern = String
let aussiePhoneNumber = 0424_555_123
let brokePhoneNumber = 0424_555_1231
enum PhoneNumberType: RegexPattern {
// List all regex patterns
case AussieMobile = "(?:\\+?61|0)4(?:[01]\\d{3}|(?:2[1-9]|3[0-57-9]|4[7-9]|5[0-15-9]|6[679]|7[3-8]|8[1478]|9[07-9])\\d{2}|(?:20[2-9]|444|52[0-6]|68[3-9]|70[0-7]|79[01]|820|890|91[0-4])\\d|(?:200[0-3]|201[01]|8984))\\d{4}$"
You can’t perform that action at this time.