File: Tense.hs

package info (click to toggle)
git-annex 5.20141125%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 37,832 kB
  • sloc: haskell: 42,603; sh: 1,080; ansic: 498; makefile: 316; perl: 125
file content (57 lines) | stat: -rw-r--r-- 1,514 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{- Past and present tense text.
 -
 - Copyright 2012 Joey Hess <joey@kitenet.net>
 -
 - License: BSD-2-clause
 -}

{-# LANGUAGE OverloadedStrings #-}

module Utility.Tense where

import qualified Data.Text as T
import Data.Text (Text)
import GHC.Exts( IsString(..) )

data Tense = Present | Past
	deriving (Eq)

data TenseChunk = Tensed Text Text | UnTensed Text
	deriving (Eq, Ord, Show)

newtype TenseText = TenseText [TenseChunk]
	deriving (Eq, Ord)

{- Allows OverloadedStrings to be used, to build UnTensed chunks. -}
instance IsString TenseChunk where
	fromString = UnTensed . T.pack

{- Allows OverloadedStrings to be used, to provide UnTensed TenseText. -}
instance IsString TenseText where
	fromString s = TenseText [fromString s]

renderTense :: Tense -> TenseText -> Text
renderTense tense (TenseText chunks) = T.concat $ map render chunks
  where
	render (Tensed present past)
		| tense == Present = present
		| otherwise = past
	render (UnTensed s) = s

{- Builds up a TenseText, separating chunks with spaces.
 -
 - However, rather than just intersperse new chunks for the spaces,
 - the spaces are appended to the end of the chunks.
 -}
tenseWords :: [TenseChunk] -> TenseText
tenseWords = TenseText . go []
  where
	go c [] = reverse c
	go c (w:[]) = reverse (w:c)
	go c ((UnTensed w):ws) = go (UnTensed (addspace w) : c) ws
	go c ((Tensed w1 w2):ws) =
		go (Tensed (addspace w1) (addspace w2) : c) ws
	addspace w = T.append w " "

unTensed :: Text -> TenseText
unTensed t = TenseText [UnTensed t]