VOOZH about

URL: https://www.nuget.org/packages/PowMaybe/

⇱ NuGet Gallery | PowMaybe 0.2.0




PowMaybe 0.2.0

dotnet add package PowMaybe --version 0.2.0
 
 
NuGet\Install-Package PowMaybe -Version 0.2.0
 
 
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="PowMaybe" Version="0.2.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PowMaybe" Version="0.2.0" />
 
Directory.Packages.props
<PackageReference Include="PowMaybe" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PowMaybe --version 0.2.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: PowMaybe, 0.2.0"
 
 
#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.
#:package PowMaybe@0.2.0
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PowMaybe&version=0.2.0
 
Install as a Cake Addin
#tool nuget:?package=PowMaybe&version=0.2.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

PowMaybe

Table of content

Introduction

Lightweight Maybe monad library to simplify code that can fail.

It will help you transform code like this:

Before

Person? ParsePage(string url)
{
 var html = client.Query(url);
 if (html == null)
 return null;
 var root = Html.GetRoot(html);
 var node = root.SelectSingleNode("xpath query");
 if (node == null)
 return null;
 var personInfo = Utils.ParseNode(node);
 if (personInfo == null)
 return null;
 return new Person(personInfo);
}

Into this:

After

Maybe<Person> ParsePage(string url) =>
 from html in Utils.Query(url)
 let root = Utils.GetRoot(html)
 from node in root.MaySelectSingleNode("xpath query")
 from personInfo in Utils.MayParseNode(node)
 select new Person(personInfo);

The result is:

  • shorter code
  • less nested
  • no if conditions
  • no nullable references

It will not apply for all the code everywhere, but when it does apply it will drastically reduce the potential for bugs.

Usage

Creation

var a = May.Some(47);
var b = May.None<string>();
// nullable reference -> Maybe<>
var mayPerson = person.ToMaybe();
// Maybe<> -> nullable reference
var person = mayPerson.ToNullable();

Combining

Maybe<string> QueryHtml(string url);
Maybe<Person> ParsePerson(string html);

// use any number of from/in statements with a select at the end
Maybe<Person> QueryAndParse(string url) =>
 from html in QueryHtml(url)
 from person in ParsePerson(html)
 where person.Name != "John" // you can also use where statements
 select parson;

Unwrapping

Maybe<Person> mayPerson = ...

if (mayPerson.IsSome(out var person))
{
 // Success, you can access person here
}
else
{
 // Failure
}

Person person = mayPerson.Ensure(); // throws an Exception if mayPerson is None

Person person = mayPerson.FailWith(peter); // returns peter if mayPerson is None

Enumerations

IEnumerable<T> WhereSome<T>(this IEnumerable<Maybe<T>> source);
Maybe<T> FirstOrMaybe<T>(this IEnumerable<T> source, Func<T, bool>? predicate = null)
// and similar LastOrMaybe

// Examples
// ========
new [] { May.Some(4), May.None<int>() May.Some(12) }.WhereSome();
// int[] { 4, 12 }

new [] { 2, 6, 5 }.FirstOrMaybe(e => e % 3 == 0)
// Some(6)

new [] { 2, 6, 5 }.FirstOrMaybe(e => e % 3 == 1)
// None<int>()

Example

Let's say you want to read your configuration from multiple sources:

  • environment variables
  • command line arguments
  • json file

And once a source returns a configuration, you do not want to read the other sources.

You could write it this way:

Maybe<Config> ReadFromEnvVars();
Maybe<Config> ReadFromArgs(string[] args);
Maybe<Config> ReadFromFile(string file);

Maybe<Config> ReadConfig(string[] args, string file) =>
	new[]
	{
		() => ReadFromEnvVars(),
		() => ReadFromArgs(args),
		() => ReadFromFile(file),
	}
	.Select(readFun => readFun())
	.WhereSome()
	.FirstOrMaybe();

License

MIT

Product Versions Compatible and additional computed target framework versions.
.NET net6.0 net6.0 is compatible.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 was computed.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 is compatible.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on PowMaybe:

Package Downloads
PowRxVar.Maybe

Composable reactive variables

PowRxVar.WinForms

Composable reactive variables

PowWeb

Puppeteer and Chrome DOMSnapshot API wrapper

ImdbLib

IMDB scraping

ParserLib

Parsing utilities

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 271 1/17/2025
0.1.0 223 6/30/2024
0.0.12 404 8/9/2023
0.0.11 795 6/12/2023
0.0.10 428 6/5/2023
0.0.9 697 5/28/2023
0.0.7 338 4/30/2023
0.0.6 757 11/26/2022
0.0.5 505 11/26/2022
0.0.4 762 11/15/2022
0.0.2 594 8/13/2022
0.0.1 506 8/13/2022