RG.System.Collections.Generic.One 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package RG.System.Collections.Generic.One --version 1.0.2
NuGet\Install-Package RG.System.Collections.Generic.One -Version 1.0.2
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="RG.System.Collections.Generic.One" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RG.System.Collections.Generic.One --version 1.0.2
#r "nuget: RG.System.Collections.Generic.One, 1.0.2"
#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 RG.System.Collections.Generic.One as a Cake Addin
#addin nuget:?package=RG.System.Collections.Generic.One&version=1.0.2

// Install RG.System.Collections.Generic.One as a Cake Tool
#tool nuget:?package=RG.System.Collections.Generic.One&version=1.0.2

Release Note

Version 1.0.2

  1. Moved LINQ methods out of One<T> into a new class Qlosure<T>.
  2. Renamed One.Of() to One.Value()
  3. Removed object extension method obj.ToOne()
  4. Removed IEnumerable extension method obj.AsOne()
  5. Removed Join clause

What is One<T>?

One<T> is a generic collection that contains exactly one item. This kind of generic collection allows you to use LINQ expression to write closures and functional programming.

Installation

Using Package Manager Console

Install-Package RG.System.Collections.Generic.One

Using dotnet CLI

dotnet add package RG.System.Collections.Generic.One

Usage

Include using directive

using System.Collections.Generic;

Create instance of One<T>

// using constructor
var oneInt = new One<int>(1);

// using factory
var oneDecimal = One.Value(100.0m);

Use it in LINQ expression as a closure

decimal total = from price in One.Value(199m)
                let quantity = 2m
                let subtotal = price * quantity
                let tax = subtotal * 0.1m
                let discount = 10m
                select subtotal + tax - discount;

Qlosure: Closures Written in LINQ

You can write closures in LINQ by enumerating from a One<T> or a Qlosure<T>.

Start a qlosure

from x in One.Value(e).ToQlosure() // ToQlosure is optional

Introduce a new range variable

from x in One.Value(e)
let y = 10

Introduce a new range variable from another One<T>

from x in One.Value(e)
from y in one // one is a One<int>, z is an Int32

Implicitly cast qlosure to its final value

int result = from x in One.Value(e)
             let y = x * x
             select y;

Auto Disposing Range Variables

All disposable range variables are automatically disposed before stepping into next statement.

string ComputeSecretProof(string accessToken) {
    string secretProof = from appSecret in One.Value(Configuration["AppSecret"])
                         let appSecretBytes = Encoding.UTF8.GetBytes(appSecret)
                         let accessTokenBytes = Encoding.UTF8.GetBytes(accessToken)
                         let hmacssha256 = new HMACSHA256(appSecretBytes)
                         let proofBytes = hmacsha256.ComputeHash(accessTokenBytes)
                         select string.Join("", proofBytes.Select(b => b.ToString("x2")));
    // hmacsha256 already disposed at this point
    return secretProof;
}
                     

Disabling Auto Dispose

string json = from client in One.Value(httpClient).DoNotDisposeValue()
              let uri = new Uri(uriString)
              select client.GetString(uri);
// httpClient is not disposed and can still be reused

Planned Features

  1. Full Support for .Net Core 3.0 Nullability
  2. Task Chaining
Person person = await from response in httpClient.GetAsync(uri)
                      from json in response.Content.ReadAsStringAsync()
                      select JsonConvert<Person>(json);

Please submit an issue if you need to report bugs or request a feature.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on RG.System.Collections.Generic.One:

Package Downloads
RG.Ninja

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 3,916 4/2/2021
1.0.4 471 3/29/2021
1.0.3 387 3/9/2021
1.0.2 547 8/19/2019
1.0.1 496 8/18/2019
1.0.0 504 8/18/2019

Version 1.0.2
1. Moved LINQ methods out of One<T> into a new class Qlosure<T>.
2. Renamed One.Of() to One.Value()
3. Removed object extension method obj.ToOne()
4. Removed IEnumerable extension method obj.AsOne()
5. Removed Join clause