C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# R Foreach Iterator - Walkforward

By : user2955073
Date : November 22 2020, 10:38 AM
wish helps you You could use an iterator that returns overlapping sub-matrices as you describe, but that would use much more memory than is required. It would be better to use an iterator that returns the indices of those sub-matrices. Here's one way to do that:
code :
``````iwalk <- function(n, m) {
if (m > n)
stop('m > n')
it <- icount(n - m + 1)

nextEl <- function() {
i <- nextElem(it)
c(i, i + m - 1)
}

obj <- list(nextElem=nextEl)
class(obj) <- c('abstractiter', 'iter')
obj
}
``````
``````library(doMC)
nworkers <- 3
registerDoMC(nworkers)
x <- matrix(1:100, 10)
m <- 5
r1 <- foreach(ix=iwalk(nrow(x), m)) %dopar% {
x[ix[1]:ix[2],, drop=FALSE]
}
``````
``````ioverlap <- function(x, m, chunks) {
if (m > nrow(x))
stop('m > nrow(x)')
i <- 1
it <- idiv(nrow(x) - m + 1, chunks=chunks)

nextEl <- function() {
ifirst <- i
ilast <- i + ntasks + m - 2
x[ifirst:ilast,, drop=FALSE]
}

obj <- list(nextElem=nextEl)
class(obj) <- c('abstractiter', 'iter')
obj
}

library(doParallel)
nworkers <- 3
cl <- makePSOCKcluster(nworkers)
registerDoParallel(cl)
x <- matrix(1:100, 10)
m <- 5
r2 <- foreach(y=ioverlap(x, m, nworkers), .combine='c',
.packages=c('foreach', 'iterators')) %dopar% {
foreach(iy=iwalk(nrow(y), m)) %do% {
y[iy[1]:iy[2],, drop=FALSE]
}
}
``````

Share :

## Does foreach work on Iterator?

By : AtwoodDuan
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further The Gee iterators/Traversables implement foreach as a method:
code :
``````list.filter (s => s > "a").foreach (s => { stdout.printf (@"s\n"); return true; });
``````

## Get iterator value using foreach

By : Bafty
Date : March 29 2020, 07:55 AM
it should still fix some issue You are using PDO the wrong way. Use parameterized queries instead of substituting dynamic values into the query yourself. If you blindly insert user input into your database queries, then your query will be vulnerable to SQL injection, meaning a user with malicious intent could damage with your data.
Here's the correct way to do it:
code :
``````// prepare the statement -- the place holders allow PDO to handle substituting
// the values, which also prevents SQL injection
\$stmt = \$pdo->prepare('SELECT * FROM prekes WHERE pirkejo_id=:pirkejas');

// bind the parameters
\$stmt->bindValue(':pirkejas', \$pirkejas);

// if statement executed successfully, print the results
if (\$stmt->execute()) {
while (\$row = \$stmt->fetch(PDO::FETCH_ASSOC)) {
if (!empty(\$row['prek_pav'])) {
echo '<tr>';
echo '<td>' . \$row['prek_pav'] . '</td>';
echo '<td >' . \$row['prek_kaina'] . '</td>';
echo '</tr>';
}
}
}
``````

## PHP An iterator cannot be used with foreach by reference

By : user3523437
Date : March 29 2020, 07:55 AM
will help you I have an object that implements Iterator and holds 2 arrays: "entries" and "pages". Whenever I loop through this object, I want to modify the entries array but I get the error An iterator cannot be used with foreach by reference which I see started in PHP 5.2. , I just tried creating an iterator which used:
code :
``````public function &current() {
\$element = &\$this->array[\$this->position];
return \$element;
}
``````
``````foreach (\$flavors as \$key => \$flavor) {
\$flavors[\$key] = \$flavor->stdClassForApi();
}
``````
``````class PaginatedResultSet {
public \$entries = array();

public function &iterate()
{
foreach (\$this->entries as &\$v) {
yield \$v;
}
}
}

\$flavors = new PaginatedResultSet(/* args */);

foreach (\$flavors->iterate() as &\$flavor) {
\$flavor = \$flavor->stdClassForApi();
}
``````

## HEX to RGB using an iterator - better way than using .forEach?

By : MANOJ
Date : March 29 2020, 07:55 AM
I hope this helps . I've created a rather ugly function for converting hex to rgb and I really don't like the way I've used .forEach and the need for defining an empty array before the iteration. , Maybe you can do as follows;
code :
``````function hex2rgb(h){
return "rgb(" + [(h & 0xff0000) >> 16, (h & 0x00ff00) >> 8, h & 0x0000ff].reduce((p,c) => p+","+c) + ")";
}

console.log(hex2rgb(0xffffff));
console.log(hex2rgb(0x12abf0));
console.log(hex2rgb(0x000000));``````

## Iterator within a foreach loop

By : user7521885
Date : March 29 2020, 07:55 AM
will help you I have a site where we've loaded a hidden input and array with a value that was coming from a php function which we've now removed. Tha variable in question is rownum and the function was: , Delete unneeded &s from your local variables in foreach loops:
code :
``````foreach(\$this->pgroups as &\$pgroup){
foreach(\$pgroup->pskus as &\$psku){
\$psku->rownum = \$r;
\$r++;
}
}
``````
``````foreach(\$this->pgroups as \$pgroup){ // & deleted
foreach(\$pgroup->pskus as \$psku){ // & deleted
\$psku->rownum = \$r;
\$r++;
}
}
``````