Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
category:
- Functions and subroutines
from: http://rosettacode.org/wiki/Variadic_function
note: Basic language learning

View file

@ -0,0 +1,13 @@
;Task:
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]].
;Related task:
*   [[Call a function]]
<br><br>

View file

@ -0,0 +1,8 @@
(defun print-all-fn (xs)
(if (endp xs)
nil
(prog2$ (cw "~x0~%" (first xs))
(print-all-fn (rest xs)))))
(defmacro print-all (&rest args)
`(print-all-fn (quote ,args)))

View file

@ -0,0 +1,17 @@
main:(
MODE STRINT = UNION(STRING, INT, PROC(REF FILE)VOID, VOID);
PROC print strint = (FLEX[]STRINT argv)VOID: (
FOR i TO UPB argv DO
CASE argv[i] IN
(INT i):print(whole(i,-1)),
(STRING s):print(s),
(PROC(REF FILE)VOID f):f(stand out),
(VOID):print(error char)
ESAC;
IF i NE UPB argv THEN print((" ")) FI
OD
);
print strint(("Mary","had",1,"little",EMPTY,new line))
)

View file

@ -0,0 +1,11 @@
function f(a, b, c){
if (a != "") print a
if (b != "") print b
if (c != "") print c
}
BEGIN {
print "[1 arg]"; f(1)
print "[2 args]"; f(1, 2)
print "[3 args]"; f(1, 2, 3)
}

View file

@ -0,0 +1,13 @@
function f(a, b, c) {
if (a != "") print a
if (b != "") print b
if (c != "") print c
}
BEGIN {
# Set ary[1] and ary[2] at runtime.
split("Line 1:Line 2", ary, ":")
# Pass to f().
f(ary[1], ary[2], ary[3])
}

View file

@ -0,0 +1,9 @@
function g(len, ary, i) {
for (i = 1; i <= len; i++) print ary[i];
}
BEGIN {
c = split("Line 1:Line 2:Next line is empty::Last line", a, ":")
g(c, a) # Pass a[1] = "Line 1", a[4] = "", ...
}

View file

@ -0,0 +1,5 @@
public function printArgs(... args):void
{
for (var i:int = 0; i < args.length; i++)
trace(args[i]);
}

View file

@ -0,0 +1,30 @@
with Ada.Strings.Unbounded, Ada.Text_IO;
procedure Variadic is
subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
use type U_String;
function "+"(S: String) return U_String
renames Ada.Strings.Unbounded.To_Unbounded_String;
function "-"(U: U_String) return String
renames Ada.Strings.Unbounded.To_String;
type Variadic_Array is array(Positive range <>) of U_String;
procedure Print_Line(Params: Variadic_Array) is
begin
for I in Params'Range loop
Ada.Text_IO.Put(-Params(I));
if I < Params'Last then
Ada.Text_IO.Put(" ");
end if;
end loop;
Ada.Text_IO.New_Line;
end Print_Line;
begin
Print_Line((+"Mary", +"had", +"a", +"little", +"lamb.")); -- print five strings
Print_Line((1 => +"Rosetta Code is cooool!")); -- print one string
end;

View file

@ -0,0 +1,20 @@
void
f(...)
{
integer i;
i = 0;
while (i < count()) {
o_text($i);
o_byte('\n');
i += 1;
}
}
integer
main(void)
{
f("Mary", "had", "a", "little", "lamb");
return 0;
}

View file

@ -0,0 +1,32 @@
void
output_date(date d)
{
o_form("~%//f2/%//f2/", d.year, d.y_month, d.m_day);
}
void
g(...)
{
integer i;
record r;
r["integer"] = o_integer;
r["real"] = o_;
r["text"] = o_text;
r["date"] = output_date;
i = 0;
while (i < count()) {
r[__type($i)]($i);
o_byte('\n');
i += 1;
}
}
integer
main(void)
{
g("X.1", 707, .5, date().now);
return 0;
}

View file

@ -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 |λ|(k)
my putStrLn(k & " -> " & keyValue(rec, k))
end |λ|
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 |λ|(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 |λ| : f
end script
end if
end mReturn

View file

@ -0,0 +1,9 @@
10 P$(0) = STR$(5)
20 P$(1) = "MARY"
30 P$(2) = "HAD"
40 P$(3) = "A"
50 P$(4) = "LITTLE"
60 P$(5) = "LAMB"
70 GOSUB 90"VARIADIC FUNCTION
80 END
90 FOR I = 1 TO VAL(P$(0)) : ? P$(I) : P$(I) = "" : NEXT I : P$(0) = "" : RETURN

View file

@ -0,0 +1,29 @@
;-------------------------------------------
; a quasi-variadic function
;-------------------------------------------
variadic: function [args][
loop args 'arg [
print arg
]
]
; calling function with one block param
; and the arguments inside
variadic ["one" 2 "three"]
;-------------------------------------------
; a function with optional attributes
;-------------------------------------------
variable: function [args][
print ["args:" args]
if? attr? "with" [
print ["with:" attr "with"]
]
else [
print "without attributes"
]
]
variable "yes"
variable.with:"something" "yes!"

View file

@ -0,0 +1,5 @@
printAll(args*) {
for k,v in args
t .= v "`n"
MsgBox, %t%
}

