Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
2
Task/Repeat/00-META.yaml
Normal file
2
Task/Repeat/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Repeat
|
||||
6
Task/Repeat/00-TASK.txt
Normal file
6
Task/Repeat/00-TASK.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
;Task:
|
||||
Write a procedure which accepts as arguments another procedure and a positive integer.
|
||||
|
||||
The latter procedure is executed a number of times equal to the accepted integer.
|
||||
<br><br>
|
||||
|
||||
8
Task/Repeat/11l/repeat.11l
Normal file
8
Task/Repeat/11l/repeat.11l
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
F repeat(f, n)
|
||||
L 1..n
|
||||
f()
|
||||
|
||||
F procedure()
|
||||
print(‘Example’)
|
||||
|
||||
repeat(procedure, 3)
|
||||
27
Task/Repeat/6502-Assembly/repeat-1.6502
Normal file
27
Task/Repeat/6502-Assembly/repeat-1.6502
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
macro RepeatProc,addr,count ;VASM macro syntax
|
||||
; input:
|
||||
; addr = the label of the routine you wish to call repeatedly
|
||||
; count = how many times you want to DO the procedure. 1 = once, 2 = twice, 3 = three times, etc. Enter "0" for 256 times.
|
||||
lda #<\addr
|
||||
sta z_L ;a label for a zero-page memory address
|
||||
lda #>\addr
|
||||
sta z_H ;a label for the zero-page memory address immediately after z_L
|
||||
lda \count
|
||||
jsr doRepeatProc
|
||||
endm
|
||||
|
||||
doRepeatProc:
|
||||
sta z_C ;another zero-page memory location
|
||||
loop_RepeatProc:
|
||||
jsr Trampoline_RepeatProc
|
||||
dec z_C
|
||||
lda z_C
|
||||
bne loop_RepeatProc
|
||||
rts
|
||||
|
||||
Trampoline_RepeatProc:
|
||||
db $6c,z_L,$00
|
||||
;when executed, becomes an indirect JMP to the address stored at z_L and z_H. Some assemblers will let you type
|
||||
;JMP (z_L) and it will automatically replace it with the above during the assembly process.
|
||||
;This causes an indirect JMP to the routine. Its RTS will return execution to just after the "JSR Trampoline_RepeatProc"
|
||||
;and flow into the loop overhead.
|
||||
1
Task/Repeat/6502-Assembly/repeat-2.6502
Normal file
1
Task/Repeat/6502-Assembly/repeat-2.6502
Normal file
|
|
@ -0,0 +1 @@
|
|||
RepeatProc foo,#20 ;perform the subroutine "foo" twenty times.
|
||||
12
Task/Repeat/6502-Assembly/repeat-3.6502
Normal file
12
Task/Repeat/6502-Assembly/repeat-3.6502
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
RepeatProc:
|
||||
;input: low byte of desired function address in A
|
||||
; high byte of desired function address in X
|
||||
; repeat count in Y
|
||||
|
||||
STA smc_repeatproc+1
|
||||
STX smc_repeatproc+2
|
||||
smc_repeatproc:
|
||||
jsr $0000 ;this is modified by the STA and STX above.
|
||||
dey
|
||||
bne smc_repeatproc
|
||||
rts
|
||||
19
Task/Repeat/68000-Assembly/repeat.68000
Normal file
19
Task/Repeat/68000-Assembly/repeat.68000
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
lea foo,a5 ;function to execute
|
||||
move.w #4-1,d7 ;times to repeat
|
||||
jsr Repeater
|
||||
|
||||
jmp * ;halt the CPU, we're done
|
||||
|
||||
repeater:
|
||||
jsr repeaterhelper ;this also need to be a call, so that the RTS of the desired procedure
|
||||
;returns us to the loop rather than the line after "jsr Repeater".
|
||||
DBRA D7,repeater
|
||||
rts
|
||||
|
||||
repeaterhelper:
|
||||
jmp (a5) ;keep in mind, this is NOT a dereference, it simply sets the program counter equal to A5.
|
||||
;A bit misleading if you ask me.
|
||||
foo:
|
||||
MOVE.B #'!',D0
|
||||
JSR PrintChar
|
||||
rts
|
||||
25
Task/Repeat/ALGOL-68/repeat.alg
Normal file
25
Task/Repeat/ALGOL-68/repeat.alg
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
# operator that executes a procedure the specified number of times #
|
||||
OP REPEAT = ( INT count, PROC VOID routine )VOID:
|
||||
TO count DO routine OD;
|
||||
|
||||
# make REPEAT a low priority operater #
|
||||
PRIO REPEAT = 1;
|
||||
|
||||
|
||||
# can also create variant that passes the iteration count as a parameter #
|
||||
OP REPEAT = ( INT count, PROC( INT )VOID routine )VOID:
|
||||
FOR iteration TO count DO routine( iteration ) OD;
|
||||
|
||||
main: (
|
||||
|
||||
# PROC to test the REPEAT operator with #
|
||||
PROC say something = VOID: print( ( "something", newline ) );
|
||||
|
||||
3 REPEAT say something;
|
||||
|
||||
# PROC to test the variant #
|
||||
PROC show squares = ( INT n )VOID: print( ( n, n * n, newline ) );
|
||||
|
||||
3 REPEAT show squares
|
||||
|
||||
)
|
||||
19
Task/Repeat/ALGOL-W/repeat.alg
Normal file
19
Task/Repeat/ALGOL-W/repeat.alg
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
begin
|
||||
% executes the procedure routine the specified number of times %
|
||||
procedure repeat ( integer value count; procedure routine ) ;
|
||||
for i := 1 until count do routine;
|
||||
begin
|
||||
integer x;
|
||||
% print "hello" three times %
|
||||
repeat( 3, write( "hello" ) );
|
||||
% print the first 10 squares %
|
||||
write();
|
||||
x := 1;
|
||||
repeat( 10
|
||||
, begin
|
||||
writeon( i_w := s_w := 1, x * x );
|
||||
x := x + 1
|
||||
end
|
||||
)
|
||||
end
|
||||
end.
|
||||
18
Task/Repeat/AWK/repeat.awk
Normal file
18
Task/Repeat/AWK/repeat.awk
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# syntax: GAWK -f REPEAT.AWK
|
||||
BEGIN {
|
||||
for (i=0; i<=3; i++) {
|
||||
f = (i % 2 == 0) ? "even" : "odd"
|
||||
@f(i) # indirect function call
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
function even(n, i) {
|
||||
for (i=1; i<=n; i++) {
|
||||
printf("inside even %d\n",n)
|
||||
}
|
||||
}
|
||||
function odd(n, i) {
|
||||
for (i=1; i<=n; i++) {
|
||||
printf("inside odd %d\n",n)
|
||||
}
|
||||
}
|
||||
32
Task/Repeat/Action-/repeat.action
Normal file
32
Task/Repeat/Action-/repeat.action
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
DEFINE PTR="CARD"
|
||||
|
||||
PROC OutputText(CHAR ARRAY s)
|
||||
PrintE(s)
|
||||
RETURN
|
||||
|
||||
PROC Procedure=*(CHAR ARRAY s)
|
||||
DEFINE JSR="$20"
|
||||
DEFINE RTS="$60"
|
||||
[JSR $00 $00 ;JSR to address set by SetProcedure
|
||||
RTS]
|
||||
|
||||
PROC SetProcedure(PTR p)
|
||||
PTR addr
|
||||
|
||||
addr=Procedure+1 ;location of address of JSR
|
||||
PokeC(addr,p)
|
||||
RETURN
|
||||
|
||||
PROC Repeat(PTR procFun CHAR ARRAY s BYTE n)
|
||||
BYTE i
|
||||
|
||||
SetProcedure(procFun)
|
||||
FOR i=1 TO n
|
||||
DO
|
||||
Procedure(s)
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Repeat(OutputText,"Action!",5)
|
||||
RETURN
|
||||
19
Task/Repeat/Ada/repeat.ada
Normal file
19
Task/Repeat/Ada/repeat.ada
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Repeat_Example is
|
||||
|
||||
procedure Repeat(P: access Procedure; Reps: Natural) is
|
||||
begin
|
||||
for I in 1 .. Reps loop
|
||||
P.all; -- P points to a procedure, and P.all actually calls that procedure
|
||||
end loop;
|
||||
end Repeat;
|
||||
|
||||
procedure Hello is
|
||||
begin
|
||||
Ada.Text_IO.Put("Hello! ");
|
||||
end Hello;
|
||||
|
||||
begin
|
||||
Repeat(Hello'Access, 3); -- Hello'Access points to the procedure Hello
|
||||
end Repeat_Example;
|
||||
83
Task/Repeat/AppleScript/repeat.applescript
Normal file
83
Task/Repeat/AppleScript/repeat.applescript
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
-- applyN :: Int -> (a -> a) -> a -> a
|
||||
on applyN(n, f, x)
|
||||
script go
|
||||
on |λ|(a, g)
|
||||
|λ|(a) of mReturn(g)
|
||||
end |λ|
|
||||
end script
|
||||
foldl(go, x, replicate(n, f))
|
||||
end applyN
|
||||
|
||||
|
||||
-------- SAMPLE FUNCTIONS FOR REPEATED APPLICATION --------
|
||||
|
||||
on double(x)
|
||||
2 * x
|
||||
end double
|
||||
|
||||
|
||||
on plusArrow(s)
|
||||
s & " -> "
|
||||
end plusArrow
|
||||
|
||||
|
||||
on squareRoot(n)
|
||||
n ^ 0.5
|
||||
end squareRoot
|
||||
|
||||
-------------------------- TESTS --------------------------
|
||||
on run
|
||||
log applyN(10, double, 1)
|
||||
--> 1024
|
||||
|
||||
log applyN(5, plusArrow, "")
|
||||
--> " -> -> -> -> -> "
|
||||
|
||||
log applyN(3, squareRoot, 65536)
|
||||
--> 4.0
|
||||
end run
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS --------------------
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
-- 2nd class handler function lifted into 1st class script wrapper.
|
||||
if script is class of f then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- Egyptian multiplication - progressively doubling a list, appending
|
||||
-- stages of doubling to an accumulator where needed for binary
|
||||
-- assembly of a target length
|
||||
-- replicate :: Int -> a -> [a]
|
||||
on replicate(n, a)
|
||||
set out to {}
|
||||
if 1 > n then return out
|
||||
set dbl to {a}
|
||||
|
||||
repeat while (1 < n)
|
||||
if 0 < (n mod 2) then set out to out & dbl
|
||||
set n to (n div 2)
|
||||
set dbl to (dbl & dbl)
|
||||
end repeat
|
||||
return out & dbl
|
||||
end replicate
|
||||
25
Task/Repeat/Arturo/repeat.arturo
Normal file
25
Task/Repeat/Arturo/repeat.arturo
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
print "---------------------------"
|
||||
print "As a loop"
|
||||
print "---------------------------"
|
||||
loop 4 'x ->
|
||||
print "Example 1"
|
||||
|
||||
repeatFunc: function [f,times][
|
||||
loop times 'x ->
|
||||
do f
|
||||
]
|
||||
|
||||
print "---------------------------"
|
||||
print "With a block param"
|
||||
print "---------------------------"
|
||||
repeatFunc [print "Example 2"] 4
|
||||
|
||||
repeatFunc: function [f,times][
|
||||
loop times 'x ->
|
||||
f
|
||||
]
|
||||
|
||||
print "---------------------------"
|
||||
print "With a function param"
|
||||
print "---------------------------"
|
||||
repeatFunc $[][print "Example 3"] 4
|
||||
11
Task/Repeat/AutoHotkey/repeat.ahk
Normal file
11
Task/Repeat/AutoHotkey/repeat.ahk
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
repeat("fMsgBox",3)
|
||||
return
|
||||
|
||||
repeat(f, n){
|
||||
loop % n
|
||||
%f%()
|
||||
}
|
||||
|
||||
fMsgBox(){
|
||||
MsgBox hello
|
||||
}
|
||||
12
Task/Repeat/BASIC256/repeat.basic
Normal file
12
Task/Repeat/BASIC256/repeat.basic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
subroutine proc()
|
||||
print " Inside loop"
|
||||
end subroutine
|
||||
|
||||
subroutine repeat(func, times)
|
||||
for i = 1 to times
|
||||
call proc()
|
||||
next
|
||||
end subroutine
|
||||
|
||||
call repeat("proc", 5)
|
||||
print "Loop Ended
|
||||
5
Task/Repeat/BQN/repeat-1.bqn
Normal file
5
Task/Repeat/BQN/repeat-1.bqn
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
•Show {2+𝕩}⍟3 1
|
||||
|
||||
_repeat_ ← {(𝕘>0)◶⊢‿(𝔽_𝕣_(𝕘-1)𝔽)𝕩}
|
||||
|
||||
•Show {2+𝕩} _repeat_ 3 1
|
||||
2
Task/Repeat/BQN/repeat-2.bqn
Normal file
2
Task/Repeat/BQN/repeat-2.bqn
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
7
|
||||
7
|
||||
17
Task/Repeat/Batch-File/repeat.bat
Normal file
17
Task/Repeat/Batch-File/repeat.bat
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
@echo off
|
||||
|
||||
:_main
|
||||
setlocal
|
||||
call:_func1 _func2 3
|
||||
pause>nul
|
||||
exit/b
|
||||
|
||||
:_func1
|
||||
setlocal enabledelayedexpansion
|
||||
for /l %%i in (1,1,%2) do call:%1
|
||||
exit /b
|
||||
|
||||
:_func2
|
||||
setlocal
|
||||
echo _func2 has been executed
|
||||
exit /b
|
||||
5
Task/Repeat/C++/repeat-1.cpp
Normal file
5
Task/Repeat/C++/repeat-1.cpp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
template <typename Function>
|
||||
void repeat(Function f, unsigned int n) {
|
||||
for(unsigned int i=n; 0<i; i--)
|
||||
f();
|
||||
}
|
||||
6
Task/Repeat/C++/repeat-2.cpp
Normal file
6
Task/Repeat/C++/repeat-2.cpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include <iostream>
|
||||
void example() {
|
||||
std::cout << "Example\n";
|
||||
}
|
||||
|
||||
repeat(example, 4);
|
||||
1
Task/Repeat/C++/repeat-3.cpp
Normal file
1
Task/Repeat/C++/repeat-3.cpp
Normal file
|
|
@ -0,0 +1 @@
|
|||
repeat([]{std::cout << "Example\n";}, 4);
|
||||
18
Task/Repeat/C-sharp/repeat.cs
Normal file
18
Task/Repeat/C-sharp/repeat.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
|
||||
namespace Repeat {
|
||||
class Program {
|
||||
static void Repeat(int count, Action<int> fn) {
|
||||
if (null == fn) {
|
||||
throw new ArgumentNullException("fn");
|
||||
}
|
||||
for (int i = 0; i < count; i++) {
|
||||
fn.Invoke(i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args) {
|
||||
Repeat(3, x => Console.WriteLine("Example {0}", x));
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Task/Repeat/C/repeat.c
Normal file
15
Task/Repeat/C/repeat.c
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void repeat(void (*f)(void), unsigned int n) {
|
||||
while (n-->0)
|
||||
(*f)(); //or just f()
|
||||
}
|
||||
|
||||
void example() {
|
||||
printf("Example\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
repeat(example, 4);
|
||||
return 0;
|
||||
}
|
||||
11
Task/Repeat/Chipmunk-Basic/repeat.basic
Normal file
11
Task/Repeat/Chipmunk-Basic/repeat.basic
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
10 sub proc()
|
||||
20 print " Inside loop"
|
||||
30 end sub
|
||||
40 sub repeat(func$,times)
|
||||
50 for i = 1 to times
|
||||
60 proc()
|
||||
70 next i
|
||||
80 end sub
|
||||
90 repeat("proc",5)
|
||||
100 print "Loop Ended"
|
||||
110 end
|
||||
2
Task/Repeat/Clojure/repeat.clj
Normal file
2
Task/Repeat/Clojure/repeat.clj
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(defn repeat-function [f n]
|
||||
(dotimes [i n] (f)))
|
||||
4
Task/Repeat/Common-Lisp/repeat.lisp
Normal file
4
Task/Repeat/Common-Lisp/repeat.lisp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(defun repeat (f n)
|
||||
(dotimes (i n) (funcall f)))
|
||||
|
||||
(repeat (lambda () (format T "Example~%")) 5)
|
||||
23
Task/Repeat/Cowgol/repeat.cowgol
Normal file
23
Task/Repeat/Cowgol/repeat.cowgol
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
include "cowgol.coh";
|
||||
|
||||
# Only functions that implement an interface can be passed around
|
||||
# The interface is a type and must be defined before it is used
|
||||
# This defines an interface for a function that takes no arguments
|
||||
interface Fn();
|
||||
|
||||
# This function repeats a function that implements Fn
|
||||
sub Repeat(f: Fn, n: uint32) is
|
||||
while n != 0 loop
|
||||
f();
|
||||
n := n - 1;
|
||||
end loop;
|
||||
end sub;
|
||||
|
||||
# Here is a function
|
||||
sub Foo implements Fn is
|
||||
print("foo ");
|
||||
end sub;
|
||||
|
||||
# Prints "foo foo foo foo"
|
||||
Repeat(Foo, 4);
|
||||
print_nl();
|
||||
13
Task/Repeat/D/repeat.d
Normal file
13
Task/Repeat/D/repeat.d
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
void repeat(void function() fun, in uint times) {
|
||||
foreach (immutable _; 0 .. times)
|
||||
fun();
|
||||
}
|
||||
|
||||
void procedure() {
|
||||
import std.stdio;
|
||||
"Example".writeln;
|
||||
}
|
||||
|
||||
void main() {
|
||||
repeat(&procedure, 3);
|
||||
}
|
||||
26
Task/Repeat/Delphi/repeat-1.delphi
Normal file
26
Task/Repeat/Delphi/repeat-1.delphi
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
program Repeater;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
{$R *.res}
|
||||
|
||||
type
|
||||
TSimpleProc = procedure; // Can also define types for procedures (& functions) which
|
||||
// require params.
|
||||
|
||||
procedure Once;
|
||||
begin
|
||||
writeln('Hello World');
|
||||
end;
|
||||
|
||||
procedure Iterate(proc : TSimpleProc; Iterations : integer);
|
||||
var
|
||||
i : integer;
|
||||
begin
|
||||
for i := 1 to Iterations do
|
||||
proc;
|
||||
end;
|
||||
|
||||
begin
|
||||
Iterate(Once, 3);
|
||||
readln;
|
||||
end.
|
||||
24
Task/Repeat/Delphi/repeat-2.delphi
Normal file
24
Task/Repeat/Delphi/repeat-2.delphi
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
program Repeater;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
{$R *.res}
|
||||
|
||||
uses
|
||||
System.SysUtils;
|
||||
|
||||
procedure Iterate(proc: TProc; Iterations: integer);
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
for i := 1 to Iterations do
|
||||
proc;
|
||||
end;
|
||||
|
||||
begin
|
||||
Iterate(
|
||||
procedure
|
||||
begin
|
||||
writeln('Hello World');
|
||||
end, 3);
|
||||
readln;
|
||||
end.
|
||||
13
Task/Repeat/EchoLisp/repeat.l
Normal file
13
Task/Repeat/EchoLisp/repeat.l
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(define (repeat f n) (for ((i n)) (f)))
|
||||
|
||||
(repeat (lambda () (write (random 1000))) 5)
|
||||
→ 287 798 930 989 794
|
||||
|
||||
;; Remark
|
||||
;; It is also possible to iterate a function : f(f(f(f( ..(f x)))))
|
||||
(define cos10 (iterate cos 10)
|
||||
(define cos100 (iterate cos10 10))
|
||||
(cos100 0.6)
|
||||
→ 0.7390851332151605
|
||||
(cos 0.7390851332151605)
|
||||
→ 0.7390851332151608 ;; fixed point found
|
||||
14
Task/Repeat/F-Sharp/repeat.fs
Normal file
14
Task/Repeat/F-Sharp/repeat.fs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
open System
|
||||
|
||||
let Repeat c f =
|
||||
for _ in 1 .. c do
|
||||
f()
|
||||
|
||||
let Hello _ =
|
||||
printfn "Hello world"
|
||||
|
||||
[<EntryPoint>]
|
||||
let main _ =
|
||||
Repeat 3 Hello
|
||||
|
||||
0 // return an integer exit code
|
||||
1
Task/Repeat/Factor/repeat-1.factor
Normal file
1
Task/Repeat/Factor/repeat-1.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
3 [ "Hello!" print ] times
|
||||
2
Task/Repeat/Factor/repeat-2.factor
Normal file
2
Task/Repeat/Factor/repeat-2.factor
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: times ( ... n quot: ( ... -- ... ) -- ... )
|
||||
[ drop ] prepose each-integer ; inline
|
||||
12
Task/Repeat/Fe/repeat.fe
Normal file
12
Task/Repeat/Fe/repeat.fe
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(= repeat (mac (i n . body)
|
||||
(list 'do
|
||||
(list 'let i 0)
|
||||
(list 'while (list '< i n)
|
||||
(list '= i (list '+ i 1))
|
||||
(cons 'do body)))))
|
||||
|
||||
; print multiplication table
|
||||
(repeat i 10
|
||||
(repeat j 10
|
||||
(print i "x" j "=" (* i j)))
|
||||
(print))
|
||||
2
Task/Repeat/Forth/repeat-1.fth
Normal file
2
Task/Repeat/Forth/repeat-1.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: times ( xt n -- )
|
||||
0 ?do dup execute loop drop ;
|
||||
2
Task/Repeat/Forth/repeat-2.fth
Normal file
2
Task/Repeat/Forth/repeat-2.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: times { xt n -- }
|
||||
n 0 ?do xt execute loop ;
|
||||
2
Task/Repeat/Forth/repeat-3.fth
Normal file
2
Task/Repeat/Forth/repeat-3.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: times[ ]] 0 ?do [[ ; immediate compile-only
|
||||
: ]times postpone loop ; immediate compile-only
|
||||
4
Task/Repeat/Forth/repeat-4.fth
Normal file
4
Task/Repeat/Forth/repeat-4.fth
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
[: cr ." Hello" ;] 3 times
|
||||
|
||||
: 3-byes ( -- ) 3 times[ cr ." Bye" ]times ;
|
||||
3-byes
|
||||
17
Task/Repeat/FreeBASIC/repeat.basic
Normal file
17
Task/Repeat/FreeBASIC/repeat.basic
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Sub proc()
|
||||
Print " proc called"
|
||||
End Sub
|
||||
|
||||
Sub repeat(s As Sub, n As UInteger)
|
||||
For i As Integer = 1 To n
|
||||
Print Using "##"; i;
|
||||
s()
|
||||
Next
|
||||
End Sub
|
||||
|
||||
repeat(@proc, 5)
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
18
Task/Repeat/FutureBasic/repeat.basic
Normal file
18
Task/Repeat/FutureBasic/repeat.basic
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
void local fn Example( value as long )
|
||||
NSLog(@"Example %ld",value)
|
||||
end fn
|
||||
|
||||
void local fn DoIt( fnAddress as ptr, count as long )
|
||||
def fn Repeat( j as long ) using fnAddress
|
||||
|
||||
long i
|
||||
for i = 1 to count
|
||||
fn Repeat( i )
|
||||
next
|
||||
end fn
|
||||
|
||||
fn DoIt( @fn Example, 3 )
|
||||
|
||||
HandleEvents
|
||||
13
Task/Repeat/GW-BASIC/repeat.basic
Normal file
13
Task/Repeat/GW-BASIC/repeat.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
100 let f$ = "proc"
|
||||
110 let c = 5
|
||||
120 gosub 170
|
||||
130 print "Loop Ended"
|
||||
140 goto 220
|
||||
150 print " Inside loop"
|
||||
160 return
|
||||
170 rem repeat(f$,c)
|
||||
180 for i = 1 to c
|
||||
190 gosub 150
|
||||
200 next i
|
||||
210 return
|
||||
220 end
|
||||
28
Task/Repeat/Gambas/repeat.gambas
Normal file
28
Task/Repeat/Gambas/repeat.gambas
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
Public Sub Main()
|
||||
|
||||
RepeatIt("RepeatableOne", 2)
|
||||
|
||||
RepeatIt("RepeatableTwo", 3)
|
||||
|
||||
End
|
||||
|
||||
'Cannot pass procedure pointer in Gambas; must pass procedure name and use Object.Call()
|
||||
Public Sub RepeatIt(sDelegateName As String, iCount As Integer)
|
||||
|
||||
For iCounter As Integer = 1 To iCount
|
||||
Object.Call(Me, sDelegateName, [])
|
||||
Next
|
||||
|
||||
End
|
||||
|
||||
Public Sub RepeatableOne()
|
||||
|
||||
Print "RepeatableOne"
|
||||
|
||||
End
|
||||
|
||||
Public Sub RepeatableTwo()
|
||||
|
||||
Print "RepeatableTwo"
|
||||
|
||||
End
|
||||
17
Task/Repeat/Go/repeat.go
Normal file
17
Task/Repeat/Go/repeat.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func repeat(n int, f func()) {
|
||||
for i := 0; i < n; i++ {
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
func fn() {
|
||||
fmt.Println("Example")
|
||||
}
|
||||
|
||||
func main() {
|
||||
repeat(4, fn)
|
||||
}
|
||||
6
Task/Repeat/Haskell/repeat-1.hs
Normal file
6
Task/Repeat/Haskell/repeat-1.hs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import Control.Monad (replicateM_)
|
||||
|
||||
sampleFunction :: IO ()
|
||||
sampleFunction = putStrLn "a"
|
||||
|
||||
main = replicateM_ 5 sampleFunction
|
||||
5
Task/Repeat/Haskell/repeat-2.hs
Normal file
5
Task/Repeat/Haskell/repeat-2.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
applyN :: Int -> (a -> a) -> a -> a
|
||||
applyN n f = foldr (.) id (replicate n f)
|
||||
|
||||
main :: IO ()
|
||||
main = print $ applyN 10 (\x -> 2 * x) 1
|
||||
55
Task/Repeat/Isabelle/repeat.isabelle
Normal file
55
Task/Repeat/Isabelle/repeat.isabelle
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
theory Scratch
|
||||
imports Main
|
||||
begin
|
||||
|
||||
text‹
|
||||
Given the function we want to execute multiple times is of
|
||||
type \<^typ>‹unit ⇒ unit›.
|
||||
›
|
||||
fun pure_repeat :: "(unit ⇒ unit) ⇒ nat ⇒ unit" where
|
||||
"pure_repeat _ 0 = ()"
|
||||
| "pure_repeat f (Suc n) = f (pure_repeat f n)"
|
||||
|
||||
text‹
|
||||
Functions are pure in Isabelle. They don't have side effects.
|
||||
This means, the \<^const>‹pure_repeat› we implemented is always equal
|
||||
to \<^term>‹() :: unit›, independent of the function \<^typ>‹unit ⇒ unit›
|
||||
or \<^typ>‹nat›.
|
||||
Technically, functions are not even "executed", but only evaluated.
|
||||
›
|
||||
lemma "pure_repeat f n = ()" by simp
|
||||
|
||||
text‹
|
||||
But we can repeat a value of \<^typ>‹'a› \<^term>‹n› times and return the result
|
||||
in a list of length \<^term>‹n›
|
||||
›
|
||||
fun repeat :: "'a ⇒ nat ⇒ 'a list" where
|
||||
"repeat _ 0 = []"
|
||||
| "repeat f (Suc n) = f # (repeat f n)"
|
||||
|
||||
lemma "repeat ''Hello'' 4 = [''Hello'', ''Hello'', ''Hello'', ''Hello'']"
|
||||
by code_simp
|
||||
|
||||
lemma "length (repeat a n) = n" by(induction n) simp+
|
||||
|
||||
text‹
|
||||
Technically, \<^typ>‹'a› is not a function. We can wrap it in a dummy function
|
||||
which takes a \<^typ>‹unit› as first argument. This gives a function of type
|
||||
\<^typ>‹unit ⇒ 'a›.
|
||||
›
|
||||
|
||||
fun fun_repeat :: "(unit ⇒ 'a) ⇒ nat ⇒ 'a list" where
|
||||
"fun_repeat _ 0 = []"
|
||||
| "fun_repeat f (Suc n) = (f ()) # (fun_repeat f n)"
|
||||
|
||||
lemma "fun_repeat (λ_. ''Hello'') 4 =
|
||||
[''Hello'', ''Hello'', ''Hello'', ''Hello'']"
|
||||
by code_simp
|
||||
|
||||
text‹
|
||||
Yet, \<^const>‹fun_repeat› with the dummy function \<^typ>‹unit ⇒ 'a› is
|
||||
equivalent to \<^const>‹repeat› with the value \<^typ>‹'a› directly.
|
||||
›
|
||||
lemma "fun_repeat (λ_. a) n = repeat a n" by(induction n) simp+
|
||||
|
||||
end
|
||||
53
Task/Repeat/J/repeat.j
Normal file
53
Task/Repeat/J/repeat.j
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
NB. ^: (J's power conjunction) repeatedly evaluates a verb.
|
||||
|
||||
NB. Appending to a vector the sum of the most recent
|
||||
NB. 2 items can generate the Fibonacci sequence.
|
||||
|
||||
(, [: +/ _2&{.) (^:4) 0 1
|
||||
0 1 1 2 3 5
|
||||
|
||||
|
||||
NB. Repeat an infinite number of times
|
||||
NB. computes the stable point at convergence
|
||||
|
||||
cosine =: 2&o.
|
||||
|
||||
cosine (^:_ ) 2 NB. 2 is the initial value
|
||||
0.739085
|
||||
|
||||
cosine 0.739085 NB. demonstrate the stable point x==Cos(x)
|
||||
0.739085
|
||||
|
||||
|
||||
cosine^:(<_) 2 NB. show the convergence
|
||||
2 _0.416147 0.914653 0.610065 0.819611 0.682506 0.775995 0.713725 0.755929 0.727635 0.74675 0.733901 0.742568 0.736735 0.740666 0.738019 0.739803 0.738602 0.739411 0.738866 0.739233 0.738986 0.739152 0.73904 0.739116 0.739065 0.739099 0.739076 0.739091 0.7...
|
||||
|
||||
|
||||
# cosine^:(<_) 2 NB. iteration tallyft
|
||||
78
|
||||
|
||||
f =: 3 :'smoutput ''hi'''
|
||||
|
||||
f''
|
||||
hi
|
||||
|
||||
NB. pass verbs via a gerund
|
||||
repeat =: dyad def 'for_i. i.y do. (x`:0)0 end. EMPTY'
|
||||
|
||||
(f`'')repeat 4
|
||||
hi
|
||||
hi
|
||||
hi
|
||||
hi
|
||||
|
||||
|
||||
|
||||
NB. pass a verb directly to an adverb
|
||||
|
||||
Repeat =: adverb def 'for_i. i.y do. u 0 end. EMPTY'
|
||||
|
||||
f Repeat 4
|
||||
hi
|
||||
hi
|
||||
hi
|
||||
hi
|
||||
17
Task/Repeat/Java/repeat-1.java
Normal file
17
Task/Repeat/Java/repeat-1.java
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import java.lang.reflect.Method;
|
||||
|
||||
public class Program {
|
||||
public static void main(String[] args) throws ReflectiveOperationException {
|
||||
Method method = Program.class.getMethod("printRosettaCode");
|
||||
repeat(method, 5);
|
||||
}
|
||||
|
||||
public static void printRosettaCode() {
|
||||
System.out.println("Rosetta Code");
|
||||
}
|
||||
|
||||
public static void repeat(Method method, int count) throws ReflectiveOperationException {
|
||||
while (count-- > 0)
|
||||
method.invoke(null);
|
||||
}
|
||||
}
|
||||
13
Task/Repeat/Java/repeat-2.java
Normal file
13
Task/Repeat/Java/repeat-2.java
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import java.util.function.Consumer;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Repeat {
|
||||
|
||||
public static void main(String[] args) {
|
||||
repeat(3, (x) -> System.out.println("Example " + x));
|
||||
}
|
||||
|
||||
static void repeat (int n, Consumer<Integer> fun) {
|
||||
IntStream.range(0, n).forEach(i -> fun.accept(i + 1));
|
||||
}
|
||||
}
|
||||
4
Task/Repeat/Jq/repeat-1.jq
Normal file
4
Task/Repeat/Jq/repeat-1.jq
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def unoptimized_repeat(f; n):
|
||||
if n <= 0 then empty
|
||||
else f, repeat(f; n-1)
|
||||
end;
|
||||
5
Task/Repeat/Jq/repeat-2.jq
Normal file
5
Task/Repeat/Jq/repeat-2.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def repeat(f; n):
|
||||
# state: [count, in]
|
||||
def r:
|
||||
if .[0] >= n then empty else (.[1] | f), (.[0] += 1 | r) end;
|
||||
[0, .] | r;
|
||||
9
Task/Repeat/Jq/repeat-3.jq
Normal file
9
Task/Repeat/Jq/repeat-3.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# If n is a non-negative integer,
|
||||
# then emit a stream of (n + 1) terms: ., f, f|f, f|f|f, ...
|
||||
def repeatedly(f; n):
|
||||
# state: [count, in]
|
||||
def r:
|
||||
if .[0] < 0 then empty
|
||||
else .[1], ([.[0] - 1, (.[1] | f)] | r)
|
||||
end;
|
||||
[n, .] | r;
|
||||
1
Task/Repeat/Jq/repeat-4.jq
Normal file
1
Task/Repeat/Jq/repeat-4.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
0 | [ repeat(.+1; 3) ]
|
||||
1
Task/Repeat/Jq/repeat-5.jq
Normal file
1
Task/Repeat/Jq/repeat-5.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
0 | repeatedly(.+1; 3)
|
||||
9
Task/Repeat/Julia/repeat.julia
Normal file
9
Task/Repeat/Julia/repeat.julia
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function sayHi()
|
||||
println("Hi")
|
||||
end
|
||||
|
||||
function rep(f, n)
|
||||
for i = 1:n f() end
|
||||
end
|
||||
|
||||
rep(sayHi, 3)
|
||||
12
Task/Repeat/Kotlin/repeat.kotlin
Normal file
12
Task/Repeat/Kotlin/repeat.kotlin
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun repeat(n: Int, f: () -> Unit) {
|
||||
for (i in 1..n) {
|
||||
f()
|
||||
println(i)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
repeat(5) { print("Example ") }
|
||||
}
|
||||
6
Task/Repeat/Lean/repeat-1.lean
Normal file
6
Task/Repeat/Lean/repeat-1.lean
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def repeat : ℕ → (ℕ → string) → string
|
||||
| 0 f := "done"
|
||||
| (n + 1) f := (f n) ++ (repeat n f)
|
||||
|
||||
|
||||
#eval repeat 5 $ λ b : ℕ , "me "
|
||||
9
Task/Repeat/Lean/repeat-2.lean
Normal file
9
Task/Repeat/Lean/repeat-2.lean
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def repeatf (f : Nat -> String) (n : Nat) : String :=
|
||||
match n with
|
||||
| 0 => "."
|
||||
| (k + 1) => (f k) ++ (repeatf f k)
|
||||
|
||||
def example1 : String :=
|
||||
repeatf (fun (x : Nat) => toString (x) ++ " ") (10)
|
||||
|
||||
#eval example1
|
||||
7
Task/Repeat/LiveCode/repeat.livecode
Normal file
7
Task/Repeat/LiveCode/repeat.livecode
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
rep "answer",3
|
||||
|
||||
command rep x,n
|
||||
repeat n times
|
||||
do merge("[[x]] [[n]]")
|
||||
end repeat
|
||||
end rep
|
||||
11
Task/Repeat/Lua/repeat.lua
Normal file
11
Task/Repeat/Lua/repeat.lua
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function myFunc ()
|
||||
print("Sure looks like a function in here...")
|
||||
end
|
||||
|
||||
function rep (func, times)
|
||||
for count = 1, times do
|
||||
func()
|
||||
end
|
||||
end
|
||||
|
||||
rep(myFunc, 4)
|
||||
2
Task/Repeat/Mathematica/repeat.math
Normal file
2
Task/Repeat/Mathematica/repeat.math
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
repeat[f_, n_] := Do[f[], {n}];
|
||||
repeat[Print["Hello, world!"] &, 5];
|
||||
1
Task/Repeat/Min/repeat.min
Normal file
1
Task/Repeat/Min/repeat.min
Normal file
|
|
@ -0,0 +1 @@
|
|||
("Hello" puts!) 3 times
|
||||
11
Task/Repeat/MiniScript/repeat.mini
Normal file
11
Task/Repeat/MiniScript/repeat.mini
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
sayHi = function()
|
||||
print "Hi!"
|
||||
end function
|
||||
|
||||
rep = function(f, n)
|
||||
for i in range(1, n)
|
||||
f
|
||||
end for
|
||||
end function
|
||||
|
||||
rep @sayHi, 3
|
||||
24
Task/Repeat/Modula-2/repeat.mod2
Normal file
24
Task/Repeat/Modula-2/repeat.mod2
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
MODULE Repeat;
|
||||
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
|
||||
|
||||
TYPE F = PROCEDURE;
|
||||
|
||||
PROCEDURE Repeat(fun : F; c : INTEGER);
|
||||
VAR i : INTEGER;
|
||||
BEGIN
|
||||
FOR i:=1 TO c DO
|
||||
fun
|
||||
END
|
||||
END Repeat;
|
||||
|
||||
PROCEDURE Print;
|
||||
BEGIN
|
||||
WriteString("Hello");
|
||||
WriteLn
|
||||
END Print;
|
||||
|
||||
BEGIN
|
||||
Repeat(Print, 3);
|
||||
|
||||
ReadChar
|
||||
END Repeat.
|
||||
11
Task/Repeat/Nanoquery/repeat.nanoquery
Normal file
11
Task/Repeat/Nanoquery/repeat.nanoquery
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def repeat(f,n)
|
||||
for i in range(1, n)
|
||||
f()
|
||||
end
|
||||
end
|
||||
|
||||
def procedure()
|
||||
println "Example"
|
||||
end
|
||||
|
||||
repeat(procedure, 3)
|
||||
34
Task/Repeat/Nim/repeat.nim
Normal file
34
Task/Repeat/Nim/repeat.nim
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
proc example =
|
||||
echo "Example"
|
||||
|
||||
# Ordinary procedure
|
||||
proc repeatProc(fn: proc, n: int) =
|
||||
for x in 0..<n:
|
||||
fn()
|
||||
|
||||
repeatProc(example, 4)
|
||||
|
||||
# Template (code substitution), simplest form of metaprogramming
|
||||
# that Nim has
|
||||
template repeatTmpl(n: int, body: untyped): untyped =
|
||||
for x in 0..<n:
|
||||
body
|
||||
|
||||
# This gets rewritten into a for loop
|
||||
repeatTmpl 4:
|
||||
example()
|
||||
|
||||
import std/macros
|
||||
# A macro which takes some code block and returns code
|
||||
# with that code block repeated n times. Macros run at
|
||||
# compile-time
|
||||
macro repeatMacro(n: static[int], body: untyped): untyped =
|
||||
result = newStmtList()
|
||||
|
||||
for x in 0..<n:
|
||||
result.add body
|
||||
|
||||
# This gets rewritten into 4 calls to example()
|
||||
# at compile-time
|
||||
repeatMacro 4:
|
||||
example()
|
||||
10
Task/Repeat/OCaml/repeat.ocaml
Normal file
10
Task/Repeat/OCaml/repeat.ocaml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
let repeat ~f ~n =
|
||||
for i = 1 to n do
|
||||
f ()
|
||||
done
|
||||
|
||||
let func () =
|
||||
print_endline "Example"
|
||||
|
||||
let () =
|
||||
repeat ~n:4 ~f:func
|
||||
15
Task/Repeat/Objeck/repeat.objeck
Normal file
15
Task/Repeat/Objeck/repeat.objeck
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
class Repeat {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
Repeat(Example() ~ Nil, 3);
|
||||
}
|
||||
|
||||
function : Repeat(e : () ~ Nil, i : Int) ~ Nil {
|
||||
while(i-- > 0) {
|
||||
e();
|
||||
};
|
||||
}
|
||||
|
||||
function : Example() ~ Nil {
|
||||
"Example"->PrintLine();
|
||||
}
|
||||
}
|
||||
2
Task/Repeat/Oforth/repeat.fth
Normal file
2
Task/Repeat/Oforth/repeat.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: hello "Hello, World!" println ;
|
||||
10 #hello times
|
||||
16
Task/Repeat/Ol/repeat.ol
Normal file
16
Task/Repeat/Ol/repeat.ol
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
; sample function
|
||||
(define (function) (display "+"))
|
||||
|
||||
; simple case for 80 times
|
||||
(for-each (lambda (unused) (function)) (iota 80))
|
||||
(print) ; print newline
|
||||
; ==> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
; detailed case for 80 times
|
||||
(let loop ((fnc function) (n 80))
|
||||
(unless (zero? n)
|
||||
(begin
|
||||
(fnc)
|
||||
(loop fnc (- n 1)))))
|
||||
(print) ; print newline
|
||||
; ==> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
2
Task/Repeat/PARI-GP/repeat.parigp
Normal file
2
Task/Repeat/PARI-GP/repeat.parigp
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
repeat(f, n)=for(i=1,n,f());
|
||||
repeat( ()->print("Hi!"), 2);
|
||||
21
Task/Repeat/Pascal/repeat.pas
Normal file
21
Task/Repeat/Pascal/repeat.pas
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
program Repeater;
|
||||
|
||||
type
|
||||
TProc = procedure(I: Integer);
|
||||
|
||||
procedure P(I: Integer);
|
||||
begin
|
||||
WriteLn('Iteration ', I);
|
||||
end;
|
||||
|
||||
procedure Iterate(P: TProc; N: Integer);
|
||||
var
|
||||
I: Integer;
|
||||
begin
|
||||
for I := 1 to N do
|
||||
P(I);
|
||||
end;
|
||||
|
||||
begin
|
||||
Iterate(P, 3);
|
||||
end.
|
||||
10
Task/Repeat/Perl/repeat.pl
Normal file
10
Task/Repeat/Perl/repeat.pl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
sub repeat {
|
||||
my ($sub, $n) = @_;
|
||||
$sub->() for 1..$n;
|
||||
}
|
||||
|
||||
sub example {
|
||||
print "Example\n";
|
||||
}
|
||||
|
||||
repeat(\&example, 4);
|
||||
13
Task/Repeat/Phix/repeat.phix
Normal file
13
Task/Repeat/Phix/repeat.phix
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
-->
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">Repeat</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">rid</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">Hello</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #008000;">"Hello"</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #000000;">Repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Hello</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
12
Task/Repeat/Phixmonti/repeat.phixmonti
Normal file
12
Task/Repeat/Phixmonti/repeat.phixmonti
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def myFunc
|
||||
"Sure looks like a function in here..." print nl
|
||||
enddef
|
||||
|
||||
def rep /# func times -- #/
|
||||
for drop
|
||||
dup exec
|
||||
endfor
|
||||
drop
|
||||
enddef
|
||||
|
||||
getid myFunc 4 rep
|
||||
12
Task/Repeat/PicoLisp/repeat.l
Normal file
12
Task/Repeat/PicoLisp/repeat.l
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# The built-in function "do" can be used to achieve our goal,
|
||||
# however, it has a slightly different syntax than what the
|
||||
# problem specifies.
|
||||
|
||||
# Native solution.
|
||||
(do 10 (version))
|
||||
|
||||
# Our solution.
|
||||
(de dofn (Fn N)
|
||||
(do N (Fn)) )
|
||||
|
||||
(dofn version 10)
|
||||
14
Task/Repeat/PowerShell/repeat.psh
Normal file
14
Task/Repeat/PowerShell/repeat.psh
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function Out-Example
|
||||
{
|
||||
"Example"
|
||||
}
|
||||
|
||||
function Step-Function ([string]$Function, [int]$Repeat)
|
||||
{
|
||||
for ($i = 1; $i -le $Repeat; $i++)
|
||||
{
|
||||
"$(Invoke-Expression -Command $Function) $i"
|
||||
}
|
||||
}
|
||||
|
||||
Step-Function Out-Example -Repeat 3
|
||||
8
Task/Repeat/Prolog/repeat.pro
Normal file
8
Task/Repeat/Prolog/repeat.pro
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
repeat(_, 0).
|
||||
repeat(Callable, Times) :-
|
||||
succ(TimesLess1, Times),
|
||||
Callable,
|
||||
repeat(Callable, TimesLess1).
|
||||
|
||||
test :- write('Hello, World'), nl.
|
||||
test(Name) :- format('Hello, ~w~n', Name).
|
||||
11
Task/Repeat/PureBasic/repeat.basic
Normal file
11
Task/Repeat/PureBasic/repeat.basic
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
Prototype.i fun(x.i)
|
||||
|
||||
Procedure.i quark(z.i)
|
||||
Debug "Quark "+Str(z) : ProcedureReturn z-1
|
||||
EndProcedure
|
||||
|
||||
Procedure rep(q.fun,n.i)
|
||||
Repeat : n=q(n) : Until n=0
|
||||
EndProcedure
|
||||
|
||||
rep(@quark(),3)
|
||||
9
Task/Repeat/Python/repeat-1.py
Normal file
9
Task/Repeat/Python/repeat-1.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#!/usr/bin/python
|
||||
def repeat(f,n):
|
||||
for i in range(n):
|
||||
f();
|
||||
|
||||
def procedure():
|
||||
print("Example");
|
||||
|
||||
repeat(procedure,3); #prints "Example" (without quotes) three times, separated by newlines.
|
||||
99
Task/Repeat/Python/repeat-2.py
Normal file
99
Task/Repeat/Python/repeat-2.py
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
'''Application of a given function, repeated N times'''
|
||||
|
||||
from itertools import repeat
|
||||
from functools import reduce
|
||||
from inspect import getsource
|
||||
|
||||
|
||||
# applyN :: Int -> (a -> a) -> a -> a
|
||||
def applyN(n):
|
||||
'''n compounding applications of the supplied
|
||||
function f. Equivalent to Church numeral n.
|
||||
'''
|
||||
def go(f):
|
||||
return lambda x: reduce(
|
||||
lambda a, g: g(a), repeat(f, n), x
|
||||
)
|
||||
return lambda f: go(f)
|
||||
|
||||
|
||||
# MAIN ----------------------------------------------------
|
||||
def main():
|
||||
'''Tests - compounding repetition
|
||||
of function application.
|
||||
'''
|
||||
def f(x):
|
||||
return x + 'Example\n'
|
||||
|
||||
def g(x):
|
||||
return 2 * x
|
||||
|
||||
def h(x):
|
||||
return 1.05 * x
|
||||
|
||||
print(
|
||||
fTable(__doc__ + ':')(
|
||||
lambda fx: '\nRepeated * 3:\n (' + (
|
||||
getsource(fst(fx)).strip() + ')(' +
|
||||
repr(snd(fx)) + ')'
|
||||
)
|
||||
)(str)(
|
||||
liftA2(applyN(3))(fst)(snd)
|
||||
)([(f, '\n'), (g, 1), (h, 100)])
|
||||
)
|
||||
|
||||
|
||||
# GENERIC -------------------------------------------------
|
||||
|
||||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
def compose(g):
|
||||
'''Right to left function composition.'''
|
||||
return lambda f: lambda x: g(f(x))
|
||||
|
||||
|
||||
# fst :: (a, b) -> a
|
||||
def fst(tpl):
|
||||
'''First member of a pair.'''
|
||||
return tpl[0]
|
||||
|
||||
|
||||
# liftA2 :: (a0 -> b -> c) -> (a -> a0) -> (a -> b) -> a -> c
|
||||
def liftA2(op):
|
||||
'''Lift a binary function to a composition
|
||||
over two other functions.
|
||||
liftA2 (*) (+ 2) (+ 3) 7 == 90
|
||||
'''
|
||||
def go(f, g):
|
||||
return lambda x: op(
|
||||
f(x)
|
||||
)(g(x))
|
||||
return lambda f: lambda g: go(f, g)
|
||||
|
||||
|
||||
# snd :: (a, b) -> b
|
||||
def snd(tpl):
|
||||
'''Second member of a pair.'''
|
||||
return tpl[1]
|
||||
|
||||
|
||||
# fTable :: String -> (a -> String) ->
|
||||
# (b -> String) -> (a -> b) -> [a] -> String
|
||||
def fTable(s):
|
||||
'''Heading -> x display function -> fx display function ->
|
||||
f -> xs -> tabular string.
|
||||
'''
|
||||
def go(xShow, fxShow, f, xs):
|
||||
ys = [xShow(x) for x in xs]
|
||||
w = max(map(len, ys))
|
||||
return s + '\n' + '\n'.join(map(
|
||||
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
|
||||
xs, ys
|
||||
))
|
||||
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
|
||||
xShow, fxShow, f, xs
|
||||
)
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
15
Task/Repeat/QBasic/repeat.basic
Normal file
15
Task/Repeat/QBasic/repeat.basic
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
DECLARE SUB rep (func AS STRING, c AS INTEGER)
|
||||
DECLARE SUB proc ()
|
||||
|
||||
CALL rep("proc", 5)
|
||||
PRINT "Loop Ended"
|
||||
|
||||
SUB proc
|
||||
PRINT " Inside loop"
|
||||
END SUB
|
||||
|
||||
SUB rep (func AS STRING, c AS INTEGER)
|
||||
FOR i = 1 TO c
|
||||
proc
|
||||
NEXT
|
||||
END SUB
|
||||
33
Task/Repeat/Quackery/repeat.quackery
Normal file
33
Task/Repeat/Quackery/repeat.quackery
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
[ stack ] is times.start ( --> s )
|
||||
protect times.start
|
||||
|
||||
[ stack ] is times.count ( --> s )
|
||||
protect times.count
|
||||
|
||||
[ stack ] is times.action ( --> s )
|
||||
protect times.action
|
||||
|
||||
[ ]'[ times.action put
|
||||
dup times.start put
|
||||
[ 1 - dup -1 > while
|
||||
times.count put
|
||||
times.action share do
|
||||
times.count take again ]
|
||||
drop
|
||||
times.action release
|
||||
times.start release ] is times ( n --> )
|
||||
|
||||
[ times.count share ] is i ( --> n )
|
||||
|
||||
[ times.start share i 1+ - ] is i^ ( --> n )
|
||||
|
||||
[ 0 times.count replace ] is conclude ( --> )
|
||||
|
||||
[ times.start share
|
||||
times.count replace ] is refresh ( --> )
|
||||
|
||||
[ times.count take 1+
|
||||
swap - times.count put ] is step ( --> s )
|
||||
|
||||
[ nested ' times nested
|
||||
swap join do ] is rosetta-times ( n x --> )
|
||||
7
Task/Repeat/R/repeat.r
Normal file
7
Task/Repeat/R/repeat.r
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
f1 <- function(...){print("coucou")}
|
||||
|
||||
f2 <-function(f,n){
|
||||
lapply(seq_len(n),eval(f))
|
||||
}
|
||||
|
||||
f2(f1,4)
|
||||
11
Task/Repeat/REXX/repeat.rexx
Normal file
11
Task/Repeat/REXX/repeat.rexx
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/*REXX program executes a named procedure a specified number of times. */
|
||||
parse arg pN # . /*obtain optional arguments from the CL*/
|
||||
if #=='' | #=="," then #= 1 /*assume once if not specified. */
|
||||
if pN\=='' then call repeats pN, # /*invoke the REPEATS procedure for pN.*/
|
||||
exit 0 /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
repeats: procedure; parse arg x,n /*obtain the procedureName & # of times*/
|
||||
do n; interpret 'CALL' x; end /*repeat the invocation N times. */
|
||||
return /*return to invoker of the REPEATS proc*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
yabba: say 'Yabba, yabba do!'; return /*simple code; no need for PROCEDURE.*/
|
||||
12
Task/Repeat/Racket/repeat.rkt
Normal file
12
Task/Repeat/Racket/repeat.rkt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#lang racket/base
|
||||
(define (repeat f n) ; the for loop is idiomatic of (although not exclusive to) racket
|
||||
(for ((_ n)) (f)))
|
||||
|
||||
(define (repeat2 f n) ; This is a bit more "functional programmingy"
|
||||
(when (positive? n) (f) (repeat2 f (sub1 n))))
|
||||
|
||||
(display "...")
|
||||
(repeat (λ () (display " and over")) 5)
|
||||
(display "...")
|
||||
(repeat2 (λ () (display " & over")) 5)
|
||||
(newline)
|
||||
5
Task/Repeat/Raku/repeat.raku
Normal file
5
Task/Repeat/Raku/repeat.raku
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
sub repeat (&f, $n) { f() xx $n };
|
||||
|
||||
sub example { say rand }
|
||||
|
||||
repeat(&example, 3);
|
||||
5
Task/Repeat/Red/repeat.red
Normal file
5
Task/Repeat/Red/repeat.red
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Red[]
|
||||
|
||||
myrepeat: function [fn n] [loop n [do fn]]
|
||||
|
||||
myrepeat [print "hello"] 3
|
||||
10
Task/Repeat/Ring/repeat.ring
Normal file
10
Task/Repeat/Ring/repeat.ring
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
Func Main
|
||||
times(5,:test)
|
||||
|
||||
Func Test
|
||||
see "Message from the test function!" + nl
|
||||
|
||||
Func Times nCount, F
|
||||
for x = 1 to nCount
|
||||
Call F()
|
||||
next
|
||||
7
Task/Repeat/Ruby/repeat.rb
Normal file
7
Task/Repeat/Ruby/repeat.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
4.times{ puts "Example" } # idiomatic way
|
||||
|
||||
def repeat(proc,num)
|
||||
num.times{ proc.call }
|
||||
end
|
||||
|
||||
repeat(->{ puts "Example" }, 4)
|
||||
3
Task/Repeat/Rust/repeat-1.rust
Normal file
3
Task/Repeat/Rust/repeat-1.rust
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fn repeat(f: impl FnMut(usize), n: usize) {
|
||||
(0..n).for_each(f);
|
||||
}
|
||||
3
Task/Repeat/Rust/repeat-2.rust
Normal file
3
Task/Repeat/Rust/repeat-2.rust
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
repeat(|x| print!("{};", x), 5);
|
||||
}
|
||||
7
Task/Repeat/Rust/repeat-3.rust
Normal file
7
Task/Repeat/Rust/repeat-3.rust
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fn function(x: usize) {
|
||||
print!("{};", x);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
repeat(function, 4);
|
||||
}
|
||||
10
Task/Repeat/Rust/repeat-4.rust
Normal file
10
Task/Repeat/Rust/repeat-4.rust
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
struct Foo;
|
||||
impl Foo {
|
||||
fn associated(x: usize) {
|
||||
print!("{};", x);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
repeat(Foo::associated, 8);
|
||||
}
|
||||
13
Task/Repeat/Rust/repeat-5.rust
Normal file
13
Task/Repeat/Rust/repeat-5.rust
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
trait Bar {
|
||||
fn run(self);
|
||||
}
|
||||
|
||||
impl Bar for usize {
|
||||
fn run(self) {
|
||||
print!("{};", self);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
repeat(Bar::run, 6);
|
||||
}
|
||||
11
Task/Repeat/Rust/repeat-6.rust
Normal file
11
Task/Repeat/Rust/repeat-6.rust
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fn repeat(f: impl FnMut(usize), n: usize) {
|
||||
(0..n).for_each(f);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut mult = 1;
|
||||
repeat(|x| {
|
||||
print!("{};", x * mult);
|
||||
mult += x;
|
||||
}, 5);
|
||||
}
|
||||
3
Task/Repeat/Scala/repeat-1.scala
Normal file
3
Task/Repeat/Scala/repeat-1.scala
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def repeat[A](n:Int)(f: => A)= ( 0 until n).foreach(_ => f)
|
||||
|
||||
repeat(3) { println("Example") }
|
||||
16
Task/Repeat/Scala/repeat-2.scala
Normal file
16
Task/Repeat/Scala/repeat-2.scala
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
object Repeat2 extends App {
|
||||
|
||||
implicit class IntWithTimes(x: Int) {
|
||||
def times[A](f: => A):Unit = {
|
||||
@tailrec
|
||||
def loop( current: Int): Unit =
|
||||
if (current > 0) {
|
||||
f
|
||||
loop(current - 1)
|
||||
}
|
||||
loop(x)
|
||||
}
|
||||
}
|
||||
|
||||
5 times println("ha") // Not recommended infix for 5.times(println("ha")) aka dot notation
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue