Skip to content

Commit 8dddba3

Browse files
tejasbubanesshine
authored andcommitted
Port raindrops test-suite to support Data.Text
Issue: #841
1 parent 633f8b5 commit 8dddba3

File tree

6 files changed

+107
-2
lines changed

6 files changed

+107
-2
lines changed

exercises/raindrops/.meta/hints.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Hints
2+
3+
You need to implement the `convert` function that returns number converted to
4+
raindrop sound. You can use the provided signature if you are unsure
5+
about the types, but don't let it restrict your creativity:
6+
7+
```haskell
8+
convert :: Int -> String
9+
```
10+
11+
This exercise works with textual data. For historical reasons, Haskell's
12+
`String` type is synonymous with `[Char]`, a list of characters. For more
13+
efficient handling of textual data, the `Text` type can be used.
14+
15+
As an optional extension to this exercise, you can
16+
17+
- Read about [string types](https://haskell-lang.org/tutorial/string-types) in Haskell.
18+
- Add `- text` to your list of dependencies in package.yaml.
19+
- Import `Data.Text` in [the following way](https://hackernoon.com/4-steps-to-a-better-imports-list-in-haskell-43a3d868273c):
20+
21+
```haskell
22+
import qualified Data.Text as T
23+
import Data.Text (Text)
24+
```
25+
26+
- You can now write e.g. `convert :: Int -> Text` and refer to `Data.Text` combinators as e.g. `T.pack`.
27+
- Look up the documentation for [`Data.Text`](https://hackage.haskell.org/package/text-1.2.3.1/docs/Data-Text.html),
28+
- You can then replace all occurrences of `String` with `Text` in Raindrops.hs:
29+
30+
```haskell
31+
convert :: Int -> Text
32+
```
33+
34+
This part is entirely optional.

exercises/raindrops/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,42 @@ The rules of `raindrops` are that if a given number:
1515
- 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang".
1616
- 34 is not factored by 3, 5, or 7, so the result would be "34".
1717

18+
## Hints
19+
20+
You need to implement the `convert` function that returns number converted to
21+
raindrop sound. You can use the provided signature if you are unsure
22+
about the types, but don't let it restrict your creativity:
23+
24+
```haskell
25+
convert :: Int -> String
26+
```
27+
28+
This exercise works with textual data. For historical reasons, Haskell's
29+
`String` type is synonymous with `[Char]`, a list of characters. For more
30+
efficient handling of textual data, the `Text` type can be used.
31+
32+
As an optional extension to this exercise, you can
33+
34+
- Read about [string types](https://haskell-lang.org/tutorial/string-types) in Haskell.
35+
- Add `- text` to your list of dependencies in package.yaml.
36+
- Import `Data.Text` in [the following way](https://hackernoon.com/4-steps-to-a-better-imports-list-in-haskell-43a3d868273c):
37+
38+
```haskell
39+
import qualified Data.Text as T
40+
import Data.Text (Text)
41+
```
42+
43+
- You can now write e.g. `convert :: Int -> Text` and refer to `Data.Text` combinators as e.g. `T.pack`.
44+
- Look up the documentation for [`Data.Text`](https://hackage.haskell.org/package/text-1.2.3.1/docs/Data-Text.html),
45+
- You can then replace all occurrences of `String` with `Text` in Raindrops.hs:
46+
47+
```haskell
48+
convert :: Int -> Text
49+
```
50+
51+
This part is entirely optional.
52+
53+
1854

1955
## Getting Started
2056

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: raindrops
2+
3+
dependencies:
4+
- base
5+
- text
6+
7+
library:
8+
exposed-modules: Raindrops
9+
source-dirs: src
10+
11+
tests:
12+
test:
13+
main: Tests.hs
14+
source-dirs: test
15+
dependencies:
16+
- raindrops
17+
- hspec
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
module Raindrops (convert) where
3+
4+
import Data.Maybe (fromMaybe)
5+
import qualified Data.Text as T
6+
import Data.Text (Text)
7+
8+
convert :: Int -> Text
9+
convert n = fromMaybe (T.pack $ show n) $
10+
sound "Pling" 3 <>
11+
sound "Plang" 5 <>
12+
sound "Plong" 7
13+
where
14+
sound noise factor
15+
| n `rem` factor == 0 = Just noise
16+
| otherwise = Nothing

exercises/raindrops/package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: raindrops
2-
version: 1.1.0.5
2+
version: 1.1.0.6
33

44
dependencies:
55
- base

exercises/raindrops/test/Tests.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
2+
{-# LANGUAGE OverloadedStrings #-}
23

34
import Data.Foldable (for_)
45
import Test.Hspec (Spec, describe, it, shouldBe)
56
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
7+
import Data.String (fromString)
68

79
import Raindrops (convert)
810

@@ -16,7 +18,7 @@ specs = describe "convert" $ for_ cases test
1618
test (number, expected) = it description assertion
1719
where
1820
description = show number
19-
assertion = convert number `shouldBe` expected
21+
assertion = convert number `shouldBe` fromString expected
2022

2123
cases = [ ( 1, "1" )
2224
, ( 3, "Pling" )

0 commit comments

Comments
 (0)