Variadic function: Difference between revisions
Content added Content deleted
(Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag) |
(Add lang example) |
||
Line 1,768: | Line 1,768: | ||
10 |
10 |
||
</syntaxhighlight> |
|||
=={{header|Lang}}== |
|||
<syntaxhighlight lang="lang"> |
|||
fp.printAll = (&values...) -> { |
|||
fn.arrayForEach(&values, fn.println) |
|||
} |
|||
fp.printAll(1, 2, 3) |
|||
# 1 |
|||
# 2 |
|||
# 3 |
|||
fp.printAll() # No output |
|||
fp.printAll(abc, def, xyz) |
|||
# abc |
|||
# def |
|||
# xyz |
|||
# Array un-packing |
|||
&arr $= [1, abc, xyz, 42.42f] |
|||
fp.printAll(&arr...) |
|||
# 1 |
|||
# abc |
|||
# xyz |
|||
# 42.42 |
|||
fp.printAll(&arr..., last) |
|||
# 1 |
|||
# abc |
|||
# xyz |
|||
# 42.42 |
|||
# last |
|||
fp.printAll(first, &arr...) |
|||
# first |
|||
# 1 |
|||
# abc |
|||
# xyz |
|||
# 42.42 |
|||
</syntaxhighlight> |
|||
Solution with the use of arguments auto-pack operator and combinator function: |
|||
<syntaxhighlight lang="lang"> |
|||
fp.printAllComb $= -|fn.combC(fn.arrayForEach, fn.println) |
|||
fp.printAllComb(42, 2, abc) |
|||
# 42 |
|||
# 2 |
|||
# abc |
|||
fp.printAllComb() # No output |
|||
fp.printAllComb(&arr...) |
|||
# 1 |
|||
# abc |
|||
# xyz |
|||
# 42.42 |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||