<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Felice Pollano Blog - Recipes</title>
    <link>http://www.felicepollano.com/</link>
    <description>The official Fatica Labs Blog!</description>
    <language>en-us</language>
    <copyright>Felice Pollano</copyright>
    <lastBuildDate>Sat, 17 Dec 2011 13:24:25 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>felice@felicepollano.com</managingEditor>
    <webMaster>felice@felicepollano.com</webMaster>
    <item>
      <trackback:ping>http://www.felicepollano.com/Trackback.aspx?guid=9b711bcd-fc88-4925-bc5c-9bb23238d5d9</trackback:ping>
      <pingback:server>http://www.felicepollano.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.felicepollano.com/PermaLink.aspx?guid=9b711bcd-fc88-4925-bc5c-9bb23238d5d9</pingback:target>
      <dc:creator>Felice Pollano</dc:creator>
      <wfw:comment>http://www.felicepollano.com/CommentView.aspx?guid=9b711bcd-fc88-4925-bc5c-9bb23238d5d9</wfw:comment>
      <wfw:commentRss>http://www.felicepollano.com/SyndicationService.asmx/GetEntryCommentsRss?guid=9b711bcd-fc88-4925-bc5c-9bb23238d5d9</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I would like to present here a little argument verification library that does not
require you to type any string for specifying the name of the parameter you are checking.
This lets the library faster to use, not intrusive in the actual method code, and
refactor friendly. As a bonus you can use it by just embedding a single file. We can
see below an example, just to get immediately to the point:
</p>
        <script src="https://bitbucket.org/Felice_Pollano/ncontracts/src/fbae7759ea72/NContracts/Demo.cs?embed=t">
        </script>
        <p>
As we can see, there is no magic string at all. All the argument name are guessed
thanks to the metadata contained in the linq Expression we use. For example the method
at <strong>line 14</strong> if called with a null value will report:
</p>
        <p>
          <font face="Courier New" size="2">Value cannot be null.<br />
Parameter name: arg1</font>
        </p>
        <p>
The same happens to the more complex check we do at line 46, when we write:
</p>
        <p>
          <font face="Courier New" size="2">Contract.Expect(() =&gt; array).Meet(a =&gt; a.Length
&gt; 0 &amp;&amp; a.First() == 0);</font>
        </p>
        <p>
We have a complex predicate do meet, described by a lambda, standing that the input
array should have first element zero, and non zero length. Notice that the name of
the parameter is array, but we need to use another name for the argument of the lambda
( in this case I used ‘a’ ), the library is smart enough to <font style="background-color: #ffff00">understand
that ‘a’ actually refers to array</font>, and the error message will report it correctly
if the condition does not meet. Just to clarify, the message in case of failure would
be:
</p>
        <p>
          <font face="Courier New" size="2">Precondition not verified:((array.First() == 0)
AndAlso (ArrayLength(array) &gt; 1))<br />
Parameter name: array</font>
        </p>
        <p>
Well it is not supposed to be a message to an end real user, it is a programmer friendly
message, but such validation error are supposed to be reported to a developer ( an
end user should not see method validation errors at all, should he ? )
</p>
        <p>
Well Meet is a cutting edge function we can use for complex validations. Out of the
box, for simpler cases we have some functions too, as we can see on the IContract
interface definition:
</p>
        <script src="https://gist.github.com/1490175.js?file=IContract.cs">
        </script>
        <p>
An interesting portion of the codebase proposed is the one renaming the parameter
on the lambda expression, to achieve the reported message reflect the correct offending
parameter. It is not so easy because plain string replacement would not work:we can
have a parameter named ‘a’, seen in any place in the expression string representation
and a plain replacement would resolve in a big mess, furthermore Expressions are immutable.
So I found help on <a href="http://stackoverflow.com" target="_blank">StackOverflow</a>,
and a <a href="http://stackoverflow.com/questions/8540954/changing-parameter-name-in-a-lambdaexpression-just-for-display" target="_blank">reply
to this question solved the problem</a>, let see the “Renamer” at work ( Thanks to <a href="http://stackoverflow.com/users/115650/phil-klein" target="_blank">Phil</a> ):
</p>
        <script src="https://gist.github.com/1490197.js?file=PredicateRewriter.cs">
        </script>
        <p>
Basically is a reusable class that take the new name of the parameter and returns
a copy of the input expression with the (single) argument changed.
</p>
        <p>
