Header menu logo ArrayT

Logo

ArrayT

ArrayT on nuget.org Build Status Docs Build Status Test Status

license code size

ArrayT is an F# extension and module library for Array<'T>

It also works in Javascript and Typescript with Fable.

Motivation

I was always annoyed that an IndexOutOfRangeException does not include the actual index that was out of bounds nor the actual size of the array. This library fixes that in array.Get, array.Set, array.Slice and other item access functions.

This library was designed for use with F# scripting.
Functions and methods never return null.
Only functions starting with try... will return an F# Option.
Otherwise when a function fails on invalid input it will throw a descriptive exception.

See also https://github.com/goswinr/ResizeArray/ for a similar library for ResizeArray<'T>.

It Includes

Full API Documentation

goswinr.github.io/ArrayT

Usage

Just open the namespace

open ArrayT

this namespace contains:

Examples

Better error messages

The core motivation: descriptive exceptions when accessing out-of-range indices.

#r "nuget: ArrayT"
open ArrayT

let xs = [| 0 .. 88 |]

xs.Get(99)

throws

System.IndexOutOfRangeException: Array.Get: Can't get index 99 from:
array<Int32> with 89 items:
  0: 0
  1: 1
  2: 2
  3: 3
  4: 4
  ...
  88: 88

instead of the usual unhelpful System.IndexOutOfRangeException: Index was outside the bounds of the array.

The same applies to Set:

xs.Set(99, 0)
// throws: Array.Set: Can't set index 99 to value '0' in array<Int32> with 89 items.

If you want to use bracket notation xs.[i] with these descriptive errors, use the DebugIdx member:

let item = xs.DebugIdx.[99]   // throws with same descriptive message
xs.DebugIdx.[99] <- 0         // setter also has descriptive errors

Positional access

Convenient properties for accessing items at common positions:

let arr = [| "a"; "b"; "c"; "d"; "e" |]

arr.First       // "a"
arr.Second      // "b"
arr.Third       // "c"
arr.Last        // "e"
arr.SecondLast  // "d"
arr.ThirdLast   // "c"

// These are settable too
arr.Last <- "z"          // arr is now [| "a"; "b"; "c"; "d"; "z" |]

// For single-element arrays
let single = [| 42 |]
single.FirstAndOnly  // 42

Or via module functions for use in pipelines:

[| 10; 20; 30 |] |> Array.first       // 10
[| 10; 20; 30 |] |> Array.last        // 30
[| 10; 20; 30 |] |> Array.secondLast  // 20

Negative indexing (Python-style)

Use negative indices where -1 is the last item, -2 the second last, and so on:

let arr = [| "a"; "b"; "c"; "d"; "e" |]

arr.GetNeg(-1)    // "e" (last)
arr.GetNeg(-2)    // "d" (second last)
arr.SetNeg(-1, "z")

// Module functions
arr |> Array.getNeg -1   // "e"
arr |> Array.setNeg -2 "x"

Looped indexing

Treats the array as circular, wrapping around in both directions:

let arr = [| "a"; "b"; "c" |]

arr.GetLooped(3)    // "a" (wraps around)
arr.GetLooped(4)    // "b"
arr.GetLooped(-1)   // "c" (wraps backward)
arr.GetLooped(-4)   // "c"

Slicing with negative indices

Unlike the built-in a.[1..3] slice syntax, Slice supports negative indices:

let arr = [| 0; 1; 2; 3; 4; 5; 6; 7; 8; 9 |]

arr.Slice(2, 5)     // [| 2; 3; 4; 5 |]     (inclusive on both ends)
arr.Slice(0, -1)    // [| 0; 1; .. ; 9 |]   (full copy, -1 = last)
arr.Slice(1, -2)    // [| 1; 2; .. ; 8 |]   (skip first and last)
arr.Slice(-3, -1)   // [| 7; 8; 9 |]        (last 3 items)

// Module function
arr |> Array.slice 1 -2    // [| 1; 2; .. ; 8 |]

Trimming

Remove items from the start and end:

let arr = [| 0; 1; 2; 3; 4; 5 |]

