This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,8 @@
Loop over multiple arrays (or lists or tuples or whatever they're called in your language) and print the ''i''th element of each. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop.
For this example, loop over the arrays <code>(a,b,c)</code>, <code>(A,B,C)</code> and <code>(1,2,3)</code> to produce the output
<pre>aA1
bB2
cC3</pre>
If possible, also describe what happens when the arrays are of different lengths.

View file

@ -0,0 +1,2 @@
---
note: Iteration

View file

@ -0,0 +1,12 @@
(defun print-lists (xs ys zs)
(if (or (endp xs) (endp ys) (endp zs))
nil
(progn$ (cw (first xs))
(cw "~x0~x1~%"
(first ys)
(first zs))
(print-lists (rest xs)
(rest ys)
(rest zs)))))
(print-lists '("a" "b" "c") '(A B C) '(1 2 3))

View file

@ -0,0 +1,4 @@
[]UNION(CHAR,INT) x=("a","b","c"), y=("A","B","C"), z=(1,2,3);
FOR i TO UPB x DO
printf(($ggd$, x[i], y[i], z[i], $l$))
OD

View file

@ -0,0 +1,9 @@
BEGIN {
split("a,b,c", a, ",");
split("A,B,C", b, ",");
split("1,2,3", c, ",");
for(i = 1; i <= length(a); i++) {
print a[i] b[i] c[i];
}
}

View file

@ -0,0 +1,12 @@
with Ada.Text_IO; use Ada.Text_IO;
procedure Array_Loop_Test is
type Array_Index is range 1..3;
A1 : array (Array_Index) of Character := "abc";
A2 : array (Array_Index) of Character := "ABC";
A3 : array (Array_Index) of Integer := (1, 2, 3);
begin
for Index in Array_Index'Range loop
Put_Line (A1 (Index) & A2 (Index) & Integer'Image (A3 (Index))(2));
end loop;
end Array_Loop_Test;

View file

@ -0,0 +1,22 @@
List1 = a,b,c
List2 = A,B,C
List3 = 1,2,3
MsgBox, % LoopMultiArrays()
List1 = a,b,c,d,e
List2 = A,B,C,D
List3 = 1,2,3
MsgBox, % LoopMultiArrays()
;---------------------------------------------------------------------------
LoopMultiArrays() { ; print the ith element of each
;---------------------------------------------------------------------------
local Result
StringSplit, List1_, List1, `,
StringSplit, List2_, List2, `,
StringSplit, List3_, List3, `,
Loop, % List1_0
Result .= List1_%A_Index% List2_%A_Index% List3_%A_Index% "`n"
Return, Result
}

View file

@ -0,0 +1,17 @@
List1 := ["a", "b", "c"]
List2 := ["A", "B", "C"]
List3 := [ 1 , 2 , 3 ]
MsgBox, % LoopMultiArrays()
List1 := ["a", "b", "c", "d", "e"]
List2 := ["A", "B", "C", "D"]
List3 := [1,2,3]
MsgBox, % LoopMultiArrays()
LoopMultiArrays() {
local Result
For key, value in List1
Result .= value . List2[key] . List3[key] "`n"
Return, Result
}

View file

@ -0,0 +1,8 @@
DIM array1$(2), array2$(2), array3%(2)
array1$() = "a", "b", "c"
array2$() = "A", "B", "C"
array3%() = 1, 2, 3
FOR index% = 0 TO 2
PRINT array1$(index%) ; array2$(index%) ; array3%(index%)
NEXT

View file

@ -0,0 +1,5 @@
main: { (('a' 'b' 'c')('A' 'B' 'C')('1' '2' '3')) simul_array }
simul_array!:
{ trans
{ { << } each "\n" << } each }

View file

@ -0,0 +1,18 @@
main: { (('a' 'b' 'c')('A' 'B' 'C')('1' '2' '3')) simul_array }
simul_array!:
{{ dup
{ car << } each
cdrall }
{ allnil? not }
while }
cdrall!: { { { cdr } each -1 take } nest }
-- only returns true if all elements of a list are nil
allnil?!:
{ 1 <->
{ car nil?
{ zap 0 last }
{ nil }
if} each }

View file

@ -0,0 +1,18 @@
#include <iostream>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<char> ls(3); ls[0] = 'a'; ls[1] = 'b'; ls[2] = 'c';
std::vector<char> us(3); us[0] = 'A'; us[1] = 'B'; us[2] = 'C';
std::vector<int> ns(3); ns[0] = 1; ns[1] = 2; ns[2] = 3;
std::vector<char>::const_iterator lIt = ls.begin();
std::vector<char>::const_iterator uIt = us.begin();
std::vector<int>::const_iterator nIt = ns.begin();
for(; lIt != ls.end() && uIt != us.end() && nIt != ns.end();
++lIt, ++uIt, ++nIt)
{
std::cout << *lIt << *uIt << *nIt << "\n";
}
}

View file

@ -0,0 +1,15 @@
#include <iostream>
int main(int argc, char* argv[])
{
char ls[] = {'a', 'b', 'c'};
char us[] = {'A', 'B', 'C'};
int ns[] = {1, 2, 3};
for(size_t li = 0, ui = 0, ni = 0;
li < sizeof(ls) && ui < sizeof(us) && ni < sizeof(ns) / sizeof(int);
++li, ++ui, ++ni)
{
std::cout << ls[li] << us[ui] << ns[ni] << "\n";
}
}

View file

@ -0,0 +1,11 @@
#include <stdio.h>
char a1[] = {'a','b','c'};
char a2[] = {'A','B','C'};
int a3[] = {1,2,3};
int main(void) {
for (int i = 0; i < 3; i++) {
printf("%c%c%i\n", a1[i], a2[i], a3[i]);
}
}

View file

@ -0,0 +1,90 @@
#include <stdio.h>
typedef struct closure {
void (*f)( void *elem, void *data);
void *data;
} *Closure;
typedef struct listSpec{
void (*print)(const void *);
void *ary;
int count;
size_t elem_size;
} *ListSpec;
#define LIST_SPEC( pf,typ,aa) {pf, aa, (sizeof(aa)/sizeof(typ)), sizeof(typ) }
/* calls closure's function f for each element in list */
void DoForEach( Closure c, ListSpec aspec )
{
int i;
void *val_ptr;
for (i=0, val_ptr=aspec->ary; i< aspec->count; i++) {
(*c->f)(val_ptr, c->data);
val_ptr = ((char *)val_ptr) + aspec->elem_size;
}
}
/* Used to find the minimum array length of list of lists */
void FindMin( ListSpec *paspec, int *minCount )
{
ListSpec aspec = *paspec;
if (*minCount>aspec->count) *minCount = aspec->count;
}
/* prints an element of a list using the list's print function */
void PrintElement( ListSpec *paspec, int *indx )
{
ListSpec aspec = *paspec;
(*aspec->print)( ((char*)aspec->ary) + (*indx)*aspec->elem_size);
}
/* Loop Over multiple lists (a list of lists)*/
void LoopMultiple( ListSpec arrays)
{
int indx;
int minCount = 100000;
struct closure c1 = { &FindMin, &minCount };
struct closure xclsr = { &PrintElement, &indx };
DoForEach( &c1, arrays);
printf("min count = %d\n", minCount);
for (indx=0; indx<minCount; indx++) {
DoForEach(&xclsr, arrays );
printf("\n");
}
}
/* Defining our Lists */
void PrintInt(const int *ival)
{ printf("%3d,", *ival);
}
int ary1[] = { 6,5,4,9,8,7 };
struct listSpec lspec1 = LIST_SPEC( &PrintInt, int, ary1 );
void PrintShort(const short *ival)
{ printf("%3d,", *ival);
}
short ary2[] = { 3, 66, 20, 15, 7, 22, 10 };
struct listSpec lspec2 = LIST_SPEC( &PrintShort , short, ary2 );
void PrintStrg(const char *const *strg )
{ printf(" %s", *strg);
}
const char *ary3[] = {"Hello", "all", "you","good", "folks","out", "there" };
struct listSpec lspec3 = LIST_SPEC( &PrintStrg , const char *, ary3 );
void PrintLstSpec(const ListSpec *ls)
{ printf("not-implemented");
}
ListSpec listList[] = { &lspec1, &lspec2, &lspec3 };
struct listSpec llSpec = LIST_SPEC(&PrintLstSpec, ListSpec, listList);
int main(int argc, char *argv[])
{
LoopMultiple( &llSpec);
return 0;
}

View file

@ -0,0 +1,2 @@
(doseq [s (map #(str %1 %2 %3) "abc" "ABC" "123")]
(println s))

View file

@ -0,0 +1,5 @@
(mapc (lambda (&rest args)
(format t "~{~A~}~%" args))
'(|a| |b| |c|)
'(a b c)
'(1 2 3))

View file

@ -0,0 +1,4 @@
(loop for x in '("a" "b" "c")
for y in '(a b c)
for z in '(1 2 3)
do (format t "~a~a~a~%" x y z))

View file

@ -0,0 +1,6 @@
import std.stdio, std.range;
void main () {
foreach (a, b, c; zip("abc", "ABC", [1, 2, 3]))
writeln(a, b, c);
}

View file

@ -0,0 +1,21 @@
import std.stdio, std.range;
void main () {
auto a1 = [1, 2];
auto a2 = [1, 2, 3];
alias StoppingPolicy sp;
// Stops when the shortest range is exhausted
foreach (p; zip(sp.shortest, a1, a2))
writeln(p.tupleof);
writeln();
// Stops when the longest range is exhausted
foreach (p; zip(sp.longest, a1, a2))
writeln(p.tupleof);
writeln();
// Requires that all ranges are equal
foreach (p; zip(sp.requireSameLength, a1, a2))
writeln(p.tupleof);
}

View file

@ -0,0 +1,15 @@
import std.stdio, std.range;
void main() {
auto arr1 = [1, 2, 3, 4, 5];
auto arr2 = [6, 7, 8, 9, 10];
foreach (ref a, ref b; lockstep(arr1, arr2))
a += b;
assert(arr1 == [7, 9, 11, 13, 15]);
// Lockstep also supports iteration with an index variable
foreach (index, a, b; lockstep(arr1, arr2))
writefln("Index %s: a = %s, b = %s", index, a, b);
}

View file

@ -0,0 +1,10 @@
import std.stdio, std.algorithm;
void main () {
auto s1 = "abc";
auto s2 = "ABC";
auto a1 = [1, 2];
foreach (i; 0 .. min(s1.length, s2.length, a1.length))
writeln(s1[i], s2[i], a1[i]);
}

View file

@ -0,0 +1,7 @@
const a1 = ['a', 'b', 'c'];
const a2 = ['A', 'B', 'C'];
const a3 = [1, 2, 3];
var i : Integer;
for i := 0 to 2 do
PrintLn(Format('%s%s%d', [a1[i], a2[i], a3[i]]));

View file

@ -0,0 +1,18 @@
program LoopOverArrays;
{$APPTYPE CONSOLE}
uses SysUtils;
const
ARRAY1: array [1..3] of string = ('a', 'b', 'c');
ARRAY2: array [1..3] of string = ('A', 'B', 'C');
ARRAY3: array [1..3] of Integer = (1, 2, 3);
var
i: Integer;
begin
for i := 1 to 3 do
Writeln(Format('%s%s%d', [ARRAY1[i], ARRAY2[i], ARRAY3[i]]));
Readln;
end.

View file

@ -0,0 +1,7 @@
def a1 := ["a","b","c"]
def a2 := ["A","B","C"]
def a3 := ["1","2","3"]
for i => v1 in a1 {
println(v1, a2[i], a3[i])
}

View file

@ -0,0 +1,3 @@
for [v1, v2, v3] in zip(a1, a2, a3) {
println(v1, v2, v3)
}

View file

@ -0,0 +1,28 @@
def zip {
to run(l1, l2) {
def zipped {
to iterate(f) {
for i in int >= 0 {
f(i, [l1.fetch(i, fn { return }),
l2.fetch(i, fn { return })])
}
}
}
return zipped
}
match [`run`, lists] {
def zipped {
to iterate(f) {
for i in int >= 0 {
var tuple := []
for l in lists {
tuple with= l.fetch(i, fn { return })
}
f(i, tuple)
}
}
}
zipped
}
}

View file

@ -0,0 +1,4 @@
@public
run = fn () {
lists.foreach(fn ((A, B, C)) { io.format("~s~n", [[A, B, C]]) }, lists.zip3("abc", "ABC", "123"))
}

View file

@ -0,0 +1,5 @@
open console list imperative
xs = zipWith3 (\x y z -> show x ++ show y ++ show z) ['a','b','c'] ['A','B','C'] [1,2,3]
each writen xs

View file

@ -0,0 +1 @@
xs = zipWith3 (\x -> (x++) >> (++)) "abc" "ABC" "123"

View file

@ -0,0 +1 @@
lists:zipwith3(fun(A,B,C)-> io:format("~s~n",[[A,B,C]]) end, "abc", "ABC", "123").

View file

@ -0,0 +1,2 @@
lists:foreach(fun({A,B,C}) -> io:format("~s~n",[[A,B,C]]) end,
lists:zip3("abc", "ABC", "123")).

View file

@ -0,0 +1,9 @@
sequence a, b, c
a = "abc"
b = "ABC"
c = "123"
for i = 1 to length(a) do
puts(1, a[i] & b[i] & c[i] & "\n")
end for

View file

@ -0,0 +1,9 @@
sequence a, b, c
a = "abc"
b = "ABC"
c = {1, 2, 3}
for i = 1 to length(a) do
printf(1, "%s%s%g\n", {a[i], b[i], c[i]})
end for

View file

@ -0,0 +1,12 @@
for i = 1 to length(a) do
if (a[i] >= '0' and a[i] <= '9') then
a[i] -= '0'
end if
if (b[i] >= '0' and b[i] <= '9') then
b[i] -= '0'
end if
if (c[i] >= '0' and c[i] <= '9') then
c[i] -= '0'
end if
printf(1, "%s%s%s\n", {a[i], b[i], c[i]})
end for

View file

@ -0,0 +1 @@
"abc" "ABC" "123" [ [ write1 ] tri@ nl ] 3each

View file

@ -0,0 +1,13 @@
class LoopMultiple
{
public static Void main ()
{
List arr1 := ["a", "b", "c"]
List arr2 := ["A", "B", "C"]
List arr3 := [1, 2, 3]
[arr1.size, arr2.size, arr3.size].min.times |Int i|
{
echo ("${arr1[i]}${arr2[i]}${arr3[i]}")
}
}
}

View file

@ -0,0 +1,19 @@
create a char a , char b , char c ,
create b char A , char B , char C ,
create c char 1 , char 2 , char 3 ,
: main
3 0 do cr
a i cells + @ emit
b i cells + @ emit
c i cells + @ emit
loop
cr
a b c
3 0 do cr
3 0 do
rot dup @ emit cell+
loop
loop
drop drop drop
;

View file

@ -0,0 +1,15 @@
program main
implicit none
integer,parameter :: n_vals = 3
character(len=*),dimension(n_vals),parameter :: ls = ['a','b','c']
character(len=*),dimension(n_vals),parameter :: us = ['A','B','C']
integer,dimension(n_vals),parameter :: ns = [1,2,3]
integer :: i !counter
do i=1,n_vals
write(*,'(A1,A1,I1)') ls(i),us(i),ns(i)
end do
end program main

View file

@ -0,0 +1,13 @@
package main
import "fmt"
var a1 = []int{'a','b','c'}
var a2 = []int{'A','B','C'}
var a3 = []int{1,2,3}
func main() {
for i := range a1 {
fmt.Printf("%c%c%d\n", a1[i], a2[i], a3[i])
}
}

View file

@ -0,0 +1,4 @@
["a" "b" "c"]:a;
["A" "B" "C"]:b;
["1" "2" "3"]:c;
[a b c]zip{puts}/

View file

@ -0,0 +1,6 @@
def synchedConcat = { a1, a2, a3 ->
assert a1 && a2 && a3
assert a1.size() == a2.size()
assert a2.size() == a3.size()
[a1, a2, a3].transpose().collect { "${it[0]}${it[1]}${it[2]}" }
}

View file

@ -0,0 +1,5 @@
def x = ['a', 'b', 'c']
def y = ['A', 'B', 'C']
def z = [1, 2, 3]
synchedConcat(x, y, z).each { println it }

View file

@ -0,0 +1 @@
main = mapM_ putStrLn $ zipWith3 (\a b c -> [a,b,c]) "abc" "ABC" "123"

View file

@ -0,0 +1,22 @@
using Lambda;
using Std;
class Main
{
static function main()
{
var a = ['a', 'b', 'c'];
var b = ['A', 'B', 'C'];
var c = [1, 2, 3];
//Find smallest array
var len = [a, b, c]
.map(function(a) return a.length)
.fold(Math.min, 0x0FFFFFFF)
.int();
for (i in 0...len)
Sys.println(a[i] + b[i] + c[i].string());
}
}

View file

@ -0,0 +1,8 @@
CHARACTER :: A = "abc"
REAL :: C(3)
C = $ ! 1, 2, 3
DO i = 1, 3
WRITE() A(i), "ABC"(i), C(i)
ENDDO

View file

@ -0,0 +1,6 @@
procedure main()
a := create !["a","b","c"]
b := create !["A","B","C"]
c := create !["1","2","3"]
while write(@a,@b,@c)
end

View file

@ -0,0 +1,11 @@
link numbers # for max
procedure main()
a := ["a","b","c"]
b := ["A","B","C","D"]
c := [1,2,3]
every i := 1 to max(*a,*b,*c) do
write(a[i]|"","\t",b[i]|"","\t",c[i]|"")
end

View file

@ -0,0 +1 @@
,.&:(":"0@>)/ 'abc' ; 'ABC' ; 1 2 3

View file

@ -0,0 +1 @@
,.&:>/ 'abc' ; 'ABC' ; '123'

View file

@ -0,0 +1 @@
|: 'abc', 'ABC' ,:;":&> 1 2 3

View file

@ -0,0 +1 @@
|: 'abc', 'ABC',: '123'

View file

@ -0,0 +1 @@
|:>]&.>L:_1 'abc';'ABC';<1 2 3

View file

@ -0,0 +1,6 @@
String[] a = {"a","b","c"};
String[] b = {"A","B","C"};
int[] c = {1,2,3};
for (int i = 0;i < a.length;i++) {
System.out.println(a[i] + b[i] + c[i] + "\n");
}

View file

@ -0,0 +1,8 @@
var a = ["a","b","c"],
b = ["A","B","C"],
c = [1,2,3],
output = "",
i;
for (i = 0; i < a.length; i += 1) {
output += a[i] + b[i] + c[i] + "\n";
}

View file

@ -0,0 +1 @@
{,/$x}'+("abc";"ABC";1 2 3)

View file

@ -0,0 +1,3 @@
("aA1"
"bB2"
"cC3")

View file

@ -0,0 +1 @@
{+x[;!(&/#:'x)]}("abc";"ABC";"1234")

View file

@ -0,0 +1,3 @@
("aA1"
"bB2"
"cC3")

View file

@ -0,0 +1 @@
{a:,/'($:'x);+a[;!(&/#:'a)]}("abc";"ABC";1 2 3 4)

View file

@ -0,0 +1,8 @@
a$(1)="a" : a$(2)="b" : a$(3)="c"
b$(1)="A" : b$(2)="B" : b$(3)="C"
c(1)=1 : c(2)=2 : c(3)=3
for i = 1 to 3
print a$(i);b$(i);c(i)
next

View file

@ -0,0 +1,27 @@
Section Header
+ name := ARRAY_LOOP_TEST;
Section Public
- main <- (
+ a1, a2 : ARRAY[CHARACTER];
+ a3 : ARRAY[INTEGER];
a1 := ARRAY[CHARACTER].create 1 to 3;
a2 := ARRAY[CHARACTER].create 1 to 3;
a3 := ARRAY[INTEGER].create 1 to 3;
1.to 3 do { i : INTEGER;
a1.put ((i - 1 + 'a'.code).to_character) to i;
a2.put ((i - 1 + 'A'.code).to_character) to i;
a3.put i to i;
};
1.to 3 do { i : INTEGER;
a1.item(i).print;
a2.item(i).print;
a3.item(i).print;
'\n'.print;
};
);

View file

@ -0,0 +1,3 @@
show (map [(word ?1 ?2 ?3)] [a b c] [A B C] [1 2 3]) ; [aA1 bB2 cC3]
(foreach [a b c] [A B C] [1 2 3] [print (word ?1 ?2 ?3)]) ; as above, one per line

View file

@ -0,0 +1,2 @@
a1, a2, a3 = {a , b , c } , { A , B , C } , { 1 , 2 , 3 }
for i = 1, 3 do print(a1[i],a2[i],a3[i]) end

View file

@ -0,0 +1,9 @@
function iter(a, b, c)
local i = 0
return function()
i = i + 1
return a[i], b[i], c[i]
end
end
for u, v, w in iter(a1, a2, a3) do print(u, v, w) end

View file

@ -0,0 +1,9 @@
LOOPMULT
N A,B,C,D,%
S A="a,b,c,d"
S B="A,B,C,D"
S C="1,2,3"
S D=","
F %=1:1:$L(A,",") W !,$P(A,D,%),$P(B,D,%),$P(C,D,%)
K A,B,C,D,%
Q

View file

@ -0,0 +1,8 @@
LOOPMULU
N A,B,C,D,%
S A(1)="a",A(2)="b",A(3)="c",A(4)="d"
S B(1)="A",B(2)="B",B(3)="C",B(4)="D"
S C(1)="1",C(2)="2",C(3)="3"
; will error S %=$O(A("")) F Q:%="" W !,A(%),B(%),C(%) S %=$O(A(%))
S %=$O(A("")) F Q:%="" W !,$G(A(%)),$G(B(%)),$G(C(%)) S %=$O(A(%))
K A,B,C,D,%

View file

@ -0,0 +1 @@
MapThread[Print, {{"a", "b", "c"}, {"A", "B", "C"}, {1, 2, 3}}];

View file

@ -0,0 +1,19 @@
:- module multi_array_loop.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module char, list, string.
main(!IO) :-
A = ['a', 'b', 'c'],
B = ['A', 'B', 'C'],
C = [1, 2, 3],
list.foldl_corresponding3(print_elems, A, B, C, !IO).
:- pred print_elems(char::in, char::in, int::in, io::di, io::uo) is det.
print_elems(A, B, C, !IO) :-
io.format("%c%c%i\n", [c(A), c(B), i(C)], !IO).

View file

@ -0,0 +1,16 @@
MODULE MultiArray EXPORTS Main;
IMPORT IO, Fmt;
TYPE ArrIdx = [1..3];
VAR
arr1 := ARRAY ArrIdx OF CHAR {'a', 'b', 'c'};
arr2 := ARRAY ArrIdx OF CHAR {'A', 'B', 'C'};
arr3 := ARRAY ArrIdx OF INTEGER {1, 2, 3};
BEGIN
FOR i := FIRST(ArrIdx) TO LAST(ArrIdx) DO
IO.Put(Fmt.Char(arr1[i]) & Fmt.Char(arr2[i]) & Fmt.Int(arr3[i]) & "\n");
END;
END MultiArray.

View file

@ -0,0 +1,10 @@
$a = array('a', 'b', 'c');
$b = array('A', 'B', 'C');
$c = array('1', '2', '3'); //These don't *have* to be strings, but it saves PHP from casting them later
if ((sizeOf($a) !== sizeOf($b)) || (sizeOf($b) !== sizeOf($c))){
throw new Exception('All three arrays must be the same length');
}
foreach ($a as $key => $value){
echo "{$a[$key]}{$b[$key]}{$c[$key]}\n";
}

View file

@ -0,0 +1,13 @@
sub zip (&@)
{
my $code = shift;
my $min;
$min = $min && $#$_ > $min ? $min : $#$_ for @_;
for my $i(0..$min){ $code->(map $_->[$i] ,@_) }
}
my @a1 = qw( a b c );
my @a2 = qw( A B C );
my @a3 = qw( 1 2 3 );
zip { print @_,"\n" }\(@a1, @a2, @a3);

View file

@ -0,0 +1,4 @@
(mapc prinl
'(a b c)
'(A B C)
(1 2 3) )

View file

@ -0,0 +1,5 @@
multiple_arrays(L1, L2, L3) :-
maplist(display, L1, L2, L3).
display(A,B,C) :-
writef('%s%s%s\n', [[A],[B],[C]]).

View file

@ -0,0 +1,5 @@
>>> print ( '\n'.join(''.join(x) for x in zip('abc', 'ABC', '123')) )
aA1
bB2
cC3
>>>

View file

@ -0,0 +1,5 @@
>>> print ( '\n'.join(map(lambda *x: ''.join(x), 'abc', 'ABC', '123')) )
aA1
bB2
cC3
>>>

View file

@ -0,0 +1,6 @@
from itertools import imap
def join3(a,b,c):
print a+b+c
imap(join3,'abc','ABC','123')

View file

@ -0,0 +1,8 @@
>>> from itertools import zip_longest
>>> print ( '\n'.join(''.join(x) for x in zip_longest('abc', 'ABCD', '12345', fillvalue='#')) )
aA1
bB2
cC3
#D4
##5
>>>

View file

@ -0,0 +1,21 @@
multiloop <- function(...)
{
# Retrieve inputs and convert to a list of character strings
arguments <- lapply(list(...), as.character)
# Get length of each input
lengths <- sapply(arguments, length)
# Loop over elements
for(i in seq_len(max(lengths)))
{
# Loop over inputs
for(j in seq_len(nargs()))
{
# print a value or a space (if that input has finished)
cat(ifelse(i <= lengths[j], arguments[[j]][i], " "))
}
cat("\n")
}
}
multiloop(letters[1:3], LETTERS[1:3], 1:3)

View file

@ -0,0 +1,2 @@
apply(data.frame(letters[1:3], LETTERS[1:3], 1:3), 1,
function(row) { cat(row, "\n", sep='') })

View file

@ -0,0 +1,11 @@
/*REXX program shows how to simultaneously loop over multiple arrays.*/
x. = ' '; x.1 = "a"; x.2 = 'b'; x.3 = "c"
y. = ' '; y.1 = "A"; y.2 = 'B'; y.3 = "C"
z. = ' '; z.1 = "1"; z.2 = '2'; z.3 = "3"
do j=1 until output=''
output=x.j || y.j || z.j
say output
end /*j*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,11 @@
/*REXX program shows how to simultaneously loop over multiple arrays.*/
x.=' '; x.1="a"; x.2='b'; x.3="c"; x.4='d'
y.=' '; y.1="A"; y.2='B'; y.3="C";
z.=' '; z.1= 1 ; z.2= 2 ; z.3= 3 ; z.4= 4; z.5= 5
do j=1 until output=''
output= x.j || y.j || z.j
say output
end /*j*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,6 @@
#lang racket
(for ([i-1 '(a b c)]
[i-2 '(A B C)]
[i-3 '(1 2 3)])
(printf "~a ~a ~a~n" i-1 i-2 i-3))

View file

@ -0,0 +1 @@
['a','b','c'].zip(['A','B','C'], [1,2,3]) {|i,j,k| puts "#{i}#{j}#{k}"}

View file

@ -0,0 +1 @@
['a','b','c'].zip(['A','B','C'], [1,2,3]) {|a| puts a.join('')}

View file

@ -0,0 +1,10 @@
class MAIN is
main is
a :ARRAY{STR} := |"a", "b", "c"|;
b :ARRAY{STR} := |"A", "B", "C"|;
c :ARRAY{STR} := |"1", "2", "3"|;
loop i ::= 0.upto!(2);
#OUT + a[i] + b[i] + c[i] + "\n";
end;
end;
end;

View file

@ -0,0 +1,3 @@
("abc", "ABC", "123").zipped foreach { (x, y, z) =>
println(x.toString + y + z)
}

View file

@ -0,0 +1,10 @@
(let ((a '("a" "b" "c"))
(b '("A" "B" "C"))
(c '(1 2 3)))
(for-each
(lambda (i1 i2 i3)
(display i1)
(display i2)
(display i3)
(newline))
a b c))

View file

@ -0,0 +1,10 @@
(let ((a (vector "a" "b" "c"))
(b (vector "A" "B" "C"))
(c (vector 1 2 3)))
(vector-for-each
(lambda (current-index i1 i2 i3)
(display i1)
(display i2)
(display i3)
(newline))
a b c))

View file

@ -0,0 +1,10 @@
|a b c|
a := OrderedCollection new addAll: #('a' 'b' 'c').
b := OrderedCollection new addAll: #('A' 'B' 'C').
c := OrderedCollection new addAll: #(1 2 3).
1 to: (a size) do: [ :i |
(a at: i) display.
(b at: i) display.
(c at: i) displayNl.
].

View file

@ -0,0 +1,6 @@
set list1 {a b c}
set list2 {A B C}
set list3 {1 2 3}
foreach i $list1 j $list2 k $list3 {
puts "$i$j$k"
}