[Haskell-cafe] All-fail case in asum
Oleg Grenrus
oleg.grenrus at iki.fi
Fri Jun 10 17:01:58 UTC 2016
If you aren’t scared of the dependencies: semigroupoids has asum1 (yet it’s easy to write yourself)
https://s3.amazonaws.com/haddock.stackage.org/lts-6.2/semigroupoids-5.0.1/Data-Semigroup-Foldable.html#v:asum1
λ Prelude Data.Semigroup.Foldable Data.List.NonEmpty > asum1 ([Right 1, Right 2, Left "error"] :: NonEmpty (Either String Int))
Right 1
λ Prelude Data.Semigroup.Foldable Data.List.NonEmpty > asum1 ([Left "error"] :: NonEmpty (Either String Int))
Left "error"
- Oleg
> On 10 Jun 2016, at 17:37, Юрий Сыровецкий (Yuriy Syrovetskiy) <cblp at cblp.su> wrote:
>
> Hello
>
> I want to define some parser such way:
>
> myParser = tryA <|> tryB <|> fail "input must be either A or B"
>
> It works. But then I want to rewrite it with asum:
>
> myParser = asum [tryA, tryB, fail "must be A or B"]
>
> It works, but the wrong way. Instead of my error it writes "empty".
> Just "empty".
>
> It is so because in base library
>
> asum = foldr (<|>) empty
>
> What if it was defined
>
> asum [] = empty
> asum [x:xs] = x <|> asum xs
>
> It would help me with my parser. But what can this break? Why isn't
> this done yet?
>
> --
> Yuriy Syrovetskiy, http://cblp.su
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 842 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160610/d8d277ee/attachment.sig>
More information about the Haskell-Cafe
mailing list