Sha256: fc4f027691109f7ffb8de0ee1186f4a2427cd04fa06afc7fcc50c0be0b0d143a

Contents?: true

Size: 1.09 KB

Versions: 74

Compression:

Stored size: 1.09 KB

Contents

module LargestSeriesProduct exposing (largestProduct)

import String


largestProduct : Int -> String -> Maybe Int
largestProduct length series =
    if length < 0 then
        Nothing
    else if length == 0 then
        Just 1
    else
        String.split "" series
            |> List.map String.toInt
            |> combineResults
            |> Result.toMaybe
            |> Maybe.map
                (takeRuns length
                    >> List.map List.product
                    >> List.maximum
                )
            |> joinMaybe


takeRuns : Int -> List a -> List (List a)
takeRuns size items =
    let
        candidate =
            List.take size items
    in
    if List.length candidate < size || size < 1 then
        []
    else
        candidate :: takeRuns size (List.drop 1 items)



-- inlined from Results.Extra and Maybe.Extra


combineResults : List (Result x a) -> Result x (List a)
combineResults =
    List.foldr (Result.map2 (::)) (Ok [])


joinMaybe : Maybe (Maybe a) -> Maybe a
joinMaybe mx =
    case mx of
        Just x ->
            x

        Nothing ->
            Nothing

Version data entries

74 entries across 74 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.179 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.178 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.177 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.176 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.175 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.174 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.173 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.172 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.171 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.170 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.169 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.167 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.166 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.165 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.164 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.163 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.162 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.161 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.160 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm