ResizeArrayT 0.29.0

dotnet add package ResizeArrayT --version 0.29.0
                    
NuGet\Install-Package ResizeArrayT -Version 0.29.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ResizeArrayT" Version="0.29.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ResizeArrayT" Version="0.29.0" />
                    
Directory.Packages.props
<PackageReference Include="ResizeArrayT" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ResizeArrayT --version 0.29.0
                    
#r "nuget: ResizeArrayT, 0.29.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ResizeArrayT@0.29.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ResizeArrayT&version=0.29.0
                    
Install as a Cake Addin
#tool nuget:?package=ResizeArrayT&version=0.29.0
                    
Install as a Cake Tool

Logo

ResizeArrayT

ResizeArrayT on nuget.org Build Status Docs Build Status Test Status license code size

ResizeArrayT is an F# extension and module library for ResizeArray<'T> ( = System.Collections.Generic.List<'T>).
It provides all the functions from the Array module in FSharp.Core for ResizeArray.
And more.

It also works in Javascript and Typescript with Fable.

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

I was always annoyed that an IndexOutOfRangeException does not include the actual bad index nor the actual size of the array.
This library fixes that in the resizeArray.Get, resizeArray.Set, resizeArray.SliceIdx and similar instance methods for item access.
I made a similar library for array<'T>: https://github.com/goswinr/ArrayT/ .

Why ?

Yes, F#'s array and list modules can do these kind of operations on collections too.
But ResizeArray, being mutable, still offers the best performance for collections that can expand & shrink and need random access via an index.
In fact FSharp.Core uses a very similar module internally.

It Includes:

  • A ResizeArray module that has all functions from Array module from FSharp.Core reimplemented.
    Including a Parallel sub module with all functions from Array.Parallel, like map, choose, reduce, sum, groupBy and sort.

  • A Computational Expressions resizeArray that can be used like existing ones for seq.

  • Support for F# slicing operator and indexing from the end. e.g: items.[ 1 .. ^1].
    (Indexing from the end with ^ still needs <LangVersion>preview</LangVersion> in your project, or dotnet fsi --langversion:preview.)

  • Extension members on ResizeArray like .Get .Set .First .Last .SecondLast and more.
    With nicer IndexOutOfRangeExceptions that include the bad index and the actual size.

  • All Tests from FSharp.Core's Array module ported and adapted to run in both javascript and dotnet.

Namespace

The main namespace is ResizeArrayT.
It was renamed from ResizeArray to ResizeArrayT in release 0.23. When used in scripting this helps avoid name collisions with the
module inside of the same name. Reference Issue

Older versions of the library will still work with the old namespace ResizeArray.
And can be found on nuget.org with the name ResizeArray.

Usage

Just open the namespace

open ResizeArrayT

this namespace contains:

  • a module also called ResizeArray
  • a Computational Expressions called resizeArray
  • this will also auto open the extension members on ResizeArray<'T>

then you can do:

let evenNumbers =
    resizeArray {  // a Computational Expression like seq
        for i = 0 to 99 do
            if i % 2 = 0 then
                i
    }

let oddNumbers = evenNumbers |> ResizeArray.map (fun x -> x + 1) // ResizeArray module

let ninetyNine = oddNumbers.Last // Extension member to access the last item in the list

Computational Expression

The resizeArray { ... } builder supports for, while, yield, yield!, try/with, try/finally, and use:

// Yield individual items and sequences
let mixed =
    resizeArray {
        1
        2
        yield! [3; 4; 5]   // yield from any seq
        for i in 6..10 do
            i
    }

// Filter inside the builder
let primes =
    resizeArray {
        for n = 2 to 50 do
            let mutable isPrime = true
            for d = 2 to int (sqrt (float n)) do
                if n % d = 0 then isPrime <- false
            if isPrime then n
    }

Extension Members

Access items with descriptive error messages that include the bad index and the collection size:

let items = ResizeArray([| "a"; "b"; "c"; "d"; "e" |])

items.First        // "a"
items.Second       // "b"
items.Last         // "e"
items.SecondLast   // "d"
items.LastIndex    // 4

// Negative indexing (Python-style: -1 is last item)
items.GetNeg(-1)   // "e"
items.GetNeg(-2)   // "d"

