Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
31
Task/Function-composition/Icon/function-composition-2.icon
Normal file
31
Task/Function-composition/Icon/function-composition-2.icon
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
procedure main(arglist)
|
||||
h := compose(sqrt,abs)
|
||||
k := compose(integer,"sqrt",ord)
|
||||
m := compose("-",k)
|
||||
every write(i := -2 to 2, " h=(sqrt,abs)-> ", h(i))
|
||||
every write(c := !"1@Q", " k=(integer,\"sqrt\",ord)-> ", k(c))
|
||||
write(c := "1"," m=(\"-\",k) -> ",m(c))
|
||||
end
|
||||
|
||||
invocable all # permit string invocations
|
||||
|
||||
procedure compose(fL[]) #: compose(f1,f2,...) returns the functional composition of f1,f2,... as a co-expression
|
||||
local x,f,saveSource
|
||||
|
||||
every case type(x := !fL) of {
|
||||
"procedure"|"co-expression": &null # procedures and co-expressions are fine
|
||||
"string" : if not proc(x,1) then runnerr(123,fL) # as are invocable strings (unary operators, and procedures)
|
||||
default: runerr(123,fL)
|
||||
}
|
||||
|
||||
fL := reverse(fL) # reverse and isolate from mutable side-effects
|
||||
cf := create { saveSource := &source # don't forget where we came from
|
||||
repeat {
|
||||
x := (x@saveSource)[1] # return result and resume here
|
||||
saveSource := &source # ...
|
||||
every f := !fL do x := f(x) # apply the list of 'functions'
|
||||
}
|
||||
}
|
||||
return (@cf, cf) # 'prime' the co-expr before returning it
|
||||
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue