Effect TS

From bibbleWiki
Jump to navigation Jump to search

Introduction

Dipped my toe into this. It seems to be a combination of RxJs and Microsoft DI. Not sure why it is worthwhile using types rather than a typed language. If all you have is TS resource then it makes sense. Make change my mind.

First Program

And its not hello world

import { Effect, pipe, Schema } from 'effect';

const Pokemon = Schema.Struct({
  name: Schema.String,
  weight: Schema.Number,
});

type Pokemon = Schema.Schema.Type<typeof Pokemon>;

const getPokemon = (id: number) => 
    pipe(
       Effect.tryPromise({
        try: () => fetch(`https://pokeapi.co/api/v2/pokemon/${id}`)
            .then(response => response.json()),
        catch: (unknown) => new Error(`Failed to fetch Pokemon with id ${id}, Error %s ${unknown}`),
        }),
        Effect.flatMap((data) => Schema.decodeUnknown(Pokemon)((data))),
    )

const getRandomNumberArray = Effect.all(
    Array.from({ length: 10 }, () => 
        Effect.sync(
            () => Math.floor(Math.random() * 100) +1
        )
    )
)

 const calculateHeaviestPokemon = (pokemons: Pokemon[]) => 
    Effect.reduce(pokemons, 0, (highest, pokemon) =>
        pokemon.weight === highest 
            ? Effect.fail(new Error("Two pokemons have the same weight"))
            : Effect.succeed(pokemon.weight > highest ? pokemon.weight : highest)
    )

const program = pipe(
    getRandomNumberArray,
    Effect.flatMap((arr) =>
        Effect.all(
            arr.map((arr) => getPokemon(arr))
        )
    ),
    Effect.tap((pokemons) =>
    Effect.log("\n" + pokemons.map((pokemon) => `${pokemon.name} ${pokemon.weight}`).join("\n"))),
    Effect.flatMap((pokemon) => calculateHeaviestPokemon(pokemon)),
    Effect.flatMap((heaviestWeight) => 
        Effect.log(`The heaviest Pokemon weighs: ${heaviestWeight}`)    
    ),
)

Effect.runPromise(program).then(console.log)