changeset 33:56de71ae6f7e

Implementation of fmap.
author Kazuma Takeda
date Sat, 28 Jan 2017 19:12:18 +0900
parents 07318c10b894
children a79781723862
files Main/jungle-main/JungleTreeEditor.cs Main/jungle-main/transaction/DefaultJungleTreeEditor.cs Main/jungle-main/util/DefaultEither.cs Main/jungle-main/util/Either.cs Main/jungle-network/transaction/NetworkDefaultJungleTreeEditor.cs
diffstat 5 files changed, 34 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Main/jungle-main/JungleTreeEditor.cs	Fri Jan 20 07:43:43 2017 +0900
+++ b/Main/jungle-main/JungleTreeEditor.cs	Sat Jan 28 19:12:18 2017 +0900
@@ -5,6 +5,7 @@
 		Either<Error,JungleTreeEditor> addNewChildAt(NodePath path,int pos);
 		Either<Error,JungleTreeEditor> deleteChildAt(NodePath path,int pos);
 		Either<Error,JungleTreeEditor> putAttribute(NodePath path, string key, object value);
+		Either<Error,JungleTreeEditor> putAttribute(NodePath path, object value);
 		Either<Error,JungleTreeEditor> putAttribute(string key, object value);
 		Either<Error,JungleTreeEditor> putAttribute(object value);
 		// add Method put Attribute (path, T?);
--- a/Main/jungle-main/transaction/DefaultJungleTreeEditor.cs	Fri Jan 20 07:43:43 2017 +0900
+++ b/Main/jungle-main/transaction/DefaultJungleTreeEditor.cs	Sat Jan 28 19:12:18 2017 +0900
@@ -50,6 +50,7 @@
 
 			JungleTreeEditor newEditor = new DefaultJungleTreeEditor (newNode, txManager, editor, newTreeOpLog);
 			newEditor.SetPrevPath (this.prevPath);
+
 			return DefaultEither<Error, JungleTreeEditor>.newB (newEditor);
 
 		}
@@ -77,6 +78,11 @@
 			return _edit (_path, putAttribute);
 		}
 
+		public Either<Error, JungleTreeEditor> putAttribute(NodePath _path, object _value) {
+			PutAttribute putAttribute = new PutAttribute (_value.ToString(), _value);
+			return _edit (_path, putAttribute);
+		}
+
 		public Either<Error, JungleTreeEditor> putAttribute(string _key, object _value) {
 			PutAttribute putAttribute = new PutAttribute (_key, _value);
 			return _edit (prevPath, putAttribute);
--- a/Main/jungle-main/util/DefaultEither.cs	Fri Jan 20 07:43:43 2017 +0900
+++ b/Main/jungle-main/util/DefaultEither.cs	Sat Jan 28 19:12:18 2017 +0900
@@ -3,7 +3,17 @@
 	private A theA;
 	private B theB;
 
-	private DefaultEither(A _theA, B _theB){
+	public void SetB (B b) {
+		this.theB = b;
+	}
+
+	public void SetA (A a) {
+		this.theA = a;
+	}
+
+	public delegate B Func (B b);
+
+	public DefaultEither(A _theA, B _theB){
 		theA = _theA;
 		theB = _theB;
 	}
@@ -41,4 +51,12 @@
 		return theB != null;
 	}
 
+	public Either<A, B> fmap(System.Func<B, B> f, Either<A,B> e) {
+		if (e.isA ()) {
+			return e;
+		}
+		this.SetB (f (e.b()));
+		return this;
+	}
+
 }
--- a/Main/jungle-main/util/Either.cs	Fri Jan 20 07:43:43 2017 +0900
+++ b/Main/jungle-main/util/Either.cs	Sat Jan 28 19:12:18 2017 +0900
@@ -1,7 +1,8 @@
 
-public interface Either<A,B> {	
+public interface Either<A,B> {
 	A a();
 	bool isA();
 	B b();
 	bool isB();
+	Either<A, B> fmap (System.Func<B, B> f, Either<A, B> e);
 }
--- a/Main/jungle-network/transaction/NetworkDefaultJungleTreeEditor.cs	Fri Jan 20 07:43:43 2017 +0900
+++ b/Main/jungle-network/transaction/NetworkDefaultJungleTreeEditor.cs	Sat Jan 28 19:12:18 2017 +0900
@@ -103,6 +103,12 @@
 			return this.TreeEditor(_path, putAttribute);
 		}
 
+		public Either<Error,JungleTreeEditor> putAttribute(NodePath _path, object _value)
+		{
+			PutAttribute putAttribute = new PutAttribute(_value.ToString(), _value);
+			return this.TreeEditor(_path, putAttribute);
+		}
+
 		public Either<Error,JungleTreeEditor> putAttribute(string _key, object _value)
 		{
 			PutAttribute putAttribute = new PutAttribute(_key, _value);