Skip to content

Instantly share code, notes, and snippets.

View rjchatfield's full-sized avatar

Smiley Rob rjchatfield

View GitHub Profile
@rjchatfield
rjchatfield / 1Playground.swift
Last active January 21, 2020 10:26
Sudoku solver Pt.1: Value types
import PlaygroundSupport
import SwiftUI
Grid.easy // 8ms
Grid.medium // 356ms
Grid.hard // 103ms
Grid.expert // 54,426ms
Grid.empty // 101ms
Grid.solved // 0ms
Grid.unsolvable // 0ms
@rjchatfield
rjchatfield / either2.swift
Last active November 19, 2019 22:02
SwiftUI either type many different (non-scalable) approaches
// 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
rjchatfield / contrast.swift
Created October 15, 2019 03:30
UIColour contrast and luminance
/// Rip off of the JavaScript implementation of:
/// https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
extension UIColor {
typealias Contrast = (ratio: CGFloat, error: CGFloat, min: CGFloat, max: CGFloat)
/// Range: 1 ... 21
func contrast(_ other: UIColor) -> Contrast {
var other = other
@rjchatfield
rjchatfield / inoutMutation.swift
Created October 13, 2019 12:48
inout Mutation
func doNothing(_: inout String) { /* do nothing!! */ }
var value = "foo" {
didSet { print("didSet") }
}
doNothing(&value) // Unfortunately, triggers didSet!
var proxy: String {
get { value }
@rjchatfield
rjchatfield / CombineExtensions.swift
Last active October 1, 2019 13:14
EffectsHandler: (Publisher<Effect>) -> Publisher<ReAction>
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 {
@rjchatfield
rjchatfield / ComponentsWithRenderProps.jsx
Created September 26, 2019 03:12
ComponentsWithRenderProps
const ComponentsWithRenderProps = () => (
<ComponentA value={'Value'}>
{({ data, doSomethingWithComponentA }) => (
<ComponentB value={'B-value'}>
{something => (
<ComponentC
data={data}
something={something}
onAnotherThing={doSomethingWithComponentA}
/>
@rjchatfield
rjchatfield / FluxDescription.swift
Created September 8, 2015 15:54
Explaining Flux data flow in 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
rjchatfield / types.md
Last active December 13, 2015 20:15
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...

Types.

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
rjchatfield / phone.swift
Last active July 8, 2016 10:32
Validating PhoneNumber type in Swift with a failable initialiser and an enum (for type safety)
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}$"
@rjchatfield
rjchatfield / stateReduce.swift
Created August 22, 2015 16:59
Inspired by Redux, but not really Redux
// ACTIONS & REDUCERS
enum CounterAction {
case Increment
case Decrement
case ChangeBy(Int)
}
enum NameAction {
case Set(String)
case Clear
}