Kotlin.ScopeFunctions
0.5.0
See the version list below for details.
dotnet add package Kotlin.ScopeFunctions --version 0.5.0
NuGet\Install-Package Kotlin.ScopeFunctions -Version 0.5.0
<PackageReference Include="Kotlin.ScopeFunctions" Version="0.5.0" />
<PackageVersion Include="Kotlin.ScopeFunctions" Version="0.5.0" />
<PackageReference Include="Kotlin.ScopeFunctions" />
paket add Kotlin.ScopeFunctions --version 0.5.0
#r "nuget: Kotlin.ScopeFunctions, 0.5.0"
#:package Kotlin.ScopeFunctions@0.5.0
#addin nuget:?package=Kotlin.ScopeFunctions&version=0.5.0
#tool nuget:?package=Kotlin.ScopeFunctions&version=0.5.0
Kotlin.ScopeFunctions
Use Kotlin's Scope Functions in .NET
Like LINQ for single values, Scope Functions are a powerful way to write code that is more declarative and reads like the flow of information through your program.
Installation
The package is available from nuget.org as Kotlin.ScopeFunctions
Or, you can run install-package Kotlin.ScopeFunctions from within your IDE
Getting started
Most .NET developers are very comfortable with using chains of LINQ extension methods to process data in a pipeline that reads fluently from left to right, describing how data maps from source to result through a series of transformation steps.
Typically this is easier to read than the same thing written as nested method calls, since developers are usually happier to read a flow from left to right than to unpick a nested set of method calls.
So, what if you could do the same thing with single values as you can with enumerables?
That's where Kotlin's scope functions come in. They allow you to use an extension method that takes the source object as the argument to a supplied delegate, just like LINQ functions allow you to use an extension method that takes the elements of the source sequence as arguments to a supplied delegate.
This simple library re-implements Kotlin Scope Functions so that you can use the same pattern to improve your C# code.
They come in three flavours:
- Firstly
Let()which takes the source object and returns the result of calling the delegate. This is like a scalar version ofEnumerable.Select()and is useful for fluently mapping an object through a transformation, or a series of transformations. - Secondly
Also()which takes the source object and calls the supplied delegate on the object, but returs the original object. This is like a scalar version ofList<>.ForEach(). This is useful for applying an action to an object (perhaps part of its initialisation) without needing to declare a local variable specifically for the purpose. - Finally,
TakeIf()andTakeUnless()which apply a predicate to the source object, returning either the object ordefault(T), depending on ther result and the sense ofIf/Unless. This is like a scalar version ofEnumerable.Where()and is useful alongside the null-conditional operator?.to allow a subsequent method call to execute only if a condition is met.
LetAsync() and AlsoAsync are also available for async use cases
| Product | Versions 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 is compatible. 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 is compatible. 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. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.