2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,4 +1,12 @@
|
|||
Create a function which takes in a variable number of arguments and prints each one on its own line. 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]].
|
||||
;Task:
|
||||
Create a function which takes in a variable number of arguments and prints each one on its own line.
|
||||
|
||||
Related: [[Call a function]]
|
||||
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]].
|
||||
|
||||
|
||||
;Related task:
|
||||
* [[Call a function]]
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Functions and subroutines
|
||||
note: Basic language learning
|
||||
|
|
|
|||
|
|
@ -0,0 +1,95 @@
|
|||
use framework "Foundation"
|
||||
|
||||
-- positionalArgs :: [a] -> String
|
||||
on positionalArgs(xs)
|
||||
|
||||
-- follow each argument with a line feed
|
||||
map(my putStrLn, xs) as string
|
||||
end positionalArgs
|
||||
|
||||
-- namedArgs :: Record -> String
|
||||
on namedArgs(rec)
|
||||
script showKVpair
|
||||
on lambda(k)
|
||||
my putStrLn(k & " -> " & keyValue(rec, k))
|
||||
end lambda
|
||||
end script
|
||||
|
||||
-- follow each argument name and value with line feed
|
||||
map(showKVpair, allKeys(rec)) as string
|
||||
end namedArgs
|
||||
|
||||
-- TEST
|
||||
on run
|
||||
intercalate(linefeed, ¬
|
||||
{positionalArgs(["alpha", "beta", "gamma", "delta"]), ¬
|
||||
namedArgs({epsilon:27, zeta:48, eta:81, theta:8, iota:1})})
|
||||
|
||||
--> "alpha
|
||||
-- beta
|
||||
-- gamma
|
||||
-- delta
|
||||
--
|
||||
-- epsilon -> 27
|
||||
-- eta -> 81
|
||||
-- iota -> 1
|
||||
-- zeta -> 48
|
||||
-- theta -> 8
|
||||
-- "
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS
|
||||
|
||||
-- putStrLn :: a -> String
|
||||
on putStrLn(a)
|
||||
(a as string) & linefeed
|
||||
end putStrLn
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
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)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- intercalate :: Text -> [Text] -> Text
|
||||
on intercalate(strText, lstText)
|
||||
set {dlm, my text item delimiters} to {my text item delimiters, strText}
|
||||
set strJoined to lstText as text
|
||||
set my text item delimiters to dlm
|
||||
return strJoined
|
||||
end intercalate
|
||||
|
||||
-- allKeys :: Record -> [String]
|
||||
on allKeys(rec)
|
||||
(current application's NSDictionary's dictionaryWithDictionary:rec)'s allKeys() as list
|
||||
end allKeys
|
||||
|
||||
-- keyValue :: Record -> String -> Maybe String
|
||||
on keyValue(rec, strKey)
|
||||
set ca to current application
|
||||
set v to (ca's NSDictionary's dictionaryWithDictionary:rec)'s objectForKey:strKey
|
||||
if v is not missing value then
|
||||
item 1 of ((ca's NSArray's arrayWithObject:v) as list)
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end keyValue
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -33,7 +33,7 @@ printAll_string (4, a, b, c)
|
|||
Print
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Var _key_ = InKey : Wend
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
|
|||
13
Task/Variadic-function/BASIC/variadic-function-3.basic
Normal file
13
Task/Variadic-function/BASIC/variadic-function-3.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
100 DEF PROC printAll DATA
|
||||
110 DO UNTIL ITEM()=0
|
||||
120 IF ITEM()=1 THEN
|
||||
READ a$
|
||||
PRINT a$
|
||||
130 ELSE
|
||||
READ num
|
||||
PRINT num
|
||||
140 LOOP
|
||||
150 END PROC
|
||||
|
||||
200 printAll 3.1415, 1.4142, 2.71828
|
||||
210 printAll "Mary", "had", "a", "little", "lamb",
|
||||
58
Task/Variadic-function/COBOL/variadic-function.cobol
Normal file
58
Task/Variadic-function/COBOL/variadic-function.cobol
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
program-id. dsp-str is external.
|
||||
data division.
|
||||
linkage section.
|
||||
1 cnt comp-5 pic 9(4).
|
||||
1 str pic x.
|
||||
procedure division using by value cnt
|
||||
by reference str delimited repeated 1 to 5.
|
||||
end program dsp-str.
|
||||
|
||||
program-id. variadic.
|
||||
procedure division.
|
||||
call "dsp-str" using 4 "The" "quick" "brown" "fox"
|
||||
stop run
|
||||
.
|
||||
end program variadic.
|
||||
|
||||
program-id. dsp-str.
|
||||
data division.
|
||||
working-storage section.
|
||||
1 i comp-5 pic 9(4).
|
||||
1 len comp-5 pic 9(4).
|
||||
1 wk-string pic x(20).
|
||||
linkage section.
|
||||
1 cnt comp-5 pic 9(4).
|
||||
1 str1 pic x(20).
|
||||
1 str2 pic x(20).
|
||||
1 str3 pic x(20).
|
||||
1 str4 pic x(20).
|
||||
1 str5 pic x(20).
|
||||
procedure division using cnt str1 str2 str3 str4 str5.
|
||||
if cnt < 1 or > 5
|
||||
display "Invalid number of parameters"
|
||||
stop run
|
||||
end-if
|
||||
perform varying i from 1 by 1
|
||||
until i > cnt
|
||||
evaluate i
|
||||
when 1
|
||||
unstring str1 delimited low-value
|
||||
into wk-string count in len
|
||||
when 2
|
||||
unstring str2 delimited low-value
|
||||
into wk-string count in len
|
||||
when 3
|
||||
unstring str3 delimited low-value
|
||||
into wk-string count in len
|
||||
when 4
|
||||
unstring str4 delimited low-value
|
||||
into wk-string count in len
|
||||
when 5
|
||||
unstring str5 delimited low-value
|
||||
into wk-string count in len
|
||||
end-evaluate
|
||||
display wk-string (1:len)
|
||||
end-perform
|
||||
exit program
|
||||
.
|
||||
end program dsp-str.
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
<?php
|
||||
function printAll() {
|
||||
foreach (func_get_args() as $x) // first way
|
||||
echo "$x\n";
|
||||
|
|
@ -9,3 +10,4 @@ function printAll() {
|
|||
printAll(4, 3, 5, 6, 4, 3);
|
||||
printAll(4, 3, 5);
|
||||
printAll("Rosetta", "Code", "Is", "Awesome!");
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
<?php
|
||||
$args = array("Rosetta", "Code", "Is", "Awesome!");
|
||||
call_user_func_array('printAll', $args);
|
||||
?>
|
||||
|
|
|
|||
9
Task/Variadic-function/PHP/variadic-function-3.php
Normal file
9
Task/Variadic-function/PHP/variadic-function-3.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
function printAll(...$things) {
|
||||
foreach ($things as $x)
|
||||
echo "$x\n";
|
||||
}
|
||||
printAll(4, 3, 5, 6, 4, 3);
|
||||
printAll(4, 3, 5);
|
||||
printAll("Rosetta", "Code", "Is", "Awesome!");
|
||||
?>
|
||||
4
Task/Variadic-function/PHP/variadic-function-4.php
Normal file
4
Task/Variadic-function/PHP/variadic-function-4.php
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?php
|
||||
$args = ["Rosetta", "Code", "Is", "Awesome!"];
|
||||
printAll(...$args);
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue