-- | Text utility functions.
module NLP.Minimorph.Util
 ( (<+>), tshow )
 where

-- Only needed for older GHC, but let's avoid CPP for this instance.
import Data.Monoid ((<>))

import           Data.Text (Text)
import qualified Data.Text as T

infixr 6 <+>  -- matches Monoid.<>
-- | Separated by space unless one of them is empty (in which case just
--   the non-empty one).
(<+>) :: Text -> Text -> Text
t1 :: Text
t1 <+> :: Text -> Text -> Text
<+> t2 :: Text
t2 | Text -> Bool
T.null Text
t1 = Text
t2
          | Text -> Bool
T.null Text
t2 = Text
t1
          | Bool
otherwise = Text
t1 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> " " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t2

-- | Show a value in `Text` format.
tshow :: Show a => a -> Text
tshow :: a -> Text
tshow = String -> Text
T.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show