Dear Lyle,
you could write a (normal) function that creates and returns a functor
-- the functor syntax is nestable and thus can also be wrapped in a
function call. Arguments to the function can then be used within the
function definition.
Hope this helps.
Best wishes,
Torsten
--
Torsten Anders
Interdisciplinary Centre for Computer Music Research
University of Plymouth
Office: +44-1752-586219
Private: +44-1752-558917
http://strasheela.sourceforge.net
http://www.torsten-anders.de
On 27.07.2010, at 08:12, Lyle Kopnicky wrote:
Hi folks,
I wonder if I'm missing something in the definition of functors.
According to CTM (pp. 221), "A functor is a function whose arguments
are the modules it needs and shose result is a new module." That
sounds wonderful, as it allows me to compose modules at will. On pp.
223-4, the following function:
fun {MyListFunctor}
proc {Append ... }
proc {MergeSort ...} ... end
proc {Sort ... } ... {MergeSort ... } ... end
proc {Member ...} ... end
in
`export`(append: Append
sort: Sort
member: Member
...)
end
can be written with the functor syntax as:
functor
export
append:Append
sort:Sort
member:Member
...
define
proc {Append ... } ... end
proc {MergeSort ... } ... end
proc {Sort ... } ... {MergeSort ...} ... end
proc {Member ...} ... end
end
The book does not say how to instantiate this module directly, but
this documentation page implies it should look like this:
declare [MyListModule] = {Module.apply [MyListFunctor]}
if the functor were bound to the variable MyListFunctor.
But I noticed in this example that no arguments were passed to the
functor. Let's go back to the original function representation, and
enhance it to take an argument:
fun {MyListFunctor SortingModule}
proc {Append ... }
proc {MergeSort ...} ... end
proc {Sort ... } ... {SortingModule.sort ... } ... end
proc {Member ...} ... end
in
`export`(append: Append
sort: Sort
member: Member
...)
end
Now what I'd really like to pass here is just a function, but the
definition of functors only promised that I could pass in modules.
Now suppose I have such a sorting module in a variable:
declare [MergeSortModule] = {Module.link ["MyMergeSort.ozf"]}
Then I could call MyListFunctor with it:
declare MyListModule = {MyListFunctor MergeSortModule}
I ought to be able to do the same with the functor syntax, right?
Except I don't know how to write it. The book says that the
arguments are implemented through import statements. So...
functor MyListFunctor
import
SortingModule
export
append:Append
sort:Sort
member:Member
...
define
proc {Append ... } ... end
proc {MergeSort ... } ... end
proc {Sort ... } ... {SortingModule ...} ... end
proc {Member ...} ... end
end
And then how would I pass the appropriate module in when I
instantiate the functor? I guess I would like to write something like:
declare MyListModule = {Module.apply [MyListFunctor MergeSortModule]}
But this doesn't work. I could wrap the functor declaration in a
function declaration that takes the module argument, but that
defeats the purpose.
Am I missing something? I'm not clear how to apply functors to
arbitrary modules (or to any other sort of argument), rather than
just importing from specific hard-wired places in the filesystem.
Thanks,
Lyle
<ATT00001..txt>
_________________________________________________________________________________
mozart-users mailing list
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users