View file

@ -0,0 +1,3 @@
printAll(4, 3, 5, 6, 4, 3)
printAll(4, 3, 5)
printAll("Rosetta", "Code", "Is", "Awesome!")

View file

@ -0,0 +1,2 @@
args := ["Rosetta", "Code", "Is", "Awesome!"]
printAll(args*)

View file

@ -0,0 +1,13 @@
string = Mary had a little lamb
StringSplit, arg, string, %A_Space%
Function(arg1,arg2,arg3,arg4,arg5) ;Calls the function with 5 arguments.
Function() ;Calls the function with no arguments.
return
Function(arg1="",arg2="",arg3="",arg4="",arg5="") {
Loop,5
If arg%A_Index% !=
out .= arg%A_Index% "`n"
MsgBox,% out ? out:"No non-blank arguments were passed."
}

View file

@ -0,0 +1,12 @@
SUB printAll cdecl (count As Integer, ... )
DIM arg AS Any Ptr
DIM i AS Integer
arg = va_first()
FOR i = 1 To count
PRINT va_arg(arg, Double)
arg = va_next(arg, Double)
NEXT i
END SUB
printAll 3, 3.1415, 1.4142, 2.71828

View file

@ -0,0 +1,13 @@
get "libhdr"
// A, B, C, etc are dummy arguments. If more are needed, more can be added.
// Eventually you will run into the compiler limit.
let foo(num, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) be
// The arguments can be indexed starting from the first one.
for i=1 to num do writef("%S*N", (@num)!i)
// You can pass as many arguments as you want. The declaration above guarantees
// that at least the first 16 arguments (including the number) will be available,
// but you certainly needn't use them all.
let start() be
foo(5, "Mary", "had", "a", "little", "lamb")

View file

@ -0,0 +1,3 @@
Fun1 •Show¨
Fun2 {•Show¨𝕩}
Fun3 { 1=𝕩 ? •Show 𝕩; "too many arguments " ! 𝕩}

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,13 @@
@echo off
:_main
call:_variadicfunc arg1 "arg 2" arg-3
pause>nul
:_variadicfunc
setlocal
for %%i in (%*) do echo %%~i
exit /b
:: Note: if _variadicfunc was called from cmd.exe with arguments parsed to it, it would only need to contain:
:: @for %%i in (%*) do echo %%i

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,21 @@
#include <iostream>
template<typename T>
void print(T const& t)
{
std::cout << t;
}
template<typename First, typename ... Rest>
void print(First const& first, Rest const& ... rest)
{
std::cout << first;
print(rest ...);
}
int main()
{
int i = 10;
std::string s = "Hello world";
print("i = ", i, " and s = \"", s, "\"\n");
}

View file

@ -0,0 +1,13 @@
using System;
class Program {
static void Main(string[] args) {
PrintAll("test", "rosetta code", 123, 5.6);
}
static void PrintAll(params object[] varargs) {
foreach (var i in varargs) {
Console.WriteLine(i);
}
}
}

View file

