Archive for the '.NET' Category

2) DLR - Scanner

Thursday, September 11th, 2008

In the first chapter I described common architecture of .NET language compiler. There are usually three parts - scanner, parser and generator of CIL code. But building a language on the DLR slightly differs because instead of CIL code is produced DLR abstract syntax tree (DLR AST).
(more…)

1) DLR - Introduction into implementation of .NET languages

Thursday, August 14th, 2008

I’m not an English native speaker however I decided to write this tutorial in my poor Czech English because there isn’t much information about implementing dynamic languages on the DLR.

Why dynamic languages?

Dynamic languages have more flexible type system and they are more convenient to rapid development. Two examples of dynamic languages are Python and Ruby, both of which are widely used for web development.

(more…)

How to interact Bluetooth from.NET Compact Framework

Thursday, May 22nd, 2008

Bluetooth is wireless technology used mostly in small devices and electronic gadgets like PDA for data transfer between devices. As a common user it is very easy to use Bluetooth and you can fully enjoy all provided features, but from developer’s point of view, it can be quite frustrating to develop applications using Bluetooth in C# or VB.NET, because it isn’t as easy as you would expect.
(more…)

Unit testing events

Friday, February 22nd, 2008

Maybe you have already been in a situation, when you needed to test public events on some tested class. Problem is, how to suspend a test for some time when event calls are expected and how to log if events were really invoked as expected.
Here is my simple solution …
(more…)

Foldr in C#

Sunday, February 10th, 2008

Do you know Foldr? Foldr is a function we can find in most of functional programming languages like Haskell (Foldr) or F# (List.Fold_r). It is useful when you need to process all elements in a list and return just one result value (not list), such as ’sum all numbers in a list’.

Foldr is not part of .NET framework, implementation of this function you can find at the end of this article.

Foldr takes list of elements (list), default value (def) which behaves like a last value of the list and function (fun) which is applied to every two adjacent elements.

form:           Foldr ( list, def, fun )

How does it work? Let’s say you need to sum all elements in the list…

Theory

form:           Foldr (  list,                 def,  fun )
code:           Foldr ( { 1,   2,   3,   4 },  0,    + )
execution:                1 + (2 + (3 + (4 +   0)))      = 10

(more…)

TIP! C# 3.0 recursive functions

Sunday, January 27th, 2008

In C# 3.0 there are some new features such as lambda expressions we can find mostly in a functional languages. But when I tried some functional-like possibilities of version 3.0 I was quite dispapointed because I couldn’t declare recursive functions.

So what was the problem? Lets look at this implementation of factorial function int -> int (takes one int and returns int) using C# 3.0 lambda expressions:

Test 1

Func<int, int> fac = x => x == 0 ? 1 : x + fac(x - 1)

This is an optimal solution which unfortunatelly doesn’t work, we get an error message “Use of unassigned local variable ‘fac’“. We can’t use recursive calls like this, because compiler complains that fac is not declared when used.

Test 2
Lets try to declare fac function one line before:

Func<int, int> fac;
fac = x => x == 0 ? 1 : x + fac(x - 1);

Again, same error “Use of unassigned local variable ‘fac’

Test 3

Func<int, int> fac = null;
fac = x => x == 0 ? 1 : x + fac(x - 1);

Now it works! If you want to use recursive calls inside function you have to declare and assign null before body declaration. However here we can see that C# 3.0 doesn’t allow us to write sexy-one-line code like in a first test case.

TIP! LINQ local variable

Monday, January 7th, 2008

I found a new simple feature in LINQ, if you need localy defined variable in your query then just use let keyword.
Sample - function returning only even double numbers:

  int[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  IEnumerable<int> x = from l in list
                       let dbl = l * l
                       where dbl % 2 == 0
                       select dbl;

          // result => 4, 16, 36, 64, 100

Haskel and F# comparsion

Sunday, January 6th, 2008

This is quick introduction to F# and its syntax for Haskell programmers. In this article, you can find essential operations written in both Haskell and F#. So if you have already some experience with Haskell then it will be easy to get into the secrets of F#.

Basics, Functions, Lists, Types and some others…

(more…)