Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cons #38

Merged
merged 1 commit into from
Nov 11, 2019
Merged

cons #38

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/Maybe/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Maybe.Extra exposing
, values
, combine, traverse, combineArray, traverseArray
, toList, toArray
, cons
, andMap, next, prev
)

Expand Down Expand Up @@ -37,6 +38,7 @@ Work with 1 `Maybe`
# toList

@docs toList, toArray
@docs cons


# Applicative Functions
Expand Down Expand Up @@ -299,16 +301,7 @@ Equivalent to `List.filterMap identity`.
-}
values : List (Maybe a) -> List a
values =
let
step maybe acc =
case maybe of
Nothing ->
acc

Just x ->
x :: acc
in
List.foldr step []
List.foldr cons []


{-| If every `Maybe` in the list is present, return all of the values unwrapped.
Expand Down Expand Up @@ -406,6 +399,25 @@ toArray m =
Array.repeat 1 x


{-| Add an item to a list only if it's a `Just`.

cons (Just 1) [ 2, 3 ]
--> [ 1, 2, 3 ]

cons Nothing [2, 3 ]
--> [ 2, 3 ]

-}
cons : Maybe a -> List a -> List a
cons item list =
case item of
Just v ->
v :: list

Nothing ->
list



-- Applicative Functions

Expand Down