elm/exercises/series/Series.example.elm

34 lines
750 B
Elm
Raw Permalink Normal View History

module Series exposing (..)
2016-03-27 11:29:28 +00:00
import String
import List
import Result
slices : Int -> String -> Result String (List (List Int))
slices size input =
if size < 1 then
Err ("Invalid size: " ++ toString size)
else
String.split "" input
|> List.map String.toInt
|> combine
|> Result.map (takeRuns size)
2016-03-27 11:29:28 +00:00
takeRuns : Int -> List Int -> List (List Int)
takeRuns size numbers =
let
candidate =
List.take size numbers
in
if List.length candidate < size || size < 1 then
[]
else
candidate :: takeRuns size (List.drop 1 numbers)
2016-03-27 11:29:28 +00:00
combine : List (Result x a) -> Result x (List a)
combine =
List.foldr (Result.map2 (::)) (Ok [])