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

module complex

-- A basic complex number class with static typing. This example illustrates
-- the following features:
--
--   * module definition
--   * class definition
--   * operator overloading (methods such as _eq and _add)
--
-- See also the example "complex2" for a more elaborate implementation of
-- complex numbers.

import math


-- Examples:
--   Complex(0.0, -2.0)                         (complex number -2.0j)
--   Complex(0.0, 2.0) + Complex(1.5, -0.5)     (complex addition)


class Complex
  const re as Float
  const im as Float
  
  -- Since we don't define a constructor, we get the default constructor 
  -- create(re, im).
  
  -- Absolute value.
  def abs() as Float
    return Sqrt(re**2 + im**2)
  end

  -- Complex conjugate.
  def conj() as Complex
    return Complex(re, -im)
  end

  -- Conversion to string; allows Str(complex).
  def _str() as Str
    if im >= 0
      return '({} + {}j)'.format(re, im)
    else
      return '({} - {}j)'.format(re, -im)
    end
  end

  -- The "==" operation.
  def _eq(n as Object) as Boolean
    if n is Complex
      var c = (n as Complex)    -- Downcast to Complex
      return re == c.re and im == c.im
    else
      return False
    end
  end

  -- The "+" operation.
  def _add(c as Complex) as Complex
    return Complex(re + c.re, im + c.im)
  end

  -- The "-" operation.
  def _sub(c as Complex) as Complex
    return Complex(re - c.re, im - c.im)
  end

  -- The "*" operation.
  def _mul(c as Complex) as Complex
    return Complex(re * c.re - im * c.im,
                   im * c.re + re * c.im)
  end

  -- The "/" operation.
  def _div(c as Complex) as Complex
    var d = c.re**2 + c.im**2
    return Complex((re * c.re + im * c.im) / d,
                   (im * c.re - re * c.im) / d)
  end
end