A Composition of composition

Nils Eriksson

Consultant at Mirado

http://package.elm-lang.org/packages/elm-lang/core/5.1.1/List

:C

LIST EXTRA :D

http://package.elm-lang.org/packages/elm-community/list-extra/6.1.0/List-Extra

{-| Return all elements of the list except the last one.
    init [1,2,3] == Just [1,2]
    init [] == Nothing
-}
init : List a -> Maybe (List a)
init =
    let
        maybe : b -> (List a -> b) -> Maybe (List a) -> b
        maybe d f =
            Maybe.withDefault d << Maybe.map f
    in
        foldr (\x -> maybe [] ((::) x) >> Just) Nothing

VS

|>
<<
>>
{-| Return all elements of the list except the last one.
    init [1,2,3] == Just [1,2]
    init [] == Nothing
-}
init : List a -> Maybe (List a)
init =
    let
        maybe : b -> (List a -> b) -> Maybe (List a) -> b
        maybe d f =
            Maybe.withDefault d << Maybe.map f
    in
        foldr (\x -> maybe [] ((::) x) >> Just) Nothing
{-| Return all elements of the list except the last one.
    init [1,2,3] == Just [1,2]
    init [] == Nothing
-}
init : List a -> Maybe (List a)
init =
    let
        maybe : b -> (List a -> b) -> Maybe (List a) -> b
        maybe d f =
            Maybe.withDefault d << Maybe.map f
    in
        foldr (\x y -> (maybe [] ((::) x) >> Just) y) Nothing
{-| Return all elements of the list except the last one.
    init [1,2,3] == Just [1,2]
    init [] == Nothing
-}
init : List a -> Maybe (List a)
init =
    let
        maybe : (List a -> List b) -> Maybe (List a) -> List b
        maybe f =
            Maybe.withDefault [] << Maybe.map f
    in
        foldr (\x y -> ((maybe ((::) x)) >> Just) y) Nothing
{-| Return all elements of the list except the last one.
    init [1,2,3] == Just [1,2]
    init [] == Nothing
-}
init : List a -> Maybe (List a)
init =
    let
        maybe : a -> Maybe (List a) -> List a
        maybe x y =
            Maybe.map ((::) x) y 
                |> Maybe.withDefault [] 
    in
        foldr (\x y -> Just (maybe x y)) Nothing
{-| Return all elements of the list except the last one.
    init [1,2,3] == Just [1,2]
    init [] == Nothing
-}
init : List a -> Maybe (List a)
init list =
    let
        maybe : a -> Maybe (List a) -> List a
        maybe item maybeList =
            Maybe.map (\list -> item :: list ) maybeList 
                |> Maybe.withDefault [] 
    in
        foldr (\item acc -> Just (maybe item acc)) Nothing list
allButLast : List a -> List a
allButLast aList =
    case aList of
        [] ->
            []

        [ _ ] ->
            []

        x :: xs ->
            x :: allButLast xs






init = reverse >> tail >> Maybe.map reverse

A Composition of composition

By Nils Eriksson

A Composition of composition

What is this fancy composition thing ?

  • 835