﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Diego Mijelshon commented on C# Coding Challenge: What will this code do?</title><description>"It works as you expect it to, but it doesn't look like it will work." -&gt; which is why it's important to write the tests first. If it passes, you don't need to code anything else.
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment18</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment18</guid><pubDate>Sat, 22 May 2010 17:40:15 GMT</pubDate></item><item><title>fschwiet commented on C# Coding Challenge: What will this code do?</title><description>well the loop input
  
  
  from inst in new[]{inst1, inst2, new object()} where instanceToKey.TryGetValue(inst, out value)  select value
  
  
is equivalent to:
  
  
   new[] {inst1, inst2, new object()}.Where(inst =&gt; instanceToKey.TryGetValue(inst, out value)).Select(
  
                    inst =&gt; value)
  
  
I assumed this was equivalent to
  
  
  new[] {inst1, inst2, new object()}.Where(inst =&gt; instanceToKey.TryGetValue(inst, out value)).ToArray().Select(
  
                    inst =&gt; value)
  
  
But its not.  So I was all sorts of wrong.
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment17</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment17</guid><pubDate>Fri, 21 May 2010 18:43:11 GMT</pubDate></item><item><title>meisinger commented on C# Coding Challenge: What will this code do?</title><description>Thats funny...  have what is called "reverse reverse gotchas" all over the place
  
  
Code that looks like it shouldn't work and doesn't work
  
(oh I love the double negative)
  
  
I haven't checked but I am pretty sure that I can also produce just the simple "gotcha"
  
  
Code that looks like it should work but doesn't work
  
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment16</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment16</guid><pubDate>Fri, 21 May 2010 16:13:45 GMT</pubDate></item><item><title>configurator commented on C# Coding Challenge: What will this code do?</title><description>Daniel: I don't see why it would.  And it doesn't.
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment15</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment15</guid><pubDate>Fri, 21 May 2010 12:04:22 GMT</pubDate></item><item><title>Daniel commented on C# Coding Challenge: What will this code do?</title><description>To everyone who thinks the is working correctly: change the order in the array so that the 3rd instance is in the middle:
  
  new[]{inst1, new object(), inst2}
  
  
Then try to explain out why changing the position of an element that seems to be ignored changes the output of the program.
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment14</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment14</guid><pubDate>Fri, 21 May 2010 11:19:29 GMT</pubDate></item><item><title>Steve Py commented on C# Coding Challenge: What will this code do?</title><description>"This is actually what I call a reverse gotcha.
  
It works as you expect it to, but it doesn't look like it will work. "
  
  
rofl... Intentionally making something more obscure than it needs to be? That whole debate over Raven DB licensing must have put you in that sort of mood. ;)
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment13</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment13</guid><pubDate>Fri, 21 May 2010 11:02:24 GMT</pubDate></item><item><title>Andrey Titov commented on C# Coding Challenge: What will this code do?</title><description>I forgot second overload:
  
  
public static IEnumerable
&lt;tresult ApplyMap
&lt;tresult,&gt;
(
  
    this IEnumerable
&lt;tsource source,
  
    IDictionary
&lt;tsource,&gt;
 map,
  
    Func
&lt;tsource,&gt;
 resultSelector
  
)
  
{
  
    return ApplyMap(source, key =&gt; key, map, (item, key/*=item*/, value) =&gt; resultSelector(item, value));
  
}
  
&gt;</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment12</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment12</guid><pubDate>Fri, 21 May 2010 10:46:14 GMT</pubDate></item><item><title>Andrey Titov commented on C# Coding Challenge: What will this code do?</title><description>I'd rewrite this in something like:
  
  
foreach (var keyCost in
  
    new[] { inst1, inst2, new object() }
  
    .ApplyMap(instanceToKey, (inst, key) =&gt; key)
  
    .ApplyMap(keyToCost, (key, cost) =&gt; new { key, cost }))
  
{
  
    Console.WriteLine("Key: {0}, Cost: {1}", keyCost.key, keyCost.cost);
  
}
  
  
  
public static IEnumerable
&lt;tresult ApplyMap
&lt;tresult,&gt;
(
  
    this IEnumerable
&lt;tsource source,
  
    Func
&lt;tsource,&gt;
 keySelector,
  
    IDictionary
&lt;tkey,&gt;
 map,
  
    Func
&lt;tsource,&gt;
 resultSelector
  
)
  