// Looped indexing (wraps around)
items.GetLooped(7) // "c"  (index 7 wraps to index 2)

// Status checks
items.IsEmpty      // false
items.IsNotEmpty   // true
items.HasItems     // true
items.IsSingleton  // false

Slicing

F# slicing notation is fully supported, including indexing from the end with ^.
Just like for F# arrays, out-of-range indices are clamped when getting a slice.
Indexing from the end still needs <LangVersion>preview</LangVersion> in your project, or dotnet fsi --langversion:preview in scripts.

let nums = ResizeArray([| 10; 20; 30; 40; 50 |])

nums.[1..3]        // ResizeArray [20; 30; 40]
nums.[..2]         // ResizeArray [10; 20; 30]
nums.[2..]         // ResizeArray [30; 40; 50]
nums.[1..^1]       // ResizeArray [20; 30; 40]  (from index 1 to second-last)
nums.[^0]          // 50  (last item)
nums.[3..9]        // ResizeArray [40; 50]  (clamped)

To get an exception for out-of-range indices instead, use SliceNeg or SliceIdx. SliceLooped wraps any index around with modulo. They all include the end index:

nums.SliceNeg(1, -2)       // ResizeArray [20; 30; 40]  (negative indices allowed, -1 is the last item)
nums.SliceIdx(1, 3)        // ResizeArray [20; 30; 40]  (throws on negative indices)
nums.SliceLooped(-2, -1)   // ResizeArray [40; 50]      (any index wraps around with modulo)
nums.SliceLooped(5, 6)     // ResizeArray [10; 20]      (5 wraps to 0, 6 to 1)

// Module functions
nums |> ResizeArray.sliceNeg 1 -2
nums |> ResizeArray.sliceIdx 1 3
nums |> ResizeArray.sliceLooped -2 -1

These work the same in ArrayT and Str.
Don't confuse them with the built-in nums.Slice(start, count) method of .NET, which takes a count instead of an end index. For a start index and a count, you can also use ResizeArray.sub.

Pop, Clone, and InsertAtStart

let xs = ResizeArray([| 1; 2; 3; 4; 5 |])

let last = xs.Pop()            // returns 5, xs is now [1; 2; 3; 4]
let second = xs.Pop(1)         // returns 2, xs is now [1; 3; 4]

xs.InsertAtStart(0)            // xs is now [0; 1; 3; 4]

let copy = xs.Clone()          // shallow copy, same as xs.Copy()

ResizeArray Module

All functions from FSharp.Core's Array module are available, plus many extras:

let items = ResizeArray [ 1 .. 100 ]

// Standard functional operations
let doubled = items |> ResizeArray.map (fun x -> x * 2)
let evens   = items |> ResizeArray.filter (fun x -> x % 2 = 0)
let total   = items |> ResizeArray.sum

// Positional access
let first     = items |> ResizeArray.first
let last      = items |> ResizeArray.last
let secLast   = items |> ResizeArray.secondLast

// Grouping and counting
let grouped  = items |> ResizeArray.groupBy (fun x -> x % 3)
let counts   = items |> ResizeArray.countBy (fun x -> x % 3)

// Finding duplicates
let dupes    = items |> ResizeArray.duplicates
let dupesBy  = items |> ResizeArray.duplicatesBy (fun x -> x % 10)

// Partitioning into multiple groups
let trueOnes, falseOnes = items |> ResizeArray.partition (fun x -> x > 3)

let small, medium, large =
    items |> ResizeArray.partition3
        (fun x -> x < 10)
        (fun x -> x < 50)

