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

View file

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

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,6 @@
BEGIN {
split("Mary had a little lamb", strs, " ")
for(el in strs) {
print strs[el]
}
}

View file

@ -0,0 +1,3 @@
for(i=1; i <= length(strs); i++) {
print strs[i]
}

View file

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

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,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,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,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,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::output_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,6 @@
#include <iterator_concepts>
for (auto element: container)
{
std::cout << element << "\n";
}

View file

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

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,5 @@
set(list one.c two.c three.c)
foreach(file ${list})
message(${file})
endforeach(file)

View file

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

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,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 e in theCollection {
println(e)
}

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,3 @@
open console imperative
each writen [1..10]

View file

@ -0,0 +1,2 @@
each f (x::xs) = f x $ each f xs
each _ [] = ()

View file

@ -0,0 +1,3 @@
open console list
_ = map writen [1..10]

View file

@ -0,0 +1,4 @@
each (x::xs) = writen x $ each xs
each [] = ()
each [1..10]

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 @@
{ 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,3 @@
create a 3 , 2 , 1 ,
: .array ( a len -- )
cells bounds do i @ . cell +loop ; \ 3 2 1

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,5 @@
func printAll(values []int) {
for i, x := range values {
fmt.Printf("Item %d = %d\n", i, x)
}
}

View file

@ -0,0 +1,5 @@
def beatles = ["John", "Paul", "George", "Ringo"]
for(name in beatles) {
println name
}

View file

@ -0,0 +1,3 @@
beatles.each {
println it
}

View file

@ -0,0 +1,2 @@
import Control.Monad (forM_)
forM_ collect print

View file

@ -0,0 +1 @@
mapM_ print collect

View file

@ -0,0 +1 @@
for(i in 1...10) Sys.println(i);

View file

@ -0,0 +1,7 @@
CHARACTER days="Monday Tuesday Wednesday Thursday Friday Saturday Sunday "
items = INDEX(days, ' ', 256) ! 256 = count option
DO j = 1, items
EDIT(Text=days, ITeM=j, Parse=today)
WRITE() today
ENDDO

View file

@ -0,0 +1,5 @@
procedure main()
X := [1,2,3,-5,6,9]
every x := !L do
write(x)
end

View file

@ -0,0 +1 @@
every write(!L)

View file

@ -0,0 +1 @@
collection foreach(println)

View file

@ -0,0 +1 @@
smoutput each i.10

View file

@ -0,0 +1,5 @@
Iterable<Type> collect;
...
for(Type i:collect){
System.out.println(i);
}

View file

@ -0,0 +1,3 @@
for (var a in o) {
print(o[a]);
}

View file

@ -0,0 +1,5 @@
for (var a in o) {
if (o.hasOwnProperty(a)) {
print(o[a]);
}
}

View file

@ -0,0 +1,14 @@
h = {"one":1, "two":2, "three":3}
for (x in h) print(x);
/*
two
one
three
*/
for each (y in h) print(y);
/*
2
1
3
*/

View file

@ -0,0 +1,14 @@
h = {"one":1, "two":2, "three":3}
for (x in h) print(x);
/*
two
one
three
*/
for (y of h) print(y);
/*
2
1
3
*/

View file

@ -0,0 +1 @@
{`0:$x} ' !10

View file

@ -0,0 +1 @@
_sin ' (1; 2; 3;)

View file

@ -0,0 +1,2 @@
: >>say.(*) . ;
5 iota >>say.

View file

@ -0,0 +1,12 @@
in$ ="Not,Hardly,Just,Adequately,Quite,Really,Very,Fantastically,xyzzy"
element$ =""
i =1 ' used to point to successive elements
do
element$ =word$( in$, i, ",")
if element$ ="xyzzy" then exit do
print element$; " good!"
i =i +1
loop until 1 =2
end

View file

@ -0,0 +1,4 @@
"Lisaac loop foreach".split.foreach { word : STRING;
word.print;
'\n'.print;
};

View file

@ -0,0 +1 @@
foreach [red green blue] [print ?]

View file

@ -0,0 +1,4 @@
t={monday=1, tuesday=2, wednesday=3, thursday=4, friday=5, saturday=6, sunday=0, [7]="fooday"}
for key, value in pairs(t) do
print(value, key)
end

View file

@ -0,0 +1,4 @@
l={'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', not_a_number='fooday', [0]='today', [-1]='yesterday' }
for key, value in ipairs(l) do
print(key, value)
end

View file

@ -0,0 +1,4 @@
list1 = [1,5,6,7,-7,-9];
for k = list1, % list1 must be a row vector (i.e. array of size 1xn)
printf('%i\n',k)
end;

View file

@ -0,0 +1,4 @@
list2 = {'AA','BB','CC'};
for k = list2, % list2 must be a row vector (i.e. array of size 1xn)
printf('%s\n',k{1})
end;

View file

@ -0,0 +1,2 @@
printf('%d\n',list1);
printf('%s\n',list2{:});

View file

@ -0,0 +1,4 @@
for i in collect do
(
print i
)

View file

@ -0,0 +1,5 @@
things = {"Apple", "Banana", "Coconut"};
for thing in (things)
player:tell(thing);
endfor

View file

@ -0,0 +1,5 @@
s = (StringSplit@Import["ExampleData/USConstitution.txt"])[[1;;7]];
Do[
Print@i,
{i, s}
]

View file

@ -0,0 +1 @@
for n in [2, 3, 5, 7] do print(n);

View file

@ -0,0 +1,2 @@
for x = "mary", "had", "a", "little", "lamb": message x; endfor
end

View file

@ -0,0 +1,2 @@
for x = for i = 1 upto 9: a[i], endfor, a[10]: show x; endfor
end

View file

@ -0,0 +1,7 @@
foreach ($collect as $i) {
echo "$i\n";
}
foreach ($collect as $key => $i) {
echo "\$collect[$key] = $i\n";
}

View file

@ -0,0 +1,3 @@
foreach my $i (@collection) {
print "$i\n";
}

View file

@ -0,0 +1 @@
print "$_\n" foreach @collection

View file

@ -0,0 +1,3 @@
foreach $l ( "apples", "bananas", "cherries" ) {
print "I like $l\n";
}

View file

@ -0,0 +1 @@
(mapc println '(Apple Banana Coconut))

View file

@ -0,0 +1,2 @@
for i in collection:
print i

View file

@ -0,0 +1,10 @@
lines = words = characters = 0
f = open('somefile','r')
for eachline in f:
lines += 1
for eachword in eachline.split():
words += 1
for eachchar in eachword:
chracters += 1
print lines, words, characters

View file

@ -0,0 +1,2 @@
a <- list("First", "Second", "Third", 5, 6)
for(i in a) print(i)

View file

@ -0,0 +1,6 @@
days = 'zuntik montik dinstik mitvokh donershtik fraytik shabes'
do j=1 for words(days) /*loop through days of the week. */
say word(days,j) /*display the weekday to screen. */
end /*j*/
lang postscript> /*stick a fork in it, we're done.*/

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