TWiki . Doc . SpellcheckBeginnerExample

/* Please try the NoviceExamples before the BeginnerExamples
To compile:
   nicec --sourcepath=.. -a spellcheck.jar spellcheck
To run:
   java -jar spellcheck.jar < input.txt
*/

import java.io.*;

void main(String[] args){
   HashMap<String,int> dictionary = new HashMap();

   try {
      let f = new BufferedReader(new FileReader("Usr.Dict.Words"));
      f.foreach(String word => { dictionary[word] = 1; });
      f.close;
   } 
   catch (IOException e) { 
      System.err.println(e); 
      return; 
   }

   try {
      let r = new BufferedReader(new InputStreamReader(System.in));
      r.foreach(String word => { 
         if (!dictionary.containsKey(word)) println(word); });
   } 
   catch (IOException e) { 
      System.err.println(e); 
   }
}


void foreach(BufferedReader r, String -> void expr) { 
   ?String s;
   while ((s = r.readLine) != null) expr(s);
}


/* Notes - language
Compare with Java at the "Win32 Language Shootout"

   f.foreach(String word => { dictionary[word] = 1; });
Method foreach takes a function as a parameter, and in this call
the function is defined inline - it's an anonymous function.

*/
-- IsaacGouy - 27 Aug 2003

----- Revision r1.4 - 10 Jan 2004 - 19:06 GMT - IsaacGouy
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.