arr |> Array.trim 1 1   // [| 1; 2; 3; 4 |]  (trim 1 from each end)
arr |> Array.trim 2 0   // [| 2; 3; 4; 5 |]  (trim 2 from start)

Rotating

Shift elements circularly:

let arr = [| 1; 2; 3; 4; 5 |]

arr |> Array.rotate 1    // [| 2; 3; 4; 5; 1 |]  (rotate up by 1)
arr |> Array.rotate -1   // [| 5; 1; 2; 3; 4 |]  (rotate down by 1)
arr |> Array.rotate 2    // [| 3; 4; 5; 1; 2 |]  (rotate up by 2)

// Rotate until a condition is met
[| 3; 1; 4; 1; 5 |] |> Array.rotateUpTill (fun x -> x = 5)
// [| 5; 3; 1; 4; 1 |]

Windowed pairs and triples

Iterate over consecutive elements:

let arr = [| "a"; "b"; "c"; "d" |]

// Consecutive pairs (not looped, result is 1 shorter)
arr |> Array.windowed2
// seq { ("a","b"); ("b","c"); ("c","d") }

// Looped pairs (includes wrap-around, same length as input)
arr |> Array.thisNext
// seq { ("a","b"); ("b","c"); ("c","d"); ("d","a") }

arr |> Array.prevThis
// seq { ("d","a"); ("a","b"); ("b","c"); ("c","d") }

// Consecutive triples (not looped, result is 2 shorter)
arr |> Array.windowed3
// seq { ("a","b","c"); ("b","c","d") }

// Looped triples (same length as input)
arr |> Array.prevThisNext
// seq { ("d","a","b"); ("a","b","c"); ("b","c","d"); ("c","d","a") }

With indices:

let arr = [| 10; 20; 30; 40 |]

arr |> Array.windowed2i
// seq { (0, 10, 20); (1, 20, 30); (2, 30, 40) }

arr |> Array.iThisNext
// seq { (0, 10, 20); (1, 20, 30); (2, 30, 40); (3, 40, 10) }

Min and max (top 2 and top 3)

Find the smallest or largest elements:

let arr = [| 5; 1; 9; 3; 7 |]

Array.min2 arr          // (1, 3)     smallest and second smallest
Array.max2 arr          // (9, 7)     biggest and second biggest
Array.min3 arr          // (1, 3, 5)
Array.max3 arr          // (9, 7, 5)

// By projection
let words = [| "hi"; "hello"; "hey" |]
Array.min2By String.length words   // ("hi", "hey")
Array.max2By String.length words   // ("hello", "hey")

// Get indices instead of values
Array.min2IndicesBy String.length words   // (0, 2)
Array.max2IndicesBy String.length words   // (1, 2)

Finding duplicates

Array.duplicates [| 1; 2; 3; 2; 4; 3 |]
// [| 2; 3 |]    (each duplicate reported once, ordered by first occurrence)

Array.duplicatesBy String.length [| "hi"; "hey"; "go"; "bye" |]
// [| "hi"; "hey" |]    (length 2 and length 3 both have duplicates)

Searching

let arr = [| 10; 20; 30; 40; 50 |]

Array.findValue 30 0 4 arr       // 2     (found at index 2)
Array.findValue 99 0 4 arr       // -1    (not found)
Array.findLastValue 30 0 4 arr   // 2     (search from end)

// Search for sub-array pattern
let hay = [| 1; 2; 3; 4; 5; 3; 4 |]
Array.findArray [| 3; 4 |] 0 6 hay       // 2   (first match)
Array.findLastArray [| 3; 4 |] 0 6 hay   // 5   (last match)

Array checks

let arr = [| 1; 2; 3 |]

arr.IsEmpty         // false
arr.IsNotEmpty      // true
arr.HasItems        // true   (same as IsNotEmpty)
arr.IsSingleton     // false

// Module functions
arr |> Array.isNotEmpty         // true
arr |> Array.isSingleton        // false
arr |> Array.hasItems 3         // true   (exactly 3 items)
arr |> Array.hasMinimumItems 2  // true   (at least 2 items)
arr |> Array.hasMaximumItems 5  // true   (at most 5 items)
arr |> Array.count              // 3
arr |> Array.countIf (fun x -> x > 1)  // 2

