Skip to content

Basic Construction

NN--- edited this page Apr 11, 2012 · 11 revisions

Basic Construction

  • Category: Sequences
  • Description: This sample demonstrates basic sequence construction.
  • Comment: For use this example you must include ComputationExpressions.dll and ComputationExpressions.Macros.dll references (from Nemerle program directory)
  • Code:
using System;
using System.Console;
using Nemerle;
using Nemerle.Collections;
using Nemerle.ComputationExpressions;
using System.Linq;
using System.IO.Directory;

  // Create an empty sequence
def emptySeq = Enumerable.Empty.[int]();

// Create a simple sequence
def simpleSeq = Enumerable.Range(1, 50); 
// sequence terms are not evaluated until needed

def seq = Enumerable.Range(1, 5);
// Create a sequence using a sequence expression
def squaresSeq = comp enumerable { foreach (i in seq) yield i * i }
    
// Another sequence expression example
def getDirs(dir) 
{ 
  comp enumerable 
  { 
    foreach (folder in GetDirectories(dir)) yield folder 
  } 
}
    
WriteLine($"emptySeq = ..$emptySeq");
WriteLine($"simpleSeq = ..$simpleSeq" );
WriteLine($"squaresSeq = ..$squaresSeq" );
WriteLine($<#get directories in C: ..$(getDirs(@"C:\ ")) #>);
  • Execution Result (output directories may vary on some of systems):
emptySeq = 
simpleSeq = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50
squaresSeq = 1, 4, 9, 16, 25
get directories in C: C:\ \$Recycle.Bin, C:\ \Backup, C:\ \Boot, C:\ \cfc4b433596e551adfc95d5ff4aa4d9c, C:\ \Config.Msi, C:\ \docs, C:\ \Documents and Settings, C:\ \lm.dat, C:\ \MSOCache, C:\ \PerfLogs, C:\ \Program Files, C:\ \Program Files (x86), C:\ \ProgramData, C:\ \Programs, C:\ \programs_config, C:\ \PSFonts, C:\ \Recovery, C:\ \RECYCLER, C:\ \System Volume Information, C:\ \TEMP, C:\ \Tmp, C:\ C:\ \Users, C:\ \Windows

[Copyright ©](Terms of use, legal notice)

Clone this wiki locally