Skip to content

Commit cba34ed

Browse files
authored
Merge pull request #561 from gsscoder/csharpx-upgrade
Upgraded parts of CSharpx from Version 1.6.2-alpha
2 parents 31fa871 + 5cc4a01 commit cba34ed

11 files changed

+551
-516
lines changed

src/CommandLine/CommandLine.csproj

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<AssemblyName>CommandLine</AssemblyName>
55
<OutputType>Library</OutputType>
66
<TargetFrameworks>netstandard2.0;net40;net45;net461</TargetFrameworks>
7-
<DefineConstants>$(DefineConstants);CSX_EITHER_INTERNAL;CSX_REM_EITHER_BEYOND_2;CSX_ENUM_INTERNAL;ERRH_INTERNAL;ERRH_DISABLE_INLINE_METHODS;CSX_MAYBE_INTERNAL;CSX_REM_EITHER_FUNC</DefineConstants>
7+
<DefineConstants>$(DefineConstants);CSX_EITHER_INTERNAL;CSX_REM_EITHER_BEYOND_2;CSX_ENUM_INTERNAL;ERRH_INTERNAL;ERRH_DISABLE_INLINE_METHODS;CSX_MAYBE_INTERNAL;CSX_REM_EITHER_FUNC;CSX_REM_CRYPTORAND</DefineConstants>
88
<DefineConstants Condition="'$(BuildTarget)' != 'fsharp'">$(DefineConstants);SKIP_FSHARP</DefineConstants>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1010
<AssemblyOriginatorKeyFile>..\..\CommandLine.snk</AssemblyOriginatorKeyFile>
@@ -47,7 +47,10 @@
4747
<PackageReference Include="FSharp.Core" Version="4.0.0.1" Condition="'$(BuildTarget)' == 'fsharp'" />
4848
</ItemGroup>
4949
<ItemGroup>
50-
<None Include="..\..\License.md" Pack="true" PackagePath="$(PackageLicenseFile)"/>
51-
<None Include="..\..\art\CommandLine20.png" Pack="true" PackagePath="$(PackageIcon)"/>
50+
<None Include="..\..\License.md" Pack="true" PackagePath="$(PackageLicenseFile)" />
51+
<None Include="..\..\art\CommandLine20.png" Pack="true" PackagePath="$(PackageIcon)" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<Folder Include="Infrastructure\CSharpx\" />
5255
</ItemGroup>
5356
</Project>

src/CommandLine/Core/InstanceBuilder.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public static ParserResult<T> Build<T>(
2828

2929
var specProps = typeInfo.GetSpecifications(pi => SpecificationProperty.Create(
3030
Specification.FromProperty(pi), pi, Maybe.Nothing<object>()))
31-
.Memorize();
31+
.Memoize();
3232

3333
var specs = from pt in specProps select pt.Specification;
3434

3535
var optionSpecs = specs
3636
.ThrowingValidate(SpecificationGuards.Lookup)
3737
.OfType<OptionSpecification>()
38-
.Memorize();
38+
.Memoize();
3939

4040
Func<T> makeDefault = () =>
4141
typeof(T).IsMutable()
@@ -46,19 +46,19 @@ public static ParserResult<T> Build<T>(
4646
Func<IEnumerable<Error>, ParserResult<T>> notParsed =
4747
errs => new NotParsed<T>(makeDefault().GetType().ToTypeInfo(), errs);
4848

49-
var argumentsList = arguments.Memorize();
49+
var argumentsList = arguments.Memoize();
5050
Func<ParserResult<T>> buildUp = () =>
5151
{
5252
var tokenizerResult = tokenizer(argumentsList, optionSpecs);
5353

54-
var tokens = tokenizerResult.SucceededWith().Memorize();
54+
var tokens = tokenizerResult.SucceededWith().Memoize();
5555

5656
var partitions = TokenPartitioner.Partition(
5757
tokens,
5858
name => TypeLookup.FindTypeDescriptorAndSibling(name, optionSpecs, nameComparer));
59-
var optionsPartition = partitions.Item1.Memorize();
60-
var valuesPartition = partitions.Item2.Memorize();
61-
var errorsPartition = partitions.Item3.Memorize();
59+
var optionsPartition = partitions.Item1.Memoize();
60+
var valuesPartition = partitions.Item2.Memoize();
61+
var errorsPartition = partitions.Item3.Memoize();
6262

6363
var optionSpecPropsResult =
6464
OptionMapper.MapValues(
@@ -80,7 +80,7 @@ public static ParserResult<T> Build<T>(
8080
.FromOptionSpecification());
8181

8282
var specPropsWithValue =
83-
optionSpecPropsResult.SucceededWith().Concat(valueSpecPropsResult.SucceededWith()).Memorize();
83+
optionSpecPropsResult.SucceededWith().Concat(valueSpecPropsResult.SucceededWith()).Memoize();
8484

8585
var setPropertyErrors = new List<Error>();
8686

@@ -104,7 +104,7 @@ public static ParserResult<T> Build<T>(
104104
.Concat(valueSpecPropsResult.SuccessfulMessages())
105105
.Concat(validationErrors)
106106
.Concat(setPropertyErrors)
107-
.Memorize();
107+
.Memoize();
108108

109109
var warnings = from e in allErrors where nonFatalErrors.Contains(e.Tag) select e;
110110

@@ -115,7 +115,7 @@ public static ParserResult<T> Build<T>(
115115
argumentsList.Any()
116116
? arguments.Preprocess(PreprocessorGuards.Lookup(nameComparer, autoHelp, autoVersion))
117117
: Enumerable.Empty<Error>()
118-
).Memorize();
118+
).Memoize();
119119

120120
var result = argumentsList.Any()
121121
? preprocessorErrors.Any()

src/CommandLine/Core/OptionMapper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ select Tuple.Create(
4343
((OptionSpecification)pt.Specification).FromOptionSpecification()))))
4444
: Tuple.Create(pt, Maybe.Nothing<Error>());
4545
}
46-
).Memorize();
46+
).Memoize();
4747
return Result.Succeed(
4848
sequencesAndErrors.Select(se => se.Item1),
4949
sequencesAndErrors.Select(se => se.Item2).OfType<Just<Error>>().Select(se => se.Value));

src/CommandLine/Core/TokenPartitioner.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ Tuple<IEnumerable<KeyValuePair<string, IEnumerable<string>>>, IEnumerable<string
1717
{
1818
IEqualityComparer<Token> tokenComparer = ReferenceEqualityComparer.Default;
1919

20-
var tokenList = tokens.Memorize();
20+
var tokenList = tokens.Memoize();
2121
var switches = new HashSet<Token>(Switch.Partition(tokenList, typeLookup), tokenComparer);
2222
var scalars = new HashSet<Token>(Scalar.Partition(tokenList, typeLookup), tokenComparer);
2323
var sequences = new HashSet<Token>(Sequence.Partition(tokenList, typeLookup), tokenComparer);
2424
var nonOptions = tokenList
2525
.Where(t => !switches.Contains(t))
2626
.Where(t => !scalars.Contains(t))
27-
.Where(t => !sequences.Contains(t)).Memorize();
28-
var values = nonOptions.Where(v => v.IsValue()).Memorize();
29-
var errors = nonOptions.Except(values, (IEqualityComparer<Token>)ReferenceEqualityComparer.Default).Memorize();
27+
.Where(t => !sequences.Contains(t)).Memoize();
28+
var values = nonOptions.Where(v => v.IsValue()).Memoize();
29+
var errors = nonOptions.Except(values, (IEqualityComparer<Token>)ReferenceEqualityComparer.Default).Memoize();
3030

3131
return Tuple.Create(
3232
KeyValuePairHelper.ForSwitch(switches)

src/CommandLine/Core/Tokenizer.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public static Result<IEnumerable<Token>, Error> Tokenize(
3434
? TokenizeLongName(arg, onError)
3535
: TokenizeShortName(arg, nameLookup)
3636
select token)
37-
.Memorize();
37+
.Memoize();
3838

39-
var normalized = normalize(tokens).Memorize();
39+
var normalized = normalize(tokens).Memoize();
4040

41-
var unkTokens = (from t in normalized where t.IsName() && nameLookup(t.Text) == NameLookupResult.NoOptionFound select t).Memorize();
41+
var unkTokens = (from t in normalized where t.IsName() && nameLookup(t.Text) == NameLookupResult.NoOptionFound select t).Memoize();
4242

4343
return Result.Succeed(normalized.Where(x => !unkTokens.Contains(x)), errors.Concat(from t in unkTokens select new UnknownOptionError(t.Text)));
4444
}
@@ -60,12 +60,12 @@ public static Result<IEnumerable<Token>, Error> ExplodeOptionList(
6060
Result<IEnumerable<Token>, Error> tokenizerResult,
6161
Func<string, Maybe<char>> optionSequenceWithSeparatorLookup)
6262
{
63-
var tokens = tokenizerResult.SucceededWith().Memorize();
63+
var tokens = tokenizerResult.SucceededWith().Memoize();
6464

6565
var replaces = tokens.Select((t, i) =>
6666
optionSequenceWithSeparatorLookup(t.Text)
6767
.MapValueOrDefault(sep => Tuple.Create(i + 1, sep),
68-
Tuple.Create(-1, '\0'))).SkipWhile(x => x.Item1 < 0).Memorize();
68+
Tuple.Create(-1, '\0'))).SkipWhile(x => x.Item1 < 0).Memoize();
6969

7070
var exploded = tokens.Select((t, i) =>
7171
replaces.FirstOrDefault(x => x.Item1 == i).ToMaybe()
@@ -205,4 +205,4 @@ private static IEnumerable<Token> TokenizeLongName(
205205
}
206206
}
207207
}
208-
}
208+
}

src/CommandLine/Infrastructure/Either.cs src/CommandLine/Infrastructure/CSharpx/Either.cs

