PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/tutorial/agent/example3.e

http://github.com/tybor/Liberty
Specman e | 51 lines | 36 code | 8 blank | 7 comment | 0 complexity | f91ceaaabbaaf468afcf7282cc6d943e MD5 | raw file
 1class EXAMPLE3
 2   --
 3   -- In the DICTIONARY class, the `for_each' feature has an agent argument
 4   -- which is a procedure with two arguments, one for the value (type `V') and
 5   -- one for the key (type `K').
 6   --
 7
 8create {ANY}
 9   make
10
11feature {ANY}
12   make
13      local
14         my_array: ARRAY[STRING]
15      do
16         my_array := {ARRAY[STRING] 1, << "Benedicte", "Lucien", "Marie" >> }
17         create my_dictionary.make
18         -- Using an agent to fill `my_dictionary' with `my_array':
19         my_array.for_each(agent fill_it(?))
20         -- Using another agent to print `my_dictionary':
21         my_dictionary.for_each(agent print_key_value(?, ?))
22      end
23
24feature {}
25   my_dictionary: HASHED_DICTIONARY[INTEGER, STRING]
26
27   fill_it (value: STRING)
28      do
29         my_dictionary.put(next_key, value)
30      end
31
32   next_key: INTEGER
33      do
34         counter.increment
35         Result := counter.item
36      end
37
38   counter: COUNTER
39      once
40         create Result
41      end
42
43   print_key_value (key: INTEGER; value: STRING)
44      do
45         std_output.put_integer(key)
46         std_output.put_character(' ')
47         std_output.put_string(value)
48         std_output.put_character('%N')
49      end
50
51end -- class EXAMPLE3