September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -4,7 +4,7 @@ Create a function which takes in a variable number of arguments and prints each
Also show, if possible in your language, how to call the function on a list of arguments constructed at runtime.
Functions of this type are also known as   [[wp:Variadic_function|Variadic Functions]].
Functions of this type are also known as [[wp:Variadic_function|Variadic Functions]].
;Related task:

View file

@ -10,9 +10,9 @@ end positionalArgs
-- namedArgs :: Record -> String
on namedArgs(rec)
script showKVpair
on lambda(k)
on |λ|(k)
my putStrLn(k & " -> " & keyValue(rec, k))
end lambda
end |λ|
end script
-- follow each argument name and value with line feed
@ -52,7 +52,7 @@ on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
@ -89,7 +89,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn

View file

@ -0,0 +1,16 @@
' Variadic functions
OPTION BASE 1
SUB demo (VAR arg$ SIZE argc)
LOCAL x
PRINT "Amount of incoming arguments: ", argc
FOR x = 1 TO argc
PRINT arg$[x]
NEXT
END SUB
' No argument
demo(0)
' One argument
demo("abc")
' Three arguments
demo("123", "456", "789")

View file

@ -0,0 +1,18 @@
/* Version a */
define f(a[], l) {
auto i
for (i = 0; i < l; i++) a[i]
}
/* Version b */
define g(a[]) {
auto i
for (i = 0; a[i] != -1; i++) a[i]
}
/* Version c */
define h(a[]) {
auto i
for (i = 1; i <= a[0]; i++) a[i]
}

View file

@ -0,0 +1,18 @@
import system'routines.
import extensions.
extension variadicOp
{
generic printAll args:list
[
list forEach(:v)
[
self printLine:v
]
]
}
program =
[
console printAll("test", "rosetta code", 123, 5.6r).
].

View file

@ -0,0 +1,12 @@
(() => {
'use strict';
// show :: a -> String
const show = x => JSON.stringify(x, null, 2);
// printAll [any] -> String
const printAll = (...a) => a.map(show)
.join('\n');
return printAll(1, 2, 3, 2 + 2, "five", 6);
})();

View file

@ -1,5 +0,0 @@
julia> x = (3,4)
(3,4)
julia> bar(1,2,x...)
(1,2,(3,4))

View file

@ -1,11 +0,0 @@
julia> x = (2,3,4)
(2,3,4)
julia> bar(1,x...)
(1,2,(3,4))
julia> x = (1,2,3,4)
(1,2,3,4)
julia> bar(x...)
(1,2,(3,4))

View file

@ -1,17 +0,0 @@
julia> x = [3,4]
2-element Int64 Array:
3
4
julia> bar(1,2,x...)
(1,2,(3,4))
julia> x = [1,2,3,4]
4-element Int64 Array:
1
2
3
4
julia> bar(x...)
(1,2,(3,4))

View file

@ -1,17 +0,0 @@
baz(a,b) = a + b
julia> args = [1,2]
2-element Int64 Array:
1
2
julia> baz(args...)
3
julia> args = [1,2,3]
3-element Int64 Array:
1
2
3
julia> baz(args...)

View file

@ -0,0 +1,17 @@
// version 1.1
fun variadic(vararg va: String) {
for (v in va) println(v)
}
fun main(args: Array<String>) {
variadic("First", "Second", "Third")
println("\nEnter four strings for the function to print:")
val va = Array(4) { "" }
for (i in 1..4) {
print("String $i = ")
va[i - 1] = readLine()!!
}
println()
variadic(*va)
}

View file

@ -1,3 +1,3 @@
def print_all(*things)
things.each { |x| puts x }
puts things
end

View file

@ -0,0 +1,2 @@
fcn f{vm.arglist.apply2("println")}
f("Mary","had","a","little");

View file

@ -0,0 +1,2 @@
a:="This is a test".split(); //-->L("This","is","a","test")
f(a.xplode()); // xplode takes a list and blows it apart into call args

View file

@ -0,0 +1,2 @@
fcn g{f(vm.pasteArgs(2)}
g(a.xplode());