I made mistakes in developing WPF application.
1. The context menu does not show on the canvas.
→ I needed to set the background of the canvas.
2. The data binding is failed.
→ The binding source must be the property not the field. The binding target must be the dependency properties. See the site.
3. It is failed that the data binding to a property of an object.
→ The binding path is not separated by '/' but is separated by '.'.
Saturday, March 26, 2011
Monday, March 21, 2011
Monad(Kleisli triple) in C#
Monad is very powerful tool for combining functions.
The definition of Monad(Kleisli triple) is following:
C : category
T : C → C : endofunctor
A, B : objects of category C
f : morphism of category C
u : A → TA
and
for any morphism f : A → TB, a morphism f' : TA → TB exists and satisfies:
1. u' = id
2. f = f'* u
3. f : A → TB, g : B → TC, (g' * f)' = g' * f'
I show the example of the validation for given value by C# code.
I prepare the following struct:
public struct Param<A>
{
private A value;
public A Value
{
get
{
if (!IsValid)
throws new Exception();
return this.value;
}
set { this.value = value; }
}
public bool IsValid { get; set; }
}
See Param as endfunctor T.
u : A → TA is defined as Unit : A → Param<A> :
Param<A> Unit(Param<A> a)
{
return new Param<A>() { Value = a, IsValid = true, };
}
f : A → TB i.e. Param<B> Validate(B b) { ... }
A function Validate2 : Param<A> → Param<B> , which is (Validate)' :
Param<B> Validate2(Param<A> pa)
{
return (pa.IsValid
? Validate(pa.Value)
: new Param<B>() { Value = default(B), IsValid = false, });
}
If we define the following function Extend, we obtain Validate2 as Extend(Validate).
Func<Param<A>, Param<B>> Extend(Func<A, Param<B>> f)
{
return (pa => (pa.IsValid
? f(pa.Value)
: new Param<B>() { Value = default(B), IsValid = false, }));
}
I confirm Extend(Unit),
Extend(Unit)(pa) =
return (pa => (pa.IsValid
? new Param<A>() { Value = pa.Value, IsValid = true, }
: new Param<A>() { Value = default(A), IsValid = false, }));
This result pa2 = Extend(Unit)(pa) satisfies :
if pa.IsValid then pa2.IsValid and pa2.Value == pa.Value,
if not pa.IsValid then not pa2.IsValid and pa2.Value throws Exception.
i.e. Extend(Unit) is Id.
For ValidateX : A → TB and ValidateY : B → TC,
Extend(Extend(ValidateY)(ValidateX)) =
return (pa => ((Extend(ValidateY)(ValidateX))(pa)).IsValid
? (Extend(ValidateY)(ValidateX))(pa)
: new Param<C>() { Value = default(C), IsValid = false, }));
=
return (pa => ((Extend(ValidateY)(Extend(ValidateX)(pa))).IsValid
? (Extend(ValidateY)(Extend(ValidateX)(pa))
: new Param<C>() { Value = default(C), IsValid = false, }));
=
Extend(ValidateY)(Extend(ValidateX))
These are equal.
The definition of Monad(Kleisli triple) is following:
C : category
T : C → C : endofunctor
A, B : objects of category C
f : morphism of category C
u : A → TA
and
for any morphism f : A → TB, a morphism f' : TA → TB exists and satisfies:
1. u' = id
2. f = f'* u
3. f : A → TB, g : B → TC, (g' * f)' = g' * f'
I show the example of the validation for given value by C# code.
I prepare the following struct:
public struct Param<A>
{
private A value;
public A Value
{
get
{
if (!IsValid)
throws new Exception();
return this.value;
}
set { this.value = value; }
}
public bool IsValid { get; set; }
}
See Param as endfunctor T.
u : A → TA is defined as Unit : A → Param<A> :
Param<A> Unit(Param<A> a)
{
return new Param<A>() { Value = a, IsValid = true, };
}
f : A → TB i.e. Param<B> Validate(B b) { ... }
A function Validate2 : Param<A> → Param<B> , which is (Validate)' :
Param<B> Validate2(Param<A> pa)
{
return (pa.IsValid
? Validate(pa.Value)
: new Param<B>() { Value = default(B), IsValid = false, });
}
If we define the following function Extend, we obtain Validate2 as Extend(Validate).
Func<Param<A>, Param<B>> Extend(Func<A, Param<B>> f)
{
return (pa => (pa.IsValid
? f(pa.Value)
: new Param<B>() { Value = default(B), IsValid = false, }));
}
I confirm Extend(Unit),
Extend(Unit)(pa) =
return (pa => (pa.IsValid
? new Param<A>() { Value = pa.Value, IsValid = true, }
: new Param<A>() { Value = default(A), IsValid = false, }));
This result pa2 = Extend(Unit)(pa) satisfies :
if pa.IsValid then pa2.IsValid and pa2.Value == pa.Value,
if not pa.IsValid then not pa2.IsValid and pa2.Value throws Exception.
i.e. Extend(Unit) is Id.
For ValidateX : A → TB and ValidateY : B → TC,
Extend(Extend(ValidateY)(ValidateX)) =
return (pa => ((Extend(ValidateY)(ValidateX))(pa)).IsValid
? (Extend(ValidateY)(ValidateX))(pa)
: new Param<C>() { Value = default(C), IsValid = false, }));
=
return (pa => ((Extend(ValidateY)(Extend(ValidateX)(pa))).IsValid
? (Extend(ValidateY)(Extend(ValidateX)(pa))
: new Param<C>() { Value = default(C), IsValid = false, }));
=
Extend(ValidateY)(Extend(ValidateX))
These are equal.
Sunday, March 13, 2011
My coding rule for C# from functional programming
1. Use conditional operator than if statement.
The following code:
if (a == 1) {
obj.X = 10;
}
else {
obj.X = 20;
}
Rewrite following code:
obj.X = (a == 1 ? 10 : 20);
Assignment should be limited.
2. Use Func delegate for modifying the action.
The following code:
int func(int a)
{
return (a == 1 ? 10 : 20);
}
Rewrite following code:
int func(Func<bool> f)
{
return (f() ? 10 : 20);
}
If necessary, make the wrapper function.
ex:
int func(int a)
{
return func(() => a == 1);
}
The following code:
if (a == 1) {
obj.X = 10;
}
else {
obj.X = 20;
}
Rewrite following code:
obj.X = (a == 1 ? 10 : 20);
Assignment should be limited.
2. Use Func delegate for modifying the action.
The following code:
int func(int a)
{
return (a == 1 ? 10 : 20);
}
Rewrite following code:
int func(Func<bool> f)
{
return (f() ? 10 : 20);
}
If necessary, make the wrapper function.
ex:
int func(int a)
{
return func(() => a == 1);
}
Saturday, March 5, 2011
metrics
I knew Sonar, it's a tool for managing code quality.
I often think the architecture of the program how I build well.
I focus the metrics and metrics represent an aspect for the program architecture.
Sonar does not show the coverage of test and Cyclomatic complexity but I think they are useful.
I want to advance software quality using these metrics.
I often think the architecture of the program how I build well.
I focus the metrics and metrics represent an aspect for the program architecture.
Sonar does not show the coverage of test and Cyclomatic complexity but I think they are useful.
I want to advance software quality using these metrics.
Saturday, February 26, 2011
Reactive Programming with Events
I am reading a pdf document in the site.
It's interesting to try to integrate declarative and imperative approaches to reactive programming.
The event processing, asynchronous, concurrent and parallel programming are more and more important. I want to understand many techniques and solve the problems as simple as possible.
It's interesting to try to integrate declarative and imperative approaches to reactive programming.
The event processing, asynchronous, concurrent and parallel programming are more and more important. I want to understand many techniques and solve the problems as simple as possible.
Sunday, February 20, 2011
Functional Reactive Programming(3)
I am studying Functional Reactive Programming(FRP).
FRP has important 2 concepts, those are Behavior and Event, and I tried to implement Behavior and Event in C#.
The result is following :
Where Time is a class and has Beat() static method for returning the instance at regular time intervals.
FRP has important 2 concepts, those are Behavior and Event, and I tried to implement Behavior and Event in C#.
The result is following :
public delegate T Behavior<T>(Time t);
public delegate IEnumerable<Tuple<Time, T>> Event<T>();
public static class FrpFunctions
{
public static Behavior<TResult> DollarStar<T, TResult>(Behavior<Func<T, TResult>> ff, Behavior<T> fb)
{
return new Behavior<TResult>(t => ZipWith(HaskellDollar, ff(t), fb(t)));
}
public static TResult HaskellDollar<T, TResult>(Func<T, TResult> f, T v)
{
return f(v);
}
public static TResult ZipWith<T1, T2, TResult>(Func<T1, T2, TResult> op, T1 arg1, T2 arg2)
{
return op(arg1, arg2);
}
public static Behavior<T> Lift0<T>(T v)
{
return new Behavior<T>(e => v);
}
public static Func<Behavior<T>, Behavior<TResult>> Lift1<T, TResult>(Func<T, TResult> f)
{
return b1 => DollarStar(Lift0(f), b1);
}
public static Func<Behavior<T1>, Behavior<T2>, Behavior<TResult>> Lift2<T1, T2, TResult>(Func<T1, T2, TResult> f)
{
return (b1, b2) => DollarStar(Lift1((T1 x) => (Func<T2, TResult>)(y => f(x, y)))(b1), b2);
}
public static Event<T> Choice<T>(Event<T> fe1, Event<T> fe2)
{
public static Event<T> Choice<T>(Event<T> fe1, Event<T> fe2)
{
return new Event<T>(() => Aux(fe1(), fe2()));
}
public static IEnumerable<Tuple<Time, T>> Aux<T>(IEnumerable<Tuple<Time, T>> e1, IEnumerable<Tuple<Time, T>> e2)
{
var en1 = e1.GetEnumerator();
var en2 = e2.GetEnumerator();
bool b1 = en1.MoveNext();
bool b2 = en2.MoveNext();
while(b1 && b2) {
if (en1.Current.Item1.Value < en2.Current.Item1.Value){
yield return en1.Current;
b1 = en1.MoveNext();
}
else{
yield return en2.Current;
b2 = en1.MoveNext();
}
}
while(b1){
yield return en1.Current;
b1 = en1.MoveNext();
}
while(b2){
yield return en2.Current;
b2 = en1.MoveNext();
}
}
public static Event<Tuple<T1, T2>> Snapshot<T1, T2>(Event<T1> fe, Behavior<T2> fb)
{
return new Event<Tuple<T1, T2>>(() => Aux(fe(), t => fb(t)));
}
public static IEnumerable<Tuple<Time, Tuple<T1, T2>>> Aux<T1, T2>(IEnumerable<Tuple<Time, T1>> e1, Func<Time, T2> e2)
{
foreach (var v in e1) {
yield return Tuple.Create(v.Item1, Tuple.Create(v.Item2, e2(v.Item1)));
}
}
public static Event<Unit> Sharp()
{
return When(new Behavior<bool>(t => t.Value == 1));
}
public static Event<Unit> Sharp2()
{
return When(new Behavior<bool>(t => t.Value >= 1));
}
public static Event<Unit> When(Behavior<bool> fb)
{
return new Event<Unit>(() => Up(t => fb(t)));
}
public static IEnumerable<Tuple<Time, Unit>> Up(Func<Time, bool> e2)
{
foreach (var t in Time.Beat()) {
if (e2(t)) {
yield return Tuple.Create(t, new Unit());
}
}
}
}
Where Time is a class and has Beat() static method for returning the instance at regular time intervals.
Saturday, February 12, 2011
Functional Reactive Programming(2)
I am studying Functional Reactive Programming(FRP).
I understand that FRP has 2 important concepts, they are Behavior and Event.
The behavior is function of time and it is continuous. The event is the list of pair of time and data and it is discrete.
I made a simple picture.
I understand that FRP has 2 important concepts, they are Behavior and Event.
The behavior is function of time and it is continuous. The event is the list of pair of time and data and it is discrete.
I made a simple picture.
Subscribe to:
Posts (Atom)
