Variadic function: Difference between revisions

m
{{header|F Sharp|F#}}
m (syntax highlighting fixup automation)
m ({{header|F Sharp|F#}})
 
(19 intermediate revisions by 12 users not shown)
Line 1,026:
println(y)
}</syntaxhighlight>
 
=={{header|Ecstasy}}==
Ecstasy does not support a true variadic function (<i>a la</i> C with "<tt>...</tt>") or a syntactic sugar for the same (<i>a la</i> Java with "<tt>...</tt>" and the underlying <tt>Object[]</tt>). Instead, when a variadic call is needed, the method or function is declared with the desired array type, and the caller simply passes an array value of any length using the literal array syntax:
 
<syntaxhighlight lang="java">
module VariadicFunction {
void show(String[] strings) {
@Inject Console console;
strings.forEach(s -> console.print(s));
}
 
void run() {
show(["hello", "world"]);
 
String s1 = "not";
String s2 = "a";
String s3 = "constant";
String s4 = "literal";
show([s1, s2, s3, s4]);
}
}
</syntaxhighlight>
 
{{out}}
<pre>
hello
world
not
a
constant
literal
</pre>
 
=={{header|Egel}}==
Line 1,035 ⟶ 1,067:
 
=={{header|Elena}}==
ELENA 46.1x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 1,043 ⟶ 1,075:
printAll(params object[] list)
{
for(int i := 0,; i < list.Length,; i+=1+)
{
self.printLine(list[i])
Line 1,101 ⟶ 1,133:
<syntaxhighlight lang="lisp">(let ((arg-list '("some thing %d %d %d" 1 2 3)))
(apply 'message arg-list))</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
^|EMal supports variadic functions in more than one way|^
fun print = void by text mode, List args do
writeLine("== " + mode + " ==")
for each var arg in args do writeLine(arg) end
end
fun printArgumentsList = void by List args
print("accepting a list", args)
end
fun printArgumentsUnchecked = void by some var args
print("unchecked variadic", args)
end
fun printArgumentsChecked = void by text subject, logic isTrue, int howMany, some text values
print("checked variadic", var[subject, isTrue, howMany, +values]) # unary plus on lists does list expansion
end
printArgumentsList(var["These are the ", true, 7, "seas", "of", "Rhye"])
printArgumentsUnchecked("These are the ", true, 7, "seas", "of", "Rhye")
printArgumentsChecked("These are the ", true, 7, "seas", "of", "Rhye")
</syntaxhighlight>
{{out}}
<pre>
== accepting a list ==
These are the
7
seas
of
Rhye
== unchecked variadic ==
These are the
7
seas
of
Rhye
== checked variadic ==
These are the
7
seas
of
Rhye
</pre>
 
=={{header|Erlang}}==
Line 1,137 ⟶ 1,214:
 
print_args({"Mary", "had", "a", "little", "lamb"})</syntaxhighlight>
 
=={{header|F Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Variadic function. Nigel Galloway: March 6th., 2024
open System
type X()=static member F([<ParamArray>] args: Object[]) = args|>Array.iter(printfn "%A")
X.F(23, 3.142, "Nigel", 1u, true)
</syntaxhighlight>
{{output}}
<pre>
23
3.142
Nigel
1
true
</pre>
 
=={{header|Factor}}==
Line 1,255 ⟶ 1,348:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Variadic_function}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
Fōrmulæ does not have variadic functions. However an array can be provided as argument:
In '''[https://formulae.org/?example=Variadic_function this]''' page you can see the program(s) related to this task and their results.
 
'''Examples'''
 
Tee following function accepts a list as its unique parameter. It retrieves the list in vertical form (as a matrix of 1 column):
 
[[File:Fōrmulæ - Variadic function 01.png]]
 
[[File:Fōrmulæ - Variadic function 02.png]]
 
[[File:Fōrmulæ - Variadic function 03.png]]
 
[[File:Fōrmulæ - Variadic function 04.png]]
 
[[File:Fōrmulæ - Variadic function 05.png]]
 
With this approach we can use several parameters being lists with variable number of elements each, for example:
 
[[File:Fōrmulæ - Variadic function 06.png]]
 
[[File:Fōrmulæ - Variadic function 07.png]]
 
[[File:Fōrmulæ - Variadic function 08.png]]
 
=={{header|FutureBasic}}==
Line 1,418 ⟶ 1,533:
c
d</pre>
 
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">(function f
(print (join "\n" args)))
 
(f 1 2 3 4)</syntaxhighlight>
 
=={{header|Io}}==
Line 1,707 ⟶ 1,828:
 
=={{header|Lambdatalk}}==
 
Lambdas are de facto variadic in lambdatalk
 
<syntaxhighlight lang="scheme">
 
{def foo
{lambda {:s} // :s will get any sequence of words
{if {S.empty? {S.restfirst :s}}
{if {S.empty? {S.rest :s}} then else {foo {S.rest :s}}}}}
then {br}{S.first :s}
-> foo
else {br}{S.first :s} {foo {S.rest :s}}}}}
 
{foo hello brave new world} ->
-> hello brave new world
brave
new
world
 
{foo {S.serie 1 10}} ->
-> 1 2 3 4 5 6 7 8 9 10
1
</syntaxhighlight>
2
 
3
=={{header|Lang}}==
4
<syntaxhighlight lang="lang">
5
fp.printAll = (&values...) -> {
6
fn.arrayForEach(&values, fn.println)
7
}
8
 
9
fp.printAll(1, 2, 3)
10
# 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
 
&arr $= [1, abc, xyz, 42.42f]
fp.printAllComb(&arr...)
# 1
# abc
# xyz
# 42.42
</syntaxhighlight>
 
Line 2,327 ⟶ 2,497:
if ^omitted(d) then put skip list (d);
end s;</syntaxhighlight>
 
=={{header|Plain English}}==
The only built-in imperative in Plain English that has variadic arguments is the 'call' imperative:
<syntaxhighlight lang="text">Call [dll name] [dll function] with [a value] and [another value].</syntaxhighlight>
The number of arguments used in the statement varies on the number of arguments needed by the DLL function. Variadic functions cannot be user-defined.
 
=={{header|PowerShell}}==
Line 2,696 ⟶ 2,871:
[1 2 3] 6
[1 2 3 4] 10
</pre>
 
=={{header|RPL}}==
Variable number of arguments are idiomatically passed through the stack, with the last argument specifying how many items shall be taken into account.
{{works with|HP|48G}}
≪ ""
1 ROT '''START'''
" " ROT + SWAP +
'''END''' TAIL
≫ '<span style="color:blue">MKLINE</span>' STO
 
"Mary" "has" "a" "little" "lamb" 5 <span style="color:blue">MKLINE</span>
{{out}}
<pre>
1: "Mary has a little lamb"
</pre>
 
Line 2,999 ⟶ 3,189:
11</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn print_all(things ...string) {
for x in things {
println(x)
Line 3,024 ⟶ 3,214:
=={{header|Wren}}==
Wren doesn't support variadic functions and doesn't really need to as we can just write a function which takes one (or one more) argument and pass it a list.
<syntaxhighlight lang="ecmascriptwren">var printArgs = Fn.new { |args| args.each { |arg| System.print(arg) } }
 
printArgs.call(["Mary", "had", "3", "little", "lambs"])</syntaxhighlight>
2,171

edits