<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Coding Day</title>
	
	<link>http://www.codingday.com</link>
	<description>Adventures in Computing, Can Erten's blog</description>
	<lastBuildDate>Mon, 01 Jun 2009 22:17:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<creativeCommons:license>http://creativecommons.org/licenses/by-sa/2.0/</creativeCommons:license><image><link>http://www.canerten.com</link><url>http://www.canerten.com/gravatar.jpg</url><title>Can Erten</title></image><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/CanErten" type="application/rss+xml" /><feedburner:emailServiceId>CanErten</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Poker : Programming Problem</title>
		<link>http://feedproxy.google.com/~r/CanErten/~3/Gn3r6L7UTlw/</link>
		<comments>http://www.codingday.com/poker-programming-problem/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 01:19:36 +0000</pubDate>
		<dc:creator>Can</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[54]]></category>
		<category><![CDATA[euler]]></category>
		<category><![CDATA[fsharp]]></category>
		<category><![CDATA[poker]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[projecteuler]]></category>

		<guid isPermaLink="false">http://www.canerten.com/?p=333</guid>
		<description>Although it has been a while since Brian posted the poker problem in his blog, I haven&amp;#8217;t got the chance to look at it until I came across in ProjectEuler Problem 54. It is not the elegant or best solution at all, but just wanted to join the crew and can confirm it works with [...]</description>
			<content:encoded><![CDATA[<p>Although it has been a while since Brian posted the <a title="A programming problem," href="http://lorgonblog.spaces.live.com/Blog/cns!701679AD17B6D310!546.entry" target="_blank">poker problem</a> in his blog, I haven&#8217;t got the chance to look at it until I came across in ProjectEuler <a href="http://projecteuler.net/index.php?section=problems&amp;id=54" target="_blank">Problem 54</a>. It is not the elegant or best solution at all, but just wanted to join the crew and can confirm it works with the problem&#8217;s 1000 games&#8217; dataset.</p>
<p>Hope this helps.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #066; font-weight: bold;">#light</span>
<span style="color: #060; font-style: italic;">(* Problem 54 *)</span>
<span style="color: #06c; font-weight: bold;">type</span> suit <span style="color: #000080;">=</span> <span style="color: #000080;">|</span>Spades <span style="color: #000080;">|</span>Hearts <span style="color: #000080;">|</span>Clubs <span style="color: #000080;">|</span>Diamonds 
<span style="color: #06c; font-weight: bold;">type</span> card<span style="color: #000080;">=</span>  <span style="color: #000080;">|</span>Ace <span style="color: #000080;">=</span> <span style="color: #c6c;">14</span> <span style="color: #000080;">|</span>Two <span style="color: #000080;">=</span> <span style="color: #c6c;">2</span>  <span style="color: #000080;">|</span>Three <span style="color: #000080;">=</span><span style="color: #c6c;">3</span>  <span style="color: #000080;">|</span>Four <span style="color: #000080;">=</span> <span style="color: #c6c;">4</span> <span style="color: #000080;">|</span>Five <span style="color: #000080;">=</span> <span style="color: #c6c;">5</span> <span style="color: #000080;">|</span>Six <span style="color: #000080;">=</span> <span style="color: #c6c;">6</span> 
            <span style="color: #000080;">|</span>Seven <span style="color: #000080;">=</span><span style="color: #c6c;">7</span>  <span style="color: #000080;">|</span>Eight <span style="color: #000080;">=</span> <span style="color: #c6c;">8</span> <span style="color: #000080;">|</span>Nine <span style="color: #000080;">=</span> <span style="color: #c6c;">9</span> <span style="color: #000080;">|</span>Ten <span style="color: #000080;">=</span> <span style="color: #c6c;">10</span> <span style="color: #000080;">|</span>Jack <span style="color: #000080;">=</span> <span style="color: #c6c;">11</span> <span style="color: #000080;">|</span>Queen <span style="color: #000080;">=</span><span style="color: #c6c;">12</span> <span style="color: #000080;">|</span>King  <span style="color: #000080;">=</span><span style="color: #c6c;">13</span>
<span style="color: #06c; font-weight: bold;">type</span> acard <span style="color: #000080;">=</span> <span style="color: #000080;">&#40;</span>card <span style="color: #000080;">*</span> suit<span style="color: #000080;">&#41;</span>  
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> carder x<span style="color: #000080;">:</span>card<span style="color: #000080;">=</span> enum x
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> card_value <span style="color: #000080;">=</span> <span style="color: #06c; font-weight: bold;">function</span>
    <span style="color: #000080;">|</span> <span style="color: #000080;">'</span>A<span style="color: #000080;">'</span> <span style="color: #000080;">-&gt;</span> card<span style="color: #000080;">.</span><span style="color: #505090;">Ace</span>
    <span style="color: #000080;">|</span> <span style="color: #000080;">'</span>K<span style="color: #000080;">'</span> <span style="color: #000080;">-&gt;</span> card<span style="color: #000080;">.</span><span style="color: #505090;">King</span>
    <span style="color: #000080;">|</span> <span style="color: #000080;">'</span>Q<span style="color: #000080;">'</span> <span style="color: #000080;">-&gt;</span> card<span style="color: #000080;">.</span><span style="color: #505090;">Queen</span>
    <span style="color: #000080;">|</span> <span style="color: #000080;">'</span>J<span style="color: #000080;">'</span> <span style="color: #000080;">-&gt;</span> card<span style="color: #000080;">.</span><span style="color: #505090;">Jack</span>  
    <span style="color: #000080;">|</span> <span style="color: #000080;">'</span>T<span style="color: #000080;">'</span> <span style="color: #000080;">-&gt;</span> card<span style="color: #000080;">.</span><span style="color: #505090;">Ten</span>
    <span style="color: #000080;">|</span> c <span style="color: #000080;">-&gt;</span>  carder<span style="color: #000080;">&#40;</span>System<span style="color: #000080;">.</span><span style="color: #505090;">Int32</span><span style="color: #000080;">.</span><span style="color: #505090;">Parse</span><span style="color: #000080;">&#40;</span>c<span style="color: #000080;">.</span><span style="color: #505090;">ToString</span><span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> suit_value <span style="color: #000080;">=</span> <span style="color: #06c; font-weight: bold;">function</span>
    <span style="color: #000080;">|</span> <span style="color: #000080;">'</span>S<span style="color: #000080;">'</span> <span style="color: #000080;">-&gt;</span> Spades
    <span style="color: #000080;">|</span> <span style="color: #000080;">'</span>H<span style="color: #000080;">'</span> <span style="color: #000080;">-&gt;</span> Hearts    
    <span style="color: #000080;">|</span> <span style="color: #000080;">'</span>C<span style="color: #000080;">'</span> <span style="color: #000080;">-&gt;</span> Clubs
    <span style="color: #000080;">|</span> <span style="color: #000080;">'</span>D<span style="color: #000080;">'</span> <span style="color: #000080;">-&gt;</span> Diamonds    
    <span style="color: #000080;">|</span> a <span style="color: #000080;">-&gt;</span> invalid_arg <span style="color: #000080;">&#40;</span>a<span style="color: #000080;">.</span><span style="color: #505090;">ToString</span><span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> Create <span style="color: #000080;">&#40;</span>str<span style="color: #000080;">:</span> string<span style="color: #000080;">&#41;</span> <span style="color: #000080;">:</span>acard <span style="color: #000080;">=</span> <span style="color: #000080;">&#40;</span>card_value str<span style="color: #000080;">.</span><span style="color: #000080;">&#91;</span><span style="color: #c6c;">0</span><span style="color: #000080;">&#93;</span>,suit_value str<span style="color: #000080;">.</span><span style="color: #000080;">&#91;</span><span style="color: #c6c;">1</span><span style="color: #000080;">&#93;</span><span style="color: #000080;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> isstraigh <span style="color: #000080;">&#40;</span>mycards<span style="color: #000080;">:</span>acard list<span style="color: #000080;">&#41;</span>  <span style="color: #000080;">=</span> 
    <span style="color: #06c; font-weight: bold;">let</span> mycards <span style="color: #000080;">=</span> List<span style="color: #000080;">.</span><span style="color: #505090;">sort_by</span> <span style="color: #000080;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> <span style="color: #000080;">&#40;</span>a,b<span style="color: #000080;">&#41;</span> <span style="color: #000080;">-&gt;</span> a,b<span style="color: #000080;">&#41;</span> mycards
    <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> isstr previouscard mycards   <span style="color: #000080;">&#40;</span>straightlist <span style="color: #000080;">:</span> acard list<span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span>
        <span style="color: #06c; font-weight: bold;">if</span> straightlist<span style="color: #000080;">.</span><span style="color: #505090;">Length</span> <span style="color: #000080;">&gt;=</span> <span style="color: #c6c;">5</span> <span style="color: #06c; font-weight: bold;">then</span> straightlist
        <span style="color: #06c; font-weight: bold;">else</span> <span style="color: #06c; font-weight: bold;">match</span> mycards <span style="color: #06c; font-weight: bold;">with</span>  
                <span style="color: #000080;">|</span> cur <span style="color: #000080;">::</span> rest <span style="color: #000080;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> int <span style="color: #000080;">&#40;</span>fst cur<span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span> int <span style="color: #000080;">&#40;</span><span style="color: #000080;">&#40;</span>fst previouscard<span style="color: #000080;">&#41;</span><span style="color: #000080;">&#41;</span> <span style="color: #000080;">+</span> <span style="color: #c6c;">1</span> <span style="color: #06c; font-weight: bold;">then</span> isstr cur rest  <span style="color: #000080;">&#40;</span>cur<span style="color: #000080;">::</span>straightlist<span style="color: #000080;">&#41;</span>
                                 <span style="color: #06c; font-weight: bold;">else</span> isstr cur rest  <span style="color: #000080;">&#91;</span><span style="color: #000080;">&#93;</span>                           
                <span style="color: #000080;">|</span> <span style="color: #06c; font-weight: bold;">_</span> <span style="color: #000080;">-&gt;</span> <span style="color: #000080;">&#91;</span><span style="color: #000080;">&#93;</span>
    <span style="color: #06c; font-weight: bold;">let</span> head <span style="color: #000080;">=</span> List<span style="color: #000080;">.</span><span style="color: #505090;">hd</span> mycards
    isstr head <span style="color: #000080;">&#40;</span>List<span style="color: #000080;">.</span><span style="color: #505090;">tl</span> mycards<span style="color: #000080;">&#41;</span> <span style="color: #000080;">&#91;</span>head<span style="color: #000080;">&#93;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> pairl <span style="color: #000080;">&#40;</span>mycards <span style="color: #000080;">:</span> acard list<span style="color: #000080;">&#41;</span> groupfunction minelementCount <span style="color: #000080;">=</span>
                 mycards <span style="color: #000080;">|&gt;</span> Seq<span style="color: #000080;">.</span><span style="color: #505090;">group_by</span> groupfunction
                         <span style="color: #000080;">|&gt;</span> Seq<span style="color: #000080;">.</span><span style="color: #505090;">filter</span> <span style="color: #000080;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> a <span style="color: #000080;">-&gt;</span> Seq<span style="color: #000080;">.</span><span style="color: #505090;">length</span> <span style="color: #000080;">&#40;</span>snd a<span style="color: #000080;">&#41;</span> <span style="color: #000080;">&gt;=</span> minelementCount<span style="color: #000080;">&#41;</span>
                         <span style="color: #000080;">|&gt;</span> Seq<span style="color: #000080;">.</span><span style="color: #505090;">to_list</span>
                         <span style="color: #000080;">|&gt;</span> List<span style="color: #000080;">.</span><span style="color: #505090;">unzip</span>
&nbsp;
&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> Ranks <span style="color: #000080;">=</span>
    <span style="color: #000080;">|</span> Highest <span style="color: #06c; font-weight: bold;">of</span> card
    <span style="color: #000080;">|</span> Pair <span style="color: #06c; font-weight: bold;">of</span>  card
    <span style="color: #000080;">|</span> TwoPair <span style="color: #06c; font-weight: bold;">of</span> card<span style="color: #000080;">*</span>card
    <span style="color: #000080;">|</span> Three <span style="color: #06c; font-weight: bold;">of</span> card
    <span style="color: #000080;">|</span> Straight <span style="color: #06c; font-weight: bold;">of</span> card
    <span style="color: #000080;">|</span> Flush <span style="color: #06c; font-weight: bold;">of</span> card 
    <span style="color: #000080;">|</span> FullHouse <span style="color: #06c; font-weight: bold;">of</span>  card<span style="color: #000080;">*</span>card
    <span style="color: #000080;">|</span> Four <span style="color: #06c; font-weight: bold;">of</span>  card
    <span style="color: #000080;">|</span> StraightFlush <span style="color: #06c; font-weight: bold;">of</span> card
&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> Player <span style="color: #000080;">=</span> <span style="color: #000080;">|</span>One <span style="color: #000080;">|</span>Two  <span style="color: #000080;">|</span>Noone
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> rank <span style="color: #000080;">&#40;</span>mycards <span style="color: #000080;">:</span>  acard list<span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span>     
    <span style="color: #06c; font-weight: bold;">let</span> traverseL  <span style="color: #000080;">&#40;</span>l <span style="color: #000080;">:'</span>a list<span style="color: #000080;">&#41;</span>  <span style="color: #000080;">=</span> <span style="color: #06c; font-weight: bold;">if</span> not l<span style="color: #000080;">.</span><span style="color: #505090;">IsEmpty</span> <span style="color: #06c; font-weight: bold;">then</span> l  <span style="color: #000080;">|&gt;</span> List<span style="color: #000080;">.</span><span style="color: #505090;">hd</span> <span style="color: #000080;">|&gt;</span> Seq<span style="color: #000080;">.</span><span style="color: #505090;">to_list</span>
                                   <span style="color: #06c; font-weight: bold;">else</span> <span style="color: #000080;">&#91;</span><span style="color: #000080;">&#93;</span>                            
    <span style="color: #06c; font-weight: bold;">let</span> isflush mycards <span style="color: #000080;">=</span> snd <span style="color: #000080;">&#40;</span>pairl mycards snd <span style="color: #c6c;">5</span><span style="color: #000080;">&#41;</span>  <span style="color: #000080;">|&gt;</span> traverseL
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> FofF l <span style="color: #000080;">=</span> traverseL l <span style="color: #000080;">|&gt;</span> List<span style="color: #000080;">.</span><span style="color: #505090;">hd</span> <span style="color: #000080;">|&gt;</span> fst
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> flush <span style="color: #000080;">=</span> isflush mycards
    <span style="color: #06c; font-weight: bold;">let</span> straight <span style="color: #000080;">=</span> isstraigh mycards
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> ispair count<span style="color: #000080;">=</span> pairl mycards fst count                                                             
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> four,fours <span style="color: #000080;">=</span> <span style="color: #06c; font-weight: bold;">let</span> f,s <span style="color: #000080;">=</span> ispair <span style="color: #c6c;">4</span> 
                     f,s<span style="color: #000080;">|&gt;</span> traverseL
    <span style="color: #06c; font-weight: bold;">let</span> three,threes <span style="color: #000080;">=</span> ispair <span style="color: #c6c;">3</span> 
    <span style="color: #06c; font-weight: bold;">let</span> two,twos <span style="color: #000080;">=</span> ispair <span style="color: #c6c;">2</span>
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> maxcard c <span style="color: #000080;">=</span> fst <span style="color: #000080;">&#40;</span>List<span style="color: #000080;">.</span><span style="color: #505090;">max</span> c<span style="color: #000080;">&#41;</span>
&nbsp;
    <span style="color: #06c; font-weight: bold;">if</span> not flush<span style="color: #000080;">.</span><span style="color: #505090;">IsEmpty</span> <span style="color: #000080;">&amp;&amp;</span> not straight<span style="color: #000080;">.</span><span style="color: #505090;">IsEmpty</span> <span style="color: #06c; font-weight: bold;">then</span> StraightFlush<span style="color: #000080;">&#40;</span>maxcard flush<span style="color: #000080;">&#41;</span>,flush
    <span style="color: #06c; font-weight: bold;">elif</span>  not fours<span style="color: #000080;">.</span><span style="color: #505090;">IsEmpty</span> <span style="color: #06c; font-weight: bold;">then</span> Four<span style="color: #000080;">&#40;</span>four<span style="color: #000080;">.</span><span style="color: #505090;">Head</span><span style="color: #000080;">&#41;</span>,fours
    <span style="color: #06c; font-weight: bold;">elif</span> not threes<span style="color: #000080;">.</span><span style="color: #505090;">IsEmpty</span> <span style="color: #000080;">&amp;&amp;</span> not twos<span style="color: #000080;">.</span><span style="color: #505090;">IsEmpty</span> <span style="color: #000080;">&amp;&amp;</span> FofF twos <span style="color: #000080;">&lt;&gt;</span> FofF threes <span style="color: #06c; font-weight: bold;">then</span> FullHouse<span style="color: #000080;">&#40;</span>two<span style="color: #000080;">.</span><span style="color: #505090;">Head</span>, three<span style="color: #000080;">.</span><span style="color: #505090;">Head</span><span style="color: #000080;">&#41;</span>, List<span style="color: #000080;">.</span><span style="color: #505090;">append</span> <span style="color: #000080;">&#40;</span>threes <span style="color: #000080;">|&gt;</span> traverseL<span style="color: #000080;">&#41;</span> <span style="color: #000080;">&#40;</span>traverseL twos<span style="color: #000080;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">elif</span> not flush<span style="color: #000080;">.</span><span style="color: #505090;">IsEmpty</span> <span style="color: #06c; font-weight: bold;">then</span> Flush<span style="color: #000080;">&#40;</span>maxcard flush<span style="color: #000080;">&#41;</span>,flush
    <span style="color: #06c; font-weight: bold;">elif</span> not straight<span style="color: #000080;">.</span><span style="color: #505090;">IsEmpty</span> <span style="color: #06c; font-weight: bold;">then</span> Straight<span style="color: #000080;">&#40;</span>maxcard straight<span style="color: #000080;">&#41;</span>,straight
    <span style="color: #06c; font-weight: bold;">elif</span> not three<span style="color: #000080;">.</span><span style="color: #505090;">IsEmpty</span> <span style="color: #06c; font-weight: bold;">then</span> Three<span style="color: #000080;">&#40;</span> three<span style="color: #000080;">.</span><span style="color: #505090;">Head</span><span style="color: #000080;">&#41;</span>, threes <span style="color: #000080;">|&gt;</span> traverseL
    <span style="color: #06c; font-weight: bold;">elif</span> List<span style="color: #000080;">.</span><span style="color: #505090;">length</span> two <span style="color: #000080;">=</span> <span style="color: #c6c;">2</span> <span style="color: #06c; font-weight: bold;">then</span> TwoPair<span style="color: #000080;">&#40;</span>two<span style="color: #000080;">.</span><span style="color: #505090;">Head</span>,two<span style="color: #000080;">.</span><span style="color: #505090;">Tail</span><span style="color: #000080;">.</span><span style="color: #505090;">Head</span><span style="color: #000080;">&#41;</span>,Seq<span style="color: #000080;">.</span><span style="color: #505090;">append</span> <span style="color: #000080;">&#40;</span>twos<span style="color: #000080;">.</span><span style="color: #505090;">Head</span><span style="color: #000080;">&#41;</span> <span style="color: #000080;">&#40;</span>twos<span style="color: #000080;">.</span><span style="color: #505090;">Tail</span><span style="color: #000080;">.</span><span style="color: #505090;">Head</span><span style="color: #000080;">&#41;</span> <span style="color: #000080;">|&gt;</span> Seq<span style="color: #000080;">.</span><span style="color: #505090;">to_list</span>
    <span style="color: #06c; font-weight: bold;">elif</span> not <span style="color: #000080;">&#40;</span>twos <span style="color: #000080;">|&gt;</span> traverseL<span style="color: #000080;">&#41;</span><span style="color: #000080;">.</span><span style="color: #505090;">IsEmpty</span> <span style="color: #06c; font-weight: bold;">then</span> Pair<span style="color: #000080;">&#40;</span>two<span style="color: #000080;">.</span><span style="color: #505090;">Head</span><span style="color: #000080;">&#41;</span>, twos <span style="color: #000080;">|&gt;</span> traverseL
    <span style="color: #06c; font-weight: bold;">else</span> Highest<span style="color: #000080;">&#40;</span>maxcard mycards<span style="color: #000080;">&#41;</span>, <span style="color: #000080;">&#91;</span>List<span style="color: #000080;">.</span><span style="color: #505090;">max</span> mycards<span style="color: #000080;">&#93;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> play input <span style="color: #000080;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> convert <span style="color: #000080;">&#40;</span>line<span style="color: #000080;">:</span> string<span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #06c; font-weight: bold;">let</span> l <span style="color: #000080;">=</span> line<span style="color: #000080;">.</span><span style="color: #505090;">Split</span><span style="color: #000080;">&#40;</span><span style="color: #000080;">&#91;</span><span style="color: #000080;">|'</span> <span style="color: #000080;">'|</span><span style="color: #000080;">&#93;</span><span style="color: #000080;">&#41;</span>                                  
                                 <span style="color: #000080;">&#91;</span><span style="color: #000080;">|</span><span style="color: #000080;">&#91;</span><span style="color: #06c; font-weight: bold;">for</span> j <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #c6c;">0</span> <span style="color: #000080;">..</span> <span style="color: #c6c;">4</span> <span style="color: #06c; font-weight: bold;">do</span> <span style="color: #06c; font-weight: bold;">yield</span> Create<span style="color: #000080;">&#40;</span> l<span style="color: #000080;">.</span><span style="color: #000080;">&#91;</span>j<span style="color: #000080;">&#93;</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&#93;</span><span style="color: #000080;">;</span> <span style="color: #000080;">&#91;</span><span style="color: #06c; font-weight: bold;">for</span> j <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #c6c;">5</span> <span style="color: #000080;">..</span> <span style="color: #c6c;">9</span> <span style="color: #06c; font-weight: bold;">do</span> <span style="color: #06c; font-weight: bold;">yield</span> Create<span style="color: #000080;">&#40;</span> l<span style="color: #000080;">.</span><span style="color: #000080;">&#91;</span>j<span style="color: #000080;">&#93;</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&#93;</span><span style="color: #000080;">|</span><span style="color: #000080;">&#93;</span>                                                                  
    <span style="color: #06c; font-weight: bold;">let</span> playercrds <span style="color: #000080;">=</span> convert input              
    <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> iswinner pcards<span style="color: #000080;">=</span>               
                <span style="color: #06c; font-weight: bold;">let</span> ranks,rankcards <span style="color: #000080;">=</span> pcards <span style="color: #000080;">|&gt;</span> Array<span style="color: #000080;">.</span><span style="color: #505090;">map</span> <span style="color: #000080;">&#40;</span>rank<span style="color: #000080;">&#41;</span>  <span style="color: #000080;">|&gt;</span> Array<span style="color: #000080;">.</span><span style="color: #505090;">unzip</span>
&nbsp;
                <span style="color: #06c; font-weight: bold;">let</span> removecards <span style="color: #000080;">&#40;</span>mainlist<span style="color: #000080;">&#41;</span> <span style="color: #000080;">&#40;</span>toberemoved<span style="color: #000080;">&#41;</span>  <span style="color: #000080;">=</span>                         
                        mainlist <span style="color: #000080;">|&gt;</span> Array<span style="color: #000080;">.</span><span style="color: #505090;">map2</span> <span style="color: #000080;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> rem main<span style="color: #000080;">-&gt;</span> main <span style="color: #000080;">|&gt;</span> List<span style="color: #000080;">.</span><span style="color: #505090;">filter</span> <span style="color: #000080;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> c<span style="color: #000080;">-&gt;</span>
                                List<span style="color: #000080;">.</span><span style="color: #505090;">fold_left</span><span style="color: #000080;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> ac x<span style="color: #000080;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> x <span style="color: #000080;">=</span> c <span style="color: #06c; font-weight: bold;">then</span> ac <span style="color: #000080;">&amp;&amp;</span> <span style="color: #06c; font-weight: bold;">false</span> <span style="color: #06c; font-weight: bold;">else</span> ac <span style="color: #000080;">&amp;&amp;</span> <span style="color: #06c; font-weight: bold;">true</span><span style="color: #000080;">&#41;</span> <span style="color: #06c; font-weight: bold;">true</span> rem<span style="color: #000080;">&#41;</span><span style="color: #000080;">&#41;</span> toberemoved
&nbsp;
                <span style="color: #06c; font-weight: bold;">if</span> ranks<span style="color: #000080;">.</span><span style="color: #000080;">&#91;</span><span style="color: #c6c;">0</span><span style="color: #000080;">&#93;</span><span style="color: #000080;">&gt;</span>ranks<span style="color: #000080;">.</span><span style="color: #000080;">&#91;</span><span style="color: #c6c;">1</span><span style="color: #000080;">&#93;</span> <span style="color: #06c; font-weight: bold;">then</span> One
                <span style="color: #06c; font-weight: bold;">elif</span> ranks<span style="color: #000080;">.</span><span style="color: #000080;">&#91;</span><span style="color: #c6c;">0</span><span style="color: #000080;">&#93;</span><span style="color: #000080;">&lt;</span>ranks<span style="color: #000080;">.</span><span style="color: #000080;">&#91;</span><span style="color: #c6c;">1</span><span style="color: #000080;">&#93;</span> <span style="color: #06c; font-weight: bold;">then</span> Two
                <span style="color: #06c; font-weight: bold;">else</span> iswinner  <span style="color: #000080;">&#40;</span>removecards  pcards rankcards<span style="color: #000080;">&#41;</span> 
&nbsp;
    iswinner playercrds
&nbsp;
&nbsp;
play  <span style="color: #008080;">&quot;5H 5C 6S 7S KD 2C 3S 8S 8D TD&quot;</span> 
play  <span style="color: #008080;">&quot;5D 8C 9S JS AC 2C 5C 7D 8S QH&quot;</span>
play  <span style="color: #008080;">&quot;2D 9C AS AH AC 3D 6D 7D TD QD&quot;</span>
play  <span style="color: #008080;">&quot;4D 6S 9H QH QC 3D 6D 7H QD QS&quot;</span>  <span style="color: #060; font-style: italic;">// prob pair queens look at the highes</span>
play  <span style="color: #008080;">&quot;2H 2D 4C 4D 4S 3C 3D 3S 9S 9D&quot;</span> 
&nbsp;
play  <span style="color: #008080;">&quot;2H 2D 4C 4D 4S 2H 2D 4C 4D 4S&quot;</span> 
&nbsp;
play  <span style="color: #008080;">&quot;TH 8D 6C 4D 3S TH 8D 6C 4D 4S&quot;</span> 
&nbsp;
&nbsp;
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> rdinput <span style="color: #000080;">=</span>   <span style="color: #06c; font-weight: bold;">use</span> file <span style="color: #000080;">=</span> System<span style="color: #000080;">.</span><span style="color: #505090;">IO</span><span style="color: #000080;">.</span><span style="color: #505090;">File</span><span style="color: #000080;">.</span><span style="color: #505090;">OpenText</span><span style="color: #000080;">&#40;</span><span style="color: #008080;">&quot;poker.txt&quot;</span><span style="color: #000080;">&#41;</span>
                <span style="color: #06c; font-weight: bold;">let</span> p1count <span style="color: #000080;">=</span> ref <span style="color: #c6c;">0</span>  
&nbsp;
                <span style="color: #06c; font-weight: bold;">while</span> not file<span style="color: #000080;">.</span><span style="color: #505090;">EndOfStream</span> <span style="color: #06c; font-weight: bold;">do</span>
                 <span style="color: #06c; font-weight: bold;">if</span> play <span style="color: #000080;">&#40;</span>file<span style="color: #000080;">.</span><span style="color: #505090;">ReadLine</span><span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span> One <span style="color: #06c; font-weight: bold;">then</span> p1count <span style="color: #000080;">:=</span> <span style="color: #000080;">!</span>p1count<span style="color: #000080;">+</span><span style="color: #c6c;">1</span>
&nbsp;
                file<span style="color: #000080;">.</span><span style="color: #505090;">Close</span><span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span>
                <span style="color: #000080;">!</span>p1count</pre></div></div>


<p><a href="http://feedads.g.doubleclick.net/~a/JFdZ62QqH9JElTcJnJf7epy5Gns/0/da"><img src="http://feedads.g.doubleclick.net/~a/JFdZ62QqH9JElTcJnJf7epy5Gns/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/JFdZ62QqH9JElTcJnJf7epy5Gns/1/da"><img src="http://feedads.g.doubleclick.net/~a/JFdZ62QqH9JElTcJnJf7epy5Gns/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/CanErten/~4/Gn3r6L7UTlw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingday.com/poker-programming-problem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.codingday.com/poker-programming-problem/</feedburner:origLink></item>
		<item>
		<title>Multiple Inheritance in C# using Dynamic Features</title>
		<link>http://feedproxy.google.com/~r/CanErten/~3/nyO-YBL3mw4/</link>
		<comments>http://www.codingday.com/multiple-inheritance-in-c-using-dynamic-features/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 16:03:30 +0000</pubDate>
		<dc:creator>Can</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[multiple inheritance]]></category>

		<guid isPermaLink="false">http://www.canerten.com/?p=346</guid>
		<description>Multiple Inheritance is a feature that a class  that can inherit more than one class. Although CLR does not really care about it, it is not possible in the mainstream .NET languages to have multiple inheritance. 
It is arguable that why we would ever need multiple inheritance. I think it is the same paradox [...]</description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Multiple_inheritance">Multiple Inheritance</a> is a feature that a class  that can inherit more than one class. Although CLR does not really care about it, it is not possible in the mainstream .NET languages to have multiple inheritance. </p>
<p>It is arguable that why we would ever need multiple inheritance. I think it is the same paradox as having dynamic types in a statically typed language. I am not going into that debate as it is already decided for almost a decade. This is an attempt on how to achieve the same effect using dynamic types easily.</p>
<p>In C++, multiple inheritance can simply be expressed like this :</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Auto <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Go<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> a<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">virtual</span> ~Auto<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">class</span> Animal <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Go<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">virtual</span> ~Animal<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">class</span> SampleClass <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Auto, <span style="color: #0000ff;">public</span> Animal<span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>	
	SampleClass <span style="color: #000040;">*</span>a <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> SampleClass<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
	<span style="color: #666666;">//a-&gt;Go(5); // Compiler error for ambigouity</span>
	<span style="color: #666666;">//a-&gt;Go(&quot;going&quot;); // Compiler error for missing method</span>
	Animal<span style="color: #000040;">*</span> an <span style="color: #000080;">=</span> <span style="color: #0000ff;">dynamic_cast</span><span style="color: #000080;">&lt;</span>Animal<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>a<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	an<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Go<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
&nbsp;
	Auto<span style="color: #000040;">*</span> at <span style="color: #000080;">=</span> <span style="color: #0000ff;">dynamic_cast</span><span style="color: #000080;">&lt;</span>Auto<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>a<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	at<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Go<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>When C# introduced dynamic type system and shared type system with C# 4, I thought it should be possible to express multiple inheritance using the new syntax. More like a forced Javascript prototypal inheritance instead of classical inheritance. However it turned out that it was even easier than that. It is relying on the runtime to operate on the types underlying with dynamic conversions, simply reflection.</p>
<p>Obviously it is not forced by the compiler, and you wouldn&#8217;t get intellisense but it will behave like a multiple inherited object. To achieve that the dynamic features of C# 4 has been used. </p>
<p>The same implementation in C# using Minherit object that has been created for reflected multiple inheritance.<br />
So in C#, it would look like this :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> <span style="color: #0600FF;">Auto</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Go<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> a<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>            
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Animal
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Go<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>        
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SampleClass <span style="color: #008000;">:</span> Minherit<span style="color: #008000;">&lt;</span><span style="color: #0600FF;">Auto</span>, Animal<span style="color: #008000;">&gt;</span>
    <span style="color: #000000;">&#123;</span>                        
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            dynamic sample <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SampleClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>            
            a.<span style="color: #0000FF;">Go</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// this will succeed at runtime and call the first Go(int)            </span>
            a.<span style="color: #0000FF;">Go</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;going&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// runtime error for missing method</span>
            Animal an <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Animal<span style="color: #000000;">&#41;</span>sample<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">Auto</span> x <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">Auto</span><span style="color: #000000;">&#41;</span>sample<span style="color: #008000;">;</span>
&nbsp;
            an.<span style="color: #0000FF;">Go</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            x.<span style="color: #0000FF;">Go</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">7</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>            
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>It uses dynamic sample that has come with .NET Framework 4 CTP. I actually think that sample will go to mscorlib with the final release in the System namespace</p>
<p>As the code got a bit long, I post it as project zip file if you are interested.<br />
<div class='download-link'>
							<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/cs4trials.zip'><img alt='Download' class='leftalign' src='http://www.codingday.com/wp-content/plugins/dBeautifier/icons/tar.png' /></a>
							<h4>
								<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/cs4trials.zip'> C# 4 Dynamic Multiple Inheritance</a>
							</h4><p>  File Name: cs4trials.zip</p>
						</div></p>
<p>It is possible to create multiple inheritance abstraction as shown below. Here are the selected four methods to display how the GetMember and SetMember is implemented.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> Call<span style="color: #000000;">&#40;</span>CallAction action, <span style="color: #0600FF;">params</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var objMethod <span style="color: #008000;">=</span> GetMember<span style="color: #008000;">&lt;</span>MethodInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span>
                               GetMember<span style="color: #008000;">&lt;</span>MethodInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>base1, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span>
                                   GetMember<span style="color: #008000;">&lt;</span>MethodInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>base2, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>                                                     
&nbsp;
            <span style="color: #FF0000;">object</span> result <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
            <span style="color: #FF0000;">bool</span> executionSuccess <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var item <span style="color: #0600FF;">in</span> objMethod<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">try</span>
                <span style="color: #000000;">&#123;</span>                     
                    result <span style="color: #008000;">=</span> item.<span style="color: #0000FF;">Second</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">First</span>, args<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    executionSuccess <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                    break<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">catch</span> 
                <span style="color: #000000;">&#123;</span>                    
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>executionSuccess<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> result<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> MissingMethodException<span style="color: #000000;">&#40;</span>action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>                    
        <span style="color: #000000;">&#125;</span>
 <span style="color: #0600FF;">private</span> IEnumerable<span style="color: #008000;">&lt;</span>Pair<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">object</span>, T<span style="color: #008000;">&gt;&gt;</span> GetMember<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> obj, <span style="color: #FF0000;">string</span> name<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> from member <span style="color: #0600FF;">in</span> obj.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetMember</span><span style="color: #000000;">&#40;</span>name<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                   select <span style="color: #008000;">new</span> Pair<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">object</span>, T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>obj, member<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> GetMember<span style="color: #000000;">&#40;</span>GetMemberAction action<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var property <span style="color: #008000;">=</span> GetMember<span style="color: #008000;">&lt;</span>PropertyInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span>
                                     GetMember<span style="color: #008000;">&lt;</span>PropertyInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>base1, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span>
                                         GetMember<span style="color: #008000;">&lt;</span>PropertyInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>base2, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FirstOrDefault</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            var field <span style="color: #008000;">=</span> GetMember<span style="color: #008000;">&lt;</span>FieldInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span>
                            GetMember<span style="color: #008000;">&lt;</span>FieldInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>base1, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span>
                                GetMember<span style="color: #008000;">&lt;</span>FieldInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>base2, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FirstOrDefault</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>property <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> property.<span style="color: #0000FF;">Second</span>.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>property.<span style="color: #0000FF;">First</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>field <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> field.<span style="color: #0000FF;">Second</span>.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>field.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> MissingMemberException<span style="color: #000000;">&#40;</span>action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> SetMember<span style="color: #000000;">&#40;</span>SetMemberAction action, <span style="color: #FF0000;">object</span> value<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var properties <span style="color: #008000;">=</span> GetMember<span style="color: #008000;">&lt;</span>PropertyInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span>
                                    GetMember<span style="color: #008000;">&lt;</span>PropertyInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>base1, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span>
                                        GetMember<span style="color: #008000;">&lt;</span>PropertyInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>base2, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            var fields <span style="color: #008000;">=</span> GetMember<span style="color: #008000;">&lt;</span>FieldInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span>
                            GetMember<span style="color: #008000;">&lt;</span>FieldInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>base1, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span>
                                GetMember<span style="color: #008000;">&lt;</span>FieldInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>base2, action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #FF0000;">bool</span> issucceess <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var property <span style="color: #0600FF;">in</span> properties<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">try</span>
                <span style="color: #000000;">&#123;</span>
                    property.<span style="color: #0000FF;">Second</span>.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>property.<span style="color: #0000FF;">First</span>, value, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    issucceess <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                    break<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>             
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>issucceess<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var field <span style="color: #0600FF;">in</span> fields<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">try</span>
                    <span style="color: #000000;">&#123;</span>
                        field.<span style="color: #0000FF;">Second</span>.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>field.<span style="color: #0000FF;">First</span>, value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                        issucceess <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                        break<span style="color: #008000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                    <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>issucceess<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> MissingMemberException<span style="color: #000000;">&#40;</span>action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span></pre></div></div>


<p><a href="http://feedads.g.doubleclick.net/~a/1Tu7_GE5ohjMA2Vj4bYnGM9Dz1Y/0/da"><img src="http://feedads.g.doubleclick.net/~a/1Tu7_GE5ohjMA2Vj4bYnGM9Dz1Y/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/1Tu7_GE5ohjMA2Vj4bYnGM9Dz1Y/1/da"><img src="http://feedads.g.doubleclick.net/~a/1Tu7_GE5ohjMA2Vj4bYnGM9Dz1Y/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/CanErten/~4/nyO-YBL3mw4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingday.com/multiple-inheritance-in-c-using-dynamic-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.codingday.com/multiple-inheritance-in-c-using-dynamic-features/</feedburner:origLink></item>
		<item>
		<title>Eclipse Reference Project Management with Maven</title>
		<link>http://feedproxy.google.com/~r/CanErten/~3/hby0x8UZvew/</link>
		<comments>http://www.codingday.com/eclipse-reference-project-management-with-maven/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 00:50:33 +0000</pubDate>
		<dc:creator>Can</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://www.canerten.com/eclipse-reference-project-management-with-maven/</guid>
		<description>I was using Eclipse for one for the previous projects as a development environment. It has some powerful features. But I have to say not as powerful as Visual Studio. Anyway since it is possibly the best IDE for Java development it was my choice on the day of implementation. 
The project that I was [...]</description>
			<content:encoded><![CDATA[<p><img style="margin: 0px 5px 5px 0px" height="75" src="http://assets.devx.com/articleicons/6601.gif" width="75" align="left" border="0" />I was using <a href="http://www.eclipse.org/" target="_blank">Eclipse</a> for one for the previous projects as a development environment. It has some powerful features. But I have to say not as powerful as Visual Studio. Anyway since it is possibly the best IDE for Java development it was my choice on the day of implementation. </p>
<p>The project that I was working was done using Borland’s <a href="http://www.codegear.com/products/jbuilder" target="_blank">Jbuilder</a>. Unfortunately that IDE doesn’t exist anymore as it is discontinued by a new company. Thanks to Eclipse import project wizard, the project migration wasn&#8217;t difficult at all. </p>
<p>I just opened the project XML and the project was ready to be working. However, when using with different big open source libraries, Eclipse has a problem in managing them. Actually I have a problem in managing them, because I need to find the required version of each dependency and so on. It is the difficulty of package and build management of eclipse. </p>
<p>It is often difficult to reference a library and get all the dependencies on Eclipse. Somehow it doesn&#8217;t warn at compile time as well and you get exception at runtime which is not nice enough. </p>
<p>Than I found a project called <a href="http://maven.apache.org/" target="_blank">Maven</a> for doing proper build systems. What it does is has an online repository that have all the dependencies filed and it downloads the required assemblies for your project. So you don&#8217;t have download each package separately and link to project. All the best it is very well integrated with eclipse with its marvelous plug-in. </p>
<p>You load the plug-in and tell maven to add a reference to your project. I know Java programmers call this as something else but since I&#8217;m mainly a .NET programmer I hope somebody will find it useful. </p>
<p>Anyway, it stores the configuration in XML file that you can tweak it later if needed. All the updates to the references can happen automatically as well which is quite nice. </p>

<p><a href="http://feedads.g.doubleclick.net/~a/2MtM5WPSnK4clFzCNcnDqmdYeFo/0/da"><img src="http://feedads.g.doubleclick.net/~a/2MtM5WPSnK4clFzCNcnDqmdYeFo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/2MtM5WPSnK4clFzCNcnDqmdYeFo/1/da"><img src="http://feedads.g.doubleclick.net/~a/2MtM5WPSnK4clFzCNcnDqmdYeFo/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/CanErten/~4/hby0x8UZvew" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingday.com/eclipse-reference-project-management-with-maven/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.codingday.com/eclipse-reference-project-management-with-maven/</feedburner:origLink></item>
		<item>
		<title>Next Generation Cryptography (CNG) for .NET</title>
		<link>http://feedproxy.google.com/~r/CanErten/~3/QCBV9Y_vz0M/</link>
		<comments>http://www.codingday.com/next-generation-cryptography-cng-for-net/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 00:23:33 +0000</pubDate>
		<dc:creator>Can</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.canerten.com/next-generation-cryptography-cng-for-net/</guid>
		<description>Last year, Windows Kernel team released a new cryptography libraries called Next Generation Cryptography (CNG). Although it is only available on Windows Vista and Windows Server 2008 kernels so far, I think it is going to replace the CAPICOM with its powerful new implementations.
I quite liked the new CNG libraries as it gets over some [...]</description>
			<content:encoded><![CDATA[<p><img style="margin: 0px 5px 5px 0px" src="http://static.flickr.com/2308/2453544236_f1b1dac91d_t.jpg" border="0" alt="Droster Lock (Infinite Combination)" align="left" />Last year, Windows Kernel team released a new cryptography libraries called <a href="http://msdn.microsoft.com/en-us/library/aa376210(VS.85).aspx" target="_blank">Next Generation Cryptography (CNG).</a> Although it is only available on Windows Vista and Windows Server 2008 kernels so far, I think it is going to replace the <a href="http://msdn.microsoft.com/en-us/library/ms995332.aspx" target="_blank">CAPICOM</a> with its powerful new implementations.</p>
<p>I quite liked the new CNG libraries as it gets over some limitations of the previous library.</p>
<p>CLR security team released a new project on codeplex called <a href="http://www.codeplex.com/clrsecurity" target="_blank">CLR Security</a> for that new cryptography library on .NET. So it is possible to benefit from those libraries in managed worlds as well. For a detailed description be sure to check <a href="http://blogs.msdn.com/shawnfa/archive/2008/07/10/clr-security-team-codeplex-site.aspx" target="_blank">security blog</a>.</p>
<p>Also, the team is very quick to respond and provide fixes if necessary.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/hX-db4VGqZveyhJlh1CGPS2oeS4/0/da"><img src="http://feedads.g.doubleclick.net/~a/hX-db4VGqZveyhJlh1CGPS2oeS4/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/hX-db4VGqZveyhJlh1CGPS2oeS4/1/da"><img src="http://feedads.g.doubleclick.net/~a/hX-db4VGqZveyhJlh1CGPS2oeS4/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/CanErten/~4/QCBV9Y_vz0M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingday.com/next-generation-cryptography-cng-for-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.codingday.com/next-generation-cryptography-cng-for-net/</feedburner:origLink></item>
		<item>
		<title>Recent Titles – Common Lisp ebook for free</title>
		<link>http://feedproxy.google.com/~r/CanErten/~3/KBRBdQm0EgQ/</link>
		<comments>http://www.codingday.com/recent-titles-common-lisp-ebook-for-free/#comments</comments>
		<pubDate>Sat, 19 Jul 2008 23:43:46 +0000</pubDate>
		<dc:creator>Can</dc:creator>
				<category><![CDATA[Book]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[ebook]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://www.canerten.com/recent-titles-common-lisp-ebook-for-free/</guid>
		<description>I was thinking of reading a Lisp book. I checked the titles and decided to buy,   luckily, it&amp;#8217;s freely provided in the Apress’ Practical Common Lisp page.
I doubt that it is done by mistake, because the e-book price is also indicated but anyway there is a free link to download it.</description>
			<content:encoded><![CDATA[<p><img title="lisp" style="border-right: 0px; border-top: 0px; margin: 0px 5px; border-left: 0px; border-bottom: 0px" height="297" alt="lisp" src="http://www.codingday.com/wp-content/uploads/2008/07/lisp3.jpg" width="204" align="left" border="0" /></p>
<p>I was thinking of reading a Lisp book. I checked the titles and decided to buy,   <br />luckily, it&#8217;s freely provided in the <a href="http://www.apress.com/book/view/9781590592397" target="_blank">Apress’ Practical Common Lisp page</a>.</p>
<p>I doubt that it is done by mistake, because the e-book price is also indicated but anyway there is a free link to download it.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/YTFRhcOgMkvZii92CfiISeK53DM/0/da"><img src="http://feedads.g.doubleclick.net/~a/YTFRhcOgMkvZii92CfiISeK53DM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/YTFRhcOgMkvZii92CfiISeK53DM/1/da"><img src="http://feedads.g.doubleclick.net/~a/YTFRhcOgMkvZii92CfiISeK53DM/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/CanErten/~4/KBRBdQm0EgQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingday.com/recent-titles-common-lisp-ebook-for-free/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.codingday.com/recent-titles-common-lisp-ebook-for-free/</feedburner:origLink></item>
		<item>
		<title>String Stream for .NET</title>
		<link>http://feedproxy.google.com/~r/CanErten/~3/uJTtkbX4qfg/</link>
		<comments>http://www.codingday.com/string-stream-for-net/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 01:11:17 +0000</pubDate>
		<dc:creator>Can</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Stream]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[StringBuilder]]></category>

		<guid isPermaLink="false">http://www.canerten.com/?p=283</guid>
		<description>I was working with C++ Standard Template Library (STL) library and when using the StringStream class I felt like we need this for .NET   Yes we have a great class StringBuilder but we can&amp;#8217;t use it as a Stream (because it is not). 
Most of the cryptographic classes works with the stream object. [...]</description>
			<content:encoded><![CDATA[<p>I was working with C++ Standard Template Library (<a href="http://www.sgi.com/tech/stl/" target="_blank">STL</a>) library and when using the <a href="http://www.cplusplus.com/reference/iostream/stringstream/stringstream.html" target="_blank">StringStream</a> class I felt like we need this for .NET <img src='http://www.codingday.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Yes we have a great class StringBuilder but we can&#8217;t use it as a Stream (because it is not). </p>
<p>Most of the cryptographic classes works with the stream object. I wanted to save the encrypted information as text. Ideally we could use memoryStream but it is not really efficient, because it is bits and we need to build a model for serializing/deserializing as text.</p>
<p>String Stream class (as I call it) is just an ugly but functional wrapper around StringBuilder class as shown some snippets below. I just wanted it to use with encryption and decryption a string. So only the functions needed is there which is enough to use. But I doubt it leaks a little, so use with care if needed.</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> StringStream <span style="color: #008000;">:</span> Stream
<span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> StringBuilder strBuilder<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> StringStream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>            strBuilder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> StringStream<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> str<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>            strBuilder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span>str<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">int</span> Read<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer, <span style="color: #FF0000;">int</span> offset, <span style="color: #FF0000;">int</span> count<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> howMuchRead <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> offset<span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> count<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var actualIndex <span style="color: #008000;">=</span> i <span style="color: #008000;">*</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>                
                howMuchRead <span style="color: #008000;">=</span> i<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>actualIndex <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;=</span> strBuilder.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span> 
                <span style="color: #000000;">&#123;</span>
                    howMuchRead <span style="color: #008000;">=</span> count<span style="color: #008000;">;</span>
                    break<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>                                   
                <span style="color: #FF0000;">string</span> s <span style="color: #008000;">=</span> strBuilder<span style="color: #000000;">&#91;</span>actualIndex<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> strBuilder<span style="color: #000000;">&#91;</span>actualIndex <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                buffer<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToByte</span><span style="color: #000000;">&#40;</span>s,<span style="color: #FF0000;">16</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> howMuchRead<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
      <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Write<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer, <span style="color: #FF0000;">int</span> offset, <span style="color: #FF0000;">int</span> count<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>            
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> offset<span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> count<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                strBuilder.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>buffer<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;x2&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</p>
<p><div class='download-link'>
							<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/StringStream.cs'><img alt='Download' class='leftalign' src='http://www.codingday.com/wp-content/plugins/dBeautifier/icons/downloads.png' /></a>
							<h4>
								<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/StringStream.cs'>StringStream</a>
							</h4><p>  File Name: StringStream.cs</p>
						</div></p>
<p> The main magic is happening on reading and writing. each byte is represented as 2 characters in the string. It is using the helper function Convert.ToByte to make it happen.<br />
In the next post I will use it with a symmetric encryption helper class.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/8-OkicKmcuOww427CByFvS7aHCM/0/da"><img src="http://feedads.g.doubleclick.net/~a/8-OkicKmcuOww427CByFvS7aHCM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/8-OkicKmcuOww427CByFvS7aHCM/1/da"><img src="http://feedads.g.doubleclick.net/~a/8-OkicKmcuOww427CByFvS7aHCM/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/CanErten/~4/uJTtkbX4qfg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingday.com/string-stream-for-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.codingday.com/string-stream-for-net/</feedburner:origLink></item>
		<item>
		<title>My Visual Studio Color Scheme, Friendly IDE</title>
		<link>http://feedproxy.google.com/~r/CanErten/~3/WwS2Qdx_ST0/</link>
		<comments>http://www.codingday.com/my-visual-studio-color-scheme-friendly-ide/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 18:26:49 +0000</pubDate>
		<dc:creator>Can</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.canerten.com/my-visual-studio-color-scheme-friendly-ide/</guid>
		<description>After reading interesting posts about the fonts and colors in Visual Studio, I gave a try to some of them. Because I work quite a lot with Visual Studio, I realized that it is actually very important for the eyes and for productivity. Unfortunately none of the themes have pleased my IDE concept, low contrast, [...]</description>
			<content:encoded><![CDATA[<p>After reading interesting posts about the fonts and colors in Visual Studio, I gave a try to some of them. Because I work quite a lot with Visual Studio, I realized that it is actually very important for the eyes and for productivity. Unfortunately none of the themes have pleased my IDE concept, low contrast, dark background, pale colours, clear type fonts, and follow metaphors. </p>
<p>I also put some time to design and experiment with some fonts and colours. The themes that I found were to dark for me with very high contrast, and the fonts were glowing.&#160; </p>
<p>This theme is still dark but with pale colours. More importantly it follows the metaphors we used to, so it will be OK when discussing with developers. What I mean? You know greenish parts of your code are always comments, or red parts of SQL code is bad (strings for dynamic SQL), as we all used to from visual studio, SQL or any popular IDEs.</p>
<p>One more thing is that when working with F# some of the color features are not the same as C#. So this theme is prepared with the F# projects in mind as well. Also HTML, CSS, JavaScript, and&#160; XAML are formatted following my colour preference.</p>
<p>For fonts I have used <a href="http://dejavu.sourceforge.net/wiki/index.php/Main_Page" target="_blank">DejaVu</a> font with size 10. If <a href="http://www.microsoft.com/downloads/details.aspx?familyid=22e69ae4-7e40-4807-8a86-b3d36fab68d3&amp;displaylang=en" target="_blank">Consolas</a> is your favourite font than, it would be better to put on size 9. </p>
<p>Moreover if you use, ReSharper code refactoring tool it has also some colour highlights for it as well. But it is not a problem if you don&#8217;t.</p>
<p><div class='download-link'>
							<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/Friendly_IDE_2008.zip'><img alt='Download' class='leftalign' src='http://www.codingday.com/wp-content/plugins/dBeautifier/icons/tar.png' /></a>
							<h4>
								<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/Friendly_IDE_2008.zip'>Friendly IDE for Visual Studio 2008</a>
							</h4><p>  File Name: Friendly_IDE_2008.zip</p>
						</div><br />
<div class='download-link'>
							<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/Friendly_IDE_2005.zip'><img alt='Download' class='leftalign' src='http://www.codingday.com/wp-content/plugins/dBeautifier/icons/tar.png' /></a>
							<h4>
								<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/Friendly_IDE_2005.zip'>Friendly IDE for Visual Studio 2005</a>
							</h4><p>  File Name: Friendly_IDE_2005.zip</p>
						</div></p>
<p>C# &#8211; F# Scheme</p>
<p><a href="http://www.codingday.com/wp-content/uploads/2008/03/cs.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" border="0" alt="cs" src="http://www.codingday.com/wp-content/uploads/2008/03/cs-thumb.png" width="244" height="193" /></a>&#160;<a href="http://www.codingday.com/wp-content/uploads/2008/03/fs.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" border="0" alt="fs" src="http://www.codingday.com/wp-content/uploads/2008/03/fs-thumb.png" width="244" height="204" /></a>&#160; </p>
<p>ASPX &#8211; HTML &#8211; XML Scheme</p>
<p><a href="http://www.codingday.com/wp-content/uploads/2008/03/aspxhtml.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" border="0" alt="aspxhtml" src="http://www.codingday.com/wp-content/uploads/2008/03/aspxhtml-thumb.png" width="244" height="174" /></a>&#160; <a href="http://www.codingday.com/wp-content/uploads/2008/03/xml.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" border="0" alt="xml" src="http://www.codingday.com/wp-content/uploads/2008/03/xml-thumb.png" width="244" height="202" /></a></p></p>

<p><a href="http://feedads.g.doubleclick.net/~a/H_UIUW0Ty3ttfNWP3Mz-QI5pSUw/0/da"><img src="http://feedads.g.doubleclick.net/~a/H_UIUW0Ty3ttfNWP3Mz-QI5pSUw/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/H_UIUW0Ty3ttfNWP3Mz-QI5pSUw/1/da"><img src="http://feedads.g.doubleclick.net/~a/H_UIUW0Ty3ttfNWP3Mz-QI5pSUw/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/CanErten/~4/WwS2Qdx_ST0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingday.com/my-visual-studio-color-scheme-friendly-ide/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.codingday.com/my-visual-studio-color-scheme-friendly-ide/</feedburner:origLink></item>
		<item>
		<title>Registering Help System for Windows SDK Documentation with Visual Studio</title>
		<link>http://feedproxy.google.com/~r/CanErten/~3/LRy7HeLe6M4/</link>
		<comments>http://www.codingday.com/registering-help-system-for-windows-sdk-with-visual-studio/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 18:07:05 +0000</pubDate>
		<dc:creator>Can</dc:creator>
				<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.canerten.com/registering-help-system-for-windows-sdk-with-visual-studio/</guid>
		<description>It is explained on Windows SDK blog how to integrate the updated Windows SDK with Visual Studio. However, how about the documentation and help integration with Visual Studio or even the search for API references ? I was using the online SDK until I figured out a work around.
Here are the steps to enable the [...]</description>
			<content:encoded><![CDATA[<p>It is explained on <a href="http://blogs.msdn.com/windowssdk/archive/2008/03/01/integrating-windows-sdk-and-vs-with-new-sdk-configuration-tool.aspx" target="_blank">Windows SDK blog</a> how to integrate the updated Windows SDK with Visual Studio. However, how about the documentation and help integration with Visual Studio or even the search for API references ? I was using the <a href="http://msdn2.microsoft.com/en-us/library/cc178931.aspx" target="_blank">online SDK</a> until I figured out a work around.
<p>Here are the steps to enable the updated help collections system for Windows SDK with Visual Studio. So at the end, the internal help function will will work with the updated help if necessary.
<ol>
<li>In Visual Studio , use the menu to navigate to Help -&gt; Index</li>
<li>This will run up Microsoft Document Explorer (and yes it is different than MSDN)</li>
<li>Make sure that the results are unfiltered, in the &#8220;Look for&#8221; field search for &#8220;Collection Manager&#8221;, </li>
<li>On the results tree click to collection manager -&gt; Help</li>
<li>Than select the Microsoft Windows SDK Collection checkbox. You can also have additional help integrated with the document explorer.</li>
<li>Update VSCC.</li>
</ol>
<p>That&#8217;s it. Instead of the chm file it is useful to use from Visual Studio with the search capabilities.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/oeAgNaabGipmEtSo1DAf3Mg-s78/0/da"><img src="http://feedads.g.doubleclick.net/~a/oeAgNaabGipmEtSo1DAf3Mg-s78/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/oeAgNaabGipmEtSo1DAf3Mg-s78/1/da"><img src="http://feedads.g.doubleclick.net/~a/oeAgNaabGipmEtSo1DAf3Mg-s78/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/CanErten/~4/LRy7HeLe6M4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingday.com/registering-help-system-for-windows-sdk-with-visual-studio/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.codingday.com/registering-help-system-for-windows-sdk-with-visual-studio/</feedburner:origLink></item>
		<item>
		<title>Singularity Source Released</title>
		<link>http://feedproxy.google.com/~r/CanErten/~3/BFE1TgH-SV0/</link>
		<comments>http://www.codingday.com/singularity-source-released/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 13:46:58 +0000</pubDate>
		<dc:creator>Can</dc:creator>
				<category><![CDATA[Operating System]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.canerten.com/singularity-source-released/</guid>
		<description>Lots of releases happening this week because of Mix 08. For Mix08 releases you might want to check Tim Sneath&amp;#8217;s blog.
But beside mix conference a research operating system&amp;#160; from Microsoft Research, &amp;#8220;Singularity&amp;#8221; is released. The source code can be found in their CodePlex Singularity site. It comes with a research development license. 
It will be [...]</description>
			<content:encoded><![CDATA[<p>Lots of releases happening this week because of <a href="http://visitmix.com/2008/default.aspx" target="_blank">Mix 08.</a> For Mix08 releases you might want to check <a href="http://blogs.msdn.com/tims/archive/2008/03/05/download-links-for-mix08-announcements.aspx" target="_blank">Tim Sneath&#8217;s blog</a>.</p>
<p>But beside mix conference a research operating system&nbsp; from Microsoft Research, &#8220;Singularity&#8221; is released. The source code can be found in their <a href="http://www.codeplex.com/singularity/" target="_blank">CodePlex Singularity</a> site. It comes with a research development license. </p>
<p>It will be interesting to see how the academia will implement the new ideas in a managed operating system.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/CA8HbYWSTP-rrRmu_Mstx3PfzbE/0/da"><img src="http://feedads.g.doubleclick.net/~a/CA8HbYWSTP-rrRmu_Mstx3PfzbE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/CA8HbYWSTP-rrRmu_Mstx3PfzbE/1/da"><img src="http://feedads.g.doubleclick.net/~a/CA8HbYWSTP-rrRmu_Mstx3PfzbE/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/CanErten/~4/BFE1TgH-SV0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingday.com/singularity-source-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.codingday.com/singularity-source-released/</feedburner:origLink></item>
		<item>
		<title>Expression Trees-Lambdas to CodeDom Conversion</title>
		<link>http://feedproxy.google.com/~r/CanErten/~3/_jMYIPqwl5o/</link>
		<comments>http://www.codingday.com/meta-programming-with-expression-trees-lambdas-to-codedom-conversion/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 00:23:34 +0000</pubDate>
		<dc:creator>Can</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://www.canerten.com/meta-programming-with-expression-trees-lambdas-to-codedom-conversion/</guid>
		<description>Introduction
Some people are working to make the meta-programming possible. Some says as language oriented programming or domain specific language, but I prefer in general as meta-programming. For years programming languages supported to generate code with the powerful libraries or developers worked just with string concatenations and external linkers.
Nowadays meta-programming is getting more and more important [...]</description>
			<content:encoded><![CDATA[<h1>Introduction</h1>
<p>Some people are working to make the meta-programming possible. Some says as language oriented programming or domain specific language, but I prefer in general as meta-programming. For years programming languages supported to generate code with the powerful libraries or developers worked just with string concatenations and external linkers.
<p>Nowadays meta-programming is getting more and more important as the domain expertise required. So the languages make meta programming possible at the compiler level with compiler directives.
<p>Indeed there a lot of ideas coming from functional programming world where everything treated as expressions.&nbsp; The code becomes data and data usage happens in the code. It should sound familiar with LINQ to SQL efforts to make this possible.<br />
<h1>Libraries</h1>
<p>.NET Framework had code generators since the beginning. CodeDom is probably the best known for tree based code generation. Codedom made possible to develop the ASP.NET engine, Windows Form designer, Web form designer, Web services wrapper LINQ entity objects and more. It is used extensively by the framework for the key technologies.
<p>Although there are other APIs in .NET framework such as System.Reflection, System.Reflection.Emit, in this post we will focus on CodeDom and the new comer Expression Trees.
<p>Expression Tree is the key API behind LINQ to SQL or IQueryable interface in general. Every query is expressed as typed trees that is parsed and converted to SQL later by the library.
<p>The syntax of expressing queries is very readable with query comprehension syntax. However sometimes I want to know about the generated tree, like actually which functions are getting involved in the query. I have used <a href="http://blogs.msdn.com/charlie/archive/2008/01/31/expression-tree-basics.aspx" target="_blank">Expression Tree Debugger Visualizer</a> to draw the tree. It is pretty handy tool but for big trees it is difficult to see what is going on. This was my main motivation actually, although we had the code, we don&#8217;t see what&#8217;s the magic going on with query comprehension.<br />
<h1>Implementation</h1>
<p>So the idea is to have the code regenerated from the tree. In the real world this will involve a parser, interpreter and some more compiler theory which requires a lot of research. And because this is just for fun and since we have a powerful CodeDom library to generate code, I tried to convert the expression tree to CodeDom tree. Than used the CodeDom to generate code in any language. Finally I wrote the extension methods so that the debuggers and my code can use it directly from the type.</p>
<p>The compiler generates automatically the expression trees if we use the proper syntax. So from the beginning we have the tree. In order to convert to CodeDom objects, we need to traverse the tree and generate the necessary CodeDom objects. So I wrote a&nbsp; tree walker that generates a CodeDom object to is parent while going to the last children. I didn&#8217;t realise how far it is going but that was it. When the tree walker finished with some more few lines of code the converter was just working. </p>
<p>I would like to put the code here as well but unfortunately it is too long for a blog post, so here are some snippets. Feel free to provide suggestions or bug reports.</p>
<p>
<div class='download-link'>
							<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/Expression_Source.zip'><img alt='Download' class='leftalign' src='http://www.codingday.com/wp-content/plugins/dBeautifier/icons/tar.png' /></a>
							<h4>
								<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/Expression_Source.zip'>Expression Tree to CodeDom Source Code</a>
							</h4><p>  File Name: Expression_Source.zip</p>
						</div><br />
<div class='download-link'>
							<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/ExpressionToCodedom.dll'><img alt='Download' class='leftalign' src='http://www.codingday.com/wp-content/plugins/dBeautifier/icons/downloads.png' /></a>
							<h4>
								<a href='http://www.codingday.com/download.php?file=http://www.codingday.com/downloads/ExpressionToCodedom.dll'>Expression Tree to CodeDom DLL</a>
							</h4><p>  File Name: ExpressionToCodedom.dll</p>
						</div>
</p>
<h1>Example</h1>
<p>The extension methods enables to see the source code of any IQueryable and any Expression. Any of them have a GenerateSourceCodeMethod that gives back a string.</p>
<p><a href='http://www.codingday.com/wp-content/uploads/2008/02/visualizer.jpg' title='Expression Tree to CodeDom Visualizer'><img src='http://www.codingday.com/wp-content/uploads/2008/02/visualizer.thumbnail.jpg' alt='Expression Tree to CodeDom Visualizer' /></a></p>
<p>GenerateSourceCode(); // default C#</p>
<p>GenerateSourceCode(string language); // either cs or vb as input or&nbsp; Fully qualified name of the CodeDomProvider (like Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider) It should be added as a reference to the project if you&#8217;re going to use it. </p>
<p>
Sample program that manipulates the expression trees and usage of CodeDom Converter with &#8220;item.GetCodeDomSource(&#8221;vb&#8221;)&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">int</span> a <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span>, c <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>, d <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
var e1 <span style="color: #008000;">=</span> Expression.<span style="color: #0000FF;">Constant</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var e2 <span style="color: #008000;">=</span> Expression.<span style="color: #0000FF;">And</span><span style="color: #000000;">&#40;</span>e1, e1<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">bool</span><span style="color: #008000;">&gt;&gt;&gt;</span> e3 <span style="color: #008000;">=</span> tbool <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> a <span style="color: #008000;">&lt;</span> b <span style="color: #008000;">&amp;&amp;</span> <span style="color: #FF0000;">8</span> <span style="color: #008000;">&gt;</span> d <span style="color: #008000;">||</span> c <span style="color: #008000;">==</span> d<span style="color: #008000;">;</span>
Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">bool</span><span style="color: #008000;">&gt;&gt;</span> e4 <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> b <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">;</span>
Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>RecordName, <span style="color: #FF0000;">bool</span><span style="color: #008000;">&gt;&gt;</span> e5 <span style="color: #008000;">=</span> rn <span style="color: #008000;">=&gt;</span> rn.<span style="color: #0000FF;">LastName</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;ALFKI&quot;</span><span style="color: #008000;">;</span>
Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>StringBuilder<span style="color: #008000;">&gt;&gt;</span> e6 <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> StringBuilder <span style="color: #000000;">&#123;</span> Capacity <span style="color: #008000;">=</span> <span style="color: #FF0000;">20</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;&gt;</span> e7 <span style="color: #008000;">=</span> word <span style="color: #008000;">=&gt;</span> word <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;hello&quot;</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;yes&quot;</span> <span style="color: #008000;">:</span> <span style="color: #666666;">&quot;no&quot;</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var item <span style="color: #0600FF;">in</span> <span style="color: #008000;">new</span> Expression<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> e1, e2, e3, e4, e5, e6,e7 <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">GetCodeDomSource</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;vb&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Visual Basic Output</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">Namespace Runtime
&nbsp;
    <span style="color: #000080;">Public</span> Class LambdaExpression
&nbsp;
        <span style="color: #000080;">Private</span> LastName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
&nbsp;
        <span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> <span style="color: #000080;">New</span>()
            MyBase.<span style="color: #000080;">New</span>
            Me.Func`2
        <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
        <span style="color: #000080;">Private</span> <span style="color: #000080;">Function</span> Func`2(<span style="color: #000080;">ByVal</span> rn <span style="color: #000080;">As</span> Demo.Program.RecordName) <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
            Return (LastName <span style="color: #000080;">Is</span> <span style="color: #800000;">&quot;ALFKI&quot;</span>)
        <span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
    <span style="color: #000080;">End</span> Class
<span style="color: #000080;">End</span> Namespace</pre></div></div>

<p>C# Output</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> Runtime <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LambdaExpression <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> LastName<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> LambdaExpression<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Func</span>`<span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> Func`<span style="color: #FF0000;">2</span><span style="color: #000000;">&#40;</span>Demo.<span style="color: #0000FF;">Program</span>.<span style="color: #0000FF;">RecordName</span> rn<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>LastName <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;ALFKI&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</p>
<h1>Conclusion</h1>
<p>Codedom is too much C# centric, so it&#8217;s hard to make it available for every language. The difference between Code Statement  and Code Expressions  sometimes  makes it hard to convert from expression trees.  </p>
<p>
On the on the other hand Expression trees are too much LINQ oriented. They are less powerful than CodeDom but more easy to express. In expression trees everything is an expression unlike CodeDom. Some constructs are missing from expression trees like the assignment, but we will probably see the improvements in the expression trees in the future. So it might not be a true DSL or language generator, but sure it is enough to get the most of the databases. </p>
<p>There are some other more powerful meta-programming tools and libraries. F# quotation library supports all the available  full-set language features expressed as quotations. Dynamic Language Runtime is another expression tree like library focussed more on compiler developers. </p>
<p>
Finally this library is not build for runtime code conversion from expression tree to CodeDom, although it is possible. The CodeDom generated code is mainly for debugging to print the source code of the query. It might also be helpful for seeing what is going on under the hood.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/FdQwWBdmy1hR9_R0khxF1kWCJFM/0/da"><img src="http://feedads.g.doubleclick.net/~a/FdQwWBdmy1hR9_R0khxF1kWCJFM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/FdQwWBdmy1hR9_R0khxF1kWCJFM/1/da"><img src="http://feedads.g.doubleclick.net/~a/FdQwWBdmy1hR9_R0khxF1kWCJFM/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/CanErten/~4/_jMYIPqwl5o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codingday.com/meta-programming-with-expression-trees-lambdas-to-codedom-conversion/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.codingday.com/meta-programming-with-expression-trees-lambdas-to-codedom-conversion/</feedburner:origLink></item>
	</channel>
</rss>
