tests: tests.sh detects directories before trying to execute them
[nit.git] / examples / rosettacode / accumulator_factory.nit
1 #!/usr/bin/env nit
2 #
3 # This file is part of NIT ( http://www.nitlanguage.org ).
4 # This program is public domain
5
6 # The `accumulator factory` task.
7 # SEE: <http://rosettacode.org/wiki/Accumulator_factory>
8 #
9 # Nit has no first-class function.
10 # A class is used to store the state.
11 module accumulator_factory
12
13 class Accumulator
14 # The accumulated sum
15 # Numeric is used, so Int and Float are accepted
16 private var sum: Numeric
17 fun call(n: Numeric): Numeric
18 do
19 # `add` is the safe `+` method on Numeric
20 sum = sum.add(n)
21 return sum
22 end
23 end
24
25 var x = new Accumulator(1)
26 x.call(5)
27 var y = new Accumulator(3)
28 print x.call(2.3)