Tutorial by Examples: each

Java SE 5 With Java 5 and up, one can use for-each loops, also known as enhanced for-loops: List strings = new ArrayList(); strings.add("This"); strings.add("is"); strings.add("a for-each loop"); for (String string : strings) { System.out.println(string); } For each ...
For example, you can take the absolute value of each element: list(map(abs, (1, -1, 2, -2, 3, -3))) # the call to `list` is unnecessary in 2.x # Out: [1, 1, 2, 2, 3, 3] Anonymous function also support for mapping a list: map(lambda x:x*2, [1, 2, 3, 4, 5]) # Out: [2, 4, 6, 8, 10] or convert...
foreach is used to iterate over the elements of an array or the items within a collection which implements IEnumerable✝. var lines = new string[] { "Hello world!", "How are you doing today?", "Goodbye" }; foreach (string line in lines) { Con...
Let's say you want to generate counts or subtotals for a given value in a column. Given this table, "Westerosians": NameGreatHouseAllegienceAryaStarkCerceiLannisterMyrcellaLannisterYaraGreyjoyCatelynStarkSansaStark Without GROUP BY, COUNT will simply return a total number of rows: SELE...
Apple's Reachability class periodically checks the network status and alerts observers to changes. Reachability *internetReachability = [Reachability reachabilityForInternetConnection]; [internetReachability startNotifier];
// Java: Stream.of(1.0, 2.0, 3.0) .mapToInt(Double::intValue) .mapToObj(i -> "a" + i) .forEach(System.out::println); // a1 // a2 // a3 // Kotlin: sequenceOf(1.0, 2.0, 3.0).map(Double::toInt).map { "a$it" }.forEach(::println)
traverse_ executes an Applicative action for every element in a Foldable structure. It ignores the action's result, keeping only the side-effects. (For a version which doesn't discard results, use Traversable.) -- using the Writer applicative functor (and the Sum monoid) ghci> runWriter $ trave...
An example that uses Parallel.ForEach loop to ping a given array of website urls. static void Main() { string [] urls = { "www.stackoverflow.com", "www.google.net", "www.facebook.com", "www.twitter.com" ...
ForEach has two different meanings in PowerShell. One is a keyword and the other is an alias for the ForEach-Object cmdlet. The former is described here. This example demonstrates printing all items in an array to the console host: $Names = @('Amy', 'Bob', 'Celine', 'David') ForEach ($Name in $...
To apply a function to every item in an array, use array_map(). This will return a new array. $array = array(1,2,3,4,5); //each array item is iterated over and gets stored in the function parameter. $newArray = array_map(function($item) { return $item + 1; }, $array); $newArray now is a...
The ForEach-Object cmdlet works similarly to the foreach statement, but takes its input from the pipeline. Basic usage $object | ForEach-Object { code_block } Example: $names = @("Any","Bob","Celine","David") $names | ForEach-Object { "H...
Ruby has many types of enumerators but the first and most simple type of enumerator to start with is each. We will print out even or odd for each number between 1 and 10 to show how each works. Basically there are two ways to pass so called blocks. A block is a piece of code being passed which will...
<?php foreach ($collection as $item): do_something($item); endforeach; ?> <?php foreach ($collection as $item): ?> <p>Do something in HTML with <?php echo $item; ?></p> <?php endforeach; ?>
The foreach package brings the power of parallel processing to R. But before you want to use multi core CPUs you have to assign a multi core cluster. The doSNOW package is one possibility. A simple use of the foreach loop is to calculate the sum of the square root and the square of all numbers from...
A type that conforms to the SequenceType protocol can iterate through it's elements within a closure: collection.forEach { print($0) } The same could also be done with a named parameter: collection.forEach { item in print(item) } *Note: Control flow statements (such as break or continu...
You can use a For Each...Next loop to iterate through any IEnumerable type. This includes arrays, lists, and anything else that may be of type IEnumerable or returns an IEnumerable. An example of looping through a DataTable's Rows property would look like this: For Each row As DataRow In DataTable...
Suppose we need to do the sum of each column in a dataset set.seed(20) df1 <- data.frame(ID = rep(c("A", "B", "C"), each = 3), V1 = rnorm(9), V2 = rnorm(9)) m1 <- as.matrix(df1[-1]) There are many ways to do this. Using base R, the best option would be col...
The foreach statement is used to loop through arrays. For each iteration the value of the current array element is assigned to $value variable and the array pointer is moved by one and in the next iteration next element will be processed. The following example displays the items in the array a...
Using the Vector.<T> type and the for each loop is more performant than a conventional array and for loop: Good: var list:Vector.<Sprite> = new <Sprite>[]; for each(var sprite:Sprite in list) { sprite.x += 1; } Bad: var list:Array = []; for (var i:int = 0; i < ...

Page 1 of 4