To improve the library or just use it, please <a href="https://bitbucket.org/Felice_Pollano/ncontracts/overview" target="_blank">follow/check
out the project on Bitbucket</a>, suggestions and comments are always welcome.
</p>
        <img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=9b711bcd-fc88-4925-bc5c-9bb23238d5d9" />
      </body>
      <title>A single file argument verification library</title>
      <guid isPermaLink="false">http://www.felicepollano.com/PermaLink.aspx?guid=9b711bcd-fc88-4925-bc5c-9bb23238d5d9</guid>
      <link>http://www.felicepollano.com/2011/12/17/ASingleFileArgumentVerificationLibrary.aspx</link>
      <pubDate>Sat, 17 Dec 2011 13:24:25 GMT</pubDate>
      <description>&lt;p&gt;
I would like to present here a little argument verification library that does not
require you to type any string for specifying the name of the parameter you are checking.
This lets the library faster to use, not intrusive in the actual method code, and
refactor friendly. As a bonus you can use it by just embedding a single file. We can
see below an example, just to get immediately to the point:
&lt;/p&gt;
&lt;script src="https://bitbucket.org/Felice_Pollano/ncontracts/src/fbae7759ea72/NContracts/Demo.cs?embed=t"&gt;&lt;/script&gt;
&lt;p&gt;
As we can see, there is no magic string at all. All the argument name are guessed
thanks to the metadata contained in the linq Expression we use. For example the method
at &lt;strong&gt;line 14&lt;/strong&gt; if called with a null value will report:
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New" size="2"&gt;Value cannot be null.&lt;br&gt;
Parameter name: arg1&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
The same happens to the more complex check we do at line 46, when we write:
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New" size="2"&gt;Contract.Expect(() =&amp;gt; array).Meet(a =&amp;gt; a.Length
&amp;gt; 0 &amp;amp;&amp;amp; a.First() == 0);&lt;/font&gt; 
&lt;/p&gt;
&lt;p&gt;
We have a complex predicate do meet, described by a lambda, standing that the input
array should have first element zero, and non zero length. Notice that the name of
the parameter is array, but we need to use another name for the argument of the lambda
( in this case I used ‘a’ ), the library is smart enough to &lt;font style="background-color: #ffff00"&gt;understand
that ‘a’ actually refers to array&lt;/font&gt;, and the error message will report it correctly
if the condition does not meet. Just to clarify, the message in case of failure would
be:
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New" size="2"&gt;Precondition not verified:((array.First() == 0)
AndAlso (ArrayLength(array) &amp;gt; 1))&lt;br&gt;
Parameter name: array&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
Well it is not supposed to be a message to an end real user, it is a programmer friendly
message, but such validation error are supposed to be reported to a developer ( an
end user should not see method validation errors at all, should he ? )
&lt;/p&gt;
&lt;p&gt;
Well Meet is a cutting edge function we can use for complex validations. Out of the
box, for simpler cases we have some functions too, as we can see on the IContract
interface definition:
&lt;/p&gt;
&lt;script src="https://gist.github.com/1490175.js?file=IContract.cs"&gt;&lt;/script&gt;
&lt;p&gt;
An interesting portion of the codebase proposed is the one renaming the parameter
on the lambda expression, to achieve the reported message reflect the correct offending
parameter. It is not so easy because plain string replacement would not work:we can
have a parameter named ‘a’, seen in any place in the expression string representation
and a plain replacement would resolve in a big mess, furthermore Expressions are immutable.
So I found help on &lt;a href="http://stackoverflow.com" target="_blank"&gt;StackOverflow&lt;/a&gt;,
and a &lt;a href="http://stackoverflow.com/questions/8540954/changing-parameter-name-in-a-lambdaexpression-just-for-display" target="_blank"&gt;reply
to this question solved the problem&lt;/a&gt;, let see the “Renamer” at work ( Thanks to &lt;a href="http://stackoverflow.com/users/115650/phil-klein" target="_blank"&gt;Phil&lt;/a&gt; ):
&lt;/p&gt;
&lt;script src="https://gist.github.com/1490197.js?file=PredicateRewriter.cs"&gt;&lt;/script&gt;
&lt;p&gt;
Basically is a reusable class that take the new name of the parameter and returns
a copy of the input expression with the (single) argument changed.
&lt;/p&gt;
&lt;p&gt;
To improve the library or just use it, please &lt;a href="https://bitbucket.org/Felice_Pollano/ncontracts/overview" target="_blank"&gt;follow/check
out the project on Bitbucket&lt;/a&gt;, suggestions and comments are always welcome.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=9b711bcd-fc88-4925-bc5c-9bb23238d5d9" /&gt;</description>
      <comments>http://www.felicepollano.com/CommentView.aspx?guid=9b711bcd-fc88-4925-bc5c-9bb23238d5d9</comments>
      <category>CodeProject</category>
      <category>CSharp</category>
      <category>Linq</category>
      <category>Recipes</category>
    </item>
    <item>
      <trackback:ping>http://www.felicepollano.com/Trackback.aspx?guid=c3c5a1eb-4817-4071-954e-8d39193277de</trackback:ping>
      <pingback:server>http://www.felicepollano.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.felicepollano.com/PermaLink.aspx?guid=c3c5a1eb-4817-4071-954e-8d39193277de</pingback:target>
      <dc:creator>Felice Pollano</dc:creator>
      <wfw:comment>http://www.felicepollano.com/CommentView.aspx?guid=c3c5a1eb-4817-4071-954e-8d39193277de</wfw:comment>
      <wfw:commentRss>http://www.felicepollano.com/SyndicationService.asmx/GetEntryCommentsRss?guid=c3c5a1eb-4817-4071-954e-8d39193277de</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Sometimes happens that we need to roll over all the combinations of elements in multiple
