Effect CommunityEC
Effect Community2y ago
15 replies
bug

Title: Idiomatic Implementation of Schema Transformation

my result is fine here, but is there a more idiomatic or concise way to implement this?

import { ParseResult } from '@effect/schema'
import { Schema } from '@effect/schema'
import { Boolean, Console, Effect, Equal, flow, pipe, ReadonlyArray } from 'effect'

const value = ['a', 'b', 'b', 'c', 'b', 'e']

const schema = Schema.transformOrFail(
  Schema.array(Schema.string),
  Schema.array(Schema.string),
  flow(ReadonlyArray.filter(flow(Equal.equals('b'), Boolean.not)), ParseResult.succeed),
  ParseResult.succeed,
)

pipe(value, Schema.parse(schema), Console.log, Effect.runSync)

// { _id: 'Either', _tag: 'Right', right: [ 'a', 'c', 'e' ] }


bonus follow up question, what if i wanted to partition the b instead of filter? would i start with something like:

const schema = Schema.transformOrFail(
  Schema.array(Schema.string),
  Schema.array(Schema.either(Schema.literal('b'), Schema.string)),
  ...something,
  ...something,
)
Was this page helpful?