Feb 29, 2008

FizzBuzz Object (C#)


class Program
{
static void Main(string[] args)
{
var fizzbuzz = new
{
Case = new[] {
new {
Cond = new Func<int, bool>(n => true),
Proc = new Func<int, string>(n => null)
}
},
Convert = new Func<int, string>(n => null),
Get = new Func<int, int, IEnumerable<string>>((start, count) => null)
};

fizzbuzz = new
{
Case = new[]
{
new {
Cond = new Func<int, bool>(n => n % 15 == 0),
Proc = new Func<int, string>(n => "FizzBuzz")
},
new {
Cond = new Func<int, bool>(n => n % 3 == 0),
Proc = new Func<int, string>(n => "Fizz")
},
new {
Cond = new Func<int, bool>(n => n % 5 == 0),
Proc = new Func<int, string>(n => "Buzz")
},
new {
Cond = new Func<int, bool>(n => true),
Proc = new Func<int, string>(n => n.ToString())
}
},
Convert = new Func<int, string>(n => fizzbuzz.Case.Where(c => c.Cond(n)).First().Proc(n)),
Get = new Func<int, int, IEnumerable<string>>((start, count) => Enumerable.Range(start, count).Select(n => fizzbuzz.Convert(n)))
};

foreach (var x in fizzbuzz.Get(1, 100))
{
Console.Write(x + " ");
}

Console.ReadLine();
}
}



  • It's impossible to access fizzbuzz while initializing of fizzbuzz.

  • It's regarded as same type when an anonymous types have same properties.

  • It's impossible to set lambda expression to a property of anonymous type.

  • It's impossible to use yield keyword in anonymous method.

Feb 28, 2008

"Nabeatsu of the World" program.

It becomes "Aho" if multiples of 3 or numbers including 3.

module Enumerable
def aho(&block)
self.map do |a|
if 100 <= a
a.to_s
elsif block.call(a)
a.to_aho
else
a.to_s
end
end
end
end

class Integer
def to_aho
fig1 = [ "", "iiiichi", "niiii", "saaaan", "siiii", "goooo", "rooooku", "siiiichi", "haaaachi", "kyuuuu" ]
fig10 = [ "", "", "ni", "san", "yon", "go", "roku", "nana", "hachi", "kyu" ]

if self < 10
fig1[self]
elsif 10 <= self
fig10[self / 10] + "-ju-" + fig1[self % 10]
else
n.to_s
end
end
end


(1..100).aho{|a| (a % 3 == 0) || a.to_s.include?("3") }.each{|a| printf("%s ", a) }

Feb 27, 2008

How to make an Expression Tree.

I want to make this lambda expression.

Func mean = (x, y) => ((x + y) / 2);

Console.WriteLine(mean(10, 20));


Use Expression class.

ParameterExpression x = Expression.Parameter(typeof(int), "x");
ParameterExpression y = Expression.Parameter(typeof(int), "y");

ConstantExpression two = Expression.Constant(2, typeof(int));

BinaryExpression add = Expression.Add(x, y);
BinaryExpression body = Expression.Divide(add, two);

Expression<Func<int, int, int>> meanExp =
Expression.Lambda<Func<int, int, int>>(
body,
new ParameterExpression[] { x, y });
Func<int, int, int> mean = meanExp.Compile();

Console.WriteLine(meanExp);
Console.WriteLine(mean(10, 20));

Feb 26, 2008

Lazy Evaluation of Haskell.

From Exercise 1.5 of SICP.

What the result is this Scheme code.

(define (p) (p))

(define (test x y)
(if (= x 0)
0
y))

(test 0 (p))

It's an infinite loop.

The case of Haskell.

p = p

test x y = case x of
0 -> 0
_ -> y

test 0 p

The result is zero.
This is Lazy Evaluation of Haskell.

Feb 25, 2008

Square Roots by Newton's Method.

Square Roots by Newton's Method.

(define (sqrt x)
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess)
(average guess (/ x guess)))
(define (sqrt-iter guess)
(if (good-enough? guess)
guess
(sqrt-iter (improve guess))))
(sqrt-iter 1.0))

"x" is a free variable in the internal definitions.It gets its value from the argument with which the enclosing procedure sqrt is called.This dicipline is called "lexical scoping".

Feb 24, 2008

The SDK of iPod touch.

When I get the SDK of iPod touch?
Which programming language can I use?

Feb 23, 2008

"yield" in JavaScript 1.7

It's possible to use "yield" keyword in JavaScript 1.7.
It requires to specify the version (1.7).

<body>
<script type="text/javascript; version=1.7">

var i = 0;
var gen = (function() { while(1) { yield i++; } })();
alert(gen.next()); // 0
alert(gen.next()); // 1
alert(gen.next()); // 2

</script>
</body>