<< Prev | - Up - |
We now lift the bags in library style to the object style. This strictly follows the usual pattern.
functor
import
Bag(fromList put toList member) at 'BagLib.ozf'
export
new : NewBagObject
fromList: ListToBagObject
define
fun{ListToBagObject L}
B = {Bag.fromList L}
in
unit(put : proc{$ X} {Bag.put B X} end
toList: fun {$ } {Bag.toList B} end
member: fun {$ X} {Bag.member B X} end)
end
fun{NewBagObject}
{ListToBagObject nil}
end
end
We can now use bags by linking a compiled version of the module Bag.ozf
that are made available at the usual URL. The function Bag.new
creates a new bag object B
. Elements can be added to B
by invoking B.put
. A bag B
is converted into a list by calling B.toList
:
declare BagURL= URL#'/Bag.ozf'
declare [Bag] = {Module.link [BagURL]}
declare B = {Bag.new}
{B.put hello}
{B.put world}
{Inspect {B.toList}}
<< Prev | - Up - |