andre-dietrich / elm-conditional / Conditional.Array

Conditional functions for array manipulation.

Manipulate

setIf : Basics.Bool -> Basics.Int -> a -> Array a -> Array a

Set the element at a particular index if the condition is met. Returns an updated array. If the index is out of range, the array is unaltered.

Array.fromList [ 1, 2, 3 ]
    |> setIf False 1 7
    |> (==) Array.fromList [ 1, 2, 3 ]

setWhen : Basics.Int -> Maybe a -> Array a -> Array a

Set the element at a particular index, if it is not of type Nothing. If the index is out of range, the array is unaltered.

setWhen 1 (Just 7) (Array.fromList [ 1, 2, 3 ]) == Array.fromList [ 1, 7, 3 ]

pushIf : Basics.Bool -> a -> Array a -> Array a

Push an element onto the end of an array if the first value evalutes to True.

pushIf False 3 (Array.fromList [ 1, 2 ]) == Array.fromList [ 1, 2 ]

pushWhen : Maybe a -> Array a -> Array a

Push an element onto the end of an array, if it is not of type Nothing.

pushWhen (Just 3) (Array.fromList [ 1, 2 ]) == Array.fromList [ 1, 2, 3 ]

appendIf : Basics.Bool -> Array a -> Array a -> Array a

Append the first array only if the first parameter evalutes to True.

appendIf False (Array.repeat 2 42) (Array.repeat 3 81) == Array.fromList [ 81, 81, 81 ]

appendWhen : Maybe (Array a) -> Array a -> Array a

Append two arrays to a new one, if the first one is not of type Nothing.

append Nothing (repeat 3 81) == fromList [ 81, 81, 81 ]

Transform

mapIf : Basics.Bool -> (a -> a) -> Array a -> Array a

Conditional mapping that is only performed, if the first parameter evalutes to True. The resulting array has to be of the same type as the input array.

filterIf : Basics.Bool -> (a -> Basics.Bool) -> Array a -> Array a

Conditional filtering that is only performed, if the first parameter evalutes to True.

sliceIf : Basics.Bool -> Basics.Int -> Basics.Int -> Array a -> Array a

Conditional slice that is only performed, if the first parameter evalutes to True.