on master help sign in
Merge pull request #614 from Melvar/foldable-pack
e4d9c85Edwin Brady
lib/Prelude/Strings.idr+3−3
@@ -5,6 +5,7 @@ import Prelude.List
5import Prelude.Chars
5import Prelude.Chars
6import Prelude.Cast
6import Prelude.Cast
7import Prelude.Either
7import Prelude.Either
8+import Prelude.Foldable
8
9
9-- Some more complex string operations
10-- Some more complex string operations
10
11
@@ -38,9 +39,8 @@ unpack s with (strM s)
38 unpack "" | StrNil = []
39 unpack "" | StrNil = []
39 unpack (strCons x xs) | (StrCons x xs) = x :: unpack xs
40 unpack (strCons x xs) | (StrCons x xs) = x :: unpack xs
40
41
41−pack : List Char -> String
42+pack : (Foldable t) => t Char -> String
42−pack [] = ""
43+pack = foldr strCons ""
43−pack (x :: xs) = strCons x (pack xs)
44
44
45instance Cast String (List Char) where
45instance Cast String (List Char) where
46 cast = unpack
46 cast = unpack
test/reg024/reg024.idr+1−1
@@ -22,7 +22,7 @@ printf fmt = printFormat (fromList $ unpack fmt) where
22 rec End acc = acc
22 rec End acc = acc
23 rec (FInt rest) acc = \i: Int => rec rest (acc ++ (show i))
23 rec (FInt rest) acc = \i: Int => rec rest (acc ++ (show i))
24 rec (FStr rest) acc = \s: String => rec rest (acc ++ s)
24 rec (FStr rest) acc = \s: String => rec rest (acc ++ s)
25− rec (FChar c rest) acc = rec rest (acc ++ (pack [c]))
25+ rec (FChar c rest) acc = rec rest (acc ++ (strCons c ""))
26
26
27test : String
27test : String
28test = printf "The %s is %d" "answer" 42
28test = printf "The %s is %d" "answer" 42