File: ifilter.R

package info (click to toggle)
r-cran-iterators 1.0.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 532 kB
  • sloc: sh: 29; makefile: 1
file content (29 lines) | stat: -rw-r--r-- 601 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
library(iterators)

# Returns a filtering iterator
ifilter <- function(it, FUN, ...) {
  it <- iter(it)

  nextEl <- function() {
    repeat {
      x <- nextElem(it)
      if (FUN(x, ...))
        break
    }
    x
  }

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

# Simple example use
it <- irnorm(1, count=10)
is.positive <- function(x) x > 0
print(as.list(ifilter(it, is.positive)))

# Example using a function with an additional argument
it <- irnorm(1, count=10)
greater.than <- function(x, y) x > y
print(as.list(ifilter(it, greater.than, 1.0)))