arrays. This operation is called Cartesian Product and is defined as:
</p>
        <p>
          <b>Definition (Cartesian product):</b> Let <b><i>A<sub>1</sub></i>, ..., <i>A</i><sub><i>n</i></sub></b> be <b><i>n</i></b> sets. 
</p>
        <p>
Then the set of all ordered <b><i>n</i></b>-tuples <b>&lt;<i>x<sub>1</sub></i>, ..., <i>x</i><sub><i>n</i></sub>&gt;
,</b> where <b><i>x<sub>i</sub></i><img align="middle" src="http://www.cs.odu.edu/%7Etoida/nerzic/level-a/symbols_sets/in.gif" width="14" height="24" /><i>A<sub>i</sub></i></b> for
all <b><i>i</i></b>, <b><i>1</i><img align="middle" src="http://www.cs.odu.edu/%7Etoida/nerzic/level-a/symbols_sets/leq.gif" width="14" height="24" /><i>i</i><img align="middle" src="http://www.cs.odu.edu/%7Etoida/nerzic/level-a/symbols_sets/leq.gif" width="14" height="24" /><i>n</i> ,</b></p>
        <p>
is called the <b>Cartesian product</b> of <b><i>A<sub>1</sub></i>, ..., <i>A</i><sub><i>n</i></sub>,</b> and
is denoted by <b><i>A<sub>1</sub></i><img border="0" align="middle" src="http://www.cs.odu.edu/%7Etoida/nerzic/level-a/symbols_sets/times.gif" width="20" height="28" /> ... <img border="0" align="middle" src="http://www.cs.odu.edu/%7Etoida/nerzic/level-a/symbols_sets/times.gif" width="20" height="28" /><i>A<sub>n</sub></i> .</b></p>
        <p>
Achieving this is possible <a href="http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx" target="_blank">by
some linq tricks</a>, or by an helper class I propose in this post. But just for clarify
let’s start from the example:
</p>
        <p>
We have three array as below:
</p>
        <pre> { "JUICY", "SWEET" }<br />
{ "GREEN", "YELLOW" }<br />
{ "APPLE", "BANANA", "MANGO" }</pre>
        <p>
we want to obtain all the possible tuples, ie:
</p>
        <p>
 
</p>
        <p>
          <a href="http://www.felicepollano.com/public/WindowsLiveWriter/HelperclassfortheCartesianproductofmoret_B2B4/image_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.felicepollano.com/public/WindowsLiveWriter/HelperclassfortheCartesianproductofmoret_B2B4/image_thumb.png" width="467" height="176" />
          </a>
        </p>
        <p>
