Last active
October 12, 2025 20:33
-
-
Save arialdomartini/c25038c65a44fb102dd78067eb36f1fc to your computer and use it in GitHub Desktop.
A pure functional array without mutation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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