File fcn_util.icn |
Provides some useful utilities for mimicking some functional language elements
Author: Kevin Wampler (kevin@tapestry.tucson.az.us) Additional code added by Steve Wampler (sbw@tapestry.tucson.az.us)
This file is in the public domain.
This file is part of the lang package.
Source code.Details |
Procedures: |
A closure binds some function parameters at definition time, leaving others unbound until invocation. This procedure defines a closure based on a function and a partial argument list. Unbound parameters are marked with instances of the Arg record constructor (note that the constructor is not called, it is simply a marker). Unbound parameters may be bound at the point of call by providing the values (in order) as arguments. Missing unbound parameters are replaced by &null. Extra arguments are appended.
Sample uses:
h := closure("^",Arg, 2) # x^2 h := closure("^",2, Arg) # 2^x h := closure("^",2) # also 2^x</p>
Compose multiple single-argument functions into a new function. The composite functions are evaluated right-to-left.
Examples:
Given:
h := compose(sqrt, abs) k := compose(integer, sqrt, ord) g := compose(h, k)the call g("1") is equivalent to:
sqrt(abs(integer(sqrt(ord("1")))))
Any Unicon entity that can be evaluated using the standard single-argument function call syntax can be composed. E.g. Given
m := compose(sqrt, closure("^", 2, Arg))The call m(3.5) is equivalent to:
sqrt(2^3.5)</p>
Deprecated. There is a potential conflict with the name of this procedure and the method invoke in the Object class. For example, the assignment: call := invoke seems to think that the Object().invoke method is what's wanted, resulting in an error. Consequently, you should use invokeFcn instead.
fcn | thing (see explanation) to invoke |
args | remaining arguments are missing parameters in closure |
results of evaluating fcn, using args
to fill in gaps in closure |
invokes its first argument with the parameters provided by the remaining arguments, returning the result. Supported types are:
procedure - calls fcn!args integer - calls fcn!args string - calls fcn!args list - calls fcn[1]!(fcn[2:0] ||| args) co-expression - calls args@fcn Closure - calls fcn.call ! args
A PDCO for constructing anonymous 'functions' from co-expressions. It preactivates the co-expression argument to advance it to the first synchronization point. This allows the first 'call' to the anonymous function to pass a value in. The standard boiler plate for such anonymous functions is:
C := makeProc { repeat { inVal := outVal@&source # Code for the function goes here, where # any arguments are in the list inVal. # The return result goes into outVal } }Note that this example assumes that the resulting 'function' will be called using procedure invocation syntax, as in:
a := C(3,4,5)which is equivalent to:
a := [3,4,5]@C
Records: |
A marker that can be used in a list of function parameters to indicate a spot where an argument can be mapped. For example:
invokeFcn([f,3,Arg,5],4,6,7)invokes f(3,4,5,6,7)
This is a singleton class. At most one instance will ever exist, so the === test can be used.