Add concatMapSep (#75)

This commit is contained in:
Gabriel Gonzalez 2017-06-17 08:49:27 -07:00 committed by GitHub
parent 5afee37868
commit 33b4fcec78
2 changed files with 65 additions and 0 deletions

47
Prelude/Text/concatMapSep Normal file
View File

@ -0,0 +1,47 @@
{-
Transform each value in a `List` to `Text` and then concatenate them with a
separator in between each value
Examples:
```
./concatMapSep ", " Integer Integer/show [0, 1, 2] = "0, 1, 2"
./concatMapSep ", " Integer Integer/show ([] : List Integer) = ""
```
-}
let concatMapSep
: ∀(separator : Text) → ∀(a : Type) → (a → Text) → List a → Text
= λ(separator : Text)
→ λ(a : Type)
→ λ(f : a → Text)
→ λ(elements : List a)
→ let status
= List/fold
a
elements
< Empty : {} | NonEmpty : Text >
( λ(element : a)
→ λ(status : < Empty : {} | NonEmpty : Text >)
→ merge
{ Empty
= λ(_ : {})
→ < NonEmpty = f element
| Empty : {}
>
, NonEmpty
= λ(result : Text)
→ < NonEmpty = f element ++ separator ++ result
| Empty : {}
>
}
status : < Empty : {} | NonEmpty : Text >
)
< Empty = {=} | NonEmpty : Text >
in merge
{ Empty = λ(_ : {}) → ""
, NonEmpty = λ(result : Text) → result
}
status : Text
in concatMapSep

View File

@ -240,6 +240,10 @@ exampleTests =
[ _Text_concatSep_0
, _Text_concatSep_1
]
, Test.Tasty.testGroup "concatMapSep"
[ _Text_concatMapSep_0
, _Text_concatMapSep_1
]
]
]
@ -1152,3 +1156,17 @@ _Text_concatSep_1 = Test.Tasty.HUnit.testCase "Example #1" (do
./Prelude/Text/concatSep ", " ([] : List Text)
|]
Util.assertNormalizesTo e "\"\"" )
_Text_concatMapSep_0 :: TestTree
_Text_concatMapSep_0 = Test.Tasty.HUnit.testCase "Example #0" (do
e <- Util.code [NeatInterpolation.text|
./Prelude/Text/concatMapSep ", " Integer Integer/show [0, 1, 2]
|]
Util.assertNormalizesTo e "\"0, 1, 2\"" )
_Text_concatMapSep_1 :: TestTree
_Text_concatMapSep_1 = Test.Tasty.HUnit.testCase "Example #1" (do
e <- Util.code [NeatInterpolation.text|
./Prelude/Text/concatMapSep ", " Integer Integer/show ([] : List Integer)
|]
Util.assertNormalizesTo e "\"\"" )