|
|
|
|
|
by slaymaker1907
1704 days ago
|
|
Please write a type for the following function: def compose(start, *args):
def helper(x):
for func in reversed(args):
x = func(x)
return start(x)
return helper
There is no mainstream typed language which can write a fully general type for the vararg compose function. TypeScript is probably the one that comes closest, but last I checked it still was unable to write a sufficiently powerful array type. You can write a type for a version of compose with a fixed number of arguments, but not for one working over an arbitrary number of arguments. |
|
Your toy example, even generalised, has no practical use. If I can write this:
Then I can write that instead (Haskell): Or this (F#): And now we’ve reduced the problem to a simple function composition, which is very easy to define (Ocaml): This generalises to any fold where the programmer would provide the list statically (as they always would for a vararg function): instead of trying to type the whole thing, just define & type the underlying binary operation.