Skip to content

Commit

Permalink
Merge pull request #561 from gsscoder/csharpx-upgrade
Browse files Browse the repository at this point in the history
Upgraded parts of CSharpx from Version 1.6.2-alpha
  • Loading branch information
moh-hassan committed Jan 8, 2020
2 parents 31fa871 + 5cc4a01 commit cba34ed
Show file tree
Hide file tree
Showing 11 changed files with 551 additions and 516 deletions.
9 changes: 6 additions & 3 deletions src/CommandLine/CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AssemblyName>CommandLine</AssemblyName>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.0;net40;net45;net461</TargetFrameworks>
<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>
<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>
<DefineConstants Condition="'$(BuildTarget)' != 'fsharp'">$(DefineConstants);SKIP_FSHARP</DefineConstants>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyOriginatorKeyFile>..\..\CommandLine.snk</AssemblyOriginatorKeyFile>
Expand Down Expand Up @@ -47,7 +47,10 @@
<PackageReference Include="FSharp.Core" Version="4.0.0.1" Condition="'$(BuildTarget)' == 'fsharp'" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\License.md" Pack="true" PackagePath="$(PackageLicenseFile)"/>
<None Include="..\..\art\CommandLine20.png" Pack="true" PackagePath="$(PackageIcon)"/>
<None Include="..\..\License.md" Pack="true" PackagePath="$(PackageLicenseFile)" />
<None Include="..\..\art\CommandLine20.png" Pack="true" PackagePath="$(PackageIcon)" />
</ItemGroup>
<ItemGroup>
<Folder Include="Infrastructure\CSharpx\" />
</ItemGroup>
</Project>
20 changes: 10 additions & 10 deletions src/CommandLine/Core/InstanceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public static ParserResult<T> Build<T>(

var specProps = typeInfo.GetSpecifications(pi => SpecificationProperty.Create(
Specification.FromProperty(pi), pi, Maybe.Nothing<object>()))
.Memorize();
.Memoize();

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

var optionSpecs = specs
.ThrowingValidate(SpecificationGuards.Lookup)
.OfType<OptionSpecification>()
.Memorize();
.Memoize();

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

var argumentsList = arguments.Memorize();
var argumentsList = arguments.Memoize();
Func<ParserResult<T>> buildUp = () =>
{
var tokenizerResult = tokenizer(argumentsList, optionSpecs);
var tokens = tokenizerResult.SucceededWith().Memorize();
var tokens = tokenizerResult.SucceededWith().Memoize();
var partitions = TokenPartitioner.Partition(
tokens,
name => TypeLookup.FindTypeDescriptorAndSibling(name, optionSpecs, nameComparer));
var optionsPartition = partitions.Item1.Memorize();
var valuesPartition = partitions.Item2.Memorize();
var errorsPartition = partitions.Item3.Memorize();
var optionsPartition = partitions.Item1.Memoize();
var valuesPartition = partitions.Item2.Memoize();
var errorsPartition = partitions.Item3.Memoize();
var optionSpecPropsResult =
OptionMapper.MapValues(
Expand All @@ -80,7 +80,7 @@ public static ParserResult<T> Build<T>(
.FromOptionSpecification());
var specPropsWithValue =
optionSpecPropsResult.SucceededWith().Concat(valueSpecPropsResult.SucceededWith()).Memorize();
optionSpecPropsResult.SucceededWith().Concat(valueSpecPropsResult.SucceededWith()).Memoize();
var setPropertyErrors = new List<Error>();
Expand All @@ -104,7 +104,7 @@ public static ParserResult<T> Build<T>(
.Concat(valueSpecPropsResult.SuccessfulMessages())
.Concat(validationErrors)
.Concat(setPropertyErrors)
.Memorize();
.Memoize();
var warnings = from e in allErrors where nonFatalErrors.Contains(e.Tag) select e;
Expand All @@ -115,7 +115,7 @@ public static ParserResult<T> Build<T>(
argumentsList.Any()
? arguments.Preprocess(PreprocessorGuards.Lookup(nameComparer, autoHelp, autoVersion))
: Enumerable.Empty<Error>()
).Memorize();
).Memoize();

var result = argumentsList.Any()
? preprocessorErrors.Any()
Expand Down
2 changes: 1 addition & 1 deletion src/CommandLine/Core/OptionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ select Tuple.Create(
((OptionSpecification)pt.Specification).FromOptionSpecification()))))
: Tuple.Create(pt, Maybe.Nothing<Error>());
}
).Memorize();
).Memoize();
return Result.Succeed(
sequencesAndErrors.Select(se => se.Item1),
sequencesAndErrors.Select(se => se.Item2).OfType<Just<Error>>().Select(se => se.Value));
Expand Down
8 changes: 4 additions & 4 deletions src/CommandLine/Core/TokenPartitioner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ Tuple<IEnumerable<KeyValuePair<string, IEnumerable<string>>>, IEnumerable<string
{
IEqualityComparer<Token> tokenComparer = ReferenceEqualityComparer.Default;

var tokenList = tokens.Memorize();
var tokenList = tokens.Memoize();
var switches = new HashSet<Token>(Switch.Partition(tokenList, typeLookup), tokenComparer);
var scalars = new HashSet<Token>(Scalar.Partition(tokenList, typeLookup), tokenComparer);
var sequences = new HashSet<Token>(Sequence.Partition(tokenList, typeLookup), tokenComparer);
var nonOptions = tokenList
.Where(t => !switches.Contains(t))
.Where(t => !scalars.Contains(t))
.Where(t => !sequences.Contains(t)).Memorize();
var values = nonOptions.Where(v => v.IsValue()).Memorize();
var errors = nonOptions.Except(values, (IEqualityComparer<Token>)ReferenceEqualityComparer.Default).Memorize();
.Where(t => !sequences.Contains(t)).Memoize();
var values = nonOptions.Where(v => v.IsValue()).Memoize();
var errors = nonOptions.Except(values, (IEqualityComparer<Token>)ReferenceEqualityComparer.Default).Memoize();

return Tuple.Create(
KeyValuePairHelper.ForSwitch(switches)
Expand Down
12 changes: 6 additions & 6 deletions src/CommandLine/Core/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public static Result<IEnumerable<Token>, Error> Tokenize(
? TokenizeLongName(arg, onError)
: TokenizeShortName(arg, nameLookup)
select token)
.Memorize();
.Memoize();

var normalized = normalize(tokens).Memorize();
var normalized = normalize(tokens).Memoize();

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

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

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

var exploded = tokens.Select((t, i) =>
replaces.FirstOrDefault(x => x.Item1 == i).ToMaybe()
Expand Down Expand Up @@ -205,4 +205,4 @@ private static IEnumerable<Token> TokenizeLongName(
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//Use project level define(s) when referencing with Paket.
//#define CSX_EITHER_INTERNAL // Uncomment this to set visibility to internal.
//#define CSX_REM_MAYBE_FUNC // Uncomment this to remove dependency to Maybe.cs.
//#define CSX_EITHER_INTERNAL // Uncomment or define at build time to set accessibility to internal.
//#define CSX_REM_MAYBE_FUNC // Uncomment or define at build time to remove dependency to Maybe.cs.

using System;

Expand Down Expand Up @@ -133,8 +132,7 @@ public static Either<string, TRight> Fail<TRight>(string message)
public static Either<TLeft, TResult> Bind<TLeft, TRight, TResult>(Either<TLeft, TRight> either, Func<TRight, Either<TLeft, TResult>> func)
{
TRight right;
if (either.MatchRight(out right))
{
if (either.MatchRight(out right)) {
return func(right);
}
return Either.Left<TLeft, TResult>(either.GetLeft());
Expand All @@ -148,8 +146,7 @@ public static Either<TLeft, TResult> Bind<TLeft, TRight, TResult>(Either<TLeft,
public static Either<TLeft, TResult> Map<TLeft, TRight, TResult>(Either<TLeft, TRight> either, Func<TRight, TResult> func)
{
TRight right;
if (either.MatchRight(out right))
{
if (either.MatchRight(out right)) {
return Either.Right<TLeft, TResult>(func(right));
}
return Either.Left<TLeft, TResult>(either.GetLeft());
Expand All @@ -164,8 +161,7 @@ public static Either<TLeft, TResult> Map<TLeft, TRight, TResult>(Either<TLeft, T
public static Either<TLeft1, TRight1> Bimap<TLeft, TRight, TLeft1, TRight1>(Either<TLeft, TRight> either, Func<TLeft, TLeft1> mapLeft, Func<TRight, TRight1> mapRight)
{
TRight right;
if (either.MatchRight(out right))
{
if (either.MatchRight(out right)) {
return Either.Right<TLeft1, TRight1>(mapRight(right));
}
return Either.Left<TLeft1, TRight1>(mapLeft(either.GetLeft()));
Expand Down Expand Up @@ -196,9 +192,10 @@ public static Either<TLeft, TResult> SelectMany<TLeft, TRight, TResult>(this Eit
public static TRight GetOrFail<TLeft, TRight>(Either<TLeft, TRight> either)
{
TRight value;
if (either.MatchRight(out value))
if (either.MatchRight(out value)) {
return value;
throw new ArgumentException("either", string.Format("The either value was Left {0}", either));
}
throw new ArgumentException(nameof(either), string.Format("The either value was Left {0}", either));
}

/// <summary>
Expand All @@ -224,12 +221,10 @@ public static TRight GetRightOrDefault<TLeft, TRight>(Either<TLeft, TRight> eith
/// </summary>
public static Either<Exception, TRight> Try<TRight>(Func<TRight> func)
{
try
{
try {
return new Right<Exception, TRight>(func());
}
catch (Exception ex)
{
catch (Exception ex) {
return new Left<Exception, TRight>(ex);
}
}
Expand All @@ -244,10 +239,9 @@ public static Either<Exception, TRight> Cast<TRight>(object obj)
}

#if !CSX_REM_MAYBE_FUNC
public static Either<TLeft, TRight> OfMaybe<TLeft, TRight>(Maybe<TRight> maybe, TLeft left)
public static Either<TLeft, TRight> FromMaybe<TLeft, TRight>(Maybe<TRight> maybe, TLeft left)
{
if (maybe.Tag == MaybeType.Just)
{
if (maybe.Tag == MaybeType.Just) {
return Either.Right<TLeft, TRight>(((Just<TRight>)maybe).Value);
}
return Either.Left<TLeft, TRight>(left);
Expand All @@ -269,8 +263,7 @@ static class EitherExtensions
public static void Match<TLeft, TRight>(this Either<TLeft, TRight> either, Action<TLeft> ifLeft, Action<TRight> ifRight)
{
TLeft left;
if (either.MatchLeft(out left))
{
if (either.MatchLeft(out left)) {
ifLeft(left);
return;
}
Expand All @@ -279,7 +272,7 @@ public static void Match<TLeft, TRight>(this Either<TLeft, TRight> either, Actio
#endregion

/// <summary>
/// Equivalent to monadic <see cref="CSharpx.Either.Return{TRight}"/> operation.
/// Equivalent to monadic <see cref="CSharpx.Either.Return{TLeft, TRight}"/> operation.
/// Builds a <see cref="CSharpx.Right{TLeft, TRight}"/> value in case <paramref name="value"/> by default.
/// </summary>
public static Either<string, TRight> ToEither<TRight>(this TRight value)
Expand Down
Loading

0 comments on commit cba34ed

Please sign in to comment.