public void AndThen() { Option <int> opt1 = new Some <int>(1); Assert.Equal(2, opt1.AndThen(x => new Some <int>(x + 1)).Unwrap()); Assert.Equal(None <int> .Instance, opt1.AndThen(_ => None <int> .Instance)); Option <int> opt2 = None <int> .Instance; Assert.Equal(None <int> .Instance, opt2.AndThen(x => new Some <int>(x + 1))); Assert.Equal(None <int> .Instance, opt2.AndThen(_ => None <int> .Instance)); }
// Modeled after https://github.com/docker/cli/blob/v19.03.9/cli/command/container/stats_helpers.go#L166 Option <double> GetCpuLinux(DockerStats module) { Option <double> currentTotal = module.CpuStats.AndThen(s => s.CpuUsage).AndThen(s => s.TotalUsage); Option <double> previousTotal = module.Name.AndThen(this.previousModuleCpu.GetOption); Option <double> moduleDelta = currentTotal.AndThen(curr => previousTotal.Map(prev => curr - prev)); Option <double> currentSystem = module.CpuStats.AndThen(s => s.SystemCpuUsage); Option <double> previousSystem = module.Name.AndThen(this.previousSystemCpu.GetOption); Option <double> systemDelta = currentSystem.AndThen(curr => previousSystem.Map(prev => curr - prev)); // set previous to new current module.Name.ForEach(name => { currentTotal.ForEach(curr => this.previousModuleCpu[name] = curr); currentSystem.ForEach(curr => this.previousSystemCpu[name] = curr); }); return(moduleDelta.AndThen(moduleDif => systemDelta.AndThen(systemDif => { if (moduleDif >= 0 && systemDif > 0) { double result = 100 * moduleDif / systemDif; // Occasionally on startup results in a very large number (billions of percent). Ignore this point. if (result < 100) { return Option.Some(result); } } return Option.None <double>(); }))); }
public static async Task <Option <TItem> > InspectAsync <TItem>(this Option <TItem> option, Func <TItem, Task> action) where TItem : notnull { await option.AndThen(action); return(option); }
public void MatchSome_GivenNone_DoesNotPerformAction() { bool actionPerformed = false; void someAction(int _) => actionPerformed = true; Option <int> none = OptionType.Option.None <int>(); none.AndThen(someAction); Assert.False(actionPerformed); }
public void MatchSome_GivenSome_PerformsAction() { bool actionPerformed = false; void someAction(int _) => actionPerformed = true; Option <int> some = 2; some.AndThen(someAction); Assert.True(actionPerformed); }
private ProcessStartInfo CreateProcessStartInfo() { var startInfo = new ProcessStartInfo(_program); _arguments.ForEach(startInfo.ArgumentList.Add); _workingDirectory.AndThen(directory => startInfo.WorkingDirectory = directory); startInfo.RedirectStandardOutput = _redirectStandardOutput.GetOrElse(false); startInfo.RedirectStandardError = _redirectStandardError.GetOrElse(false); // The default is true on .NET Framework apps and false on .NET Core apps. startInfo.UseShellExecute = false; return(startInfo); }
// Modeled after https://github.com/docker/cli/blob/v19.03.9/cli/command/container/stats_helpers.go#L185 Option <double> GetCpuWindows(DockerStats module) { Option <DateTime> previousRead = module.Name.AndThen(this.previousReadTime.GetOption); Option <TimeSpan> timeBetweenReadings = module.Read.AndThen(read => previousRead.Map(preRead => read - preRead)); // Get 100ns intervals Option <long> intervalsPerCpu = timeBetweenReadings.Map(tbr => (long)tbr.TotalMilliseconds * 10000); Option <long> possibleIntervals = intervalsPerCpu.AndThen(cpuInt => module.NumProcesses.Map(numProc => cpuInt * numProc)); Option <double> currentTotal = module.CpuStats.AndThen(s => s.CpuUsage).AndThen(s => s.TotalUsage); Option <double> previousTotal = module.Name.AndThen(this.previousModuleCpu.GetOption); Option <double> intervalsUsed = currentTotal.AndThen(curr => previousTotal.Map(prev => curr - prev)); // set previous to new current module.Name.ForEach(name => { currentTotal.ForEach(curr => this.previousModuleCpu[name] = curr); module.Read.ForEach(curr => this.previousReadTime[name] = curr); }); return(intervalsUsed.AndThen(used => possibleIntervals.AndThen(possible => { if (possible > 0) { double result = 100 * used / possible; // Occasionally on startup results in a very large number (billions of percent). Ignore this point. if (result < 100) { return Option.Some(result); } } return Option.None <double>(); }))); }
public static UserInputDescriptionBuilder RegisterVariableQuestion(this UserInputDescriptionBuilder builder, Option <string> variableQuestion) { _ = variableQuestion.AndThen(question => builder.SetVariableQuestion(question)); return(builder); }
private static void AddAssetsListMetadataToPackageReference( ProjectItemElement item, string attributeName, Option <DependencyAssets> assetList) => assetList.AndThen(list => item.AddMetadataAsAttribute(attributeName, MapDependencyAssetsToString(list)));