// Windowed iteration (useful for geometry/polyline processing)
items |> ResizeArray.windowed2 |> Seq.iter (fun (a, b) -> printfn "%A -> %A" a b)
items |> ResizeArray.prevThisNext |> Seq.iter (fun (prev, this', next) -> printfn "%A %A %A" prev this' next)

Construction and Conversion

let empty  = ResizeArray.empty<int>
let single = ResizeArray.singleton 42
let filled = ResizeArray.create 10 0         // 10 zeros
let inited = ResizeArray.init 5 (fun i -> i * i) // [0; 1; 4; 9; 16]

// From other collections
let fromList  = ResizeArray.ofList [1; 2; 3]
let fromArray = ResizeArray.ofArray [|1; 2; 3|]
let fromSeq   = ResizeArray.ofSeq (seq { 1..10 })

// To other collections
let asArray = items |> ResizeArray.toArray
let asList  = items |> ResizeArray.toList
let asSeq   = items |> ResizeArray.toSeq

Error Messages

When an index is out of range, you get a descriptive exception including the bad index and the collection content:

System.IndexOutOfRangeException:
ResizeArray.Get: Can't get index 5 from:
ResizeArray<String> with 3 items:
  0: a
  1: b
  2: c

Operators

Open the Operators module to combine collections with ++ and +++:

open ResizeArrayT.Operators

let combined = xs ++ ys         // from two ICollection<'T>, preallocates capacity
let combined' = xs +++ ys       // from two seq<'T>

Parallel

A sub module for parallel operations (on .NET only):

let results = items |> ResizeArray.Parallel.map (fun x -> expensiveComputation x)
let chosen  = items |> ResizeArray.Parallel.choose (fun x -> tryProcess x)

Use of AI and LLMs

All core functions 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.

Full API Documentation

goswinr.github.io/ResizeArrayT

Tests

All Tests run in both javascript and dotnet, except the ones for the .NET only Parallel module. Successful Fable compilation to typescript is verified too. Go to the tests folder:

cd Tests

The tests use Scriptorium (Scriptorium.Quill as test runner and Scriptorium.Nib for assertions). The same test suite runs unchanged on .NET and in JS via Fable.

For testing with .NET:

dotnet run

for JS testing (Fable compiles the tests and runs them with Node.js) and TS verification:

npm test

License

MIT

Changelog

see CHANGELOG.md

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.29.0 96 9/27/2026
0.28.0 85 9/25/2026
0.27.0 119 9/8/2026
0.26.1 141 7/16/2026
0.26.0 641 3/13/2026
0.25.0 564 10/11/2025
0.24.0 168 10/11/2025
0.23.0 357 5/24/2025

### Added

- `ResizeArray.mapIfResult` and `mapIfInputAndResult`, the names used in ArrayT.
- `xs.SliceNeg` and `ResizeArray.sliceNeg` slice with an inclusive end index and allow negative indices (-1 is the last item), like in ArrayT and Str.
- `ResizeArray.slice` does the same, but is marked obsolete, because in .NET the `.Slice` method of some collections takes a start index and a length instead.
- `ResizeArray.matches`, `findValue`, `findLastValue`, `findArray` and `findLastArray` from ArrayT.
- `xs.Copy()` for a shallow copy, same as `xs.Clone()`, and named like `arr.Copy()` in ArrayT.
- `ResizeArray.Parallel` has all functions of `Array.Parallel` from FSharp.Core now. New are `average`, `averageBy`, `exists`, `filter`, `forall`, `groupBy`, `max`, `maxBy`, `min`, `minBy`, `partitionWith`, `reduce`, `reduceBy`, `sort`, `sortBy`, `sortByDescending`, `sortDescending`, `sortInPlace`, `sortInPlaceBy`, `sortInPlaceWith`, `sortWith`, `sum`, `sumBy`, `tryFind`, `tryFindIndex`, `tryPick` and `zip`.

### Changed

- `ResizeArray.applyIfResult` and `applyIfInputAndResult` are marked obsolete, use `mapIfResult` and `mapIfInputAndResult` instead.
- `xs.Duplicate()` is marked obsolete, use `xs.Copy()` or `xs.Clone()` instead.
- The exception message of `xs.FirstAndOnly` and `ResizeArray.firstAndOnly` says that exactly one item is expected, like in ArrayT, instead of reporting a bad index.
- `ResizeArray.sliceIdx` and `sliceLooped` call `xs.SliceIdx` and `xs.SliceLooped`, like in ArrayT, so their exception messages name `SliceIdx`.
- `ResizeArray.sliceIdx` and `xs.SliceIdx` fail with a "Can't slice an empty ResizeArray" message on empty input, like ArrayT and Str.
- The docs of the slicing functions match those in ArrayT and Str.

### Fixed

- `xs.ToString(Int32.MaxValue)` printed "..." and the last item twice because of an integer overflow.
- `ResizeArray.failIfEmpty`, `failIfLessThan`, `sliceIdx` and `sliceLooped` raise an `ArgumentNullException` for null input, instead of a `NullReferenceException`.