Skip to content

Using the Map type

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

Using the Map type

  • Category: Data Types

  • Description: Create a histogram of the occurrences of particular unicode characters using a Nemerle.Collections.Map

  • Code:

using System.Console; 
using Nemerle.Collections;

def data = "The big black bird flying over the moon";

def generate(i, map)
{
  if (map.Contains(i)) map.Replace(i, i :: map.Get(i)) else map.Add(i, [i])
}
           
def map = data.FoldLeft(Map(), generate);
def histogram = map.Map((x, y) => (x, y.Length));
foreach ((x, y) in histogram) 
  WriteLine($"Number of '$x' characters = $y");
  • Execution Result:
Number of ’’ characters = 7 
Number of ’T’ characters = 1
Number of ’a’ characters = 1 
Number of ’b’ characters = 3 
Number of ’c’ characters = 1 
Number of ’d’ characters = 1 
Number of ’e’ characters = 3
Number of ’f’ characters = 1 
Number of ’g’ characters = 2 
Number of ’h’ characters = 2 
Number of ’i’ characters = 3 
Number of ’k’ characters = 1
Number of ’l’ characters = 2 
Number of ’m’ characters = 1 
Number of ’n’ characters = 2 
Number of ’o’ characters = 3 
Number of ’r’ characters = 2
Number of ’t’ characters = 1 
Number of ’v’ characters = 1 
Number of ’y’ characters = 1 

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

Clone this wiki locally