Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/Loops/Foreach
note: Iteration

View file

@ -0,0 +1,23 @@
Loop through and print each element in a collection in order.
Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop.
;Related tasks:
*   [[Loop over multiple arrays simultaneously]]
*   [[Loops/Break]]
*   [[Loops/Continue]]
*   [[Loops/Do-while]]
*   [[Loops/Downward for]]
*   [[Loops/For]]
*   [[Loops/For with a specified step]]
*   [[Loops/Foreach]]
*   [[Loops/Increment loop index within loop body]]
*   [[Loops/Infinite]]
*   [[Loops/N plus one half]]
*   [[Loops/Nested]]
*   [[Loops/While]]
*   [[Loops/with multiple ranges]]
*   [[Loops/Wrong ranges]]
<br><br>

View file

@ -0,0 +1,4 @@
V things = [Apple, Banana, Coconut]
L(thing) things
print(thing)

View file

@ -0,0 +1,5 @@
(defun print-list (xs)
(if (endp xs)
nil
(prog2$ (cw "~x0~%" (first xs))
(print-list (rest xs)))))

View file

@ -0,0 +1,5 @@
[]UNION(STRING, INT, PROC(REF FILE)VOID) collection = ("Mary","Had",1,"little","lamb.",new line);
FOR index FROM LWB collection TO UPB collection DO
print((collection[index]," "))
OD

View file

@ -0,0 +1,3 @@
FORALL index IN collection DO
print((collection[index]," "))
OD

View file

@ -0,0 +1,6 @@
BEGIN {
split("Mary had a little lamb", strs, " ")
for(el in strs) {
print strs[el]
}
}

View file

@ -0,0 +1,6 @@
BEGIN {
n = split("Mary had a little lamb", strs, " ")
for(i=1; i <= n; i++) {
print strs[i]
}
}

View file

@ -0,0 +1,6 @@
# This will not work
BEGIN {
for (el in "apples","bananas","cherries") {
print "I like " el
}
}

View file

@ -0,0 +1,17 @@
PROC Main()
DEFINE PTR="CARD"
BYTE i
PTR ARRAY items(10)
items(0)="First" items(1)="Second"
items(2)="Third" items(3)="Fourth"
items(4)="Fifth" items(5)="Sixth"
items(6)="Seventh" items(7)="Eighth"
items(8)="Ninth" items(9)="Tenth"
PrintE("In Action! there is no for-each loop")
PutE()
FOR i=0 TO 9
DO
PrintE(items(i))
OD
RETURN

View file

@ -0,0 +1,14 @@
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;
procedure For_Each is
A : array (1..5) of Integer := (-1, 0, 1, 2, 3);
begin
for Num in A'Range loop
Put( A (Num) );
end loop;
end For_Each;

View file

@ -0,0 +1,3 @@
for Item of A loop
Put( Item );
end loop;

View file