{
  
    foreach (var item in source)
  
    {
  
        TKey key = keySelector(item);
  
        TValue value;
  
        if (map.TryGetValue(key, out value))
  
        {
  
            yield return resultSelector(item, key, value);
  
        }
  
    }
  
}
  
  
TryGetValue() newer fit well in LINQ :-(
&gt;</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment11</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment11</guid><pubDate>Fri, 21 May 2010 10:44:26 GMT</pubDate></item><item><title>Felipe Fujiy commented on C# Coding Challenge: What will this code do?</title><description>GetHashCode for classes by default return a unique token by instannce
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment10</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment10</guid><pubDate>Fri, 21 May 2010 10:33:36 GMT</pubDate></item><item><title>Omer Mor commented on C# Coding Challenge: What will this code do?</title><description>Why doesn't it look like it will work?
  
Looking at the code, it seemed just fine to me.
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment9</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment9</guid><pubDate>Fri, 21 May 2010 10:29:11 GMT</pubDate></item><item><title>Stefan Wenig commented on C# Coding Challenge: What will this code do?</title><description>Sweet! Code that relies on delayed execution, string interning and the semantics of mutating captured variables. That's much better than last time's prefix decrement operator!
  
Next time, can we have some lock-free multi-threaded code along with the question "what will this code print on an Itanium system?"
  
Please! ;-) (you might need to specify the chipset though, but then at least participants can't just run the code to solve the quiz!)
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment8</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment8</guid><pubDate>Fri, 21 May 2010 10:00:06 GMT</pubDate></item><item><title>Ayende Rahien commented on C# Coding Challenge: What will this code do?</title><description>This is actually what I call a reverse gotcha.
  
It works as you expect it to, but it doesn't look like it will work.
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment7</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment7</guid><pubDate>Fri, 21 May 2010 09:29:23 GMT</pubDate></item><item><title>rvin100 commented on C# Coding Challenge: What will this code do?</title><description>I'm agree with Steve too. It's a strange way to process, but you just got :
  
  
Key: One, Cost: 1
  
Key: Two, Cost: 2
  
  
The third instance is just ignored...
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment6</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment6</guid><pubDate>Fri, 21 May 2010 09:24:17 GMT</pubDate></item><item><title>kukabuka commented on C# Coding Challenge: What will this code do?</title><description>I was going to agree with dan, but if you run the code it is just 
  
  
Key: One, Cost: 1
  
Key: Two, Cost: 2
  
  
inst1.Equals(inst2) = false.  .GetHashCode() will reveal different hashes too.  I guess that makes the difference to the Dictionary type.
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment5</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment5</guid><pubDate>Fri, 21 May 2010 08:17:42 GMT</pubDate></item><item><title>Glenn commented on C# Coding Challenge: What will this code do?</title><description>+1 to Steve's comment. TryGetValue returns false for the 3rd object and it's never selected.
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment4</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment4</guid><pubDate>Fri, 21 May 2010 08:07:41 GMT</pubDate></item><item><title>dan commented on C# Coding Challenge: What will this code do?</title><description>haven't touched .net for a while, but inst1 and inst2 are both new object() - will they have identical hash codes for the dictionary?
  
  
if so, the result will be:
  
  
Key: two, Cost: 2
  
Key: two, Cost: 2
  
Key: two, Cost: 2
  
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment3</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment3</guid><pubDate>Fri, 21 May 2010 07:28:47 GMT</pubDate></item><item><title>Ken Egozi commented on C# Coding Challenge: What will this code do?</title><description>Am I missing something? it should be the trivial "one"/1 and "two"/2 pairs
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment2</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment2</guid><pubDate>Fri, 21 May 2010 06:54:31 GMT</pubDate></item><item><title>Steve Py commented on C# Coding Challenge: What will this code do?</title><description>Well it's bloody convoluded, but I couldn't see anything wrong with it. The 3rd instance will be ignored because of the TryGetValue.
  
  
Key: One, Cost: 1
  
Key: Two, Cost: 2
  
  
I thought you might have a gotcha in there so I also tried it in .Net 3.5 and 4.0 but got the same result... Is something missing?
</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment1</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do#comment1</guid><pubDate>Fri, 21 May 2010 06:51:02 GMT</pubDate></item></channel></rss>