Tutorial by Examples: each

It is possible to effectively apply a function (cb) which returns a promise to each element of an array, with each element waiting to be processed until the previous element is processed. function promiseForEach(arr, cb) { var i = 0; var nextPromise = function () { if (i >= arr.leng...
foreach is a construct, which enables you to iterate over arrays and objects easily. $array = [1, 2, 3]; foreach ($array as $value) { echo $value; } Outputs: 123. To use foreach loop with an object, it has to implement Iterator interface. When you iterate over associative arrays: $arra...
C++11 std::string supports iterators, and so you can use a ranged based loop to iterate through each character: std::string str = "Hello World!"; for (auto c : str) std::cout << c; You can use a "traditional" for loop to loop through every character: std::stri...
During development, when certain code paths must be prevented from the reach of control flow, you may use assert(0) to indicate that such a condition is erroneous: switch (color) { case COLOR_RED: case COLOR_GREEN: case COLOR_BLUE: break; default: assert(0); ...
The For Each loop construct is ideal for iterating all elements of a collection. Public Sub IterateCollection(ByVal items As Collection) 'For Each iterator must always be variant Dim element As Variant For Each element In items 'assumes element can be converted to a stri...
CSS body { counter-reset: item-counter; /* create the counter */ } .item { counter-increment: item-counter; /* increment the counter every time an element with class "item" is encountered */ } .item-header:before { content: counter(item-counter) ". "; /* print the v...
foreach will iterate over any object of a class that implements IEnumerable (take note that IEnumerable<T> inherits from it). Such objects include some built-in ones, but not limit to: List<T>, T[] (arrays of any type), Dictionary<TKey, TSource>, as well as interfaces like IQueryab...
All collection objects contain a map method that takes a Function as an argument, which must take a single argument. This returns an Iterable backed by the collection. When the Iterable is iterated, each step calls the function with a new element of the collection, and the result of the call becomes...
Often you need to change the way a set of data is structured and manipulate certain values. In the example below we got a collection of books with an attached discount amount. But we much rather have a list of books with a price that's already discounted. $books = [ ['title' => 'The Pragma...
use-fixtures allows to wrap each deftest in namespace with code that runs before and after test. It can be used for fixtures or stubbing. Fixtures are just functions that take test function and run it with other necessary steps (before/after, wrap). (ns myapp.test (require [clojure.test :refer ...
This example uses Parallel.ForEach to calculate the sum of the numbers between 1 and 10000 by using multiple threads. To achieve thread-safety, Interlocked.Add is used to sum the numbers. using System.Threading; int Foo() { int total = 0; var numbers = Enumerable.Range(1, 10000).ToLis...
We can also use macros for making code easier to read and write. For example we can implement macros for implementing the foreach construct in C for some data structures like singly- and doubly-linked lists, queues, etc. Here is a small example. #include <stdio.h> #include <stdlib.h>...
4.0 Instead of the ForEach-Object cmdlet, the here is also the possibility to use a ForEach method directly on object arrays like so (1..10).ForEach({$_ * $_}) or - if desired - the parentheses around the script block can be omitted (1..10).ForEach{$_ * $_} Both will result in the output ...
template<class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function f); Effects: Applies f to the result of dereferencing every iterator in the range [first, last) starting from first and proceeding to last - 1. Parameters: first, last -...
Similar to repeaters used in other languages. This binding will allow you to replicate a block of html for each item in an array. <div data-bind="foreach:contacts"> <div class="contact"> <h2 data-bind="text:name"> <p data-bin...
#include <vector> #include <string> #include "agent_util.hpp" //this file can be found in Java SE Development Kit 8u101 Demos and Samples //see http://download.oracle.com/otn-pub/java/jdk/8u101-b13-demos/jdk-8u101-windows-x64-demos.zip //jdk1.8.0_101.zip!\demo\jvmti\v...
If you have a foreach loop that you want to speed up and you don't mind what order the output is in, you can convert it to a parallel foreach loop by doing the following: using System; using System.Threading; using System.Threading.Tasks; public class MainClass { public static void Main...
To ensure that our collection can be iterated using iterator or for-each loop, we have to take care of following steps: The stuff we want to iterate upon has to be Iterable and expose iterator(). Design a java.util.Iterator by overriding hasNext(), next() and remove(). I have added a simple ...
The following will echo each line in the file C:\scripts\testFile.txt. Blank lines will not be processed. for /F "tokens=*" %%A in (C:\scripts\testFile.txt) do ( echo %%A rem do other stuff here ) More advanced example shows, how derived in FOR loop from a restricted files set...
# example data DT <- data.table(Titanic) Suppose that, for each sex, we want the rows with the highest survival numbers: DT[Survived == "Yes", .SD[ N == max(N) ], by=Sex] # Class Sex Age Survived N # 1: Crew Male Adult Yes 192 # 2: 1st Female Adult Yes...

Page 2 of 4