+14-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
//Use project level define(s) when referencing with Paket.
2-
//#define CSX_EITHER_INTERNAL // Uncomment this to set visibility to internal.
3-
//#define CSX_REM_MAYBE_FUNC // Uncomment this to remove dependency to Maybe.cs.
1+
//#define CSX_EITHER_INTERNAL // Uncomment or define at build time to set accessibility to internal.
2+
//#define CSX_REM_MAYBE_FUNC // Uncomment or define at build time to remove dependency to Maybe.cs.
43

54
using System;
65

@@ -133,8 +132,7 @@ public static Either<string, TRight> Fail<TRight>(string message)
133132
public static Either<TLeft, TResult> Bind<TLeft, TRight, TResult>(Either<TLeft, TRight> either, Func<TRight, Either<TLeft, TResult>> func)
134133
{
135134
TRight right;
136-
if (either.MatchRight(out right))
137-
{
135+
if (either.MatchRight(out right)) {
138136
return func(right);
139137
}
140138
return Either.Left<TLeft, TResult>(either.GetLeft());
@@ -148,8 +146,7 @@ public static Either<TLeft, TResult> Bind<TLeft, TRight, TResult>(Either<TLeft,
148146
public static Either<TLeft, TResult> Map<TLeft, TRight, TResult>(Either<TLeft, TRight> either, Func<TRight, TResult> func)
149147
{
150148
TRight right;
151-
if (either.MatchRight(out right))
152-
{
149+
if (either.MatchRight(out right)) {
153150
return Either.Right<TLeft, TResult>(func(right));
154151
}
155152
return Either.Left<TLeft, TResult>(either.GetLeft());
@@ -164,8 +161,7 @@ public static Either<TLeft, TResult> Map<TLeft, TRight, TResult>(Either<TLeft, T
164161
public static Either<TLeft1, TRight1> Bimap<TLeft, TRight, TLeft1, TRight1>(Either<TLeft, TRight> either, Func<TLeft, TLeft1> mapLeft, Func<TRight, TRight1> mapRight)
165162
{
166163
TRight right;
167-
if (either.MatchRight(out right))
168-
{
164+
if (either.MatchRight(out right)) {
169165
return Either.Right<TLeft1, TRight1>(mapRight(right));
170166
}
171167
return Either.Left<TLeft1, TRight1>(mapLeft(either.GetLeft()));
@@ -196,9 +192,10 @@ public static Either<TLeft, TResult> SelectMany<TLeft, TRight, TResult>(this Eit
196192
public static TRight GetOrFail<TLeft, TRight>(Either<TLeft, TRight> either)
197193
{
198194
TRight value;
199-
if (either.MatchRight(out value))
195+
if (either.MatchRight(out value)) {
200196
return value;
201-
throw new ArgumentException("either", string.Format("The either value was Left {0}", either));
197+
}
198+
throw new ArgumentException(nameof(either), string.Format("The either value was Left {0}", either));
202199
}
203200

204201
/// <summary>
@@ -224,12 +221,10 @@ public static TRight GetRightOrDefault<TLeft, TRight>(Either<TLeft, TRight> eith
224221
/// </summary>
225222
public static Either<Exception, TRight> Try<TRight>(Func<TRight> func)
226223
{
227-
try
228-
{
224+
try {
229225
return new Right<Exception, TRight>(func());
230226
}
231-
catch (Exception ex)
232-
{
227+
catch (Exception ex) {
233228
return new Left<Exception, TRight>(ex);
234229
}
235230
}
@@ -244,10 +239,9 @@ public static Either<Exception, TRight> Cast<TRight>(object obj)
244239
}
245240

246241
#if !CSX_REM_MAYBE_FUNC
247-
public static Either<TLeft, TRight> OfMaybe<TLeft, TRight>(Maybe<TRight> maybe, TLeft left)
242+
public static Either<TLeft, TRight> FromMaybe<TLeft, TRight>(Maybe<TRight> maybe, TLeft left)
248243
{
249-
if (maybe.Tag == MaybeType.Just)
250-
{
244+
if (maybe.Tag == MaybeType.Just) {
251245
return Either.Right<TLeft, TRight>(((Just<TRight>)maybe).Value);
252246
}
253247
return Either.Left<TLeft, TRight>(left);
@@ -269,8 +263,7 @@ static class EitherExtensions
269263
public static void Match<TLeft, TRight>(this Either<TLeft, TRight> either, Action<TLeft> ifLeft, Action<TRight> ifRight)
270264
{
271265
TLeft left;
272-
if (either.MatchLeft(out left))
273-
{
266+
if (either.MatchLeft(out left)) {
274267
ifLeft(left);
275268
return;
276269
}
@@ -279,7 +272,7 @@ public static void Match<TLeft, TRight>(this Either<TLeft, TRight> either, Actio
279272
#endregion
280273

281274
/// <summary>
282-
/// Equivalent to monadic <see cref="CSharpx.Either.Return{TRight}"/> operation.
275+
/// Equivalent to monadic <see cref="CSharpx.Either.Return{TLeft, TRight}"/> operation.
283276
/// Builds a <see cref="CSharpx.Right{TLeft, TRight}"/> value in case <paramref name="value"/> by default.
284277
/// </summary>
285278
public static Either<string, TRight> ToEither<TRight>(this TRight value)

0 commit comments

Comments
 (0)