Skip to content

Instantly share code, notes, and snippets.

struct StateMachine<State, Action> {
private(set) var state: State
private let transition: (State, Action) -> State?
init(startState: State, transition: @escaping (State, Action) -> State?) {
self.state = startState
self.transition = transition
}
mutating func apply(action: Action) -> Bool {
@chosa91
chosa91 / libdispatch-efficiency-tips.md
Created February 22, 2020 14:55 — forked from tclementdev/libdispatch-efficiency-tips.md
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

I suspect most developers are using the libdispatch inefficiently due to the way it was presented to us at the time it was introduced and for many years after that, and due to the confusing documentation and API. I realized this after reading the 'concurrency' discussion on the swift-evolution mailing-list, in particular the messages from Pierre Habouzit (who is the libdispatch maintainer at Apple) are quite enlightening (and you can also find many tweets from him on the subject).

My take-aways are:

@chosa91
chosa91 / AutoLayout.md
Created February 8, 2020 09:53 — forked from oliverkrakora/AutoLayout.md
The best damn AutoLayout guide I've ever seen

ℹ️ This article is also available on his blog.


Fundamentals

Layout and Drawing are two different things:

  • Layout defines only the positions and sizes of all views on screen.
  • Drawing specifies how each view is rendered (how it looks).
@chosa91
chosa91 / HW2020Q1 - Sentiment Analysis.md
Last active November 14, 2022 21:27
Hackweek 2020 Q1 - Sentiment analysis

👋 Hackweek 2020 Q1 - Sentiment analysis 👀

Goal

Plan

Tone analysis on text like Grammarly.

Tone detector teaser

@chosa91
chosa91 / machine.js
Created January 13, 2020 19:29
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine({
id: 'some-id',
initial: 'compact',
states: {
compact: {
on: {
PULL_UP: 'expanded',
PULL_DOWN: 'closed'
}
},
@chosa91
chosa91 / GivenWhenThen.swift
Created January 13, 2020 17:29 — forked from IanKeen/GivenWhenThen.swift
FunctionBuilder mvp for a given/when/then test setup
import XCTest
@_functionBuilder
struct Test<T> {
var data: T
var given: given<T>
var when: when<T>
var then: then<T>
func execute() {
@chosa91
chosa91 / KeyboardScrollView.swift
Created December 2, 2019 05:56 — forked from douglashill/KeyboardScrollView.swift
A UIScrollView subclass that allows scrolling using a hardware keyboard like NSScrollView. Supports arrow keys, option + arrow keys, command + arrow keys, space bar, page up, page down, home and end.
// Douglas Hill, November 2019
import UIKit
/// A scroll view that allows scrolling using a hardware keyboard like `NSScrollView`.
/// Supports arrow keys, option + arrow keys, command + arrow keys, space bar, page up, page down, home and end.
/// Limitations:
/// - Paging scroll views (isPagingEnabled = true) are not supported yet.
/// - The scroll view must become its own delegate so setting the delegate is not supported yet.
/// - Does not consider zooming. This has not been tested at all.
import Foundation
struct AnyEquatable {
private let isEqualTo: (Any) -> Bool
let value: Any
init<A: Equatable>(_ value: A) {
self.value = value
isEqualTo = { other in
guard let o = other as? A else { return false }
@chosa91
chosa91 / DynamicUserDefaults.swift
Created November 15, 2019 10:50 — forked from marty-suzuki/DynamicUserDefaults.swift
Sample usage of Swift 5.1 dynamicMemberLookup that supports KeyPath.
import Foundation
@dynamicMemberLookup
final class DynamicUserDefaults {
static var standard: DynamicUserDefaults {
return DynamicUserDefaults(.standard)
}
private let keys = Keys()
import Foundation
public enum Method: String {
case GET
case POST
case PUT
case PATCH
case DELETE
}