Relude.List

relude-1.2.2.0: Safe, performant, user-friendly and lightweight Haskell Standard Library

Copyright | (c) 2016 Stephen Diehl (c) 2016-2018 Serokell (c) 2018-2023 Kowainik
License | MIT
Maintainer | Kowainik <xrom.xkov@gmail.com>
Stability | Stable
Portability | Portable
Safe Haskell | Safe
Language | Haskell2010

Relude.List

Description

Utility functions to work with lists and `NonEmpty` lists.

Synopsis

- module Relude.List.Reexport
- module Relude.List.NonEmpty
- (!!?) :: [a] -> Int -> Maybe a
- maybeAt :: Int -> [a] -> Maybe a
- partitionWith :: (a -> Either b c) -> [a] -> ([b], [c])

# Documentation

module Relude.List.Reexport

Most of the Data.List types and function.

Note, that list partial functions (e.g. `head`) are not exported from Data.List. Instead `relude` provides safe functions that work with `NonEmpty`. You can find them in the Relude.List.NonEmpty module instead.

module Relude.List.NonEmpty

Reexports from Data.List.NonEmpty and additional safe functions to work with list type in terms of `NonEmpty`.

(!!?) :: [a] -> Int -> Maybe a infix 9 #

Safer version of `!!`, returns a Maybe.

Get element from list using index value starting from `0`.

    >>> [] !!? 0
    Nothing

    >>> ["a", "b", "c"] !!? 3
    Nothing

    >>> [1, 2, 3] !!? (-1)
    Nothing

    >>> ["a", "b", "c"] !!? 2
    Just "c"

Since: 0.6.0.0

maybeAt :: Int -> [a] -> Maybe a #

`!!?` with its arguments flipped.

Get element from list using index value starting from `0`.

    >>> maybeAt 0 []
    Nothing

    >>> maybeAt 3 ["a", "b", "c"]
    Nothing

    >>> maybeAt (-1) [1, 2, 3]
    Nothing

    >>> maybeAt 2 ["a", "b", "c"]
    Just "c"

Since: 1.0.0.0

partitionWith :: (a -> Either b c) -> [a] -> ([b], [c]) #

Partitions a list based on the result of function which produces an Either value. List of all elements producing Left are extracted, in order, to the first element of the output tuple. Similarly, a list of all elements producing Right are extracted to the second element of output.

    >>> :{
     divideEvenOrShow :: Int -> Either Int String
     divideEvenOrShow n
         | even n = Left $ n `div` 2
         | otherwise = Right $ "Odd: " <> show n
     :}

    >>> partitionWith divideEvenOrShow [1 .. 6]
    ([1,2,3],["Odd: 1","Odd: 3","Odd: 5"])

Since: 1.0.0.0