Haumohio.Lenses 1.0.4

dotnet add package Haumohio.Lenses --version 1.0.4                
NuGet\Install-Package Haumohio.Lenses -Version 1.0.4                
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="Haumohio.Lenses" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Haumohio.Lenses --version 1.0.4                
#r "nuget: Haumohio.Lenses, 1.0.4"                
#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.
// Install Haumohio.Lenses as a Cake Addin
#addin nuget:?package=Haumohio.Lenses&version=1.0.4

// Install Haumohio.Lenses as a Cake Tool
#tool nuget:?package=Haumohio.Lenses&version=1.0.4                

Haumohio Lenses

Provides the Lens pattern for hierarchical data editing

Quickstart

Define your data that has a level of nesting

type Position = {
  x: int
  y: int
  z: int
}

type Top = {
  name: string
  positions: Position list
}

let ONES : Position = {x=1;y=1;z=1}
let TWOS : Position = {x=2;y=2;z=2}
let THREES : Position = {x=3;y=3;z=3}
let FOURS : Position = {x=4;y=4;z=4}
let data = { name="TEST"; positions = [ONES; TWOS; THREES]}

Define lens(es) that you can use to access the nested data

// A lens that allows me to edit the "Positions" in a "Top"
let PositionLens: Lens<Top, Position list> = {
  get = fun t -> t.positions
  set = fun x t -> {t with positions = x}
}

Use the lens operations to access and update the nested data

// access nested data
data |> PositionLens.get // [ONES; TWOS; THREES]

// overwrite nested data
data |> PositionLens.set [ONES] // { name="TEST"; positions = [ONES]}

// list addition/subtraction
data |> PositionLens @= [FOURS] // { name="TEST"; positions = [ONES; TWOS; THREES; FOURS]}
data |> PositionLens -= [TWOS] // { name="TEST"; positions = [ONES; THREES]}

// list replace an item
data |> lensReplace (fun i -> i.x=2) (fun i -> {i with x=5}) positions // { name="TEST"; positions = [ONES; {x=5; y=2; z=2}; THREES]}
// ... or with an operator
data |> (positions &= {predicate=(fun i -> i.x=2); updater=(fun i -> {i with x=5})}) // { name="TEST"; positions = [ONES; {x=5; y=2; z=2}; THREES]}

Operators

  • @= append to a nested list
  • -= remove from a nested list
  • &= update an element in a nested list
  • >>| chain lenses together to get deeper into the hierarchy

Options

Deal with nested data that is optional (i.e. uses Some x | None )

Operators

  • >?| chain lenses together where the first lens returns an option. Shortcuts if the first operator returns None so that the whole chain returns None immediately, otherwise passes on the first lenses value to the second lens
  • >??| similar to the above, for when BOTH lenses return an option. This operator flattens the two options into a single "Some x or None", rather than an option of an option
  • @?= append to a list that may be optional due to an upstream lens
  • -?= remove from a list that may be optional due to an upstream lens
  • &?= update an element in a list that may be optional due to an upstream lens
  • nth n return the nth value in a list. The returned value is Some x, or None if the index is not found
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
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
1.0.4 129 4/20/2024
1.0.3 108 4/20/2024