Skip to content

Instantly share code, notes, and snippets.

@mikearnaldi
mikearnaldi / answers.md
Created November 14, 2021 17:31
My answers to Jan Nasiadka on ZIO
  • What do you think about ZIO’s typed errors? How do you think they help to write better applications?

Having typed errors is fundamental in order to write code that takes care of handling the edge cases, if you don’t have type information when calling a specific effect you cannot know what you may need to handle. At the moment typed errors are still limited in usability by Scala’s capabilities of dealing with unions, namely in Scala 2 unions are represented as sealed traits and you are forced to have an AppError tree with beneath a hierarchy of errors instead with the advent of Scala 3 it will be possible to represent errors using unions getting rid of the need for a big tree of sealed traits.

  • What do you think are the advantages of ZIO’s Fibers over Threads, when writing concurrent applications?

Fibers are extremely lightweight compared to threads and in ZIO they provide even a more rich api and set of capabilities. One of the huge innovations that ZIO introduces is the concept of a fork scope and a su

import * as T from "@effect-ts/core/Effect"
import { pipe } from "@effect-ts/system/Function"
import type { _A } from "@effect-ts/system/Utils"
export type Flat<A> = { readonly [k in keyof A]: A[k] } extends infer X ? X : never
export function service<T extends symbol, X extends {}>(
t: T,
x: X
): Flat<X & { readonly serviceId: T }> {

Reason for the gist

Apparently my account is somehow shadow-banned on reddit (no one's fault, never really used it).

The thread this reply was posted to is: https://www.reddit.com/r/typescript/comments/o2sn3p/which_is_the_best_functional_programming_library/h40l3iu/?utm_source=share&utm_medium=web2x&context=3

Reply

Disclaimer: I am the author of effect-ts.

Effect-ts has been developed from the experience of having both used fp-ts in production and having extended fp-ts to support patterns inspired by zio in Scala.

import * as E from "@effect-ts/core/Either"
import { identity } from "@effect-ts/system/Function"
export type Schema<I, A> =
| SchemaString<I, A>
| SchemaNumber<I, A>
| SchemaRecord<I, A>
| SchemaArray<I, A>
| SchemaDateIso<I, A>
| SchemaCompose<I, A>
import * as T from "@effect-ts/core/Effect"
import * as Ex from "@effect-ts/core/Effect/Exit"
import * as Fiber from "@effect-ts/core/Effect/Fiber"
import * as L from "@effect-ts/core/Effect/Layer"
import * as RM from "@effect-ts/core/Effect/Managed/ReleaseMap"
import * as P from "@effect-ts/core/Effect/Promise"
import * as Ref from "@effect-ts/core/Effect/Ref"
import * as React from "react"
export function makeLayerContext<R>(layer: L.Layer<T.DefaultEnv, never, R>) {
export interface None {
readonly _tag: "None"
}
export interface Some<A> {
readonly _tag: "Some"
readonly value: A
}
export type Option<A> = None | Some<A>
/**
* Base types
*/
export type X = string;
export type Y = "nominal";
export type Z = string | symbol;
export type Extends<A, B> = [A] extends [B]
? "sub-type"
: [B] extends [A]
@mikearnaldi
mikearnaldi / recursion.ts
Last active April 5, 2020 16:37
Recursion schemes in TS
import { Functor1 } from "fp-ts/lib/Functor";
import { URIS, Kind } from "fp-ts/lib/HKT";
import { pipeable } from "fp-ts/lib/pipeable";
import { flow } from "fp-ts/lib/function";
interface Algebra<F extends URIS, A> {
(_: Kind<F, A>): A;
}
interface Fix<F extends URIS> {