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.119 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.118 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.117 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.116 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.115 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.114 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.113 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.111 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.110 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.109 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.108 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.107 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.106 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm
trackler-2.2.1.105 tracks/elm/exercises/largest-series-product/LargestSeriesProduct.example.elm