never executed always true always false
1 module Data.Text.Extra (longestSpanOf, LongestSpanResult(..)) where
2
3 import Elm.Utils ((|>))
4
5 import qualified Data.Text as Text
6 import Data.Text (Text)
7 import qualified Data.Maybe as Maybe
8
9
10 data LongestSpanResult
11 = NoSpan
12 | Span Int {- >= 1 -}
13 deriving (Eq, Show)
14
15
16 longestSpanOf :: Char -> Text -> LongestSpanResult
17 longestSpanOf char input =
18 case Text.foldl' step (Nothing, 0) input |> endCurrentSpan of
19 0 -> NoSpan
20 positive -> Span positive
21 where
22 step (currentSpan, longest) c =
23 if c == char
24 then
25 ( Just (1 + Maybe.fromMaybe 0 currentSpan)
26 , longest
27 )
28 else
29 ( -- clear the current span
30 Nothing
31 , -- and update the longest
32 endCurrentSpan (currentSpan, longest)
33 )
34
35 endCurrentSpan (Nothing, longest) = longest
36 endCurrentSpan (Just current, longest) = max current longest