[svn] r6032: lb/activity/2005/Dec-21.html
malekith
svnadmin at nemerle.org
Wed Dec 21 14:58:51 CET 2005
Log:
A new entry.
Author: malekith
Date: Wed Dec 21 14:58:51 2005
New Revision: 6032
Added:
lb/activity/2005/Dec-21.html
Added: lb/activity/2005/Dec-21.html
==============================================================================
--- (empty file)
+++ lb/activity/2005/Dec-21.html Wed Dec 21 14:58:51 2005
@@ -0,0 +1,103 @@
+<h1>Recent changes</h1>
+
+<h2>Yield</h2>
+
+<p>
+Supporting <tt>yield</tt> was harder than it first seemed. The problem
+was that Nemerle already supports most of the stuff needed for yield
+(for example putting local variables in classes so they persist), but
+unfortunately in a slightly different way, so a reimplementation
+was required in a few cases. This was very irritating (as doing the
+same thing twice always is). Fortunately now it works.
+</p>
+<p>
+There was another problem with finally blocks which has to be run
+only once (we don't want finally blocks to be run, when yielding a value
+from inside try), and also when the iterator is prematurely disposed.
+Fortunately C# doesn't allow yield in try-catch, so we decided to do
+the same ;)
+</p>
+<p>
+The C# spec was quite sketchy about this issue though.
+</p>
+
+<h2>List comprehensions</h2>
+
+<p>
+Haskell (as well as Python to some extent) has this really nice
+nice notation, that comes from math. The idea is to describe
+a set in terms of <i>all such <tt>x</tt> that <tt>x</tt> comes
+from the set <tt>A</tt> and is greater than zero</i>. This would be
+written as:
+</p>
+<xmp>
+$ [ x | x in A, x > 0 ]
+</xmp>
+<p>
+But this isn't really funny, so let's look at something more complicated,
+<i>all such pairs <tt>(x, y)</tt> that <tt>x</tt> comes from <tt>A</tt>
+and <tt>y</tt> comes from <tt>B</tt></i>:
+</p>
+<xmp>
+$ [ (x, y) | x in A, y in B ]
+</xmp>
+<p>
+We can now further restrict this to <tt>x < y</tt>:
+</p>
+<xmp>
+$ [ (x, y) | x in A, y in B, x < y ]
+</xmp>
+<p>
+We can even apply some complex expressions, for example to
+return set of lengths of elements in <tt>A</tt> we would use:
+</p>
+<xmp>
+$ [ x.Length | x in A ]
+</xmp>
+<p>
+There are functions to do all that stuff in Nemerle standard library,
+but list comprehensions provide a way to combine them in one short
+expressions. The translation of this stuff to the underling core language
+is straightforward -- each <tt>e in something</tt> is translated to
+a nested <tt>foreach</tt> loop and each condition is translated to
+<tt>when</tt>, details are <a href="http://nemerle.org/List_comprehensions">here</a>.
+</p>
+<p>
+For example to list all members in all types in all assemblies used by the program
+one could use:
+</p>
+<xmp>
+def allMembers =
+ $[m | a in System.AppDomain.CurrentDomain.GetAssemblies (),
+ t in a.GetTypes (), m in t.GetMembers ()];
+</xmp>
+<p>
+To limit this to types from the "System" namespace we could use:
+</p>
+<xmp>
+def systemMembers =
+ $[m | a in System.AppDomain.CurrentDomain.GetAssemblies (),
+ t in a.GetTypes (), t.Namespace == "System", m in t.GetMembers ()];
+</xmp>
+<p>
+And so on. Note that the following statement would have similar effect:
+</p>
+<xmp>
+def systemMembers =
+ $[m | a in System.AppDomain.CurrentDomain.GetAssemblies (),
+ t in a.GetTypes (), m in t.GetMembers (), t.Namespace == "System"];
+</xmp>
+<p>
+But it would be less efficient because of calling <tt>GetMembers</tt>
+even for types outside <tt>System</tt>, and only discarding them
+later.
+</p>
+<p>
+This is really a nice feature and only one hour to implement :-)
+</p>
+<p>
+What should come next is something like:
+</p>
+<xmp>
+$[(x,y) | x in [1...3], y in [1,3,...,15]]
+</xmp>
More information about the svn
mailing list