@ -0,0 +1,13 @@
#include <stdio.h>
#include <stdarg.h>
void varstrings(int count, ...) /* the ellipsis indicates variable arguments */
{
va_list args;
va_start(args, count);
while (count--)
puts(va_arg(args, const char *));
va_end(args);
}
varstrings(5, "Mary", "had", "a", "little", "lamb");

View 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.

View file

@ -0,0 +1,6 @@
(defn foo [& args]
(doseq [a args]
(println a)))
(foo :bar :baz :quux)
(apply foo [:bar :baz :quux])

View file

@ -0,0 +1,8 @@
(defun example (&rest args)
(dolist (arg args)
(print arg)))
(example "Mary" "had" "a" "little" "lamb")
(let ((args '("Mary" "had" "a" "little" "lamb")))
(apply #'example args))

View file

@ -0,0 +1,4 @@
Fixpoint Arity (A B: Set) (n: nat): Set := match n with
|O => B
|S n' => A -> (Arity A B n')
end.

View file

@ -0,0 +1 @@
Definition nat_twobools (n: nat) := Arity nat (Arity bool nat (2*n)) n.

View file

@ -0,0 +1,5 @@
Require Import List.
Fixpoint build_list_aux {A: Set} (acc: list A) (n : nat): Arity A (list A) n := match n with
|O => acc
|S n' => fun (val: A) => build_list_aux (acc ++ (val :: nil)) n'
end.

View file

@ -0,0 +1 @@
Definition build_list {A: Set} := build_list_aux (@nil A).

View file

@ -0,0 +1 @@
Check build_list 5 1 2 5 90 42.

View file

@ -0,0 +1,11 @@
Lemma transparent_plus_zero: forall n, n + O = n.
intros n; induction n.
- reflexivity.
- simpl; rewrite IHn; trivial.
Defined.
Lemma transparent_plus_S: forall n m, n + S m = S n + m .
intros n; induction n; intros m.
- reflexivity.
- simpl; f_equal; rewrite IHn; reflexivity.
Defined.

View file

@ -0,0 +1,7 @@
Require Import Vector.
Definition build_vector_aux {A: Set} (n: nat): forall (size_acc : nat) (acc: t A size_acc), Arity A (t A (size_acc + n)) n.
induction n; intros size_acc acc.
- rewrite transparent_plus_zero; apply acc. (*Just one argument, return the accumulator*)
- intros val. rewrite transparent_plus_S. apply IHn. (*Here we use the induction hypothesis. We just have to build the new accumulator*)
apply shiftin; [apply val | apply acc]. (*Shiftin adds a term at the end of a vector*)

View file

@ -0,0 +1 @@
Definition build_vector {A: Set} (n: nat) := build_vector_aux n O (@nil A).

View file

@ -0,0 +1,2 @@
Require Import String.
Eval compute in build_vector 4 "Hello" "how" "are" "you".

View file

@ -0,0 +1,23 @@
import std.stdio, std.algorithm;
void printAll(TyArgs...)(TyArgs args) {
foreach (el; args)
el.writeln;
}
// Typesafe variadic function for dynamic array
void showSum1(int[] items...) {
items.sum.writeln;
}
// Typesafe variadic function for fixed size array
void showSum2(int[4] items...) {
items[].sum.writeln;
}
void main() {
printAll(4, 5.6, "Rosetta", "Code", "is", "awesome");
writeln;
showSum1(1, 3, 50);
showSum2(1, 3, 50, 10);
}

View file

@ -0,0 +1,7 @@
func printAll(args...) {
for i in args {
print(i)
}
}
printAll("test", "rosetta code", 123, 5.6)

View file

@ -0,0 +1,11 @@
def example {
match [`run`, args] {
for x in args {
println(x)
}
}
}
example("Mary", "had", "a", "little", "lamb")
E.call(example, "run", ["Mary", "had", "a", "little", "lamb"])

View file

@ -0,0 +1,6 @@
def non_example {
to run(x, y) {
println(x)
println(y)
}
}

View file

@ -0,0 +1,4 @@
def non_example(x, y) {
println(x)
println(y)
}

View file

@ -0,0 +1,17 @@
^|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")

View file

@ -0,0 +1,16 @@
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]);
}
}

