Someone over on the Icon newsgroup asked (indirectly) about
implementing 'anonymous functions' in Icon.  I thought folks
here might like to see one approach that works in both Icon
and Unicon.

Sample source code is attached.

--
Steve Wampler -- [EMAIL PROTECTED]
The gods that smiled on your birth are now laughing out loud.
#<p>
#  A short demonstration on one way to implement <i>anonymous functions</i>
#    in Icon or Unicon using co-expressions.  The approach is based on the
#    technique used to implement closures described in <b>The Generator</b>
#    Vol 2, No 2 [http://unicon.org/generator].
#</p>
procedure main(args)
    local C, inVal, outVal

    # The anonymous 'function', as a co-expression.  Most of the code
    #   is standard boilerplate needed to use a co-expression as
    #   an anonymous function.
    C := makeProc {                             
             repeat {
                 inVal := outVal@&source
                 outVal := inVal[1] + inVal[2]   # f(a,b) -> a+b
                 }
             }

    # Example invocations
    write(invoke(C, 1,2))
    write(invoke(C, 3,4))

end

#<p>
#  Invokes co-expression <tt>c1</tt> with <tt>a1</tt> as the argument list
#</p>
procedure invoke(c1, a1[])
   return [EMAIL PROTECTED]
end

#<p>
#  PDCO to preactivate a co-expression so it is ready to accept
#    an input value on the next activation.
#</p>
procedure makeProc(A)
    return (@A[1],A[1])
end
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Unicon-group mailing list
Unicon-group@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to