Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Use a ValueTuple instead of StateObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chtoucas committed Mar 9, 2017
1 parent 113b657 commit d5fb576
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 87 deletions.
3 changes: 1 addition & 2 deletions src/Narvalo.Common/Collections/Dictionary$.cs
Expand Up @@ -27,8 +27,7 @@ public static partial class DictionaryExtensions


if (key == null) { return Maybe<TValue>.None; } if (key == null) { return Maybe<TValue>.None; }


TValue value; return @this.TryGetValue(key, out TValue value) ? Maybe.Of(value) : Maybe<TValue>.None;
return @this.TryGetValue(key, out value) ? Maybe.Of(value) : Maybe<TValue>.None;
} }
} }
} }
6 changes: 2 additions & 4 deletions src/Narvalo.Common/Internal/TryParser$.cs
Expand Up @@ -12,8 +12,7 @@ internal static class TryParserExtensions


if (value == null) { return null; } if (value == null) { return null; }


T result; return @this.Invoke(value, out T result) ? result : (T?)null;
return @this.Invoke(value, out result) ? result : (T?)null;
} }


public static Maybe<T> MayInvoke<T>(this TryParser<T> @this, string value) where T : class public static Maybe<T> MayInvoke<T>(this TryParser<T> @this, string value) where T : class
Expand All @@ -22,8 +21,7 @@ internal static class TryParserExtensions


if (value == null) { return Maybe<T>.None; } if (value == null) { return Maybe<T>.None; }


T result; return @this.Invoke(value, out T result) ? Maybe.Of(result) : Maybe<T>.None;
return @this.Invoke(value, out result) ? Maybe.Of(result) : Maybe<T>.None;
} }
} }
} }
8 changes: 4 additions & 4 deletions src/Narvalo.Finance/Bic.cs
Expand Up @@ -181,7 +181,7 @@ public static string FromBic(string value)
} }
else else
{ {
var retval = value.Substring(StartIndex, Length); string retval = value.Substring(StartIndex, Length);
return CheckContent(retval) ? retval : null; return CheckContent(retval) ? retval : null;
} }
} }
Expand All @@ -203,7 +203,7 @@ private static class CountryPart
public static string FromBic(string value) public static string FromBic(string value)
{ {
Demand.True(value.Length >= StartIndex + Length); Demand.True(value.Length >= StartIndex + Length);
var retval = value.Substring(StartIndex, Length); string retval = value.Substring(StartIndex, Length);
return CheckContent(retval) ? retval : null; return CheckContent(retval) ? retval : null;
} }


Expand All @@ -223,7 +223,7 @@ private static class InstitutionPart


public static string FromBic(string value, BicVersion version) public static string FromBic(string value, BicVersion version)
{ {
var retval = value.Substring(StartIndex, Length); string retval = value.Substring(StartIndex, Length);


return CheckContent(retval, version) ? retval : null; return CheckContent(retval, version) ? retval : null;
} }
Expand All @@ -247,7 +247,7 @@ private static class LocationPart
public static string FromBic(string value) public static string FromBic(string value)
{ {
Demand.True(value.Length >= StartIndex + Length); Demand.True(value.Length >= StartIndex + Length);
var retval = value.Substring(StartIndex, Length); string retval = value.Substring(StartIndex, Length);
return CheckContent(retval) ? retval : null; return CheckContent(retval) ? retval : null;
} }


