packages feed

interval-patterns-0.8.1: src/Data/OneOrTwo.hs

module Data.OneOrTwo (
  OneOrTwo (..),
  oneOrTwo,
) where

import Data.Data (Data)
import GHC.Generics (Generic)

-- | Either one of something, or two of it.
--
-- Use 'oneOrTwo' to deconstruct.
data OneOrTwo x
  = One !x
  | Two !x !x
  deriving
    ( Eq
    , Ord
    , Show
    , Read
    , Generic
    , Data
    , Functor
    , Foldable
    , Traversable
    )

-- | Apply a 'oneOrTwo' argument function appropriately.
oneOrTwo :: (x -> a) -> (x -> x -> a) -> OneOrTwo x -> a
oneOrTwo f g = \case
  One x -> f x
  Two x y -> g x y
{-# INLINE oneOrTwo #-}