Validation

Chainable validation methods for defensive programming:

let result =
    someArray
    |> Array.failIfEmpty "Input array must not be empty"
    |> Array.failIfLessThan 3 "Need at least 3 elements"
    |> Array.map doSomething

// Extension member versions
someArray
    .FailIfEmpty("Input must not be empty")
    .FailIfLessThan(3, "Need at least 3 elements")

Swapping

let arr = [| "a"; "b"; "c"; "d" |]
Array.swap 0 3 arr
// arr is now [| "d"; "b"; "c"; "a" |]

String representation

let arr = [| 1; 2; 3; 4; 5; 6; 7 |]

arr.asString
// "array<Int32> with 7 items:
//   0: 1
//   1: 2
//   2: 3
//   3: 4
//   4: 5
//   ..."

arr.ToString(3)   // show only first 3 entries
// "array<Int32> with 7 items:
//   0: 1
//   1: 2
//   2: 3
//   ..."

Use of AI and LLMs

All core function are are written by hand to ensure performance and correctness.
However, AI tools have been used for code review, typo and grammar checking in documentation
and to generate not all but many of the tests.

Tests

All Tests run in both javascript and dotnet. Successful Fable compilation to typescript is verified too. Go to the tests folder:

cd Tests

For testing with .NET using Expecto:

dotnet run

for JS testing with Fable.Mocha and TS verification:

npm test

License

MIT

Changelog

see CHANGELOG.md

namespace ArrayT
val xs: int array
member array.Get: index: int -> 'T
member array.Set: index: int -> value: 'T -> unit
property array.DebugIdx: UtilArray.DebugIndexer<'T> with get
<summary> Use for Debugging index get/set operations. Just replace 'myArray.[3]' with 'myArray.DebugIdx.[3]' Throws a nice descriptive Exception if the index is out of range including the bad index and the array content. </summary>
Multiple items
val single: value: 'T -> single (requires member op_Explicit)

--------------------
type single = System.Single

--------------------
type single<'Measure> = float32<'Measure>
Multiple items
module Array from ArrayT
<summary> The main module for functions on Array&lt;'T&gt;. This module provides additional functions to ones from FSharp.Core.Array module. </summary>

