Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Php

Idiom #141 Iterate in sequence over two lists

Iterate in sequence over the elements of the list items1 then items2. For each iteration print the element.

# beginner style
foreach( $items1 as $item ) print "$item\n";
foreach( $items2 as $item ) print "$item\n";

# five times faster
print implode("\n", $items1)."\n".implode("\n", $items2)."\n" ;

In these simple examples, $items1 and $items2 must be arrays, and their elements must be scalar data types (numbers, strings, booleans).
;; for side effects
(doseq [x (concat items1 items2)]
   (println x))

concat concatenates two or more sequences https://clojuredocs.org/clojure.core/concat
doseq for side effects. Alternatively for to produce a new sequence https://clojuredocs.org/clojure.core/doseq
https://clojuredocs.org/clojure.core/for

New implementation...
< >
BBaz