Skip to content

Instantly share code, notes, and snippets.

Avatar

Yasuhiro Inami inamiy

View GitHub Profile
View cata-ana-prefixed-postfixed-points.hs
-- 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
View UnionToIntersection.ts
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)
View fix-mu-nu.hs
-- Solving Fix / Mu / Nu exercise in
-- https://stackoverflow.com/questions/45580858/what-is-the-difference-between-fix-mu-and-nu-in-ed-kmetts-recursion-scheme-pac
{-# LANGUAGE RankNTypes, GADTs #-}
----------------------------------------
-- Fix / Mu / Nu
newtype Fix f = Fix { unFix :: f (Fix f) }
@inamiy
inamiy / ReplaceProps.ts
Created Jul 2, 2020
ReplaceProps in TypeScript
View ReplaceProps.ts
// https://stackoverflow.com/questions/53011500/how-to-replace-properties-using-mapped-types-in-typescript
// https://twitter.com/inamiy/status/1278511811025711104
type ReplaceProps<T, From, To> = {
[K in keyof T]: K extends keyof From
? T[K] extends From[K]
? K extends keyof To
? To[K]
: T[K]
: T[K]
View swift-hash-table.swift
// https://twitter.com/inamiy/status/1257538502226358272
struct Hashable1: Hashable {
struct Hashable1a: Hashable {}
}
struct Hashable2: Hashable {}
Hashable1().hashValue
Hashable1.Hashable1a().hashValue
Hashable2().hashValue
View combine-delay-without-cancellable.swift
Just(())
.delay(for: 1, scheduler: DispatchQueue.main)
.sink {
print("Q1. Can you hear me?") // Yes, even without retaining cancellable. Do you know why? :)
}
Just(())
.delay(for: 1, scheduler: DispatchQueue.main)
.flatMap { Just(()) }
.sink {
View zero-one-infinite-list.hs
take 9 $ [0..] >>= flip replicateM [0, 1]
-- [[],[0],[1],[0,0],[0,1],[1,0],[1,1],[0,0,0],[0,0,1]]
View datatypes-a-la-carte.hs
-- Data types a la carte
-- http://www.cs.ru.nl/~W.Swierstra/Publications/DataTypesALaCarte.pdf
-- [Wadler's Blog: Data Types a la Carte](http://wadler.blogspot.com/2008/02/data-types-la-carte.html)
{-# LANGUAGE TypeOperators, MultiParamTypeClasses, FlexibleInstances, FlexibleContexts, ScopedTypeVariables #-}
----------------------------------------
-- Expression Problem
----------------------------------------
View fmap-fmap-fmap.hs
fmap . fmap . fmap
:: (Functor f1, Functor f2, Functor f3) =>
(a -> b) -> f1 (f2 (f3 a)) -> f1 (f2 (f3 b))
fmap . fmap . fmap
== fmap . (fmap . fmap)
== fmap . (fmap fmap fmap)
== fmap fmap (fmap fmap fmap)
== (fmap fmap . fmap fmap) fmap
== fmap (fmap fmap) (fmap fmap) fmap
@inamiy
inamiy / private-conformance-to-public-protocol.swift
Created Nov 7, 2019
Private conformance to public protocol without making internal members / methods public.
View private-conformance-to-public-protocol.swift
// Private Conformance (Generic Manifesto)
// https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#private-conformances
//
// > Right now, a protocol conformance can be no less visible than the minimum of the conforming type's access and the protocol's access.
// > Therefore, a public type conforming to a public protocol must provide the conformance publicly.
// > The main problem with private conformances is the interaction with dynamic casting
public protocol PublicProtocol {
var foo: String { get }
}
You can’t perform that action at this time.