view Main/jungle-main/util/DefaultEither.cs @ 35:f2ea780b3e80

fix
author Kazuma Takeda
date Wed, 22 Feb 2017 16:30:19 +0900
parents a79781723862
children
line wrap: on
line source


namespace JungleDB {
	public class DefaultEither<A,B> : Either<A,B> {
		private A theA;
		private B theB;

		public DefaultEither(A _theA, B _theB){
			theA = _theA;
			theB = _theB;
		}

		public static DefaultEither<A,B> newA(A _theA)
		{
			return new DefaultEither<A,B>(_theA,default(B));
		}

		public static DefaultEither<A,B> newB(B _theB)
		{
			return new DefaultEither<A,B>(default(A),_theB);
		}
			
		public A a()
		{
			return theA;
		}


		public bool isA()
		{
			return theA != null;
		}


		public B b()
		{
			return theB;
		}


		public bool isB()
		{
			return theB != null;
		}

		public Either<A, B> fmap(System.Func<B, B> f) {
			if (isA ()) {
				return this;
			}
			return newB(f(b()));
		}

		public Either<A, B> bind (System.Func<B, Either<A, B>> f) {
			if (isA ()) {
				return this;
			}

			return f (b ());
		}
	}
}