--------------------
module Array from Microsoft.FSharp.Collections
val first: arr: 'T array -> 'T
<summary>Gets the first item in the Array. Same as this.[0]</summary>
<param name="arr">The input Array.</param>
<returns>The first item.</returns>
val last: array: 'T array -> 'T
val secondLast: arr: 'T array -> 'T
<summary>Gets the second last item in the Array. Same as this.[this.Length - 2]</summary>
<param name="arr">The input Array.</param>
<returns>The second last item.</returns>
val getNeg: index: int -> arr: 'T array -> 'T
<summary>Gets an item in the Array by index. Allows for negative index too ( -1 is last item, like Python) (a negative index can also be done with '^' prefix. E.g. ^0 for the last item)</summary>
<param name="index">The index to access (can be negative).</param>
<param name="arr">The input Array.</param>
<returns>The value at the specified index.</returns>
val setNeg: index: int -> value: 'T -> arr: 'T array -> unit
<summary>Sets an item in the Array by index. Allows for negative index too ( -1 is last item, like Python) (a negative index can also be done with '^' prefix. E.g. ^0 for the last item)</summary>
<param name="index">The index to set (can be negative).</param>
<param name="value">The value to set.</param>
<param name="arr">The input Array.</param>
val slice: startIdx: int -> endIdx: int -> arr: 'T array -> 'T array
<summary>Slice the Array given start and end index. Allows for negative indices too. ( -1 is last item, like Python) The resulting Array includes the end index. Raises an IndexOutOfRangeException if indices are out of range. If you don't want an exception to be raised for index overflow or overlap use Array.trim. (A negative index can also be done with '^' prefix. E.g. ^0 for the last item, when F# Language preview features are enabled.)</summary>
<param name="startIdx">The start index (inclusive, can be negative).</param>
<param name="endIdx">The end index (inclusive, can be negative).</param>
<param name="arr">The input Array.</param>
<returns>A new array containing the sliced elements.</returns>
val trim: fromStartCount: int -> fromEndCount: int -> arr: 'T array -> 'T array
<summary>Trim items from start and end. If the sum of fromStartCount and fromEndCount is bigger than arr.Length it returns an empty Array. If you want an exception to be raised for index overlap (total trimming is bigger than count) use Array.slice with negative end index.</summary>
<param name="fromStartCount">The number of items to remove from the start.</param>
<param name="fromEndCount">The number of items to remove from the end.</param>
<param name="arr">The input Array.</param>
<returns>A new trimmed array.</returns>
val rotate: amount: int -> arr: 'T array -> 'T array
<summary>Considers array circular and move elements up for positive integers or down for negative integers. e.g.: rotate +1 [ a, b, c, d] = [ d, a, b, c] e.g.: rotate -1 [ a, b, c, d] = [ b, c, d, a] the amount can even be bigger than the array's size. I will just rotate more than one loop.</summary>
<param name="amount">How many elements to shift forward. Or backward if number is negative</param>
<param name="arr">The input Array.</param>
<returns>The new result Array.</returns>
val rotateUpTill: condition: ('T -> bool) -> arr: 'T array -> 'T array
<summary>Considers array circular and move elements up till condition is met for the first item. The algorithm takes elements from the end and put them at the start till the first element in the array meets the condition. If the first element in the input meets the condition no changes are made. But still a shallow copy is returned.</summary>
<param name="condition">The condition to meet.</param>
<param name="arr">The input Array.</param>
<returns>The new result Array.</returns>
val windowed2: arr: 'T array -> ('T * 'T) seq
<summary>Yields Seq from (first, second) up to (second-last, last). Not looped. The resulting seq is one element shorter than the input Array.</summary>
<param name="arr">The input Array.</param>
<returns>A sequence of consecutive pairs.</returns>
val thisNext: arr: 'T array -> ('T * 'T) seq
<summary>Yields looped Seq from (first, second) up to (last, first). The resulting seq has the same element count as the input Array.</summary>
<param name="arr">The input Array.</param>
<returns>A sequence of consecutive pairs (looped).</returns>
val prevThis: arr: 'T array -> ('T * 'T) seq
<summary>Yields looped Seq from (last,first) up to (second-last, last). The resulting seq has the same element count as the input Array.</summary>
<param name="arr">The input Array.</param>
<returns>A sequence of consecutive pairs (looped).</returns>
val windowed3: arr: 'T array -> ('T * 'T * 'T) seq
<summary>Yields Seq from (first, second, third) up to (third-last, second-last, last). Not looped. The resulting seq is two elements shorter than the input Array.</summary>
<param name="arr">The input Array.</param>
<returns>A sequence of consecutive triples.</returns>
val prevThisNext: arr: 'T array -> ('T * 'T * 'T) seq
<summary>Yields looped Seq of from (last, first, second) up to (second-last, last, first). The resulting seq has the same element count as the input Array.</summary>
<param name="arr">The input Array.</param>
<returns>A sequence of consecutive triples (looped).</returns>
val windowed2i: arr: 'T array -> (int * 'T * 'T) seq
<summary>Yields Seq from (0,first, second) up to (lastIndex-1 , second-last, last). Not looped. The resulting seq is one element shorter than the input Array.</summary>
<param name="arr">The input Array.</param>
<returns>A sequence of consecutive indexed pairs.</returns>
val iThisNext: arr: 'T array -> (int * 'T * 'T) seq
<summary>Yields looped Seq from (0,first, second) up to (lastIndex, last, first). The resulting seq has the same element count as the input Array.</summary>
<param name="arr">The input Array.</param>
<returns>A sequence of indexed consecutive pairs (looped).</returns>
val min2: arr: 'T array -> 'T * 'T (requires comparison)
<summary>Returns the smallest and the second smallest element of the Array. If they are equal then the order is kept</summary>
<param name="arr">The input Array.</param>
<returns>A tuple of the smallest and second smallest elements.</returns>
val max2: arr: 'T array -> 'T * 'T (requires comparison)
<summary>Returns the biggest and the second biggest element of the Array. If they are equal then the order is kept</summary>
<param name="arr">The input Array.</param>
<returns>A tuple of the biggest and second biggest elements.</returns>
val min3: arr: 'T array -> 'T * 'T * 'T (requires comparison)
<summary>Returns the smallest three elements of the Array. The first element is the smallest, the second is the second smallest and the third is the third smallest. If they are equal then the order is kept</summary>
<param name="arr">The input Array.</param>
<returns>A tuple of the three smallest elements.</returns>
val max3: arr: 'T array -> 'T * 'T * 'T (requires comparison)
<summary>Returns the biggest three elements of the Array. The first element is the biggest, the second is the second biggest and the third is the third biggest. If they are equal then the order is kept</summary>
<param name="arr">The input Array.</param>
<returns>A tuple of the three biggest elements.</returns>
val min2By: f: ('T -> 'Key) -> arr: 'T array -> 'T * 'T (requires comparison)
<summary>Returns the smallest and the second smallest element of the Array. Elements are compared by applying the predicate function first. If they are equal after function is applied then the order is kept</summary>
<param name="f">The function to transform elements for comparison.</param>
<param name="arr">The input Array.</param>
<returns>A tuple of the smallest and second smallest elements.</returns>
module String from Microsoft.FSharp.Core
val length: str: string -> int
val max2By: f: ('T -> 'Key) -> arr: 'T array -> 'T * 'T (requires comparison)
<summary>Returns the biggest and the second biggest element of the Array. Elements are compared by applying the predicate function first. If they are equal after function is applied then the order is kept</summary>
<param name="f">The function to transform elements for comparison.</param>
<param name="arr">The input Array.</param>
<returns>A tuple of the biggest and second biggest elements.</returns>
val min2IndicesBy: f: ('T -> 'Key) -> arr: 'T array -> int * int (requires comparison)
<summary>Returns the indices of the smallest and the second smallest element of the Array. Elements are compared by applying the predicate function first. If they are equal after function is applied then the order is kept</summary>
<param name="f">The function to transform elements for comparison.</param>
<param name="arr">The input Array.</param>
<returns>A tuple of the indices of the smallest and second smallest elements.</returns>
val max2IndicesBy: f: ('T -> 'Key) -> arr: 'T array -> int * int (requires comparison)
<summary>Returns the indices of the biggest and the second biggest element of the Array. Elements are compared by applying the predicate function first. If they are equal after function is applied then the order is kept</summary>
<param name="f">The function to transform elements for comparison.</param>
<param name="arr">The input Array.</param>
<returns>A tuple of the indices of the biggest and second biggest elements.</returns>
val duplicates: arr: 'T array -> 'T array
<summary>Returns all elements that exists more than once in Array. Each element that exists more than once is only returned once. Returned order is by first occurrence of first duplicate.</summary>
<param name="arr">The input Array.</param>
<returns>An array of duplicate elements.</returns>
val duplicatesBy: f: ('T -> 'U) -> arr: 'T array -> 'T array
<summary>Returns all elements that exists more than once in Array. Each element that exists more than once is only returned once. Returned order is by first occurrence of first duplicate.</summary>
<param name="f">The function to extract comparison value from each element.</param>
<param name="arr">The input Array.</param>
<returns>An array of duplicate elements.</returns>
val findValue: searchFor: 'T -> fromIdx: int -> tillIdx: int -> searchIn: 'T array -> int (requires equality)
<summary>Find first index where searchFor occurs in searchIn array. Give lower and upper bound index for search space. Returns -1 if not found</summary>
<param name="searchFor">The value to search for.</param>
<param name="fromIdx">The starting index for the search.</param>
<param name="tillIdx">The ending index for the search.</param>
<param name="searchIn">The array to search in.</param>
<returns>The index of the first occurrence, or -1 if not found.</returns>
val findLastValue: searchFor: 'T -> fromIdx: int -> tillIdx: int -> searchIn: 'T array -> int (requires equality)
<summary>Find last index where searchFor occurs in searchIn array. Searching from end. Give lower and upper bound index for search space. Returns -1 if not found</summary>
<param name="searchFor">The value to search for.</param>
<param name="fromIdx">The starting index for the search.</param>
<param name="tillIdx">The ending index for the search.</param>
<param name="searchIn">The array to search in.</param>
<returns>The index of the last occurrence, or -1 if not found.</returns>
val findArray: searchFor: 'T array -> fromIdx: int -> tillIdx: int -> searchIn: 'T array -> int (requires equality)
<summary>Find first index where searchFor array occurs in searchIn array. Give lower and upper bound index for search space. Returns index of first element or -1 if not found</summary>
<param name="searchFor">The array pattern to search for.</param>
<param name="fromIdx">The starting index for the search.</param>
<param name="tillIdx">The ending index for the search.</param>
<param name="searchIn">The array to search in.</param>
<returns>The index of the first occurrence, or -1 if not found.</returns>
val findLastArray: searchFor: 'T array -> fromIdx: int -> tillIdx: int -> searchIn: 'T array -> int (requires equality)
<summary>Find last index where searchFor array occurs in searchIn array. Searching from end. Give lower and upper bound index for search space. Returns index of first element or -1 if not found</summary>
<param name="searchFor">The array pattern to search for.</param>
<param name="fromIdx">The starting index for the search.</param>
<param name="tillIdx">The ending index for the search.</param>
<param name="searchIn">The array to search in.</param>
<returns>The index of the last occurrence, or -1 if not found.</returns>
val isNotEmpty: arr: 'T array -> bool
<summary>Returns true if the given Array is not empty.</summary>
<param name="arr">The input Array.</param>
<returns>True if the Array has at least one item.</returns>
val isSingleton: arr: 'T array -> bool
<summary>Returns true if the given Array has just one item. Same as Array.hasOne</summary>
<param name="arr">The input Array.</param>
<returns>True if the Array has exactly one item.</returns>
val hasItems: count: int -> arr: 'T array -> bool
<summary>Returns true if the given Array has count items.</summary>
<param name="count">The exact count to check for.</param>
<param name="arr">The input Array.</param>
<returns>True if the Array has exactly the specified count.</returns>
val hasMinimumItems: count: int -> arr: 'T array -> bool
<summary>Returns true if the given Array has equal or more than count items.</summary>
<param name="count">The minimum count required.</param>
<param name="arr">The input Array.</param>
<returns>True if the Array has at least the specified count.</returns>
val hasMaximumItems: count: int -> arr: 'T array -> bool
<summary>Returns true if the given Array has equal or less than count items.</summary>
<param name="count">The maximum count allowed.</param>
<param name="arr">The input Array.</param>
<returns>True if the Array has at most the specified count.</returns>
val count: arr: 'T array -> int
<summary>Return the length or count of the collection. Same as Array.length</summary>
<param name="arr">The input Array.</param>
<returns>The number of items in the Array.</returns>
val countIf: predicate: ('T -> bool) -> arr: 'T array -> int
<summary>Counts for how many items of the collection the predicate returns true. Same as Array.filter and then Array.length</summary>
<param name="predicate">The function to test each element.</param>
<param name="arr">The input Array.</param>
<returns>The count of items for which the predicate returns true.</returns>
val failIfEmpty: errorMessage: string -> arr: 'T array -> 'T array
<summary>Raises an Exception if the Array is empty. (Useful for chaining) Returns the input Array</summary>
<param name="errorMessage">The error message to include in the exception.</param>
<param name="arr">The input Array.</param>
<returns>The input Array if not empty.</returns>
val failIfLessThan: count: int -> errorMessage: string -> arr: 'T array -> 'T array
<summary>Raises an Exception if the Array has less then count items. (Useful for chaining) Returns the input Array</summary>
<param name="count">The minimum count required.</param>
<param name="errorMessage">The error message to include in the exception.</param>
<param name="arr">The input Array.</param>
<returns>The input Array if it has enough items.</returns>
val map: mapping: ('T -> 'U) -> array: 'T array -> 'U array
val swap: i: int -> j: int -> arr: 'T array -> unit
<summary>Swap the values of two given indices in Array</summary>
<param name="i">The first index to swap.</param>
<param name="j">The second index to swap.</param>
<param name="arr">The input Array.</param>

Type something to start searching.