/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

  1. class 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. create {ANY}
  8. make
  9. feature {ANY}
  10. make
  11. local
  12. my_array: ARRAY[STRING]
  13. do
  14. my_array := {ARRAY[STRING] 1, << "Benedicte", "Lucien", "Marie" >> }
  15. create my_dictionary.make
  16. -- Using an agent to fill `my_dictionary' with `my_array':
  17. my_array.for_each(agent fill_it(?))
  18. -- Using another agent to print `my_dictionary':
  19. my_dictionary.for_each(agent print_key_value(?, ?))
  20. end
  21. feature {}
  22. my_dictionary: HASHED_DICTIONARY[INTEGER, STRING]
  23. fill_it (value: STRING)
  24. do
  25. my_dictionary.put(next_key, value)
  26. end
  27. next_key: INTEGER
  28. do
  29. counter.increment
  30. Result := counter.item
  31. end
  32. counter: COUNTER
  33. once
  34. create Result
  35. end
  36. print_key_value (key: INTEGER; value: STRING)
  37. do
  38. std_output.put_integer(key)
  39. std_output.put_character(' ')
  40. std_output.put_string(value)
  41. std_output.put_character('%N')
  42. end
  43. end -- class EXAMPLE3