FRange 1.0.1
See the version list below for details.
dotnet add package FRange --version 1.0.1
NuGet\Install-Package FRange -Version 1.0.1
<PackageReference Include="FRange" Version="1.0.1" />
paket add FRange --version 1.0.1
#r "nuget: FRange, 1.0.1"
// Install FRange as a Cake Addin #addin nuget:?package=FRange&version=1.0.1 // Install FRange as a Cake Tool #tool nuget:?package=FRange&version=1.0.1
FRange
FRange is a .NET library for working with ranges of values. If you have a sortable type (e.g. int
, DateTime
, string
, etc.), you can create ranges of its values, and then compute unions, intersections, etc. of those ranges.
Ranges can be bounded or unbounded on both sides, and bounds can be either inclusive or exclusive. For example x < 3
has an exclusive upper bound and no lower bound, while 0.0 <= x <= 1.0
is bounded inclusively on both sides.
FRange has separate API's for F# and C#.
Usage (F#)
Let's say Alice has the following schedule:
let parse = DateTime.Parse
let aSchedule =
[|
parse "11:30am" +-* parse "1pm"
parse "2:30pm" +-* parse "4:30pm"
|]
The +-*
operator creates a range that has an inclusive lower bound (symbolized by +
) and an exclusive upper bound (symbolized by *
).
Using this same pattern, we can also create a schedule for Bob:
let bSchedule =
[|
parse "10am" +-* parse "11am"
parse "11am" +-* parse "3pm"
|]
If the workday goes from 9am to 5pm, what times during the day are both Alice and Bob free? We can determine this as follows:
let day = parse "9am" +-* parse "5pm"
let free =
Range.difference [day]
(Range.union aSchedule bSchedule)
Range.union
determines the union of two schedules, and then Range.difference
determines what times during the day are not occupied by either schedule. The result is:
[12/1/2021 9:00:00 AM <= x < 12/1/2021 10:00:00 AM;
12/1/2021 4:30:00 PM <= x < 12/1/2021 5:00:00 PM]
So the mutually free times are 9-10am and 4:30-5pm.
Usage (C#)
We can translate the same example to C# using the FRange.CSharp
namespace:
using FRange.CSharp;
using Range = FRange.CSharp.Range; // resolve conflict with System.Range
Use MultiRange
to hold collections of ranges, like this:
var aSchedule =
MultiRange.Create(
new[] {
Range.Create(
DateTime.Parse("11:30am"), BoundType.Inclusive,
DateTime.Parse("1pm"), BoundType.Exclusive),
Range.Create(
DateTime.Parse("2:30pm"), BoundType.Inclusive,
DateTime.Parse("4:30pm"), BoundType.Exclusive)
});
var bSchedule =
MultiRange.Create(
new[] {
Range.Create(
DateTime.Parse("10am"), BoundType.Inclusive,
DateTime.Parse("11am"), BoundType.Exclusive),
Range.Create(
DateTime.Parse("11am"), BoundType.Inclusive,
DateTime.Parse("3pm"), BoundType.Exclusive)
});
Then determine mutually free time like this:
var day =
Range.Create(
DateTime.Parse("9am"), BoundType.Inclusive,
DateTime.Parse("5pm"), BoundType.Exclusive)
.ToMultiRange();
var free =
day.Difference(aSchedule.Union(bSchedule));
Note that we've used ToMultiRange
in this case to create a multi-range that contains a single range. The result is the same as above:
[12/1/2021 9:00:00 AM <= x < 12/1/2021 10:00:00 AM;
12/1/2021 4:30:00 PM <= x < 12/1/2021 5:00:00 PM]
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. 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. |
-
net5.0
- FSharp.Core (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.