This can be achieved by the following code ( helper class ):
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">CartesianProduct</span>&lt;T&gt;
{ <span style="color: blue">int</span>[] lengths; T[][] arrays; <span style="color: blue">public </span>CartesianProduct(<span style="color: blue">params </span>T[][]
arrays) { lengths = arrays.Select(k =&gt; k.Length).ToArray(); <span style="color: blue">if </span>(lengths.Any(l
=&gt; l == 0)) <span style="color: blue">throw new </span><span style="color: #2b91af">ArgumentException</span>(<span style="color: #a31515">"Zero
lenght array unhandled."</span>); <span style="color: blue">this</span>.arrays = arrays;
} <span style="color: blue">public </span><span style="color: #2b91af">IEnumerable</span>&lt;T[]&gt;
Get() { <span style="color: blue">int</span>[] walk = <span style="color: blue">new
int</span>[arrays.Length]; <span style="color: blue">int </span>x = 0; <span style="color: blue">yield
return </span>walk.Select(k =&gt; arrays[x++][k]).ToArray(); <span style="color: blue">while </span>(Next(walk))
{ x = 0; <span style="color: blue">yield return </span>walk.Select(k =&gt; arrays[x++][k]).ToArray();
} } <span style="color: blue">private bool </span>Next(<span style="color: blue">int</span>[]
walk) { <span style="color: blue">int </span>whoIncrement = 0; <span style="color: blue">while </span>(whoIncrement
&lt; walk.Length) { <span style="color: blue">if </span>(walk[whoIncrement] &lt; lengths[whoIncrement]
- 1) { walk[whoIncrement]++; <span style="color: blue">return true</span>; } <span style="color: blue">else </span>{
walk[whoIncrement] = 0; whoIncrement++; } } <span style="color: blue">return false</span>;
} } </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
 
</p>
        <p>
        </p>
        <p>
And, just for completeness, the example application:
</p>
        <p>
 
</p>
        <pre class="code">
          <span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[]
args) { <span style="color: blue">var </span>cross = <span style="color: blue">new </span><span style="color: #2b91af">CartesianProduct</span>&lt;<span style="color: blue">string</span>&gt;( <span style="color: blue">new
string</span>[] { <span style="color: #a31515">"JUICY"</span>, <span style="color: #a31515">"SWEET" </span>}
, <span style="color: blue">new string</span>[] { <span style="color: #a31515">"GREEN"</span>, <span style="color: #a31515">"YELLOW" </span>}
, <span style="color: blue">new string</span>[] { <span style="color: #a31515">"APPLE"</span>, <span style="color: #a31515">"BANANA"</span>, <span style="color: #a31515">"MANGO" </span>}
); <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"========================================================="</span>); <span style="color: blue">foreach </span>(<span style="color: blue">var </span>item <span style="color: blue">in </span>cross.Get())
{ <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"{0}\t{1}\t{2}"</span>,
item[0], item[1], item[2]); } <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"========================================================="</span>);
} </pre>Really simple and clear to use in comparison with other linq based solution,
even when arrays are unknown at design time.<a href="http://11011.net/software/vspaste"></a><img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=c3c5a1eb-4817-4071-954e-8d39193277de" /></body>
      <title>Helper class for the Cartesian product of more than two arrays</title>
      <guid isPermaLink="false">http://www.felicepollano.com/PermaLink.aspx?guid=c3c5a1eb-4817-4071-954e-8d39193277de</guid>
      <link>http://www.felicepollano.com/2011/08/12/HelperClassForTheCartesianProductOfMoreThanTwoArrays.aspx</link>
      <pubDate>Fri, 12 Aug 2011 10:46:46 GMT</pubDate>
      <description>&lt;p&gt;
