Permalink
Browse files

Use a ValueTuple instead of StateObject.

  • Loading branch information...
chtoucas committed Mar 9, 2017
1 parent 113b657 commit d5fb57695c9268cc1687256309db1ca69394784e
@@ -27,8 +27,7 @@ public static partial class DictionaryExtensions
if (key == null) { return Maybe<TValue>.None; }
- TValue value;
- return @this.TryGetValue(key, out value) ? Maybe.Of(value) : Maybe<TValue>.None;
+ return @this.TryGetValue(key, out TValue value) ? Maybe.Of(value) : Maybe<TValue>.None;
}
}
}
@@ -12,8 +12,7 @@ internal static class TryParserExtensions
if (value == null) { return null; }
- T result;
- return @this.Invoke(value, out result) ? result : (T?)null;
+ return @this.Invoke(value, out T result) ? result : (T?)null;
}
public static Maybe<T> MayInvoke<T>(this TryParser<T> @this, string value) where T : class
@@ -22,8 +21,7 @@ internal static class TryParserExtensions
if (value == null) { return Maybe<T>.None; }
- T result;
- return @this.Invoke(value, out result) ? Maybe.Of(result) : Maybe<T>.None;
+ return @this.Invoke(value, out T result) ? Maybe.Of(result) : Maybe<T>.None;
}
}
}
@@ -181,7 +181,7 @@ public static string FromBic(string value)
}
else
{
- var retval = value.Substring(StartIndex, Length);
+ string retval = value.Substring(StartIndex, Length);
return CheckContent(retval) ? retval : null;
}
}
@@ -203,7 +203,7 @@ private static class CountryPart
public static string FromBic(string value)
{
Demand.True(value.Length >= StartIndex + Length);
- var retval = value.Substring(StartIndex, Length);
+ string retval = value.Substring(StartIndex, Length);
return CheckContent(retval) ? retval : null;
}
@@ -223,7 +223,7 @@ private static class InstitutionPart
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;
}
@@ -247,7 +247,7 @@ private static class LocationPart
public static string FromBic(string value)
{
Demand.True(value.Length >= StartIndex + Length);
- var retval = value.Substring(StartIndex, Length);
+ string retval = value.Substring(StartIndex, Length);
return CheckContent(retval) ? retval : null;
}
@@ -6,13 +6,15 @@ namespace Narvalo.Applicative
using System.Collections;
using System.Collections.Generic;
+ [Obsolete]
public static class StateObject
{
public static StateObject<T, TState> Create<T, TState>(T value, TState state)
=> new StateObject<T, TState>(value, state);
}
// To be replaced by ValueTuple<T, TState> when available.
+ [Obsolete]
public partial struct StateObject<T, TState> : IEquatable<StateObject<T, TState>>, IStructuralEquatable
{
public StateObject(T result, TState state)
@@ -4,8 +4,8 @@ namespace Narvalo.Applicative
{
using System;
- // Func<TState, StateObject<T, TState>>
- public delegate StateObject<T, TState> Stateful<T, TState>(TState state);
+ // Func<TState, (T, TState)>
+ public delegate (T result, TState state) Stateful<T, TState>(TState state);
// Provides the core Monad methods.
public static partial class Stateful
@@ -15,14 +15,14 @@ public static partial class Stateful
Func<T, Stateful<TNext, TState>> binder)
=> 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.
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)
=> square.Bind(Stubs<Stateful<T, TState>>.Identity);
@@ -32,15 +32,15 @@ public static partial class Stateful
public static partial class Stateful
{
public static Stateful<TState, TState> Get<TState>()
- => state => StateObject.Create(state, state);
+ => state => (state, state);
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)
- => state => StateObject.Create(Unit.Default, func(state));
+ => state => (Unit.Default, func(state));
public static Stateful<TResult, TState> Gets<TState, TResult>(Func<TState, TResult> func)
- => state => StateObject.Create(func(state), state);
+ => state => (func(state), state);
}
}
@@ -6,7 +6,7 @@
// behavior and will be lost if the code is regenerated.
//
// Runtime Version: 4.0.30319.42000
-// Microsoft.VisualStudio.TextTemplating: 14.0
+// Microsoft.VisualStudio.TextTemplating: 15.0
// </auto-generated>
//------------------------------------------------------------------------------
@@ -11,6 +11,9 @@
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<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.Linq" />
</ItemGroup>
@@ -106,6 +109,7 @@
</ItemGroup>
<ItemGroup>
<None Include="Narvalo.Futures.Version.props" />
+ <None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="System.ValueTuple" version="4.3.0" targetFramework="net461" />
+</packages>
@@ -52,16 +52,13 @@ public abstract partial class Either<TLeft, TRight> : IStructuralEquatable, Inte
[DebuggerTypeProxy(typeof(Either<,>.Left_.DebugView))]
private sealed partial class Left_ : Either<TLeft, TRight>, IEquatable<Left_>
{
- public Left_(TLeft value)
- {
- Left = value;
- }
+ public Left_(TLeft value) => Left = value;
public override bool IsLeft => true;
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);
@@ -97,10 +94,7 @@ private sealed class DebugView
{
private readonly Either<TLeft, TRight> _inner;
- public DebugView(Either<TLeft, TRight> inner)
- {
- _inner = inner;
- }
+ public DebugView(Either<TLeft, TRight> inner) => _inner = inner;
public TLeft Left => _inner.Left;
}
@@ -112,22 +106,19 @@ public DebugView(Either<TLeft, TRight> inner)
[DebuggerTypeProxy(typeof(Either<,>.Right_.DebugView))]
private sealed partial class Right_ : Either<TLeft, TRight>, IEquatable<Right_>
{
- public Right_(TRight value)
- {
- Right = value;
- }
+ public Right_(TRight value) => Right = value;
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; }
public override Maybe<TLeft> LeftOrNone() => Maybe<TLeft>.None;
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);
@@ -157,10 +148,7 @@ private sealed class DebugView
{
private readonly Either<TLeft, TRight> _inner;
- public DebugView(Either<TLeft, TRight> inner)
- {
- _inner = inner;
- }
+ public DebugView(Either<TLeft, TRight> inner) => _inner = inner;
public TRight Right => _inner.Right;
}
@@ -188,12 +176,12 @@ private partial class 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_
{
- public override TLeft ToLeft() { throw new InvalidCastException("XXX"); }
+ public override TLeft ToLeft() => throw new InvalidCastException("XXX");
public override TRight ToRight() => Right;
}
@@ -65,10 +65,7 @@ private sealed class DebugView
{
private readonly Fallible _inner;
- public DebugView(Fallible inner)
- {
- _inner = inner;
- }
+ public DebugView(Fallible inner) => _inner = inner;
public bool IsSuccess => _inner.IsSuccess;
@@ -115,10 +115,7 @@ private sealed class DebugView
{
private readonly Maybe<T> _inner;
- public DebugView(Maybe<T> inner)
- {
- _inner = inner;
- }
+ public DebugView(Maybe<T> inner) => _inner = inner;
public bool IsSome => _inner.IsSome;
@@ -49,10 +49,7 @@ private sealed class DebugView
{
private readonly Outcome _inner;
- public DebugView(Outcome inner)
- {
- _inner = inner;
- }
+ public DebugView(Outcome inner) => _inner = inner;
public bool IsSuccess => _inner.IsSuccess;
@@ -146,8 +143,7 @@ public bool Equals(Outcome other)
return other.IsError && Error == other.Error;
}
- public override bool Equals(object obj)
- => (obj is Outcome) && Equals((Outcome)obj);
+ public override bool Equals(object obj) => (obj is Outcome) && Equals((Outcome)obj);
public override int GetHashCode() => _error?.GetHashCode() ?? 0;
}
@@ -116,10 +116,7 @@ private sealed class DebugView
{
private readonly Outcome<T> _inner;
- public DebugView(Outcome<T> inner)
- {
- _inner = inner;
- }
+ public DebugView(Outcome<T> inner) => _inner = inner;
public bool IsSuccess => _inner.IsSuccess;
@@ -262,14 +259,10 @@ public void OnError(Action<string> action)
#region Publicly hidden methods.
bool Internal.ISecondaryContainer<string>.Contains(string value)
- {
- throw new NotSupportedException();
- }
+ => throw new NotSupportedException();
bool Internal.ISecondaryContainer<string>.Contains(string value, IEqualityComparer<string> comparer)
- {
- throw new NotSupportedException();
- }
+ => throw new NotSupportedException();
// Alias for WhenSuccess().
void Internal.IContainer<T>.When(Func<T, bool> predicate, Action<T> action)
@@ -113,10 +113,7 @@ private sealed class DebugView
{
private readonly Result<T, TError> _inner;
- public DebugView(Result<T, TError> inner)
- {
- _inner = inner;
- }
+ public DebugView(Result<T, TError> inner) => _inner = inner;
public bool IsSuccess => _inner.IsSuccess;
@@ -278,14 +275,10 @@ public void OnError(Action<TError> action)
#region Publicly hidden methods.
bool Internal.ISecondaryContainer<TError>.Contains(TError value)
- {
- throw new NotSupportedException();
- }
+ => throw new NotSupportedException();
bool Internal.ISecondaryContainer<TError>.Contains(TError value, IEqualityComparer<TError> comparer)
- {
- throw new NotSupportedException();
- }
+ => throw new NotSupportedException();
// Alias for WhenSuccess().
void Internal.IContainer<T>.When(Func<T, bool> predicate, Action<T> action)
Oops, something went wrong.

0 comments on commit d5fb576

Please sign in to comment.