Skip to content

Instantly share code, notes, and snippets.

View inamiy's full-sized avatar

Yasuhiro Inami inamiy

View GitHub Profile
@inamiy
inamiy / mimicking-extensibles.swift
Created March 7, 2021 08:00
Mimicking extensibles (intersection/union types) in Swift https://twitter.com/inamiy/status/1368468757702569986
struct State1 {
var foo1: Int
}
struct State2 {
var bar2: Bool
}
// Q. How to make a "flattened" `struct State1_2 { var foo1: Int, var bar2: Bool }`
// from `State1` and `State2`? (a.k.a. extensible record / intersection type)?
@inamiy
inamiy / recursive-struct.swift
Created February 9, 2021 06:23
Recursive struct in Swift using Box (indirect struct)
@propertyWrapper
class Box<T> {
var wrappedValue: T
var projectedValue: Box<T> {
Box(wrappedValue)
}
init(_ value: T) {
self.wrappedValue = value
/// Pseudo-Swift: public / fileprivate enum cases
public enum Action {
public case foo
public case bar(Int)
// fileprivate enum cases, where pattern matches are only allowed in this file.
// From outside, only using switch statement's `default` is possible.
// (NOTE: `private` won't make sense)
//
// This is useful to define private-Action values.
/// Swift do-defer-guard test
func test(pass: Bool) {
do {
print("1. start of scope")
defer { print("3. deferred") }
guard pass else { return }
print("2. end of scope")
}
import XCTest
import Combine
class DeinitTimingTests: XCTestCase {
func testExample() throws {
let publisher = PassthroughSubject<Void, Never>()
var cancellables = Set<AnyCancellable>()
var isDone = false
{-# LANGUAGE RankNTypes #-}
module Church where
-- Church encoding of boolean.
-- 1st arg as `true`, 2nd arg as `false`.
type Bool' = forall x. x -> x -> x
true :: Bool'
true = \t f -> t
flipAll2 :: (t1 -> t2 -> t3) -> t2 -> t1 -> t3
flipAll2 = flip
-- flip 1st & 2nd arg
flipAll3 :: (t1 -> t2 -> t3 -> t4) -> t3 -> t2 -> t1 -> t4
flipAll3 = flipAll2 . (flip .) . flip
-- flip 1st & 2nd arg, 2nd & 3rd arg, goto flipAll2
flipAll4 :: (t1 -> t2 -> t3 -> t4 -> t5) -> t4 -> t3 -> t2 -> t1 -> t5
flipAll4 = flipAll3 . ((flip .) .) . (flip .) . flip
@inamiy
inamiy / SwiftUI-emulating-React-Hooks.swift
Last active March 20, 2023 07:19
SwiftUI emulating React custom hooks (DynamicProperty composition) https://twitter.com/inamiy/status/1313343537132433409
import SwiftUI
struct ContentView: View {
@UseCounter
var counter: Int = 0
@UseCounterEffect(initialCount: 1, onUpdate: { count in
print("===> update: \(count)")
})
var counterEffect: Void
-- For all `x` that is prefixed point of `f` (f x :< x),
-- it is upper bound of `f^n ⊥` (where n = 0, 1, 2, ...)
-- so that its least upper bound `Mu f` is always less than or equal to `x`.
cata :: (f x :< x) -> Mu f :< x
-- For all `x` that is postfixed point of `f` (x :< f x),
-- it is lower bound of `f^n ⊤` (where n = 0, 1, 2, ...)
-- so that its greatest lower bound `Nu f` is always greater than or equal to `x`.
ana : (x :< f x) -> x :< Nu f
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
? I
: never
type FunctionUnion = (() => void) | ((p: string) => void)
type FunctionIntersection = UnionToIntersection<FunctionUnion>
// (() => void) & ((p: string) => void)