-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strings.hs
74 lines (59 loc) · 1.77 KB
/
Strings.hs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-----------------------------------------------------------------------------
--
-- Module : Strings
-- Copyright :
--
-- | a module with a class for strings, sucht that the normal ops are
-- all polymorphic
--
-----------------------------------------------------------------------------
{-# LANGUAGE
MultiParamTypeClasses
-- , TypeSynonymInstances
-- , FunctionalDependencies
, FlexibleInstances
-- , FlexibleContexts
-- , DeriveFunctor
-- , ScopedTypeVariables
, UndecidableInstances
, OverloadedStrings
-- , TypeFamilies
#-}
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
module Strings (BSUTF.ByteString
, PolyString (..) -- is this required?
, s2b, b2s, b2t, t2s, t2b, s2t) where
import Control.Monad
import GHC.Exts( IsString(..) )
import qualified Data.Text as T
import qualified Data.ByteString.UTF8 as BSUTF (ByteString, toString, fromString)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
newtype MyString = MyString String deriving (Eq, Show)
class PolyString s where
pshow :: s -> String
punwords :: [s] -> s
pputIOwords :: [s] -> IO ()
instance PolyString String where
pshow = id
punwords = unwords
pputIOwords = mapM_ print
instance PolyString T.Text where
pshow = show . T.unpack
punwords = T.unwords
pputIOwords = mapM_ print . map t2s
instance PolyString BSUTF.ByteString where
pshow = show . BSUTF.toString
--instance (IsString m) => PolyString m where
-- pshow = show . fromString
s2b :: String -> BSUTF.ByteString
s2b = BSUTF.fromString
b2s :: BSUTF.ByteString -> String
b2s = BSUTF.toString
s2t :: String -> T.Text
s2t = T.pack
t2s :: T.Text -> String
t2s = T.unpack
t2b :: T.Text -> BSUTF.ByteString
t2b = encodeUtf8
b2t :: BSUTF.ByteString -> T.Text
b2t = decodeUtf8