on 3feeec2 help sign in
55 lines 1.8 kB history raw
1-- |
2-- Module : Streamly.Internal.Data.Either.Strict
3-- Copyright : (c) 2019 Composewell Technologies
4-- (c) 2013 Gabriel Gonzalez
5-- License : BSD3
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
8-- Portability : GHC
9--
10-- | Strict data types to be used as accumulator for strict left folds and
11-- scans. For more comprehensive strict data types see
12-- https://hackage.haskell.org/package/strict-base-types . The names have been
13-- suffixed by a prime so that programmers can easily distinguish the strict
14-- versions from the lazy ones.
15--
16-- One major advantage of strict data structures as accumulators in folds and
17-- scans is that it helps the compiler optimize the code much better by
18-- unboxing. In a big tight loop the difference could be huge.
19--
20module Streamly.Internal.Data.Either.Strict
21 ( Either' (..)
22 , isLeft'
23 , isRight'
24 , fromLeft'
25 , fromRight'
26 )
27where
29-- | A strict 'Either'
30data Either' a b = Left' !a | Right' !b deriving Show
32-- | Return 'True' if the given value is a Left', 'False' otherwise.
33{-# INLINABLE isLeft' #-}
34isLeft' :: Either' a b -> Bool
35isLeft' (Left' _) = True
36isLeft' (Right' _) = False
38-- | Return 'True' if the given value is a Right', 'False' otherwise.
39{-# INLINABLE isRight' #-}
40isRight' :: Either' a b -> Bool
41isRight' (Left' _) = False
42isRight' (Right' _) = True
44-- XXX This is partial. We can use a default value instead.
45-- | Return the contents of a Left'-value or errors out.
46{-# INLINABLE fromLeft' #-}
47fromLeft' :: Either' a b -> a
48fromLeft' (Left' a) = a
49fromLeft' _ = error "fromLeft' expecting a Left'-value"
51-- | Return the contents of a Right'-value or errors out.
52{-# INLINABLE fromRight' #-}
53fromRight' :: Either' a b -> b
54fromRight' (Right' b) = b
55fromRight' _ = error "fromRight' expecting a Right'-value"