effect icon indicating copy to clipboard operation
effect copied to clipboard

docs: no docs for filter() and filterMap()

Open wbhob opened this issue 1 year ago • 5 comments

What is the type of issue?

Documentation is missing

What is the issue?

I'm new to Effect and still getting the hang of things. I see there's an Effect.filter and Effect.filterMap operator that I think does what I need it to do for this situation (succeed if predicate is true, otherwise stop), but I can't figure out how it works and I can't find usage docs for them, only the API reference for its signature. There's only one mention in the Getting Started Docs on the Micro page.

Where did you find it?

https://effect-ts.github.io/effect/effect/Effect.ts.html#filter https://effect.website/docs/other/micro/effect-users

wbhob avatar Jul 21 '24 19:07 wbhob

import { Effect, Option } from "effect"

const filtered = Effect.filter(
  [0, 1, 2, "ok", "maybe"],
  (v) => v === "nooo" ? Effect.fail(new Error(v)) : Effect.sync(() => typeof v === "number")
)

const filterMapped = Effect.filterMap(
  [Effect.succeed(0), Effect.succeed(1), Effect.succeed(2)],
  (v) => v > 3 ? Option.none() : Option.some(v + 1)
)

as much as we'd love for the docs to be more complete you won't be able to find examples for every single function in there

mikearnaldi avatar Jul 22 '24 10:07 mikearnaldi

Messed around a little in REPL and I think I'm understanding how these operators work. The way I think about it is:

// if your filter is simple
array.filter(() => boolean)
// if your filter is complicated or async
Effect.filter(array, (v) => Effect<boolean>)

// if you need to filter and simple map
array.map((v) => NonNullable<any> | null).filter(element => element !== null)
// if you want to filterMap items that are effects
Effect.filterMap(arrayOfEffects, (v) => Option)

Are there plans to add operators such as these? I feel like they could add some flexibility to filtering iterable effects.

Effect.filterEffects(Effect[], () => boolean)
Effect.filterMap(Iterable<A>, () => Effect<Option>)
Effect.fiterMapEffect(Effect[], () => Effect<Option>)

wbhob avatar Jul 24 '24 03:07 wbhob

https://github.com/Effect-TS/effect/issues/3318

Open for discussion what set of filtering functions would be best

mikearnaldi avatar Jul 24 '24 07:07 mikearnaldi

Cool, will follow that issue. Are y'all open to docs contributions from the community? Can write a couple things as I find them and learn how they work to make it easier for the next person :) I have a feeling this package is gonna be big one day 🚀

wbhob avatar Jul 24 '24 16:07 wbhob

Cool, will follow that issue. Are y'all open to docs contributions from the community? Can write a couple things as I find them and learn how they work to make it easier for the next person :) I have a feeling this package is gonna be big one day 🚀

of course! contributions are welcome

mikearnaldi avatar Jul 25 '24 13:07 mikearnaldi