[svn] r7667: nemerle/trunk/snippets/shootout: chameneos.n cheap-concurrency1.n

nazgul svnadmin at nemerle.org
Fri May 11 18:56:58 CEST 2007


Log:
Add cheap concurrency samples to Language Shootout ported by Micky Latowicki from Python

Author: nazgul
Date: Fri May 11 18:56:53 2007
New Revision: 7667

Added:
   nemerle/trunk/snippets/shootout/chameneos.n
   nemerle/trunk/snippets/shootout/cheap-concurrency1.n

Added: nemerle/trunk/snippets/shootout/chameneos.n
==============================================================================
--- (empty file)
+++ nemerle/trunk/snippets/shootout/chameneos.n	Fri May 11 18:56:53 2007
@@ -0,0 +1,82 @@
+/*
+ * The Computer Language Shootout
+ * http://shootout.alioth.debian.org/
+ *
+ * Contributed by: Micky Latowicki, ported from Tobias Polzin's Python program,
+ * itself ported from Mike Pall's Lua program.
+ * Date: May 8, 2007
+ */
+
+#pragma indent
+
+using System
+using System.Collections.Generic
+using Nemerle.Imperative
+
+enum Color
+    | None
+    | Red
+    | Blue
+    | Yellow
+    | Faded
+
+class Chameneos
+    mutable N : int = 0
+    mutable first : Color = Color.None
+    mutable second : Color = Color.None
+    mutable meetings : int = 0
+
+    Creature(mutable me: Color) : IEnumerator[Color]
+        mutable met = 0
+        mutable other = Color.None
+
+        def meet()
+            met += 1
+            when (me!=other)
+                me = match (me, other) 
+                    | (Blue, Red) => Color.Yellow 
+                    | (Blue, _) => Color.Red
+                    | (Red, Blue) => Color.Yellow
+                    | (Red, _) => Color.Blue
+                    | (Yellow, Blue) => Color.Red 
+                    | (Yellow, _) => Color.Blue
+                    | _ => me
+
+        while (true)
+            // wait for an available slot in the meeting place
+            while (second!=Color.None) 
+                yield me
+            if (first!=Color.None)
+                second = me
+                other = first
+            else
+                if (N<=0)
+                    me = Color.Faded
+                    meetings += met
+                    break
+                else
+                    first = me
+                    N -= 1
+                    // wait for a companion:
+                    while (second==Color.None) yield me
+                    other = second
+                    first = Color.None
+                    second = Color.None
+                    yield me
+            meet()
+
+    Schedule(threads: list[IEnumerator[Color]]) : int
+        if (threads.IsEmpty)
+            meetings
+        else
+            Schedule(threads.Filter((c)=>c.MoveNext()))
+
+    public this(n:int) 
+        this.N = n
+
+    public Run() : void 
+        def creatures = [Color.Blue, Color.Red, Color.Yellow, Color.Blue].Map(Creature)
+        Console.WriteLine(Schedule(creatures))
+
+def n = int.Parse(Environment.GetCommandLineArgs()[1])
+Chameneos(n).Run()

Added: nemerle/trunk/snippets/shootout/cheap-concurrency1.n
==============================================================================
--- (empty file)
+++ nemerle/trunk/snippets/shootout/cheap-concurrency1.n	Fri May 11 18:56:53 2007
@@ -0,0 +1,32 @@
+// The Computer Language Shootout
+// http://shootout.alioth.debian.org/
+// contributed by Micky Latowicki, translated from the python program (itself translated from the lua)
+// Date: May 7, 2007
+
+#pragma indent
+
+using System
+using System.Collections.Generic
+using Nemerle.Imperative
+
+module CheapConcurrency
+
+    MakeChain(n:int) : IEnumerable[int]
+        if (n > 1)
+            foreach (x in MakeChain(n-1))
+                yield x+1
+        else
+            while(true) 
+                yield 1
+
+    SumUpPrefix(xs: IEnumerable[int], mutable n:int) : int
+        mutable total = 0
+        foreach (x in xs)
+            when (n==0) break
+            total += x
+            n -= 1
+        total
+
+    public Main() : void 
+        def n = int.Parse(Environment.GetCommandLineArgs()[1])
+        Console.WriteLine(SumUpPrefix(MakeChain(500), n))



More information about the svn mailing list