Finch
Copyright(c) Leon Vatthauer 2026
LicenseGPL-3
MaintainerLeon Vatthauer <leon.vatthauer@fau.de>
Stabilityexperimental
Portabilitynon-portable (ghc-wasm-meta)
Safe HaskellNone
LanguageGHC2024

Util

Description

Some utility functions.

Synopsis

List utilities

allCombinations :: [NonEmpty a] -> NonEmpty [a] Source #

Returns all combinations of a list of list. Taken from package liquid-fixpoint and adjusted to use NonEmpty.

Satisfies: allCombinations :: xss:[[a]] -> [{v:[a]| len v == len xss}]

interleave :: [a] -> [a] -> [a] Source #

Interleaves two lists, taking elements alternately from each. The result ends when the first list is exhausted. If the second list runs out first, the remaining head of the first list is appended.

>>> interleave [1,2,3] [10,20,30]
[1,10,2,20,3,30]
>>> interleave [1,2,3] [10]
[1,10,2]

insertAt :: a -> Int -> [a] -> Maybe [a] Source #

insertAt x n xs inserts x at index n into xs. Returns Nothing for invalid indices.

removeAt :: Int -> [a] -> Maybe [a] Source #

removeAt n xs removes the element at index n. Returns Nothing for invalid indices.

updateAtM :: MonadFail m => Int -> (a -> m a) -> [a] -> m [a] Source #

Update nth element of a list, if it exists. O(min index n).

Precondition: the index is >= 0. (Copied from Agda.Utils.List and adjusted for monadicity)

Lens utilities

(%=?) :: MonadState record m => Lens record field -> (field -> Maybe field) -> m () Source #

A variant of (%=) that only updates the field when the function returns Just. If the function returns Nothing, the field is left unchanged.

Numeric utilities

inRange :: (Int, Int) -> Int -> Bool Source #

Returns whether the given integer lies inside the inclusive interval.