Alore Example Programs

This page contains several small example programs that illustrate different Alore features. These and other examples are included in the Alore distribution, under directory demo .

Browse examples in the repository

Contents

Hello, world

Print( 'hello, world' )

Largest directories

-- Usage: nlargest.alo [DIR [N]] -- -- Find the N largest subdirectories of DIR (default to '.' and 10). import os def Main(args) var n = 10 var dir = '.' if args != [] dir = args[0] if args.length() > 1 n = Int(args[1]) end end LargestDirs(n, dir) end -- Display the n largest subdirectories of dir. def LargestDirs(n, dir) var a = [] DirSizes(dir, a) a = Reversed(Sort(a)) for size, d in a[:n] Print( '{-8:} {}' .format(size div 1024, d)) end end -- Append to res a tuple (size, subdir) for each subdirectory of dir and return -- the total size of files in dir. def DirSizes(dir, res) var size = 0 for n in ListDir(dir) var p = Join(dir, n) if IsFile(p) size += Stat(p).size elif IsDir(p) and not IsLink(p) var s = DirSizes(p, res) res.append((s, NormPath(p))) size += s end end return size end

Most frequent words

-- Usage: wordfreq.alo N FILE ... -- -- Display the N most frequent words in file(s). Assume that the files are -- text files encoded using the platform default encoding. -- -- This example uses static typing. import io, re def Main(args as Array<Str>) if args.length() < 2 WriteLn( 'Usage: wordfreq.alo N FILE ...' ) Exit(1) end -- Construct a map (hash table) from Str to Int. var map = Map() as <Str, Int> var n = Int(args[0]) -- Calculate word counts. for fnam in args[1:] var s = TextFile(fnam).read() for word in Subst(s, '\W' , ' ' ).split() map[word] = map.get(word, 0) + 1 end end -- Construct an array of (int, string) tuples. var a = [] as <(Int, Str)> for word, freq in map a.append((freq, word)) end -- Display the n most frequent words with frequencies. for freq, word in Reversed(Sort(a)[-n:]) WriteLn( '{-6:} {}' .format(freq, word)) end end

Complex numbers