Skip to content

Commit

Permalink
Port raindrops test-suite to support Data.Text
Browse files Browse the repository at this point in the history
Issue: #841
  • Loading branch information
tejasbubane committed Oct 9, 2019
1 parent ef488d3 commit b33ff3e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
34 changes: 34 additions & 0 deletions exercises/raindrops/.meta/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Hints

You need to implement the `convert` function that returns number converted to
raindrop sound. You can use the provided signature if you are unsure
about the types, but don't let it restrict your creativity:

```haskell
convert :: Int -> String
```

This exercise works with textual data. For historical reasons, Haskell's
`String` type is synonymous with `[Char]`, a list of characters. For more
efficient handling of textual data, the `Text` type can be used.

As an optional extension to this exercise, you can

- Read about [string types](https://haskell-lang.org/tutorial/string-types) in Haskell.
- Add `- text` to your list of dependencies in package.yaml.
- Import `Data.Text` in [the following way](https://hackernoon.com/4-steps-to-a-better-imports-list-in-haskell-43a3d868273c):

```haskell
import qualified Data.Text as T
import Data.Text (Text)
```

- You can now write e.g. `convert :: Int -> Text` and refer to `Data.Text` combinators as e.g. `T.pack`.
- Look up the documentation for [`Data.Text`](https://hackage.haskell.org/package/text-1.2.3.1/docs/Data-Text.html),
- You can then replace all occurrences of `String` with `Text` in Raindrops.hs:

```haskell
convert :: Int -> Text
```

This part is entirely optional.
17 changes: 17 additions & 0 deletions exercises/raindrops/examples/success-text/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: raindrops

dependencies:
- base
- text

library:
exposed-modules: Raindrops
source-dirs: src

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- raindrops
- hspec
16 changes: 16 additions & 0 deletions exercises/raindrops/examples/success-text/src/Raindrops.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{-# LANGUAGE OverloadedStrings #-}
module Raindrops (convert) where

import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import Data.Text (Text)

convert :: Int -> Text
convert n = fromMaybe (T.pack $ show n) $
sound "Pling" 3 <>
sound "Plang" 5 <>
sound "Plong" 7
where
sound noise factor
| n `rem` factor == 0 = Just noise
| otherwise = Nothing
2 changes: 1 addition & 1 deletion exercises/raindrops/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: raindrops
version: 1.1.0.5
version: 1.1.0.6

dependencies:
- base
Expand Down
4 changes: 3 additions & 1 deletion exercises/raindrops/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# LANGUAGE OverloadedStrings #-}

import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import Data.String (fromString)

import Raindrops (convert)

Expand All @@ -16,7 +18,7 @@ specs = describe "convert" $ for_ cases test
test (number, expected) = it description assertion
where
description = show number
assertion = convert number `shouldBe` expected
assertion = convert number `shouldBe` fromString expected

cases = [ ( 1, "1" )
, ( 3, "Pling" )
Expand Down

0 comments on commit b33ff3e

Please sign in to comment.