Sometimes happens that we need to roll over all the combinations of elements in multiple
arrays. This operation is called Cartesian Product and is defined as:
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;Definition (Cartesian product):&lt;/b&gt; Let &lt;b&gt;&lt;i&gt;A&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt;, ..., &lt;i&gt;A&lt;/i&gt;&lt;sub&gt;&lt;i&gt;n&lt;/i&gt;&lt;/sub&gt;&lt;/b&gt; be &lt;b&gt;&lt;i&gt;n&lt;/i&gt;&lt;/b&gt; sets. 
&lt;/p&gt;
&lt;p&gt;
Then the set of all ordered &lt;b&gt;&lt;i&gt;n&lt;/i&gt;&lt;/b&gt;-tuples &lt;b&gt;&amp;lt;&lt;i&gt;x&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt;, ..., &lt;i&gt;x&lt;/i&gt;&lt;sub&gt;&lt;i&gt;n&lt;/i&gt;&lt;/sub&gt;&amp;gt;
,&lt;/b&gt; where &lt;b&gt;&lt;i&gt;x&lt;sub&gt;i&lt;/sub&gt;&lt;/i&gt; &lt;img align="middle" src="http://www.cs.odu.edu/%7Etoida/nerzic/level-a/symbols_sets/in.gif" width="14" height="24"&gt; &lt;i&gt;A&lt;sub&gt;i&lt;/sub&gt;&lt;/i&gt;&lt;/b&gt; for
all &lt;b&gt;&lt;i&gt;i&lt;/i&gt;&lt;/b&gt;, &lt;b&gt;&lt;i&gt;1&lt;/i&gt; &lt;img align="middle" src="http://www.cs.odu.edu/%7Etoida/nerzic/level-a/symbols_sets/leq.gif" width="14" height="24"&gt; &lt;i&gt;i&lt;/i&gt; &lt;img align="middle" src="http://www.cs.odu.edu/%7Etoida/nerzic/level-a/symbols_sets/leq.gif" width="14" height="24"&gt; &lt;i&gt;n&lt;/i&gt; ,&lt;/b&gt; 
&lt;/p&gt;
&lt;p&gt;
is called the &lt;b&gt;Cartesian product&lt;/b&gt; of &lt;b&gt;&lt;i&gt;A&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt;, ..., &lt;i&gt;A&lt;/i&gt;&lt;sub&gt;&lt;i&gt;n&lt;/i&gt;&lt;/sub&gt;,&lt;/b&gt; and
is denoted by &lt;b&gt;&lt;i&gt;A&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; &lt;img border="0" align="middle" src="http://www.cs.odu.edu/%7Etoida/nerzic/level-a/symbols_sets/times.gif" width="20" height="28"&gt; ... &lt;img border="0" align="middle" src="http://www.cs.odu.edu/%7Etoida/nerzic/level-a/symbols_sets/times.gif" width="20" height="28"&gt; &lt;i&gt;A&lt;sub&gt;n&lt;/sub&gt;&lt;/i&gt; .&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
Achieving this is possible &lt;a href="http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx" target="_blank"&gt;by
some linq tricks&lt;/a&gt;, or by an helper class I propose in this post. But just for clarify
let’s start from the example:
&lt;/p&gt;
&lt;p&gt;
We have three array as below:
&lt;/p&gt;
&lt;pre&gt; { "JUICY", "SWEET" }&lt;br&gt;
{ "GREEN", "YELLOW" }&lt;br&gt;
{ "APPLE", "BANANA", "MANGO" }&lt;/pre&gt;
&lt;p&gt;
we want to obtain all the possible tuples, ie:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.felicepollano.com/public/WindowsLiveWriter/HelperclassfortheCartesianproductofmoret_B2B4/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.felicepollano.com/public/WindowsLiveWriter/HelperclassfortheCartesianproductofmoret_B2B4/image_thumb.png" width="467" height="176"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
This can be achieved by the following code ( helper class ):
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CartesianProduct&lt;/span&gt;&amp;lt;T&amp;gt;
{ &lt;span style="color: blue"&gt;int&lt;/span&gt;[] lengths; T[][] arrays; &lt;span style="color: blue"&gt;public &lt;/span&gt;CartesianProduct(&lt;span style="color: blue"&gt;params &lt;/span&gt;T[][]
arrays) { lengths = arrays.Select(k =&amp;gt; k.Length).ToArray(); &lt;span style="color: blue"&gt;if &lt;/span&gt;(lengths.Any(l
=&amp;gt; l == 0)) &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"Zero
lenght array unhandled."&lt;/span&gt;); &lt;span style="color: blue"&gt;this&lt;/span&gt;.arrays = arrays;
} &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T[]&amp;gt;
Get() { &lt;span style="color: blue"&gt;int&lt;/span&gt;[] walk = &lt;span style="color: blue"&gt;new
int&lt;/span&gt;[arrays.Length]; &lt;span style="color: blue"&gt;int &lt;/span&gt;x = 0; &lt;span style="color: blue"&gt;yield
return &lt;/span&gt;walk.Select(k =&amp;gt; arrays[x++][k]).ToArray(); &lt;span style="color: blue"&gt;while &lt;/span&gt;(Next(walk))
{ x = 0; &lt;span style="color: blue"&gt;yield return &lt;/span&gt;walk.Select(k =&amp;gt; arrays[x++][k]).ToArray();
} } &lt;span style="color: blue"&gt;private bool &lt;/span&gt;Next(&lt;span style="color: blue"&gt;int&lt;/span&gt;[]
walk) { &lt;span style="color: blue"&gt;int &lt;/span&gt;whoIncrement = 0; &lt;span style="color: blue"&gt;while &lt;/span&gt;(whoIncrement
&amp;lt; walk.Length) { &lt;span style="color: blue"&gt;if &lt;/span&gt;(walk[whoIncrement] &amp;lt; lengths[whoIncrement]
- 1) { walk[whoIncrement]++; &lt;span style="color: blue"&gt;return true&lt;/span&gt;; } &lt;span style="color: blue"&gt;else &lt;/span&gt;{
walk[whoIncrement] = 0; whoIncrement++; } } &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
} } &lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
And, just for completeness, the example application:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[]
args) { &lt;span style="color: blue"&gt;var &lt;/span&gt;cross = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CartesianProduct&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;( &lt;span style="color: blue"&gt;new
string&lt;/span&gt;[] { &lt;span style="color: #a31515"&gt;"JUICY"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"SWEET" &lt;/span&gt;}
, &lt;span style="color: blue"&gt;new string&lt;/span&gt;[] { &lt;span style="color: #a31515"&gt;"GREEN"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"YELLOW" &lt;/span&gt;}
, &lt;span style="color: blue"&gt;new string&lt;/span&gt;[] { &lt;span style="color: #a31515"&gt;"APPLE"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"BANANA"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"MANGO" &lt;/span&gt;}
); &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"========================================================="&lt;/span&gt;); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;item &lt;span style="color: blue"&gt;in &lt;/span&gt;cross.Get())
{ &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"{0}\t{1}\t{2}"&lt;/span&gt;,
item[0], item[1], item[2]); } &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"========================================================="&lt;/span&gt;);
} &lt;/pre&gt;Really simple and clear to use in comparison with other linq based solution,
even when arrays are unknown at design time.&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=c3c5a1eb-4817-4071-954e-8d39193277de" /&gt;</description>
      <comments>http://www.felicepollano.com/CommentView.aspx?guid=c3c5a1eb-4817-4071-954e-8d39193277de</comments>
      <category>Programmin</category>
      <category>Recipes</category>
    </item>
    <item>
      <trackback:ping>http://www.felicepollano.com/Trackback.aspx?guid=23695e9b-819c-43a8-96ae-8bcd04b55854</trackback:ping>
      <pingback:server>http://www.felicepollano.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.felicepollano.com/PermaLink.aspx?guid=23695e9b-819c-43a8-96ae-8bcd04b55854</pingback:target>
      <dc:creator>Felice Pollano</dc:creator>
      <wfw:comment>http://www.felicepollano.com/CommentView.aspx?guid=23695e9b-819c-43a8-96ae-8bcd04b55854</wfw:comment>
      <wfw:commentRss>http://www.felicepollano.com/SyndicationService.asmx/GetEntryCommentsRss?guid=23695e9b-819c-43a8-96ae-8bcd04b55854</wfw:commentRss>
      <slash:comments>8</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Sometimes we want to use some shortcut key for special commands in our application,
