0 | ||| Various utility functions used across the library.
  1 | module Text.Html.Utils
  2 |
  3 | import Data.Nat
  4 | import Data.String
  5 | import Language.Reflection
  6 |
  7 | %default total
  8 |
  9 | ||| Checks whether the `left` list is a prefix of the `right` one.
 10 | |||
 11 | ||| The comparison is performed using `==`.
 12 | ||| Returns the matched prefix together with the leftover suffix.
 13 | |||
 14 | ||| @ left  The list which might be a prefix of `right`.
 15 | ||| @ right The list of elements to compare against.
 16 | prefixOf : Eq a => (left : List a) -> (right : List a) -> Maybe (List a, List a)
 17 | prefixOf = prefixOfBy (\x, y => if x == y then Just x else Nothing)
 18 |
 19 | private
 20 | lowerFirst : List Char -> List Char
 21 | lowerFirst [] = []
 22 | lowerFirst (x :: xs) = toLower x :: xs
 23 |
 24 | ||| Sets the first letter of the provided `String` to lower case.
 25 | export
 26 | lowerFirstStr : String -> String
 27 | lowerFirstStr = pack . lowerFirst . unpack
 28 |
 29 | private
 30 | upperFirst : List Char -> List Char
 31 | upperFirst [] = []
 32 | upperFirst (x :: xs) = toUpper x :: xs
 33 |
 34 | ||| Sets the first letter of the provided `String` to upper case.
 35 | export
 36 | upperFirstStr : String -> String
 37 | upperFirstStr = pack . upperFirst . unpack
 38 |
 39 | ||| Replaces all occurrences of the `subsequence` within the `list`.
 40 | |||
 41 | ||| @ subsequence The subsequence to replace.
 42 | ||| @ replacement The replacement for the subsequence.
 43 | ||| @ list        The list.
 44 | replace :
 45 |      Eq a
 46 |   => (subsequence : List a)
 47 |   -> (replacement : List a)
 48 |   -> (list : List a)
 49 |   -> List a
 50 | replace _    _  []          = []
 51 | replace []   _  xs          = xs
 52 | replace subs re l@(x :: xs) =
 53 |   case prefixOf subs l of
 54 |     Nothing          => x :: replace subs re xs
 55 |     -- Total because the suffix is smaller than the list.
 56 |     Just (_, suffix) => re ++ assert_total (replace subs re suffix)
 57 |
 58 | ||| Replaces all occurences of the `needle` within the `string`.
 59 | |||
 60 | ||| @ needle      The character to replace.
 61 | ||| @ replacement The replacement character.
 62 | ||| @ string      The string.
 63 | export
 64 | replaceChar :
 65 |      (needle : Char)
 66 |   -> (replacement : Char)
 67 |   -> (string : String)
 68 |   -> String
 69 | replaceChar subs re str = pack $ helper subs re (unpack str)
 70 |   where
 71 |     helper : Char -> Char -> List Char -> List Char
 72 |     helper _ _ [] = []
 73 |     helper s r (x :: xs) = let h := if s == x then r else x in
 74 |                            h :: helper s r xs
 75 |
 76 | ||| Replaces all occurences of the `substring` within the `string`.
 77 | |||
 78 | ||| @ substring   The substring to replace.
 79 | ||| @ replacement The replacement string.
 80 | ||| @ string      The string.
 81 | export
 82 | replaceStr :
 83 |      (substring : String)
 84 |   -> (replacement : String)
 85 |   -> (string : String)
 86 |   -> String
 87 | replaceStr subs re str = pack $ replace (unpack subs) (unpack re) (unpack str)
 88 |
 89 | ||| Maps list's elements to monadic actions collecting the results.
 90 | |||
 91 | ||| Actions get evaluated from left to right before results are collected.
 92 | public export
 93 | mapM : Monad m => (a -> m b) -> List a -> m (List b)
 94 | mapM _ Nil = pure Nil
 95 | mapM f (x :: xs) = do
 96 |   x' <- f x
 97 |   xs' <- mapM f xs
 98 |   pure (x' :: xs')
 99 |
100 | ||| Maps list's elements to monadic actions collecting the results.
101 | |||
102 | ||| Actions get evaluated from left to right before results are collected.
103 | public export
104 | mapM1 : Monad m => (a -> m b) -> List1 a -> m (List1 b)
105 | mapM1 f (x ::: []) = do
106 |   x' <- f x
107 |   pure (x' ::: [])
108 | mapM1 f (x ::: xs) = do
109 |   x' <- f x
110 |   xs' <- mapM f xs
111 |   pure (x' ::: xs')
112 |
113 | ||| Applies a function to each list's item in a sequence.
114 | export
115 | mconcatMapM : (a -> Elab ()) -> List a -> Elab ()
116 | mconcatMapM f []        = pure ()
117 | mconcatMapM f (x :: xs) = f x >> mconcatMapM f xs
118 |
119 | ||| `show` alias.
120 | |||
121 | ||| It's needed to avoid name clashes within the compile time `IsRight` function
122 | ||| so error messages are properly displayed.
123 | public export
124 | natToStr : Nat -> String
125 | natToStr 0 = "0"
126 | natToStr 1 = "1"
127 | natToStr 2 = "2"
128 | natToStr 3 = "3"
129 | natToStr 4 = "4"
130 | natToStr 5 = "5"
131 | natToStr 6 = "6"
132 | natToStr 7 = "7"
133 | natToStr 8 = "8"
134 | natToStr 9 = "9"
135 | natToStr n =
136 |   let
137 |     -- Total because `n` cannot be `0` due to the case split.
138 |     remain = assert_total (n `modNat` 10)
139 |     div10 = assert_total $ n `divNat` 10
140 |   in
141 |   -- `div10` and `remain` are smaller than `n` from their definition as `n` is
142 |   -- greater than `9` from the case split.
143 |   natToStr (assert_smaller n $ div10) <+> (natToStr $ assert_smaller n remain)
144 |