Skip to content

Instantly share code, notes, and snippets.

View inamiy's full-sized avatar

Yasuhiro Inami inamiy

View GitHub Profile
@inamiy
inamiy / actor-custom-executor.swift
Last active April 13, 2022 03:05
Swift 5.5 Actor impl with stealing GlobalActor's Executors https://twitter.com/inamiy/status/1514076289443176453
import Foundation
import _Concurrency
actor SomeSystem {
nonisolated var unownedExecutor: UnownedSerialExecutor {
#if os(Linux)
MyActor.sharedUnownedExecutor
#else
MainActor.sharedUnownedExecutor
#endif
import Foundation
import _Concurrency
// https://twitter.com/inamiy/status/1506118628839546880
var f: () -> () = {}
var fSendable: @Sendable () -> () = {}
var fMain: @MainActor () -> () = {}
var fMainSendable: @MainActor @Sendable () -> () = {}
@rethrows
protocol P {
func foo() throws
}
struct S1: P {
func foo() {}
}
struct S2: P {
// Q. How to abstract Actor's impl into protocol?
// 1. Plain protocol + async methods
protocol MyProtocol
{
var foo: Int { get async }
func bar() async
}
/// `async` monad that wraps async function.
public struct Async<T>
{
public let run: () async -> T
}
// MARK: - Functor
extension Async
{
func asyncThrowsToAsyncResult<A, B>(_ f: @escaping (A) async throws -> B)
-> (A) async -> Result<B, Error>
{
{ a in
do {
return .success(try await f(a))
} catch {
return .failure(error)
}
}
@inamiy
inamiy / withTaskCancellationHandler.swift
Last active August 19, 2021 15:33
Swift async/await: Re-inventing URLSession's `func data(for:) async throws` https://twitter.com/inamiy/status/1428378193170288645
import Foundation
let url = URL(string: "https://httpbin.org/get")!
@main
struct Main {
static func main() async throws {
print("1")
let task1 = Task.init { () -> Data? in
import SwiftUI
extension View {
func pipe<R>(@ViewBuilder _ f: (Self) -> R) -> R {
f(self)
}
}
struct ContentView: View {
@State var flag: Bool = false
@inamiy
inamiy / flattened-to-tree.hs
Created May 15, 2021 12:52
Flattened to Tree structure
-- こういうフラットな配列から木構造を作るのってどうやったらいいんだろう?
-- https://twitter.com/suin/status/1390626013814476800
{-# LANGUAGE DeriveFunctor, UndecidableInstances, LambdaCase #-}
module Main where
import Data.Functor.Classes
import Data.List
@inamiy
inamiy / what-is-monad.md
Last active October 17, 2021 05:23
What is monad? answered by OpenAI

I, as a programmer, asked professor: What is a monad?


He replied: A monad is an old russian towel, so the monad might have evolved into a soprano, but a soprano is a woman who sings on the stage.

I was confused by this answer, I did not know what a monad is. But as he explained that a monad is an old russian towel, I noticed that there was an obvious connection between the word monad and the word monastery, where people lived in seclusion, and meditated on the topic of the meaning of life.

I did not have an idea what a monad is. So I went to the internet to look for the meaning of the word "monad". I had an impression that "monad" is the highest abstraction of "bead". Beads were the reason that motivated us to invent knots, but later on we learned how to use monads to program.