but the KeyBinding objects works with just routed commands, and so it is not directly
usable with <a href="http://caliburnmicro.codeplex.com/" target="_blank">Caliburn
Micro</a> ( or with many other MVVM strategies ).<br />
In similar situation CM rely on System.Windows.Interactivity.dll, for example when
we need to map an event on a Caliburn action we can write: 
</p>
        <p>
 
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">Button </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="Save"
&gt; &lt;</span>
          <span style="color: #a31515">i</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">Interaction.Triggers</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">i</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">EventTrigger </span>
          <span style="color: red">EventName</span>
          <span style="color: blue">="Click"&gt;
&lt;</span>
          <span style="color: #a31515">cl</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">ActionMessage </span>
          <span style="color: red">MethodName</span>
          <span style="color: blue">="Save"
&gt;&lt;/</span>
          <span style="color: #a31515">cl</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">ActionMessage</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">i</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">EventTrigger</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">i</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">Interaction.Triggers</span>
          <span style="color: blue">&gt; </span>
          <span style="color: blue">&lt;/</span>
          <span style="color: #a31515">Button</span>
          <span style="color: blue">&gt; </span>
        </pre>
        <p>
 
</p>
        <p>
With the code above, we explicitly link the "Click" event from the button to the action
calling the metod Save. We can act in a similar way by creating a custom trigger,
lets see how.<br />
First look at what we can do it in XAML: 
</p>
        <p>
 
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">i</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">Interaction.Triggers</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">local</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">InputBindingTrigger</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">local</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">InputBindingTrigger.InputBinding</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">KeyBinding </span>
          <span style="color: red">Modifiers</span>
          <span style="color: blue">="Ctrl" </span>
          <span style="color: red">Key</span>
          <span style="color: blue">="S"/&gt;
&lt;/</span>
          <span style="color: #a31515">local</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">InputBindingTrigger.InputBinding</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">cl</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">ActionMessage </span>
          <span style="color: red">MethodName</span>
          <span style="color: blue">="Save"/&gt;
