Skip to content

How to branch pipe #1739

Answered by gcanti
simPod asked this question in Q&A
Aug 11, 2022 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

chain let you combine parsers under an AND condition, alt let you combine parsers under an OR condition, you could define a parser for empty strings (which could be possibly reused in other circumstances):

import { fromPredicate, alt } from 'fp-ts/Either'
import { isEmpty } from 'fp-ts/string'

const empty = fromPredicate(isEmpty, () => 'non empty string')

const program = (s: string): Either<string, string> =>
  pipe(
    empty(s),
    alt(() => validatePassword(s))
  )

pipe('', program, console.log) // { _tag: 'Right', right: '' }
pipe('aaa', program, console.log) // { _tag: 'Left', left: 'at least 6 characters' }

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@simPod
Comment options

Comment options

You must be logged in to vote
1 reply
@simPod
Comment options

Answer selected by simPod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants