File: Either.hs

package info (click to toggle)
bali-phy 4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,392 kB
  • sloc: cpp: 120,442; xml: 13,966; haskell: 9,975; python: 2,936; yacc: 1,328; perl: 1,169; lex: 912; sh: 343; makefile: 26
file content (35 lines) | stat: -rw-r--r-- 762 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Either where

import Data.Bool
import Data.List  -- for foldr
import Data.Eq

data Either a b = Left a | Right b

either f g (Left  l) = f l
either f g (Right r) = g r

lefts  xs = [ l | Left  l <- xs]
rights xs = [ r | Right r <- xs]

isLeft (Left _) = True
isLeft _        = False

isRight (Right _) = True
isRight _         = False

fromLeft _ (Left l) = l
fromLett l _        = l

fromRight _ (Right r) = r
fromRight r _         = r

partitionEithers = foldr (either left right) ([], [])
    where left  a ~(l, r) = (a:l, r)
          right a ~(l, r) = (l, a:r)

instance (Eq a, Eq b) => Eq (Either a b) where
    Left  x == Left  y  =  x == y
    Right x == Right y  =  x == y
    _       == _        =  False