&lt;/</span>
          <span style="color: #a31515">local</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">InputBindingTrigger</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">i</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">Interaction.Triggers</span>
          <span style="color: blue">&gt; </span>
        </pre>
        <p>
          <span style="color: blue">
          </span>
        </p>
        <p>
 
</p>
        <p>
We need to declare a class, deriving from <a href="http://msdn.microsoft.com/en-us/library/ff726552%28Expression.40%29.aspx" target="_blank">TriggerBase</a> in
System.Windows.Interactivity in order to fire the action(s) in place of executing
a routed command when the user press the proper key gesture. Here the code:
</p>
        <pre class="code">
          <span style="color: blue">class </span>
          <span style="color: #2b91af">InputBindingTrigger</span>:<span style="color: #2b91af">TriggerBase</span>&lt;<span style="color: #2b91af">FrameworkElement</span>&gt;,<span style="color: #2b91af">ICommand </span>{ <span style="color: blue">public </span>InputBindingTrigger()
{ } <span style="color: blue">public </span><span style="color: #2b91af">InputBinding </span>InputBinding
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>(<span style="color: #2b91af">InputBinding</span>)GetValue(InputBindingProperty);
} <span style="color: blue">set </span>{ SetValue(InputBindingProperty, <span style="color: blue">value</span>);
} } <span style="color: blue">public static readonly </span><span style="color: #2b91af">DependencyProperty </span>InputBindingProperty
= <span style="color: #2b91af">DependencyProperty</span>.Register(<span style="color: #a31515">"InputBinding"</span>, <span style="color: blue">typeof</span>(<span style="color: #2b91af">InputBinding</span>)
, <span style="color: blue">typeof</span>(<span style="color: #2b91af">InputBindingTrigger</span>)
, <span style="color: blue">new </span><span style="color: #2b91af">UIPropertyMetadata</span>(<span style="color: blue">null</span>)); <span style="color: blue">protected
override void </span>OnAttached() { <span style="color: blue">if </span>(InputBinding
!= <span style="color: blue">null</span>) { InputBinding.Command = <span style="color: blue">this</span>;
AssociatedObject.InputBindings.Add(InputBinding); } <span style="color: blue">base</span>.OnAttached();
} <span style="color: blue">#region </span>ICommand Members <span style="color: blue">public
bool </span>CanExecute(<span style="color: blue">object </span>parameter) { <span style="color: green">//
action is anyway blocked by Caliburn at the invoke level </span><span style="color: blue">return
true</span>; } <span style="color: blue">public event </span><span style="color: #2b91af">EventHandler </span>CanExecuteChanged
= <span style="color: blue">delegate </span>{ }; <span style="color: blue">public
void </span>Execute(<span style="color: blue">object </span>parameter) { InvokeActions(parameter);
} <span style="color: blue">#endregion </span>} </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
 
</p>
        <p>
Very easily we add the InputBinding to the bindings list on the object, and we attach
the trigger as a command handler. In the execute function we fire the InvokeActions
and it done. Please not the command on the KeyBinding is not required in the markup,
since there is virtually any routed command.
</p>
        <img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=23695e9b-819c-43a8-96ae-8bcd04b55854" />
      </body>
      <title>InputBinding / KeyBinding with Caliburn Micro</title>
      <guid isPermaLink="false">http://www.felicepollano.com/PermaLink.aspx?guid=23695e9b-819c-43a8-96ae-8bcd04b55854</guid>
      <link>http://www.felicepollano.com/2011/05/02/InputBindingKeyBindingWithCaliburnMicro.aspx</link>
      <pubDate>Mon, 02 May 2011 13:16:17 GMT</pubDate>
      <description>&lt;p&gt;
