Sunday, April 24, 2011

FRP and Rx

















The sample program is following:

var obs1 = Observable.FromEvent<TextChangedEventArgs>(textBox1, "TextChanged").Select(x => ((TextBox)x.Sender).Text);
var obs2 = Observable.FromEvent<TextChangedEventArgs>(textBox2, "TextChanged").Select(x => ((TextBox)x.Sender).Text);

var obs3 = obs1.Zip(obs2, (x, y) => x + y);
obs3.Subscribe(x => {
  textBox3.Text = x;
});

I recognize that (x, y) => x + y implies TextBox3.Text = TextBox1.Text + TextBox2.Text and Zip method implies time merging.

Sunday, April 17, 2011

REST Application in Java

I build the web application in REST architecture and Java.
I want to use both struts2 rest plugin and apache wink but I don't know the manner.
For example, I don't know the description for web.xml.
I want to change the filter by file extension but I got an error.
Hmm...

Sunday, April 10, 2011

REST Architecture

I have some questions for REST architecture.

1. If we implement web application (for Web Browsers) in REST architecture and Java, we use Struts2 and REST plugin as the framework. And if we implement the CSV or XML file download function as web services, we use Apache CXF (or Apache Wink ?) as the framework.
And I want to change data format in the file name extension, for example, if you access http://.../persons/list.xml then the download data format is XML and if you access http://.../persons/list.html then the format is HTML. Can we implement that with using both Struts2 REST plugin and Apache CXF(or Apache Wink)?

2. If I realize 1. I design that the resource component returns the abstract model and the presenter component for each format changes the model to the data in each format.
But it can be possible that changing models to a format requires additional information than the abstract model.

Monday, April 4, 2011

What BTS/ITS do I use ?

I think using BTS/ITS for managing to build tools.
I know Trac and Redmine but I used Trac only.
I hear that Redmine has richer functions than Trac.
Is it good that I use Redmine?
Or do I use the other BTS/ITS?

Saturday, March 26, 2011

WPF Memo

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 '.'.

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.

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);
}