Expand Down
2 changes: 2 additions & 0 deletions src/Narvalo.Futures/Applicative/StateObject`.cs
Expand Up @@ -6,13 +6,15 @@ namespace Narvalo.Applicative
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;


[Obsolete]
public static class StateObject public static class StateObject
{ {
public static StateObject<T, TState> Create<T, TState>(T value, TState state) public static StateObject<T, TState> Create<T, TState>(T value, TState state)
=> new StateObject<T, TState>(value, state); => new StateObject<T, TState>(value, state);
} }


// To be replaced by ValueTuple<T, TState> when available. // To be replaced by ValueTuple<T, TState> when available.
[Obsolete]
public partial struct StateObject<T, TState> : IEquatable<StateObject<T, TState>>, IStructuralEquatable public partial struct StateObject<T, TState> : IEquatable<StateObject<T, TState>>, IStructuralEquatable
{ {
public StateObject(T result, TState state) public StateObject(T result, TState state)
Expand Down
18 changes: 9 additions & 9 deletions src/Narvalo.Futures/Applicative/Stateful.cs
Expand Up @@ -4,8 +4,8 @@ namespace Narvalo.Applicative
{ {
using System; using System;


// Func<TState, StateObject<T, TState>> // Func<TState, (T, TState)>
public delegate StateObject<T, TState> Stateful<T, TState>(TState state); public delegate (T result, TState state) Stateful<T, TState>(TState state);


// Provides the core Monad methods. // Provides the core Monad methods.
public static partial class Stateful public static partial class Stateful
Expand All @@ -15,14 +15,14 @@ public static partial class Stateful
Func<T, Stateful<TNext, TState>> binder) Func<T, Stateful<TNext, TState>> binder)
=> state => => state =>
{ {
StateObject<T, TState> obj = @this(state); var obj = @this(state);
return binder(obj.Result).Invoke(obj.State); return binder(obj.result).Invoke(obj.state);
}; };


// Initialize a stateful computation from a given value. // Initialize a stateful computation from a given value.
public static Stateful<T, TState> Of<T, TState>(T value) public static Stateful<T, TState> Of<T, TState>(T value)
=> state => StateObject.Create(value, state); => state => (value, state);


public static Stateful<T, TState> Flatten<T, TState>(Stateful<Stateful<T, TState>, TState> square) public static Stateful<T, TState> Flatten<T, TState>(Stateful<Stateful<T, TState>, TState> square)
=> square.Bind(Stubs<Stateful<T, TState>>.Identity); => square.Bind(Stubs<Stateful<T, TState>>.Identity);
Expand All @@ -32,15 +32,15 @@ public static partial class Stateful
public static partial class Stateful public static partial class Stateful
{ {
public static Stateful<TState, TState> Get<TState>() public static Stateful<TState, TState> Get<TState>()
=> state => StateObject.Create(state, state); => state => (state, state);


public static Stateful<Unit, TState> Put<TState>(TState newState) public static Stateful<Unit, TState> Put<TState>(TState newState)
=> state => StateObject.Create(Unit.Default, newState); => state => (Unit.Default, newState);


public static Stateful<Unit, TState> Modify<TState>(Func<TState, TState> func) public static Stateful<Unit, TState> Modify<TState>(Func<TState, TState> func)
=> state => StateObject.Create(Unit.Default, func(state)); => state => (Unit.Default, func(state));


public static Stateful<TResult, TState> Gets<TState, TResult>(Func<TState, TResult> func) public static Stateful<TResult, TState> Gets<TState, TResult>(Func<TState, TResult> func)
=> state => StateObject.Create(func(state), state); => state => (func(state), state);
} }
} }
2 changes: 1 addition & 1 deletion src/Narvalo.Futures/Applicative/Stateful.g.cs
Expand Up @@ -6,7 +6,7 @@
// behavior and will be lost if the code is regenerated. // behavior and will be lost if the code is regenerated.
// //
// Runtime Version: 4.0.30319.42000 // Runtime Version: 4.0.30319.42000
// Microsoft.VisualStudio.TextTemplating: 14.0 // Microsoft.VisualStudio.TextTemplating: 15.0
// </auto-generated> // </auto-generated>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------


Expand Down
4 changes: 4 additions & 0 deletions src/Narvalo.Futures/Narvalo.Futures.csproj
Expand Up @@ -11,6 +11,9 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" /> <Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration" /> <Reference Include="System.Configuration" />
<Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
</ItemGroup> </ItemGroup>
Expand Down Expand Up @@ -106,6 +109,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Narvalo.Futures.Version.props" /> <None Include="Narvalo.Futures.Version.props" />
<None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" /> <Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
Expand Down
4 changes: 4 additions & 0 deletions src/Narvalo.Futures/packages.config
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.ValueTuple" version="4.3.0" targetFramework="net461" />
</packages>
30 changes: 9 additions & 21 deletions src/Narvalo.Fx/Applicative/Either`2.cs
Expand Up @@ -52,16 +52,13 @@ public abstract partial class Either<TLeft, TRight> : IStructuralEquatable, Inte
[DebuggerTypeProxy(typeof(Either<,>.Left_.DebugView))] [DebuggerTypeProxy(typeof(Either<,>.Left_.DebugView))]
private sealed partial class Left_ : Either<TLeft, TRight>, IEquatable<Left_> private sealed partial class Left_ : Either<TLeft, TRight>, IEquatable<Left_>
{ {
public Left_(TLeft value) public Left_(TLeft value) => Left = value;
{
Left = value;
}


public override bool IsLeft => true; public override bool IsLeft => true;


internal override TLeft Left { get; } internal override TLeft Left { get; }


internal override TRight Right { get { throw new InvalidOperationException("XXX"); } } internal override TRight Right => throw new InvalidOperationException("XXX");


public override Maybe<TLeft> LeftOrNone() => Maybe.Of(Left); public override Maybe<TLeft> LeftOrNone() => Maybe.Of(Left);


Expand Down Expand Up @@ -97,10 +94,7 @@ private sealed class DebugView
{ {
private readonly Either<TLeft, TRight> _inner; private readonly Either<TLeft, TRight> _inner;


public DebugView(Either<TLeft, TRight> inner) public DebugView(Either<TLeft, TRight> inner) => _inner = inner;
{
_inner = inner;
}


public TLeft Left => _inner.Left; public TLeft Left => _inner.Left;
} }
Expand All @@ -112,22 +106,19 @@ public DebugView(Either<TLeft, TRight> inner)
[DebuggerTypeProxy(typeof(Either<,>.Right_.DebugView))] [DebuggerTypeProxy(typeof(Either<,>.Right_.DebugView))]
private sealed partial class Right_ : Either<TLeft, TRight>, IEquatable<Right_> private sealed partial class Right_ : Either<TLeft, TRight>, IEquatable<Right_>
{ {
public Right_(TRight value) public Right_(TRight value) => Right = value;
{
Right = value;
}


public override bool IsLeft => false; public override bool IsLeft => false;


internal override TLeft Left { get { throw new InvalidOperationException("XXX"); } } internal override TLeft Left => throw new InvalidOperationException("XXX");


internal override TRight Right { get; } internal override TRight Right { get; }


public override Maybe<TLeft> LeftOrNone() => Maybe<TLeft>.None; public override Maybe<TLeft> LeftOrNone() => Maybe<TLeft>.None;


public override Maybe<TRight> RightOrNone() => Maybe.Of(Right); public override Maybe<TRight> RightOrNone() => Maybe.Of(Right);


public override Either<TRight, TLeft> Swap() { throw new InvalidOperationException("XXX"); } public override Either<TRight, TLeft> Swap() => throw new InvalidOperationException("XXX");


public override Either<TRight, TLeft> SwapUnchecked() => Either<TRight, TLeft>.OfLeft(Right); public override Either<TRight, TLeft> SwapUnchecked() => Either<TRight, TLeft>.OfLeft(Right);


Expand Down Expand Up @@ -157,10 +148,7 @@ private sealed class DebugView
{ {
private readonly Either<TLeft, TRight> _inner; private readonly Either<TLeft, TRight> _inner;


public DebugView(Either<TLeft, TRight> inner) public DebugView(Either<TLeft, TRight> inner) => _inner = inner;
{
_inner = inner;
}


public TRight Right => _inner.Right; public TRight Right => _inner.Right;
} }
Expand Down Expand Up @@ -188,12 +176,12 @@ private partial class Left_
{ {
public override TLeft ToLeft() => Left; public override TLeft ToLeft() => Left;


public override TRight ToRight() { throw new InvalidCastException("XXX"); } public override TRight ToRight() => throw new InvalidCastException("XXX");
} }


private partial class Right_ private partial class Right_
{ {
public override TLeft ToLeft() { throw new InvalidCastException("XXX"); } public override TLeft ToLeft() => throw new InvalidCastException("XXX");


public override TRight ToRight() => Right; public override TRight ToRight() => Right;
} }
Expand Down
5 changes: 1 addition & 4 deletions src/Narvalo.Fx/Applicative/Fallible.cs
Expand Up @@ -65,10 +65,7 @@ private sealed class DebugView
{ {
private readonly Fallible _inner; private readonly Fallible _inner;


public DebugView(Fallible inner) public DebugView(Fallible inner) => _inner = inner;
{
_inner = inner;
}


public bool IsSuccess => _inner.IsSuccess; public bool IsSuccess => _inner.IsSuccess;


Expand Down
5 changes: 1 addition & 4 deletions src/Narvalo.Fx/Applicative/Maybe`1.cs
Expand Up @@ -115,10 +115,7 @@ private sealed class DebugView
{ {
private readonly Maybe<T> _inner; private readonly Maybe<T> _inner;


public DebugView(Maybe<T> inner) public DebugView(Maybe<T> inner) => _inner = inner;
{
_inner = inner;
}


public bool IsSome => _inner.IsSome; public bool IsSome => _inner.IsSome;


Expand Down
8 changes: 2 additions & 6 deletions src/Narvalo.Fx/Applicative/Outcome.cs
Expand Up @@ -49,10 +49,7 @@ private sealed class DebugView
{ {
private readonly Outcome _inner; private readonly Outcome _inner;


public DebugView(Outcome inner) public DebugView(Outcome inner) => _inner = inner;
{
_inner = inner;
}


public bool IsSuccess => _inner.IsSuccess; public bool IsSuccess => _inner.IsSuccess;


Expand Down Expand Up @@ -146,8 +143,7 @@ public bool Equals(Outcome other)
return other.IsError && Error == other.Error; return other.IsError && Error == other.Error;
} }


public override bool Equals(object obj) public override bool Equals(object obj) => (obj is Outcome) && Equals((Outcome)obj);
=> (obj is Outcome) && Equals((Outcome)obj);


public override int GetHashCode() => _error?.GetHashCode() ?? 0; public override int GetHashCode() => _error?.GetHashCode() ?? 0;
} }
Expand Down
13 changes: 3 additions & 10 deletions src/Narvalo.Fx/Applicative/Outcome`1.cs
Expand Up @@ -116,10 +116,7 @@ private sealed class DebugView
{ {
private readonly Outcome<T> _inner; private readonly Outcome<T> _inner;


public DebugView(Outcome<T> inner) public DebugView(Outcome<T> inner) => _inner = inner;
{
_inner = inner;
}


public bool IsSuccess => _inner.IsSuccess; public bool IsSuccess => _inner.IsSuccess;


Expand Down Expand Up @@ -262,14 +259,10 @@ public void OnError(Action<string> action)
#region Publicly hidden methods. #region Publicly hidden methods.


bool Internal.ISecondaryContainer<string>.Contains(string value) bool Internal.ISecondaryContainer<string>.Contains(string value)
{ => throw new NotSupportedException();
throw new NotSupportedException();
}


bool Internal.ISecondaryContainer<string>.Contains(string value, IEqualityComparer<string> comparer) bool Internal.ISecondaryContainer<string>.Contains(string value, IEqualityComparer<string> comparer)
{ => throw new NotSupportedException();
throw new NotSupportedException();
}


// Alias for WhenSuccess(). // Alias for WhenSuccess().
void Internal.IContainer<T>.When(Func<T, bool> predicate, Action<T> action) void Internal.IContainer<T>.When(Func<T, bool> predicate, Action<T> action)
Expand Down
13 changes: 3 additions & 10 deletions src/Narvalo.Fx/Applicative/Result`2.cs
Expand Up @@ -113,10 +113,7 @@ private sealed class DebugView
{ {
private readonly Result<T, TError> _inner; private readonly Result<T, TError> _inner;


public DebugView(Result<T, TError> inner) public DebugView(Result<T, TError> inner) => _inner = inner;
{
_inner = inner;
}


public bool IsSuccess => _inner.IsSuccess; public bool IsSuccess => _inner.IsSuccess;


Expand Down Expand Up @@ -278,14 +275,10 @@ public void OnError(Action<TError> action)
#region Publicly hidden methods. #region Publicly hidden methods.


bool Internal.ISecondaryContainer<TError>.Contains(TError value) bool Internal.ISecondaryContainer<TError>.Contains(TError value)
{ => throw new NotSupportedException();
throw new NotSupportedException();
}


bool Internal.ISecondaryContainer<TError>.Contains(TError value, IEqualityComparer<TError> comparer) bool Internal.ISecondaryContainer<TError>.Contains(TError value, IEqualityComparer<TError> comparer)
{ => throw new NotSupportedException();
throw new NotSupportedException();
}


// Alias for WhenSuccess(). // Alias for WhenSuccess().
void Internal.IContainer<T>.When(Func<T, bool> predicate, Action<T> action) void Internal.IContainer<T>.When(Func<T, bool> predicate, Action<T> action)
Expand Down

0 comments on commit d5fb576

Please sign in to comment.