View file

@ -0,0 +1 @@
[ X Y -> "two" | X -> "one" | -> "zero" ]

View file

@ -0,0 +1,18 @@
import system'routines;
import extensions;
extension variadicOp
{
printAll(params object[] list)
{
for(int i := 0, i < list.Length, i+=1)
{
self.printLine(list[i])
}
}
}
public program()
{
console.printAll("test", "rosetta code", 123, 5.6r)
}

View file

@ -0,0 +1,8 @@
defmodule RC do
def print_each( arguments ) do
Enum.each(arguments, fn x -> IO.inspect x end)
end
end
RC.print_each([1,2,3])
RC.print_each(["Mary", "had", "a", "little", "lamb"])

View file

@ -0,0 +1,6 @@
(defun my-print-args (&rest arg-list)
(message "there are %d argument(s)" (length arg-list))
(dolist (arg arg-list)
(message "arg is %S" arg)))
(my-print-args 1 2 3)

View file

@ -0,0 +1,2 @@
(let ((arg-list '("some thing %d %d %d" 1 2 3)))
(apply 'message arg-list))

View file

@ -0,0 +1 @@
print_each( Arguments ) -> [io:fwrite( "~p~n", [X]) || X <- Arguments].

View file

@ -0,0 +1,15 @@
>function allargs () ...
$ loop 1 to argn();
$ args(#),
$ end
$endfunction
>allargs(1,3,"Test",1:2)
1
3
Test
[ 1 2 ]
>function args test (x) := {x,x^2,x^3}
>allargs(test(4))
4
16
64

View file

@ -0,0 +1,8 @@
procedure print_args(sequence args)
for i = 1 to length(args) do
puts(1,args[i])
puts(1,' ')
end for
end procedure
print_args({"Mary", "had", "a", "little", "lamb"})

View file

@ -0,0 +1 @@
MACRO: variadic-print ( n -- quot ) [ print ] n*quot ;

View file

@ -0,0 +1,13 @@
IN: scratchpad "apple" "banana" "cucumber"
--- Data stack:
"apple"
"banana"
"cucumber"
IN: scratchpad 2 variadic-print
cucumber
banana
--- Data stack:
"apple"

View file

@ -0,0 +1,2 @@
: sum ( x_1 ... x_n n -- sum ) 1 ?do + loop ;
4 3 2 1 4 sum . \ 10

View file

@ -0,0 +1 @@
: .stack ( -- ) depth 0 ?do i pick . loop ;

View file

@ -0,0 +1,33 @@
program varargs
integer, dimension(:), allocatable :: va
integer :: i
! using an array (vector) static
call v_func()
call v_func( (/ 100 /) )
call v_func( (/ 90, 20, 30 /) )
! dynamically creating an array of 5 elements
allocate(va(5))
va = (/ (i,i=1,5) /)
call v_func(va)
deallocate(va)
contains
subroutine v_func(arglist)
integer, dimension(:), intent(in), optional :: arglist
integer :: i
if ( present(arglist) ) then
do i = lbound(arglist, 1), ubound(arglist, 1)
print *, arglist(i)
end do
else
print *, "no argument at all"
end if
end subroutine v_func
end program varargs

View file

@ -0,0 +1,39 @@
program variadicRoutinesDemo(input, output, stdErr);
{$mode objFPC}
// array of const is only supported in $mode objFPC or $mode Delphi
procedure writeLines(const arguments: array of const);
var
argument: TVarRec;
begin
// inside the body `array of const` is equivalent to `array of TVarRec`
for argument in arguments do
begin
with argument do
begin
case vType of
vtInteger:
begin
writeLn(vInteger);
end;
vtBoolean:
begin
writeLn(vBoolean);
end;
vtChar:
begin
writeLn(vChar);
end;
vtAnsiString:
begin
writeLn(ansiString(vAnsiString));
end;
// and so on
end;
end;
end;
end;
begin
writeLines([42, 'is', true, #33]);
end.

View file

@ -0,0 +1,4 @@
42
is
TRUE
!

View file

@ -0,0 +1,39 @@
' version 15-09-2015
' compile with: fbc -s console
Sub printAll_string Cdecl (count As Integer, ... )
Dim arg As Any Ptr
Dim i As Integer
arg = va_first()
For i = 1 To count
Print *Va_Arg(arg, ZString Ptr)
arg = va_next(arg, ZString Ptr)
Next i
End Sub
' ------=< MAIN >=------
' direct
printAll_string (5, "Foxtrot", "Romeo", "Echo", "Echo", "BASIC")
' strings
Print : Print
Dim As String a = "one", b = "two", c = "three"
printAll_string (3, a, b, c)
' count is smaller then the number of arguments, no problem
Print : Print
printAll_string (1, a, b, c)
' count is greater then the number of arguments
' after the last valid argument garbage is displayed
' should be avoided, could lead to disaster
Print : Print
printAll_string (4, a, b, c)
Print
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View 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",

View file

@ -0,0 +1,37 @@
void local fn Function1( count as long, ... )
va_list ap
long value
va_start( ap, count )
while ( count )
value = fn va_argLong( ap )
printf @"%ld",value
count--
wend
va_end( ap )
end fn
void local fn Function2( obj as CFTypeRef, ... )
va_list ap
va_start( ap, obj )
while ( obj )
printf @"%@",obj
obj = fn va_argObj(ap)
wend
va_end( ap )
end fn
window 1
// params: num of args, 1st arg, 2nd arg, etc.
fn Function1( 3, 987, 654, 321 )
print
// params: 1st arg, 2nd arg, ..., NULL
fn Function2( @"One", @"Two", @"Three", @"O'Leary", NULL )
HandleEvents

View file

@ -0,0 +1,6 @@
func printAll(things ... string) {
// it's as if you declared "things" as a []string, containing all the arguments
for _, x := range things {
fmt.Println(x)
}
}

View file

@ -0,0 +1,2 @@
args := []string{"foo", "bar"}
printAll(args...)

View file

@ -0,0 +1,24 @@
#!/usr/bin/env golosh
----
This module demonstrates variadic functions.
----
module Variadic
import gololang.Functions
----
Varargs have the three dots after them just like Java.
----
function varargsFunc = |args...| {
foreach arg in args {
println(arg)
}
}
function main = |args| {
varargsFunc(1, 2, 3, 4, 5, "against", "one")
# to call a variadic function with an array we use the unary function
unary(^varargsFunc)(args)
}

View file

@ -0,0 +1,3 @@
def printAll( Object[] args) { args.each{ arg -> println arg } }
printAll(1, 2, "three", ["3", "4"])

View file

@ -0,0 +1,17 @@
class PrintAllType t where
process :: [String] -> t
instance PrintAllType (IO a) where
process args = do mapM_ putStrLn args
return undefined
instance (Show a, PrintAllType r) => PrintAllType (a -> r) where
process args = \a -> process (args ++ [show a])
printAll :: (PrintAllType t) => t
printAll = process []
main :: IO ()
main = do printAll 5 "Mary" "had" "a" "little" "lamb"
printAll 4 3 5
printAll "Rosetta" "Code" "Is" "Awesome!"

View file

@ -0,0 +1,7 @@
{-# LANGUAGE GADTs #-}
...
instance a ~ () => PrintAllType (IO a) where
process args = do mapM_ putStrLn args
...

View file

@ -0,0 +1,9 @@
procedure main ()
varargs("some", "extra", "args")
write()
varargs ! ["a","b","c","d"]
end
procedure varargs(args[])
every write(!args)
end

View file

@ -0,0 +1 @@
printAll := method(call message arguments foreach(println))

View file

@ -0,0 +1,6 @@
A=:2
B=:3
C=:5
sum=:+/
sum 1,A,B,4,C
15

View file

@ -0,0 +1,3 @@
commaAnd=: [: ; (<' and ') _2} ::] 1 }.&, (<', ') ,. ":each
commaAnd 'dog';A;B;'cat';C
dog, 2, 3, cat and 5

View file

@ -0,0 +1,6 @@
echo&>'dog';A;B;'cat';C
dog
2
3
cat
5

View file

@ -0,0 +1,6 @@
public static void printAll(Object... things){
// "things" is an Object[]
for(Object i:things){
System.out.println(i);
}
}

View file

@ -0,0 +1,3 @@
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awesome!");

View file

@ -0,0 +1,2 @@
Object[] args = {"Rosetta", "Code", "Is", "Awesome!"};
printAll(args);

View file

@ -0,0 +1,3 @@
Object[] args = {"Rosetta", "Code", "Is", "Awesome,"};
printAll(args, "Dude!");//does not print "Rosetta Code Is Awesome, Dude!"
//instead prints the type and hashcode for args followed by "Dude!"

View file

@ -0,0 +1 @@
printAll((Object)args);

View file

@ -0,0 +1,7 @@
function printAll() {
for (var i=0; i<arguments.length; i++)
print(arguments[i])
}
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awesome!");

View file

@ -0,0 +1,2 @@
args = ["Rosetta", "Code", "Is", "Awesome!"]
printAll.apply(null, args)

View file

@ -0,0 +1,29 @@
let
fix = // Variant of the applicative order Y combinator
f => (f => f(f))(g => f((...a) => g(g)(...a))),
forAll =
f =>
fix(
z => (a,...b) => (
(a === void 0)
||(f(a), z(...b)))),
printAll = forAll(print);
printAll(0,1,2,3,4,5);
printAll(6,7,8);
(f => a => f(...a))(printAll)([9,10,11,12,13,14]);
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
// 11
// 12
// 13
// 14

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

@ -0,0 +1 @@
def demo: .[];

View file

@ -0,0 +1 @@
args | demo

View file

@ -0,0 +1 @@
["cheese"] + [3.14] + [[range(0;3)]] | demo

View file

@ -0,0 +1,3 @@
"cheese"
3.14
[0,1,2]

View file

@ -0,0 +1,13 @@
# arity-0:
def f: "I have no arguments";
# arity-1:
def f(a1): a1;
# arity-1:
def f(a1;a2): a1,a2;
def f(a1;a2;a3): a1,a2,a3;
# Example:
f, f(1), f(2;3), f(4;5;6)

View file

@ -0,0 +1,6 @@
1
2
3
4
5
6

View file

@ -0,0 +1,6 @@
julia> print_each(X...) = for x in X; println(x); end
julia> print_each(1, "hello", 23.4)
1
hello
23.4

View file

@ -0,0 +1,10 @@
julia> args = [ "first", (1,2,17), "last" ]
3-element Array{Any,1}:
"first"
(1,2,17)
"last
julia> print_each(args...)
first
(1,2,17)
last

View file

@ -0,0 +1,12 @@
:varfunc
1 tolist flatten
len [
get print nl
] for
drop
;
"Enter any number of words separated by space: " input nl
stklen [split varfunc nl] if
nl "End " input

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

@ -0,0 +1,24 @@
#!/bin/ksh
# Variadic function
# # Variables:
#
typeset -a arr=( 0 2 4 6 8 )
# # Functions:
#
function _variadic {
while [[ -n $1 ]]; do
print $1
shift
done
}
######
# main #
######
_variadic Mary had a little lamb
echo
_variadic ${arr[@]}

View file

@ -0,0 +1,11 @@
{def foo
{lambda {:s} // :s will get any sequence of words
{S.first :s}
{if {S.empty? {S.rest :s}} then else {foo {S.rest :s}}}}}
-> foo
{foo hello brave new world}
-> hello brave new world
{foo {S.serie 1 10}}
-> 1 2 3 4 5 6 7 8 9 10

View file

@ -0,0 +1,37 @@
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

View file

@ -0,0 +1,15 @@
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

View file

@ -0,0 +1,5 @@
define printArgs(...items) => stdoutnl(#items)
define printEachArg(...) => with i in #rest do stdoutnl(#i)
printArgs('a', 2, (:3))
printEachArg('a', 2, (:3))

View file

@ -0,0 +1,2 @@
local(args = (:"Rosetta", "Code", "Is", "Awesome!"))
printEachArg(:#args)

Some files were not shown because too many files have changed in this diff Show more