Skip to content

Instantly share code, notes, and snippets.

View inamiy's full-sized avatar

Yasuhiro Inami inamiy

View GitHub Profile
-- 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 July 2, 2020 02:27
ReplaceProps in TypeScript
// 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]
// https://twitter.com/inamiy/status/1257538502226358272
struct Hashable1: Hashable {
struct Hashable1a: Hashable {}
}
struct Hashable2: Hashable {}
Hashable1().hashValue
Hashable1.Hashable1a().hashValue
Hashable2().hashValue
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 {
take 9 $ [0..] >>= flip replicateM [0, 1]
-- [[],[0],[1],[0,0],[0,1],[1,0],[1,1],[0,0,0],[0,0,1]]
-- 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
----------------------------------------
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 November 7, 2019 15:07
Private conformance to public protocol without making internal members / methods public.
// 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 }
}
// Xcode 11.1 indent style
[1, 2, 3]
.filter { x in
x.isMultiple(of: 2)
}
.map { $0 }
.map { $0 }
[1, 2, 3]
@inamiy
inamiy / PropertyWrapper-CustomCodable.swift
Created October 25, 2019 15:28
PropertyWrapper CustomCodable
public protocol Transformer {
associatedtype To
associatedtype From
static func convert(to: To) throws -> From
static func convert(from: From) throws -> To
}
@propertyWrapper
public struct CustomCodable<T: Transformer> {
public var wrappedValue: T.To