turboMaCk / non-empty-list-alias / Maybe.NonEmpty

Extensions to Maybe and Maybe.Extra modules providing functions to work with List.NoneEmpty.NonEmpty.

It's safe to import this module as Maybe:

import Maybe.NonEmpty as Maybe

all functions in this module use Ne suffix to prevent collisions with List based alternatives.

Traverse

combineNe : List.NonEmpty.NonEmpty (Maybe a) -> Maybe (List.NonEmpty.NonEmpty a)

If every Maybe in the none empty list is present, return all of the values unwrapped. If there are any Nothings, the whole function fails and returns Nothing.

combineNe ( Just 1, [] )
--> Just (1, [])

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

combineNe ( Just 1, [ Nothing, Just 3 ] )
--> Nothing

traverseNe : (a -> Maybe b) -> List.NonEmpty.NonEmpty a -> Maybe (List.NonEmpty.NonEmpty b)

Like combineNe, but map a function over each element of the list first.

If every function call succeeds (returns Just), traverseNe will return a non empty list. If any function call fails (returns Nothing), traverse will return Nothing.

combineNe is equivalent to traverseNe identity.

traverseNe (\x -> Just (x * 10)) ( 1, [ 2, 3, 4, 5 ] )
--> Just ( 10, [ 20, 30, 40, 50 ] )

traverseNe List.head ( [1], [ [2, 3], [] ] )
--> Nothing