Skip to content

Instantly share code, notes, and snippets.

@arialdomartini
Last active October 12, 2025 20:33
Show Gist options
  • Select an option

  • Save arialdomartini/c25038c65a44fb102dd78067eb36f1fc to your computer and use it in GitHub Desktop.

Select an option

Save arialdomartini/c25038c65a44fb102dd78067eb36f1fc to your computer and use it in GitHub Desktop.
A pure functional array without mutation
module FSharpBits.LambdaArray
open Xunit
open Swensen.Unquote
let newArray =
fun i -> None
let setValue index value array =
fun i ->
if i = index
then value
else array i
let set index value array =
array |> setValue (Some value)
let unset index array =
array |> setValue None
[<Fact>]
let ``an array without mutation`` () =
let array =
newArray
|> set 1 21
|> set 2 100
|> set 3 3
|> set 1 42
|> set 3 120
|> unset 2
|> set 1000 1000
test <@ array 1 = Some 42 @>
test <@ array 2 = None @>
test <@ array 3 = Some 120 @>
test <@ array 1000 = Some 1000 @>
test <@ array 999 = None @>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment