Sha256: 4f8f979382094d25da6799f51003484c8809ae28e674c0d9c6eb475316c9969e

Contents?: true

Size: 1.63 KB

Versions: 38

Compression:

Stored size: 1.63 KB

Contents

module RunLengthEncoding exposing (version, encode, decode)

import String exposing (fromChar)
import List exposing (head, tail)
import Maybe exposing (withDefault)
import Regex


{-
   To the unaware: this was written by a very green elmer, so don't consider
   it an idiomatic exemplar to emulate.
-}


version =
    2


encode : String -> String
encode string =
    String.toList string
        |> List.foldr countChars []
        |> List.map stringifyCounts
        |> String.join ""


countChars : a -> List ( number, a ) -> List ( number, a )
countChars current counted =
    case head counted of
        Just ( count, previous ) ->
            if previous == current then
                ( count + 1, current ) :: withDefault [] (tail counted)
            else
                ( 1, current ) :: counted

        Nothing ->
            [ ( 1, current ) ]


stringifyCounts : ( number, Char ) -> String
stringifyCounts ( count, char ) =
    if count > 1 then
        toString count ++ fromChar char
    else
        fromChar char


decode : String -> String
decode string =
    string
        |> Regex.find Regex.All (Regex.regex "(\\d+)|(\\D)")
        |> List.map .match
        |> List.foldl expandCounts ( "", Nothing )
        |> fst


expandCounts : String -> ( String, Maybe Int ) -> ( String, Maybe Int )
expandCounts match ( result, count ) =
    case count of
        Just number ->
            ( result ++ String.repeat number match, Nothing )

        Nothing ->
            case String.toInt match of
                Ok number ->
                    ( result, Just number )

                Err _ ->
                    ( result ++ match, Nothing )

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
trackler-2.0.5.1 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.5.0 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.4.0 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.3.9 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.3.8 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.3.7 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.3.6 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.3.5 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.3.4 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.3.3 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.3.2 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.3.1 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.3.0 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.2.0 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.1.2 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.1.1 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.1.0 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.0.10 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.0.9 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example
trackler-2.0.0.8 tracks/elm/exercises/run-length-encoding/RunLengthEncoding.example