Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- C
- Clojure
- C#
- C#
- D
- Dart
- Fortran
- Go
- Haskell
- Haskell
- JS
- Java
- Java
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Python
- Python
- Ruby
- Rust
- Scala
- Scheme
- Smalltalk
- VB
(doseq [xs (partition 2 list)]
(apply println xs))
foreach (var chunk in list.Chunk(2))
{
Console.WriteLine(string.Join(' ', chunk));
}
.NET 6 preview 7 adds a chunk function to LINQ.
list is IEnumerable<T>; chunk is T[].
list is IEnumerable<T>; chunk is T[].
for(int i = 0; i < list.length; i += 2) {
print("${list[i]}, ${list[i + 1]}");
}
write (*,'(2I8,:," ")') list
This writes out an integer array of any length. The format string, 2I8 means two integers of eight digits each. When the format is exhausted, a new line is started (this is called "format reversion"). As an extra bonus, the space between the two integers is only printed if there is an even number of items in the array, otherwise it is skipped.
pair :: [a] -> [(a, a)]
pair [] = []
pair (x:[]) = error "List had odd length"
pair (x:y:xs) = (x, y) : pair xs
mapM_ print (pair list)
Prints:
(a, b)
(c, d)
...
etc.
(a, b)
(c, d)
...
etc.
everySecond :: [a] -> [a]
everySecond [] = []
everySecond (_:[]) = []
everySecond (_:x:xs) = x : everySecond xs
everySecond' :: [a] -> [a]
everySecond' = everySecond . (undefined :)
mapM_ print (zip (everySecond list) (everySecond' list))
for (let index = 0; index < list.length; index = index + 2) {
console.log(list[index], list[index + 1])
}
for(int i = 0; i < list.length; i += 2) {
System.out.println(list[i] + ", " + list[i + 1]);
}
foreach(array_chunk($list, 2) as $x) {
echo $x[0], ' ', $x[1], PHP_EOL;
}
i := Low(L);
while (i < High(L)) do
begin
writeln(L[i],', ', L[i+1]);
Inc(i,2);
end;
Since List has no prededined meaning in Pascal, assume L is an array.
Using High() and Low() intrinsics guarantees the code also works for arrays that are not zero-based.
Using High() and Low() intrinsics guarantees the code also works for arrays that are not zero-based.
for x in range(0, len(list), 2):
print(list[x], list[x + 1])
for x in zip(list[::2], list[1::2]):
print(x)
original list is called list
for (pair <- list.grouped(2))
println(s"(${pair(0)}, ${pair(1)})")
(let print-pairs ((l list))
(if (null? l)
'()
(begin
(display (car l))
(display " ")
(display (cadr l))
(newline)
(print-pairs (cddr l)))))
list pairsDo: [:a :b |
Transcript showln: 'a: ' , a , ' b: ' , b].
Dim ItemList As New List(Of String)(New String() {"one", "one", "two", "two", "three", "three"})
For x = 0 To ItemList.Count - 1 Step 2
Console.WriteLine(ItemList(x) & vbTab & ItemList(x + 1))
Next