This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import _Concurrency | |
actor SomeSystem { | |
nonisolated var unownedExecutor: UnownedSerialExecutor { | |
#if os(Linux) | |
MyActor.sharedUnownedExecutor | |
#else | |
MainActor.sharedUnownedExecutor | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import _Concurrency | |
// https://twitter.com/inamiy/status/1506118628839546880 | |
var f: () -> () = {} | |
var fSendable: @Sendable () -> () = {} | |
var fMain: @MainActor () -> () = {} | |
var fMainSendable: @MainActor @Sendable () -> () = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@rethrows | |
protocol P { | |
func foo() throws | |
} | |
struct S1: P { | |
func foo() {} | |
} | |
struct S2: P { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Q. How to abstract Actor's impl into protocol? | |
// 1. Plain protocol + async methods | |
protocol MyProtocol | |
{ | |
var foo: Int { get async } | |
func bar() async | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// `async` monad that wraps async function. | |
public struct Async<T> | |
{ | |
public let run: () async -> T | |
} | |
// MARK: - Functor | |
extension Async | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension View { | |
func pipe<R>(@ViewBuilder _ f: (Self) -> R) -> R { | |
f(self) | |
} | |
} | |
struct ContentView: View { | |
@State var flag: Bool = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- こういうフラットな配列から木構造を作るのってどうやったらいいんだろう? | |
-- https://twitter.com/suin/status/1390626013814476800 | |
{-# LANGUAGE DeriveFunctor, UndecidableInstances, LambdaCase #-} | |
module Main where | |
import Data.Functor.Classes | |
import Data.List |