@ -0,0 +1,25 @@
with Ada.Integer_Text_IO, Ada.Containers.Doubly_Linked_Lists;
use Ada.Integer_Text_IO, Ada.Containers;
procedure Doubly_Linked_List is
package DL_List_Pkg is new Doubly_Linked_Lists (Integer);
use DL_List_Pkg;
procedure Print_Node (Position : Cursor) is
begin
Put (Element (Position));
end Print_Node;
DL_List : List;
begin
DL_List.Append (1);
DL_List.Append (2);
DL_List.Append (3);
-- Iterates through every node of the list.
DL_List.Iterate (Print_Node'Access);
end Doubly_Linked_List;

View file

@ -0,0 +1,25 @@
with Ada.Integer_Text_IO, Ada.Containers.Vectors;
use Ada.Integer_Text_IO, Ada.Containers;
procedure Vector_Example is
package Vector_Pkg is new Vectors (Natural, Integer);
use Vector_Pkg;
procedure Print_Element (Position : Cursor) is
begin
Put (Element (Position));
end Print_Element;
V : Vector;
begin
V.Append (1);
V.Append (2);
V.Append (3);
-- Iterates through every element of the vector.
V.Iterate (Print_Element'Access);
end Vector_Example;

View file

@ -0,0 +1,4 @@
var str = "hello world"
foreach ch str { // you can also use an optional 'in'
println (ch) // one character at a time
}

View file

@ -0,0 +1,4 @@
var vec = [1,2,3,4]
foreach v vec { // you can also use an optional 'in'
println (v)
}

View file

@ -0,0 +1,4 @@
var cities = {"San Ramon": 50000, "Walnut Creek": 70000, "San Francisco": 700000} // map literal
foreach city cities {
println (city.first + " has population " + city.second)
}

View file

@ -0,0 +1,13 @@
foreach i 100 {
println (i) // prints values 0..99
}
foreach i 10..20 {
println (i) // prints values 10..20
}
var a = 20
var b = 10
foreach i a..b {
println (i) // prints values from a to b (20..10)
}

View file

@ -0,0 +1,34 @@
class List {
class Element (public data) {
public var next = null
}
var start = null
public function insert (data) {
var element = new Element (data)
element.next = start
start = element
}
public operator foreach (var iter) {
if (typeof(iter) == "none") { // first iteration
iter = start
return iter.data
} elif (iter.next == null) { // check for last iteration
iter = none
} else {
iter = iter.next // somewhere in the middle
return iter.data
}
}
}
var list = new List()
list.insert (1)
list.insert (2)
list.insert (4)
foreach n list {
println (n)
}

View file

@ -0,0 +1,13 @@
// coroutine to generate the squares of a sequence of numbers
function squares (start, end) {
for (var i = start ; i < end ; i++) {
yield i*i
}
}
var start = 10
var end = 20
foreach s squares (start, end) {
println (s)
}

View file

@ -0,0 +1,4 @@
var s = openin ("input.txt")
foreach line s {
print (line)
}

View file

@ -0,0 +1,7 @@
enum Color {
RED, GREEN, BLUE
}
foreach color Color {
println (color)
}

View file

@ -0,0 +1,5 @@
# iterate over a list of integers
integer i, v;
for (i, v in list(2, 3, 5, 7, 11, 13, 17, 18)) {
}

View file

@ -0,0 +1,9 @@
PROC main()
DEF a_list : PTR TO LONG, a
a_list := [10, 12, 14]
FOR a := 0 TO ListLen(a_list)-1
WriteF('\d\n', a_list[a])
ENDFOR
-> if the "action" fits a single statement, we can do instead
ForAll({a}, a_list, `WriteF('\d\n', a))
ENDPROC

View file

@ -0,0 +1,5 @@
Integer[] myInts = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (Integer i : myInts) {
System.debug(i);
}

View file

@ -0,0 +1,3 @@
repeat with fruit in {"Apple", "Orange", "Banana"}
log contents of fruit
end repeat

View file

@ -0,0 +1,13 @@
arr: ["one" "two" "three"]
dict: #[
name: "John"
surname: "Doe"
age: 34
]
loop arr 'item ->
print item
loop dict [key val]->
print [key "=>" val]

View file

@ -0,0 +1,3 @@
string = mary,had,a,little,lamb
Loop, Parse, string, `,
MsgBox %A_LoopField%

View file

@ -0,0 +1,7 @@
DIM collection$(1)
collection$ = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog." }
FOR i = 0 TO collection$[?]-1
PRINT collection$[i]+ " ";
NEXT i
PRINT

View file

@ -0,0 +1,8 @@
DIM collection$(8)
collection$() = "The", "quick", "brown", "fox", "jumps", \
\ "over", "the", "lazy", "dog."
FOR index% = 0 TO DIM(collection$(), 1)
PRINT collection$(index%) " ";
NEXT
PRINT

View file

@ -0,0 +1,8 @@
OPTION COLLAPSE TRUE
FOR x$ IN "Hello cruel world"
PRINT x$
NEXT
FOR y$ IN "1,2,\"3,4\",5" STEP ","
PRINT y$
NEXT

View file

@ -0,0 +1,4 @@
@echo off
for %%A in (This is a sample collection) do (
echo %%A
)

View file

@ -0,0 +1,5 @@
@echo off
set "collection=This is a sample collection"
for %%A in (%collection%) do (
echo %%A
)

View file

@ -0,0 +1,6 @@
a[0] = .123
a[1] = 234
a[3] = 95.6
for (i = 0; i < 4; i++) {
a[i]
}

View file

@ -0,0 +1,7 @@
( list
= Afrikaans
Ελληνικά
עברית
മലയാളം
ئۇيغۇرچە
)

View file

@ -0,0 +1,2 @@
!list:?L
& whl'(!L:%?language ?L&out$!language)

View file

@ -0,0 +1,7 @@
!list:?L
& ( loop
= !L:%?language ?L
& out$!language
& !loop
)
& ~!loop

View file

@ -0,0 +1,4 @@
( !list
: ? (%@?language&out$!language&~) ?
|
)

View file

@ -0,0 +1,4 @@
for (container_type::iterator i = container.begin(); i != container.end(); ++i)
{
std::cout << *i << "\n";
}

View file

@ -0,0 +1,2 @@
std::copy(container.begin(), container.end(),
std::ostream_iterator<container_type::value_type>(std::cout, "\n"));

View file

@ -0,0 +1,7 @@
void print_element(container_type::value_type const& v)
{
std::cout << v << "\n";
}
...
std::for_each(container.begin(), container.end(), print_element);

View file

@ -0,0 +1,4 @@
for (auto element: container)
{
std::cout << element << "\n";
}

View file

@ -0,0 +1,4 @@
for (auto const& element: container)
{
std::cout << element << "\n";
}

View file

@ -0,0 +1,4 @@
for (auto&& element: container) //use a 'universal reference'
{
element += 42;
}

View file

@ -0,0 +1,4 @@
set collection=(first second third fourth "something else")
foreach x ($collection:q)
echo $x:q
end

View file

@ -0,0 +1,6 @@
string[] things = {"Apple", "Banana", "Coconut"};
foreach (string thing in things)
{
Console.WriteLine(thing);
}

View file

@ -0,0 +1,10 @@
#include <stdio.h>
...
const char *list[] = {"Red","Green","Blue","Black","White"};
#define LIST_SIZE (sizeof(list)/sizeof(list[0]))
int ix;
for(ix=0; ix<LIST_SIZE; ix++) {
printf("%s\n", list[ix]);
}

View file

@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
/* foreach macro for using a string as a collection of char */
#define foreach( ptrvar , strvar ) char* ptrvar; for( ptrvar=strvar ; (*ptrvar) != '\0' ; *ptrvar++)
int main(int argc,char* argv[]){
char* s1="abcdefg";
char* s2="123456789";
foreach( p1 , s1 ) {
printf("loop 1 %c\n",*p1);
}
foreach( p2 , s2 ){
printf("loop 2 %c\n",*p2);
}
exit(0);
return(0);
}

View file

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[]){
/* foreach macro viewing an array of int values as a collection of int values */
#define foreach( intpvar , intary ) int* intpvar; for( intpvar=intary; intpvar < (intary+(sizeof(intary)/sizeof(intary[0]))) ; intpvar++)
int a1[]={ 1 , 1 , 2 , 3 , 5 , 8 };
int a2[]={ 3 , 1 , 4 , 1, 5, 9 };
foreach( p1 , a1 ) {
printf("loop 1 %d\n",*p1);
}
foreach( p2 , a2 ){
printf("loop 2 %d\n",*p2);
}
exit(0);
return(0);
}

View file

@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char* argv[]){
#define foreach( idxtype , idxpvar , col , colsiz ) idxtype* idxpvar; for( idxpvar=col ; idxpvar < (col+(colsiz)) ; idxpvar++)
#define arraylen( ary ) ( sizeof(ary)/sizeof(ary[0]) )
char* c1="collection";
int c2[]={ 3 , 1 , 4 , 1, 5, 9 };
double* c3;
int c3len=4;
c3=(double*)calloc(c3len,sizeof(double));
c3[0]=1.2;c3[1]=3.4;c3[2]=5.6;c3[3]=7.8;
foreach( char,p1 , c1, strlen(c1) ) {
printf("loop 1 : %c\n",*p1);
}
foreach( int,p2 , c2, arraylen(c2) ){
printf("loop 2 : %d\n",*p2);
}
foreach( double,p3 , c3, c3len ){
printf("loop 3 : %3.1lf\n",*p3);
}
exit(0);
return(0);
}

View file

@ -0,0 +1,10 @@
start_up = proc ()
po: stream := stream$primary_output()
words: array[string] := array[string]$
["enemy", "lasagna", "robust", "below", "wax"]
for word: string in array[string]$elements(words) do
stream$putl(po, word)
end
end start_up

View file

@ -0,0 +1,5 @@
set(list one.c two.c three.c)
foreach(file ${list})
message(${file})
endforeach(file)

View file

@ -0,0 +1,6 @@
01 things occurs 3.
...
set content of things to ("Apple", "Banana", "Coconut")
perform varying thing as string through things
display thing
end-perform

View file

@ -0,0 +1,2 @@
var food = ["Milk", "Bread", "Butter"];
for f in food do writeln(f);

View file

@ -0,0 +1 @@
(doseq [item collection] (println item))

View file

@ -0,0 +1,3 @@
<Cfloop list="Fee, Fi, Foe, Fum" index="i">
<Cfoutput>#i#!</Cfoutput>
</Cfloop>

View file

@ -0,0 +1,11 @@
10 DIM A$(9) :REM DECLARE STRING ARRAY
20 REM *** FILL ARRAY WITH WORDS ***
30 FOR I = 0 TO 8
40 READ A$(I)
50 NEXT
60 REM *** PRINT ARRAY CONTENTS ***
70 FOR I = 0 TO 8
80 PRINT A$(I)" ";
90 NEXT
100 END
1000 DATA THE, QUICK, BROWN, FOX, JUMPS, OVER, THE, LAZY, DOG.

View file

@ -0,0 +1 @@
(loop for i in list do (print i))

View file

@ -0,0 +1 @@
(map nil #'print list)

View file

@ -0,0 +1 @@
(dolist (x the-list) (print x))

View file

@ -0,0 +1,4 @@
(use-package :iterate)
(iter
(for x in the-list)
(print x))

View file

@ -0,0 +1,4 @@
(let ((the-list '(1 7 "foo" 1 4))) ; Set the-list as the list
(do ((i the-list (rest i))) ; Initialize to the-list and set to rest on every loop
((null i)) ; Break condition
(print (first i)))) ; On every loop print list's first element

View file

@ -0,0 +1,19 @@
DEF AnArray[11]:INT
AnArray=0,1,2,3,4,5,6,7,8,9,10
'A console only program will work without OPENCONSOLE and
'CLOSECONSOLE; however, it does not hurt to use them.
OPENCONSOLE
FOR X=0 TO 10
PRINT AnArray[X]
NEXT X
'keep the console from closing right away.
DO:UNTIL INKEY$<>""
CLOSECONSOLE
'because this is a console only program.
END

View file

@ -0,0 +1,18 @@
import std.stdio: writeln;
void main() {
auto collection1 = "ABC";
foreach (element; collection1)
writeln(element);
auto collection2 = [1, 2, 3];
foreach (element; collection1)
writeln(element);
auto collection3 = [1:10, 2:20, 3:30];
foreach (element; collection3)
writeln(element);
foreach (key, value; collection3)
writeln(key, " ", value);
}

View file

@ -0,0 +1,2 @@
items = { 1, 2, 3 }
for( item in items ) io.writeln( item )

View file

@ -0,0 +1,10 @@
program LoopForEach;
{$APPTYPE CONSOLE}
var
s: string;
begin
for s in 'Hello' do
Writeln(s);
end.

View file

@ -0,0 +1,3 @@
for value : array {
showln value
}

View file

@ -0,0 +1,3 @@
for i in [1,2,3] {
print(i)
}

View file

@ -0,0 +1,3 @@
for i in [1,2,3].Iterate() {
print(i)
}

View file

@ -0,0 +1,9 @@
func myCollection() {
yield 1
yield 2
yield 3
}
for i in myCollection() {
print(i)
}

View file

@ -0,0 +1,3 @@
for e in theCollection {
println(e)
}

View file

@ -0,0 +1,4 @@
FOR INDEX$=("The","quick","brown","fox","jumps","over","the","lazy","dog.") DO
PRINT(INDEX$;" ";)
END FOR
PRINT

View file

@ -0,0 +1,3 @@
for i in [ 5 1 19 25 12 1 14 7 ]
print i
.

View file

@ -0,0 +1,13 @@
(define my-list '( albert simon antoinette))
(for ((h my-list)) (write h))
albert simon antoinette
(define my-vector #(55 66 soixante-dix-sept))
(for (( u my-vector)) (write u))
55 66 soixante-dix-sept
(define my-string "Longtemps")
(for ((une-lettre my-string)) (write une-lettre))
"L" "o" "n" "g" "t" "e" "m" "p" "s"
;; etc ... for other collections like Streams, Hashes, Graphs, ...

View file

@ -0,0 +1,25 @@
module LoopForEach {
@Inject Console console;
void run() {
val vals = [10, 20, 30, 40];
console.print("Array of values:");
Loop: for (val val : vals) {
console.print($" value #{Loop.count + 1}: {val}");
}
Map<String, Int> pairs = ["x"=42, "y"=69];
console.print("\nKeys and values:");
for ((String key, Int val) : pairs) {
console.print($" {key}={val}");
}
console.print("\nJust the keys:");
Loop: for (String key : pairs) {
console.print($" key #{Loop.count + 1}: {key}");
}
console.print("\nValues from a range:");
for (Int n : 1..5) {
console.print($" {n}");
}
}
}

View file

@ -0,0 +1 @@
io.format("~p~n", [Collection])

View file

@ -0,0 +1 @@
lists.foreach(fn (X) { io.format("~p~n", [X]) }, Collection)

View file

@ -0,0 +1 @@
across my_list as ic loop print (ic.item) end

View file

@ -0,0 +1 @@
across my_list as ic all ic.item.count > 3 end

View file

@ -0,0 +1 @@
across my_list as ic some ic.item.count > 3 end

View file

@ -0,0 +1,6 @@
open monad io
each [] = do return ()
each (x::xs) = do
putStrLn $ show x
each xs

View file

@ -0,0 +1,12 @@
import system'routines;
import extensions;
public program()
{
var things := new string[]{"Apple", "Banana", "Coconut"};
things.forEach:(thing)
{
console.printLine:thing
}
}

View file

@ -0,0 +1,9 @@
iex(1)> list = [1,3.14,"abc",[3],{0,5}]
[1, 3.14, "abc", [3], {0, 5}]
iex(2)> Enum.each(list, fn x -> IO.inspect x end)
1
3.14
"abc"
[3]
{0, 5}
:ok

View file

@ -0,0 +1,2 @@
(dolist (x '(1 2 3 4))
(message "x=%d" x))

View file

@ -0,0 +1,3 @@
(mapc (lambda (x)
(message "x=%d" x))
'(1 2 3 4))

View file

@ -0,0 +1 @@
(cl-loop for x in '(1 2 3 4) do (message "x=%d" x))

View file

@ -0,0 +1 @@
io:format("~p~n",[Collection]).

View file

@ -0,0 +1 @@
lists:foreach(fun(X) -> io:format("~p~n",[X]) end, Collection).

View file

@ -0,0 +1,24 @@
include std/console.e
sequence s = {-2,-1,0,1,2} --print elements of a numerical list
for i = 1 to length(s) do
? s[i]
end for
puts(1,'\n')
s = {"Name","Date","Field1","Field2"} -- print elements of a list of 'strings'
for i = 1 to length(s) do
printf(1,"%s\n",{s[i]})
end for
puts(1,'\n')
for i = 1 to length(s) do -- print subelements of elements of a list of 'strings'
for j = 1 to length(s[i]) do
printf(1,"%s\n",s[i][j])
end for
puts(1,'\n')
end for
if getc(0) then end if

View file

@ -0,0 +1,3 @@
for i in [1 .. 10] do printfn "%d" i
List.iter (fun i -> printfn "%d" i) [1 .. 10]

View file

@ -0,0 +1 @@
{ 1 2 4 } [ . ] each

View file

@ -0,0 +1,11 @@
class Main
{
public static Void main ()
{
Int[] collection := [1, 2, 3, 4, 5]
collection.each |Int item|
{
echo (item)
}
}
}

View file

@ -0,0 +1,2 @@
(each [k v (ipairs [:apple :banana :orange])]
(print k v))

View file

@ -0,0 +1,2 @@
(each [k v (pairs {:apple :x :banana 4 :orange 3})]
(print k v))

View file

@ -0,0 +1,3 @@
create a 3 , 2 , 1 ,
: .array ( a len -- )
cells bounds do i @ . cell +loop ; \ 3 2 1

View file

@ -0,0 +1,20 @@
: FOREACH ( array size XT --)
>R \ save execution token on return stack
CELLS BOUNDS \ convert addr,len -> last,first addresses
BEGIN
2DUP > \ test addresses
WHILE ( last>first )
DUP R@ EXECUTE \ apply the execution token to the address
CELL+ \ move first to the next memory cell
REPEAT
R> DROP \ clean return stack
2DROP \ and data stack
;
\ Make an operator to fetch contents of an address and print
: ? ( addr --) @ . ;
CREATE A[] 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ,
\ Usage example:
A[] 10 ' ? FOREACH

View file

@ -0,0 +1,16 @@
program main
implicit none
integer :: i
character(len=5),dimension(5),parameter :: colors = ['Red ','Green','Blue ','Black','White']
!using a do loop:
do i=1,size(colors)
write(*,'(A)') colors(i)
end do
!this will also print each element:
write(*,'(A)') colors
end program main

View file

@ -0,0 +1,18 @@
' FB 1.05.0
' FreeBASIC doesn't have a foreach loop but it's easy to manufacture one using macros
#Macro ForEach(I, A)
For _i as integer = LBound(A) To UBound(A)
#Define I (A(_i))
#EndMacro
#Define In ,
Dim a(-5 To 5) As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
ForEach(i in a)
Print i; " ";
Next
Print
Sleep

View file

@ -0,0 +1,3 @@
for path in $PATH
echo You have $path in PATH.
end

View file

@ -0,0 +1,3 @@
array = [1, 2, 3, 5, 7]
for n = array
println[n]

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