Skip to content

Commit 25abbff

Browse files
committed
more work on Options
1 parent d7ad326 commit 25abbff

17 files changed

+768
-173
lines changed

Bench.sln

-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "F", "F\F.fsproj", "{CF639BE
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WiaCopyTool", "WiaCopyTool\WiaCopyTool.csproj", "{A0D941EF-1EF6-47CD-9E4F-EDD3497DC594}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Monadic", "Monadic\Monadic.csproj", "{E2546AE9-20B3-4CE6-83CA-A8814B18969B}"
11-
EndProject
1210
Global
1311
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1412
Debug|Any CPU = Debug|Any CPU
@@ -23,10 +21,6 @@ Global
2321
{A0D941EF-1EF6-47CD-9E4F-EDD3497DC594}.Debug|Any CPU.Build.0 = Debug|Any CPU
2422
{A0D941EF-1EF6-47CD-9E4F-EDD3497DC594}.Release|Any CPU.ActiveCfg = Release|Any CPU
2523
{A0D941EF-1EF6-47CD-9E4F-EDD3497DC594}.Release|Any CPU.Build.0 = Release|Any CPU
26-
{E2546AE9-20B3-4CE6-83CA-A8814B18969B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27-
{E2546AE9-20B3-4CE6-83CA-A8814B18969B}.Debug|Any CPU.Build.0 = Debug|Any CPU
28-
{E2546AE9-20B3-4CE6-83CA-A8814B18969B}.Release|Any CPU.ActiveCfg = Release|Any CPU
29-
{E2546AE9-20B3-4CE6-83CA-A8814B18969B}.Release|Any CPU.Build.0 = Release|Any CPU
3024
EndGlobalSection
3125
GlobalSection(SolutionProperties) = preSolution
3226
HideSolutionNode = FALSE

Monadic.Test/Monadic.Test.csproj

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props" Condition="Exists('..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" />
4+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
5+
<PropertyGroup>
6+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
7+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
8+
<ProjectGuid>{B5202108-9A7D-4B05-A010-C852BCEE70EF}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Monadic.Test</RootNamespace>
12+
<AssemblyName>Monadic.Test</AssemblyName>
13+
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
<NuGetPackageImportStamp>
16+
</NuGetPackageImportStamp>
17+
<TargetFrameworkProfile />
18+
</PropertyGroup>
19+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
20+
<DebugSymbols>true</DebugSymbols>
21+
<DebugType>full</DebugType>
22+
<Optimize>false</Optimize>
23+
<OutputPath>bin\Debug\</OutputPath>
24+
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Net.Http" />
44+
<Reference Include="System.Xml" />
45+
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
46+
<HintPath>..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
47+
<Private>True</Private>
48+
</Reference>
49+
<Reference Include="xunit.assert, Version=2.0.0.2929, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
50+
<HintPath>..\packages\xunit.assert.2.0.0\lib\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.assert.dll</HintPath>
51+
<Private>True</Private>
52+
</Reference>
53+
<Reference Include="xunit.core, Version=2.0.0.2929, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
54+
<HintPath>..\packages\xunit.extensibility.core.2.0.0\lib\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.dll</HintPath>
55+
<Private>True</Private>
56+
</Reference>
57+
</ItemGroup>
58+
<ItemGroup>
59+
<Compile Include="OptionTests.cs" />
60+
<Compile Include="Properties\AssemblyInfo.cs" />
61+
</ItemGroup>
62+
<ItemGroup>
63+
<None Include="packages.config" />
64+
</ItemGroup>
65+
<ItemGroup>
66+
<ProjectReference Include="..\Monadic\Monadic.csproj">
67+
<Project>{e2546ae9-20b3-4ce6-83ca-a8814b18969b}</Project>
68+
<Name>Monadic</Name>
69+
</ProjectReference>
70+
</ItemGroup>
71+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
72+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
73+
<PropertyGroup>
74+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
75+
</PropertyGroup>
76+
<Error Condition="!Exists('..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props'))" />
77+
</Target>
78+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
79+
Other similar extension points exist, see Microsoft.Common.targets.
80+
<Target Name="BeforeBuild">
81+
</Target>
82+
<Target Name="AfterBuild">
83+
</Target>
84+
-->
85+
</Project>

Monadic.Test/OptionTests.cs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace Monadic.Test
5+
{
6+
public class OptionTests
7+
{
8+
private class ThrowingClass
9+
{
10+
public static IOption<object> ThrowAndCatch()
11+
{
12+
try
13+
{
14+
throw new ApplicationException();
15+
}
16+
catch (Exception ex)
17+
{
18+
return Option<string>.Failure(ex);
19+
}
20+
}
21+
}
22+
23+
[Fact]
24+
public void ItsFailureIncludesThePreviousStackTrace()
25+
{
26+
var className = typeof(ThrowingClass).Name;
27+
var methodName = nameof(ThrowingClass.ThrowAndCatch);
28+
var stackTrace = Assert.Throws<ApplicationException>(() => ThrowingClass.ThrowAndCatch().Value).StackTrace;
29+
Assert.Contains($"{className}.{methodName}", stackTrace);
30+
}
31+
32+
[Fact]
33+
public void ItCanRecover()
34+
{
35+
var expected = 42;
36+
var actual = Option<int>.Failure("Yuck!").Or(expected);
37+
Assert.Equal(expected, actual);
38+
}
39+
}
40+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Monadic.Test")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Monadic.Test")]
13+
[assembly: AssemblyCopyright("Copyright \u00A9 2015")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("b5202108-9a7d-4b05-a010-c852bcee70ef")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Monadic.Test/packages.config

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="xunit" version="2.0.0" targetFramework="net452" />
4+
<package id="xunit.abstractions" version="2.0.0" targetFramework="net452" />
5+
<package id="xunit.assert" version="2.0.0" targetFramework="net452" />
6+
<package id="xunit.core" version="2.0.0" targetFramework="net452" />
7+
<package id="xunit.extensibility.core" version="2.0.0" targetFramework="net452" />
8+
</packages>

Monadic.Use/Monadic.Use.csproj

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{DB187097-18DE-49A7-962F-B54184FE900E}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Monadic.Use</RootNamespace>
11+
<AssemblyName>Monadic.Use</AssemblyName>
12+
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Net.Http" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="OptionExamples.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<ProjectReference Include="..\Monadic\Monadic.csproj">
49+
<Project>{e2546ae9-20b3-4ce6-83ca-a8814b18969b}</Project>
50+
<Name>Monadic</Name>
51+
</ProjectReference>
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
55+
Other similar extension points exist, see Microsoft.Common.targets.
56+
<Target Name="BeforeBuild">
57+
</Target>
58+
<Target Name="AfterBuild">
59+
</Target>
60+
-->
61+
</Project>

Monadic/OptionExamples.cs renamed to Monadic.Use/OptionExamples.cs

+21-27
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
1-
namespace Monadic
2-
{
3-
using System;
1+
using System;
2+
using Monadic.Low;
43

5-
class OptionExamples
4+
namespace Monadic.Use
5+
{
6+
public class OptionExamples
67
{
78
public IOption<string> Simple(string name)
89
{
910
switch (name)
1011
{
1112
case null:
12-
try
13-
{
14-
throw new ArgumentNullException(nameof(name));
15-
}
16-
catch (Exception ex)
17-
{
18-
return Option<string>.Failure(ex);
19-
}
13+
2014
case "foo":
2115
return Option.Success("bar");
2216
case "bar":
2317
return Option.Failure<string>("Only foo allowed");
2418
default:
2519
return Option<string>.Failure();
2620
}
27-
2821
}
2922

3023
public IOption<int> GetById(int id = 0)
@@ -39,9 +32,9 @@ public double GetStrict(string name, int id)
3932

4033
public double Then()
4134
{
42-
return this.Simple(null)
43-
.AndThen(() => this.GetById(2))
44-
.Map2(this.GetStrict).Or(Math.E);
35+
return Simple(null)
36+
.AndThen(() => GetById(2))
37+
.Map(GetStrict).Or(Math.E);
4538
}
4639

4740
public static double FinalStep(double d1, double d2, double d3)
@@ -51,23 +44,24 @@ public static double FinalStep(double d1, double d2, double d3)
5144

5245
public object Convoluted()
5346
{
54-
return this.Simple("Not")
55-
.AndThen(() => this.GetById(2))
56-
.Map2(this.GetStrict)
57-
.And(this.Then)
58-
.And(this.Then)
59-
.Map3(FinalStep)
47+
return Simple("Not")
48+
.AndThen(() => GetById(2))
49+
.Map(GetStrict)
50+
.And(Then)
51+
.And(Then)
52+
.Map(FinalStep)
6053
.Map(_ => (int)_)
61-
.OrElse(this.GetById())
62-
.Or(() => this.GetById().Or(42));
54+
.OrElse(GetById())
55+
.Or(() => GetById().Or(42));
6356
}
6457

6558
public IOption<string> QuerySyntax()
6659
{
6760
return
68-
from i in this.GetById() where i == 42
69-
from j in this.Simple("")
70-
let x = this.GetStrict(j, i)
61+
from i in GetById()
62+
where i == 42
63+
from j in Simple("")
64+
let x = GetStrict(j, i)
7165
select $"({i},{j}) = {x}";
7266
}
7367
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Monadic.Use")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Monadic.Use")]
13+
[assembly: AssemblyCopyright("Copyright \u00A9 2015")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("db187097-18de-49a7-962f-b54184fe900e")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)