Sometimes we want to use some shortcut key for special commands in our application,
but the KeyBinding objects works with just routed commands, and so it is not directly
usable with &lt;a href="http://caliburnmicro.codeplex.com/" target="_blank"&gt;Caliburn
Micro&lt;/a&gt; ( or with many other MVVM strategies ).&lt;br&gt;
In similar situation CM rely on System.Windows.Interactivity.dll, for example when
we need to map an event on a Caliburn action we can write: 
&lt;p&gt;
&amp;nbsp;&lt;pre class="code"&gt; &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;="Save"
&amp;gt; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;i&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Interaction.Triggers&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;i&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;EventTrigger &lt;/span&gt;&lt;span style="color: red"&gt;EventName&lt;/span&gt;&lt;span style="color: blue"&gt;="Click"&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cl&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;ActionMessage &lt;/span&gt;&lt;span style="color: red"&gt;MethodName&lt;/span&gt;&lt;span style="color: blue"&gt;="Save"
&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;cl&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;ActionMessage&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;i&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;EventTrigger&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;i&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Interaction.Triggers&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
With the code above, we explicitly link the "Click" event from the button to the action
calling the metod Save. We can act in a similar way by creating a custom trigger,
lets see how.&lt;br&gt;
First look at what we can do it in XAML: 
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;i&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Interaction.Triggers&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;local&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;InputBindingTrigger&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;local&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;InputBindingTrigger.InputBinding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;KeyBinding &lt;/span&gt;&lt;span style="color: red"&gt;Modifiers&lt;/span&gt;&lt;span style="color: blue"&gt;="Ctrl" &lt;/span&gt;&lt;span style="color: red"&gt;Key&lt;/span&gt;&lt;span style="color: blue"&gt;="S"/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;local&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;InputBindingTrigger.InputBinding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cl&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;ActionMessage &lt;/span&gt;&lt;span style="color: red"&gt;MethodName&lt;/span&gt;&lt;span style="color: blue"&gt;="Save"/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;local&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;InputBindingTrigger&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;i&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Interaction.Triggers&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;span style="color: blue"&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
We need to declare a class, deriving from &lt;a href="http://msdn.microsoft.com/en-us/library/ff726552%28Expression.40%29.aspx" target="_blank"&gt;TriggerBase&lt;/a&gt; in
System.Windows.Interactivity in order to fire the action(s) in place of executing
a routed command when the user press the proper key gesture. Here the code:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;InputBindingTrigger&lt;/span&gt;:&lt;span style="color: #2b91af"&gt;TriggerBase&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;FrameworkElement&lt;/span&gt;&amp;gt;,&lt;span style="color: #2b91af"&gt;ICommand &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;InputBindingTrigger()
{ } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;InputBinding &lt;/span&gt;InputBinding
{ &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;InputBinding&lt;/span&gt;)GetValue(InputBindingProperty);
} &lt;span style="color: blue"&gt;set &lt;/span&gt;{ SetValue(InputBindingProperty, &lt;span style="color: blue"&gt;value&lt;/span&gt;);
} } &lt;span style="color: blue"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DependencyProperty &lt;/span&gt;InputBindingProperty
= &lt;span style="color: #2b91af"&gt;DependencyProperty&lt;/span&gt;.Register(&lt;span style="color: #a31515"&gt;"InputBinding"&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;InputBinding&lt;/span&gt;)
, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;InputBindingTrigger&lt;/span&gt;)
, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;UIPropertyMetadata&lt;/span&gt;(&lt;span style="color: blue"&gt;null&lt;/span&gt;)); &lt;span style="color: blue"&gt;protected
override void &lt;/span&gt;OnAttached() { &lt;span style="color: blue"&gt;if &lt;/span&gt;(InputBinding
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;) { InputBinding.Command = &lt;span style="color: blue"&gt;this&lt;/span&gt;;
AssociatedObject.InputBindings.Add(InputBinding); } &lt;span style="color: blue"&gt;base&lt;/span&gt;.OnAttached();
} &lt;span style="color: blue"&gt;#region &lt;/span&gt;ICommand Members &lt;span style="color: blue"&gt;public
bool &lt;/span&gt;CanExecute(&lt;span style="color: blue"&gt;object &lt;/span&gt;parameter) { &lt;span style="color: green"&gt;//
action is anyway blocked by Caliburn at the invoke level &lt;/span&gt;&lt;span style="color: blue"&gt;return
true&lt;/span&gt;; } &lt;span style="color: blue"&gt;public event &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventHandler &lt;/span&gt;CanExecuteChanged
= &lt;span style="color: blue"&gt;delegate &lt;/span&gt;{ }; &lt;span style="color: blue"&gt;public
void &lt;/span&gt;Execute(&lt;span style="color: blue"&gt;object &lt;/span&gt;parameter) { InvokeActions(parameter);
} &lt;span style="color: blue"&gt;#endregion &lt;/span&gt;} &lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Very easily we add the InputBinding to the bindings list on the object, and we attach
the trigger as a command handler. In the execute function we fire the InvokeActions
and it done. Please not the command on the KeyBinding is not required in the markup,
since there is virtually any routed command.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=23695e9b-819c-43a8-96ae-8bcd04b55854" /&gt;</description>
      <comments>http://www.felicepollano.com/CommentView.aspx?guid=23695e9b-819c-43a8-96ae-8bcd04b55854</comments>
      <category>Caliburn</category>
      <category>Recipes</category>
      <category>WPF</category>
    </item>
  </channel>
</rss>