Skip to content

Commit

Permalink
More null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
louthy committed Feb 25, 2015
1 parent 7d41e61 commit 5d791bf
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 14 deletions.
Binary file modified CSharpMonad/lib/Monad.dll
Binary file not shown.
23 changes: 23 additions & 0 deletions CSharpMonad/src/EitherLazy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public static L Left<L, R>(this Either<L, R> m)
/// <returns>T</returns>
public static Func<T> Match<R, L, T>(this Either<L, R> m, Func<R, T> Right, Func<L, T> Left)
{
if (Right == null) throw new ArgumentNullException("Right");
if (Left == null) throw new ArgumentNullException("Left");
return () =>
{
var res = m();
Expand All @@ -191,6 +193,7 @@ public static Func<T> Match<R, L, T>(this Either<L, R> m, Func<R, T> Right, Func
/// <returns>T</returns>
public static Func<T> MatchRight<R, L, T>(this Either<L, R> m, Func<R, T> right)
{
if (right == null) throw new ArgumentNullException("right");
return () =>
{
return right(m.Right());
Expand All @@ -206,6 +209,8 @@ public static Func<T> MatchRight<R, L, T>(this Either<L, R> m, Func<R, T> right)
/// <returns>T</returns>
public static Func<T> MatchLeft<R, L, T>(this Either<L, R> m, Func<L, T> left)
{
if (left == null) throw new ArgumentNullException("left");

return () =>
{
return left(m.Left());
Expand All @@ -220,6 +225,8 @@ public static Func<T> MatchLeft<R, L, T>(this Either<L, R> m, Func<L, T> left)
/// <returns>T</returns>
public static Func<T> MatchRight<R, L, T>(this Either<L, R> m, Func<R, T> right, T defaultValue)
{
if (right == null) throw new ArgumentNullException("right");

return () =>
{
var res = m();
Expand All @@ -237,6 +244,8 @@ public static Func<T> MatchRight<R, L, T>(this Either<L, R> m, Func<R, T> right,
/// <returns>T</returns>
public static Func<T> MatchLeft<R, L, T>(this Either<L, R> m, Func<L, T> left, T defaultValue)
{
if (left == null) throw new ArgumentNullException("left");

return () =>
{
var res = m();
Expand All @@ -254,6 +263,9 @@ public static Func<T> MatchLeft<R, L, T>(this Either<L, R> m, Func<L, T> left, T
/// <returns>Unit</returns>
public static Func<Unit> Match<L, R>(this Either<L, R> m, Action<R> Right, Action<L> Left)
{
if (Left == null) throw new ArgumentNullException("Left");
if (Right == null) throw new ArgumentNullException("Right");

return () =>
{
var res = m();
Expand All @@ -274,6 +286,8 @@ public static Func<Unit> Match<L, R>(this Either<L, R> m, Action<R> Right, Actio
/// <returns>Unit</returns>
public static Func<Unit> MatchRight<L, R>(this Either<L, R> m, Action<R> right)
{
if (right == null) throw new ArgumentNullException("right");

return () =>
{
right(m.Right());
Expand All @@ -290,6 +304,8 @@ public static Func<Unit> MatchRight<L, R>(this Either<L, R> m, Action<R> right)
/// <returns>Unit</returns>
public static Func<Unit> MatchLeft<L, R>(this Either<L, R> m, Action<L> left)
{
if (left == null) throw new ArgumentNullException("left");

return () =>
{
left(m.Left());
Expand All @@ -303,6 +319,8 @@ public static Func<Unit> MatchLeft<L, R>(this Either<L, R> m, Action<L> left)
/// </summary>
public static Either<L, R> Mappend<L, R>(this Either<L, R> lhs, Either<L, R> rhs)
{
if (rhs == null) throw new ArgumentNullException("rhs");

return () =>
{
var lhsV = lhs();
Expand Down Expand Up @@ -401,6 +419,8 @@ public static Either<L, UR> Select<L, TR, UR>(
this Either<L, TR> self,
Func<TR, UR> selector)
{
if (selector == null) throw new ArgumentNullException("selector");

return () =>
{
var resT = self();
Expand All @@ -419,6 +439,9 @@ public static Either<L, VR> SelectMany<L, TR, UR, VR>(
Func<TR, Either<L, UR>> selector,
Func<TR, UR, VR> projector)
{
if (selector == null) throw new ArgumentNullException("selector");
if (projector == null) throw new ArgumentNullException("projector");

return () =>
{
var resT = self();
Expand Down
5 changes: 5 additions & 0 deletions CSharpMonad/src/EitherStrict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ public static EitherStrict<L, UR> Select<L, TR, UR>(
this EitherStrict<L, TR> self,
Func<TR, UR> selector)
{
if (selector == null) throw new ArgumentNullException("selector");

if (self.IsLeft)
return EitherStrict.Left<L, UR>(self.Left);

Expand All @@ -542,6 +544,9 @@ public static EitherStrict<L, VR> SelectMany<L, TR, UR, VR>(
Func<TR, EitherStrict<L, UR>> selector,
Func<TR, UR, VR> projector)
{
if (selector == null) throw new ArgumentNullException("selector");
if (projector == null) throw new ArgumentNullException("projector");

if (self.IsLeft)
return EitherStrict.Left<L, VR>(self.Left);

Expand Down
10 changes: 9 additions & 1 deletion CSharpMonad/src/IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static class IOMonadExtensions
/// </summary>
public static IO<R> SelectMany<T, R>(this IO<T> self, Func<T, IO<R>> func)
{
if (func == null) throw new ArgumentNullException("func");
return func(self());
}

Expand All @@ -53,6 +54,7 @@ public static IO<R> SelectMany<T, R>(this IO<T> self, Func<T, IO<R>> func)
/// </summary>
public static IO<U> Select<T,U>(this IO<T> self, Func<T,U> select)
{
if (select == null) throw new ArgumentNullException("select");
return () => select(self());
}

Expand All @@ -64,6 +66,8 @@ public static IO<V> SelectMany<T, U, V>(
Func<T, IO<U>> select,
Func<T, U, V> bind)
{
if (bind == null) throw new ArgumentNullException("bind");
if (select == null) throw new ArgumentNullException("select");
var resT = self();
return () => bind(resT, select(resT)());
}
Expand All @@ -73,14 +77,16 @@ public static IO<V> SelectMany<T, U, V>(
/// </summary>
public static IO<U> Then<T, U>(this IO<T> self, Func<T, U> getValue)
{
return () => getValue( self.Invoke() );
if (getValue == null) throw new ArgumentNullException("getValue");
return () => getValue(self.Invoke());
}

/// <summary>
/// Mappend
/// </summary>
public static IO<T> Mappend<T>(this IO<T> lhs, IO<T> rhs)
{
if (rhs == null) throw new ArgumentNullException("rhs");
return () =>
{
var lhsValue = lhs();
Expand Down Expand Up @@ -188,6 +194,7 @@ public static IO<T> Mempty<T>()

public static IO<T> Return<T>(IO<T> func)
{
if (func == null) throw new ArgumentNullException("func");
return func;
}
}
Expand All @@ -202,6 +209,7 @@ public static class I
{
public static IO<T> O<T>(IO<T> func)
{
if (func == null) throw new ArgumentNullException("func");
return func;
}
}
Expand Down
9 changes: 9 additions & 0 deletions CSharpMonad/src/RWS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static class RWSResult
{
public static RWSResult<W, S, A> Create<W, S, A>(A value, IEnumerable<W> output, S state)
{
if (output == null) throw new ArgumentNullException("output");
return new RWSResult<W, S, A>(value, output, state);
}
}
Expand All @@ -81,6 +82,7 @@ public static RWSResult<W, S, A> Tell<W, S, A>(A a, W w)

public static RWSResult<W, S, A> Tell<W, S, A>(A a, IEnumerable<W> ws)
{
if (ws == null) throw new ArgumentNullException("ws");
return RWSResult.Create<W, S, A>(a, ws, default(S));
}

Expand All @@ -91,6 +93,7 @@ public static RWS<R, W, S, Unit> Tell<R, W, S>(W value)

public static RWS<R, W, S, R> Ask<R, W, S>(Func<R, R> f)
{
if (f == null) throw new ArgumentNullException("f");
return (R r, S s) => RWSResult.Create(f(r), new W[0], s);
}

Expand All @@ -101,6 +104,7 @@ public static RWS<R, W, S, R> Ask<R, W, S>()

public static RWS<R, W, S, S> Get<R, W, S>(Func<S, S> f)
{
if (f == null) throw new ArgumentNullException("f");
return (R r, S s) => RWSResult.Create<W, S, S>(s, new W[0], f(s));
}

Expand All @@ -123,6 +127,7 @@ public static class RWSExt
{
public static RWS<R, W, S, R> Ask<R, W, S, T>(this RWS<R, W, S, T> self, Func<R, R> f)
{
if (f == null) throw new ArgumentNullException("f");
return (R r, S s) => RWSResult.Create(f(r), new W[0], s);
}

Expand All @@ -137,6 +142,7 @@ public static RWS<R, W, S, R> Ask<R, W, S, T>(this RWS<R, W, S, T> self)
public static RWS<R, W, S, U> Select<R, W, S, T, U>(this RWS<R, W, S, T> self, Func<T, U> select)
where S : class
{
if (select == null) throw new ArgumentNullException("select");
return (R r, S s) =>
{
var resT = self(r, s);
Expand All @@ -155,6 +161,9 @@ Func<T, U, V> project
)
where S : class
{
if (bind == null) throw new ArgumentNullException("bind");
if (project == null) throw new ArgumentNullException("project");

return (R r, S s) =>
{
var resT = self(r, s);
Expand Down
5 changes: 5 additions & 0 deletions CSharpMonad/src/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static class Reader

public static Reader<E, E> Ask<E>(Func<E, E> f)
{
if (f == null) throw new ArgumentNullException("f");
return (E env) => f(env);
}

Expand All @@ -66,6 +67,7 @@ public static class ReaderExt
{
public static Reader<E, E> Ask<E, T>(this Reader<E, T> self, Func<E, E> f)
{
if (f == null) throw new ArgumentNullException("f");
return (E env) => f(env);
}

Expand All @@ -79,6 +81,7 @@ public static Reader<E, E> Ask<E, T>(this Reader<E, T> self)
/// </summary>
public static Reader<E, U> Select<E, T, U>(this Reader<E, T> self, Func<T, U> select)
{
if (select == null) throw new ArgumentNullException("select");
return (E env) => select(self(env));
}

Expand All @@ -91,6 +94,8 @@ public static Reader<E, V> SelectMany<E, T, U, V>(
Func<T, U, V> project
)
{
if (bind == null) throw new ArgumentNullException("bind");
if (project == null) throw new ArgumentNullException("project");
return (E env) =>
{
var resT = self(env);
Expand Down
6 changes: 6 additions & 0 deletions CSharpMonad/src/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static class State

public static State<S, S> Get<S>(Func<S, S> f)
{
if (f == null) throw new ArgumentNullException("f");
return (S state) => StateResult.Create<S, S>(state, f(state));
}

Expand Down Expand Up @@ -82,6 +83,7 @@ public static class StateExt
{
public static State<S, A> With<S, A>(this State<S, A> self, Func<S, S> f)
{
if (f == null) throw new ArgumentNullException("f");
return (S state) =>
{
var res = self(state);
Expand All @@ -91,6 +93,7 @@ public static State<S, A> With<S, A>(this State<S, A> self, Func<S, S> f)

public static State<S, U> Select<S, T, U>(this State<S, T> self, Func<T, U> map)
{
if (map == null) throw new ArgumentNullException("map");
return (S state) =>
{
var resT = self(state);
Expand All @@ -104,6 +107,9 @@ public static State<S, V> SelectMany<S, T, U, V>(
Func<T, U, V> project
)
{
if (bind == null) throw new ArgumentNullException("bind");
if (project == null) throw new ArgumentNullException("project");

return (S state) =>
{
var resT = self(state);
Expand Down
20 changes: 20 additions & 0 deletions CSharpMonad/src/Try.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public TryResult(T value)
/// </summary>
public TryResult(Exception e)
{
if (e == null) throw new ArgumentNullException("e");
Exception = e;
Value = default(T);
}
Expand Down Expand Up @@ -141,6 +142,8 @@ public static T Value<T>(this Try<T> self)
/// </summary>
public static Try<U> Select<T, U>(this Try<T> self, Func<T, U> select)
{
if (select == null) throw new ArgumentNullException("select");

return new Try<U>(() =>
{
TryResult<T> resT;
Expand Down Expand Up @@ -178,6 +181,9 @@ public static Try<V> SelectMany<T, U, V>(
Func<T, U, V> bind
)
{
if (select == null) throw new ArgumentNullException("select");
if (bind == null) throw new ArgumentNullException("bind");

return new Try<V>(
() =>
{
Expand Down Expand Up @@ -225,6 +231,8 @@ Func<T, U, V> bind
/// </summary>
public static Try<U> Then<T, U>(this Try<T> self, Func<T, U> getValue)
{
if (getValue == null) throw new ArgumentNullException("getValue");

var resT = self.Try();

return resT.IsFaulted
Expand Down Expand Up @@ -280,6 +288,8 @@ public static IEnumerable<T> AsEnumerableInfinite<T>(this Try<T> self)
/// </summary>
public static Try<T> Mappend<T>(this Try<T> lhs, Try<T> rhs)
{
if (rhs == null) throw new ArgumentNullException("rhs");

return () =>
{
var lhsValue = lhs();
Expand Down Expand Up @@ -355,6 +365,9 @@ public static Try<T> Mconcat<T>(this IEnumerable<Try<T>> ms)
/// </summary>
public static Func<R> Match<T, R>(this Try<T> self, Func<T, R> Success, Func<Exception, R> Fail)
{
if (Success == null) throw new ArgumentNullException("Success");
if (Fail == null) throw new ArgumentNullException("Fail");

return () =>
{
var res = self();
Expand All @@ -369,6 +382,8 @@ public static Func<R> Match<T, R>(this Try<T> self, Func<T, R> Success, Func<Exc
/// </summary>
public static Func<R> Match<T, R>(this Try<T> self, Func<T, R> Success)
{
if (Success == null) throw new ArgumentNullException("Success");

return () =>
{
var res = self();
Expand All @@ -383,6 +398,9 @@ public static Func<R> Match<T, R>(this Try<T> self, Func<T, R> Success)
/// </summary>
public static Func<Unit> Match<T>(this Try<T> self, Action<T> Success, Action<Exception> Fail)
{
if (Success == null) throw new ArgumentNullException("Success");
if (Fail == null) throw new ArgumentNullException("Fail");

return () =>
{
var res = self();
Expand All @@ -401,6 +419,8 @@ public static Func<Unit> Match<T>(this Try<T> self, Action<T> Success, Action<Ex
/// </summary>
public static Func<Unit> Match<T>(this Try<T> self, Action<T> Success)
{
if (Success == null) throw new ArgumentNullException("Success");

return () =>
{
var res = self();
Expand Down
Loading

0 comments on commit 5d791bf

Please sign in to comment.