Or maybe there is a way to emulate anonymous functions?
For example(simple example to show what I mean):
procedure myfunc(l1, p1)
return p1 ! l1
end
write(myfunc([1, 2] , procedure(a, b) return a+b)) #==> will print 3
I don't want to do something like:
procedure p1(a, b)
return a + b
end
#and than
write(myfunc([1, 2], p1)
I think there is a way to emulate anonymous functions with a
coroutine, am I right?
Thanks,
Igor
There is no easy way, unfortunately. You could write support
procedures that invoke the icon translator and then load the
result (I think - I haven't tried this, but seem to recall that
someone else has...) for use.
> Or maybe there is a way to emulate anonymous functions?
> For example(simple example to show what I mean):
>
> procedure myfunc(l1, p1)
> return p1 ! l1
> end
>
> write(myfunc([1, 2] , procedure(a, b) return a+b)) #==> will print 3
...
> I think there is a way to emulate anonymous functions with a
> coroutine, am I right?
Yes, though it's not as clean you one might like. Here's an example
matching the above:
->cat f1.icn
procedure main(args)
local C, a, outVal
write(myfunc([1,2], 1(C := create repeat {
a := outVal@&source
outVal := a[1]+a[2]
}, @C)) )
write(myfunc([3,4], C)
end
procedure myfunc(l1, c1)
return l1@c1
end
->icon f1.icn
3
7
->
There's a explanation on how this works in the 3rd issue of The Generator,
which can be found at http://unicon.org/generator (you want Vol 2, No 2).
The article discusses how to use Co-expressions to implement closures, but
uses, and decribes, the same approach as used above.
You could clean this up a (little) bit using a programmer-defined control
operation (PDCO):
procedure makeProc(a)
@a[1]
return a[1]
end
which would let you write the above as:
->cat f2.icn
procedure main(args)
local C, a, outVal
C := makeProc{ repeat {
a := outVal@&source
outVal := a[1] + a[2]
} }
write(myfunc([1,2], C))
write(myfunc([3,4], C))
end
procedure myfunc(l1, c1)
return l1@c1
end
procedure makeProc(A)
return (@A[1],A[1])
end
->icon f2.icn
3
7
->
I hope this helps!
--
Steve Wampler -- swam...@noao.edu
The gods that smiled on your birth are now laughing out loud.
I played a bit more and produced a cleaner (with comments, no less!)
version that I think helps better show how this trick works:
-----------------------------------------------------------
#<p>
# A short demonstration on one way to implement <i>anonymous functions</i>
# in Icon or Unicon using co-expressions.
#</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 a1@c1
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
----------------------------------------------------
-Steve
Thanks for your help,
Igor