Skip to content

Commit

Permalink
[#311] Add maybeAt, !!? with its arguments flipped (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalpd committed Mar 6, 2021
1 parent 3f87e02 commit 27395f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The changelog is available [on GitHub][2].
* Upgrade to GHC-8.10.3, GHC-8.8.4.
* Add `infinitely` as more strictly typed `forever`.
* Remove `Eq` constraint on `universeNonEmpty`
* Add `maybeAt`, `!!?` with its arguments flipped.

## 0.7.0.0 — May 14, 2020

Expand Down
31 changes: 27 additions & 4 deletions src/Relude/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{- |
Copyright: (c) 2016 Stephen Diehl
(c) 2016-2018 Serokell
(c) 2018-2020 Kowainik
(c) 2018-2021 Kowainik
SPDX-License-Identifier: MIT
Maintainer: Kowainik <xrom.xkov@gmail.com>
Stability: Stable
Expand All @@ -18,24 +18,26 @@ module Relude.List
, module Relude.List.NonEmpty
-- $nonempty
, (!!?)
, maybeAt
, partitionWith
) where


import Relude.Base ((<))
import Relude.Bool (otherwise)
import Relude.Function (flip, (.))
import Relude.List.NonEmpty
import Relude.List.Reexport
import Relude.Monad (Maybe (..), partitionEithers, Either)
import Relude.Monad (Either, Maybe (..), partitionEithers)
import Relude.Numeric (Int, (-))
import Relude.Function ((.))


-- $setup
-- >>> import Relude

{- | Safer version of 'Relude.Unsafe.!!', returns a Maybe.
get element from list using index value starting from `0`.
Get element from list using index value starting from `0`.
>>> [] !!? 0
Nothing
Expand Down Expand Up @@ -63,6 +65,27 @@ infix 9 !!?
go _ [] = Nothing
{-# INLINE (!!?) #-}

{- | '!!?' 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"
-}
maybeAt :: Int -> [a] -> Maybe a
maybeAt = flip (!!?)
{-# INLINE maybeAt #-}

{- | 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.
>>> :{
Expand Down

0 comments on commit 27395f2

Please sign in to comment.