"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.0/packages/collections/reverse.pony" (1 Nov 2019, 922 Bytes) of package /linux/misc/ponyc-0.33.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Pony source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 class Reverse[A: (Real[A] val & Number) = USize] is Iterator[A]
    2   """
    3   Produces a decreasing range [max, min] with step `dec`, for any `Number` type.
    4   (i.e. the reverse of `Range`)
    5 
    6   Example program: 
    7 
    8   ```pony
    9   use "collections"
   10   actor Main
   11     new create(env: Env) =>
   12       for e in Reverse(10, 2, 2) do
   13         env.out.print(e.string())
   14       end 
   15   ```
   16   Which outputs: 
   17   ```
   18   10
   19   8
   20   6
   21   4
   22   2
   23   ```
   24 
   25   If `dec` is 0, produces an infinite series of `max`.
   26 
   27   If `dec` is negative, produces a range with `max` as the only value.
   28 
   29   """
   30 
   31   let _min: A
   32   let _max: A
   33   let _dec: A
   34   var _idx: A
   35 
   36   new create(max: A, min: A, dec: A = 1) =>
   37     _min = min
   38     _max = max
   39     _dec = dec
   40     _idx = max
   41 
   42   fun has_next(): Bool =>
   43     (_idx >= _min) and (_idx <= _max)
   44 
   45   fun ref next(): A =>
   46     if has_next() then
   47       _idx = _idx - _dec
   48     else
   49       _idx + _dec
   50     end
   51 
   52   fun ref rewind() =>
   53     _idx = _max