Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
66
Task/Function-composition/ObjectIcon/function-composition.oi
Normal file
66
Task/Function-composition/ObjectIcon/function-composition.oi
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
# -*- ObjectIcon -*-
|
||||
#
|
||||
# The Rosetta Code function composition task, in Object Icon.
|
||||
# Composition will result in a co-expression.
|
||||
#
|
||||
# Object Icon co-expressions are closures: they share the local
|
||||
# variables of the context in which they are created. In Arizona Icon,
|
||||
# co-expressions obtain only the *values* of those variables. However,
|
||||
# this difference, despite its significance, is not really important
|
||||
# to the notion of composition.
|
||||
#
|
||||
# This example is adapted from the Unicon implementation of the
|
||||
# task. To simplify the example, I have removed support for string
|
||||
# invocations.
|
||||
#
|
||||
|
||||
import io
|
||||
|
||||
procedure main (arglist)
|
||||
local f, g
|
||||
|
||||
# f gets a co-expression that is a composition of three procedures.
|
||||
f := compose(append_exclamation, string_repeat, double_it)
|
||||
write(123@f)
|
||||
|
||||
# g gets a co-expression that is a composition of a procedure and f.
|
||||
g := compose(string_repeat, f)
|
||||
write(123@g)
|
||||
end
|
||||
|
||||
procedure double_it (n)
|
||||
return n + n
|
||||
end
|
||||
|
||||
procedure string_repeat (x)
|
||||
return string(x) || string(x)
|
||||
end
|
||||
|
||||
procedure append_exclamation (s)
|
||||
return s || "!"
|
||||
end
|
||||
|
||||
procedure compose (rfL[])
|
||||
local x, f, saveSource, fL, cf
|
||||
|
||||
every push(fL := [], !rfL)
|
||||
cf := create {
|
||||
saveSource := &source
|
||||
repeat {
|
||||
x := x@saveSource
|
||||
saveSource := &source
|
||||
every f := !fL do {
|
||||
case type(f) of {
|
||||
"co-expression": x := x@f
|
||||
default: x := f(x)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Co-expressions often need to be "primed" before they can be
|
||||
# used.
|
||||
@cf
|
||||
|
||||
return cf
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue