rfunctioniterationmultiple-arguments

Function with two arguments that returns when arguments are equal


I'm trying to create a function that takes two arguments. It will repeatedly call the first argument, in my case

f()

until f returns that same value 3x in a row. It will then call the second argument

g()

and if g returns the same value as f returned earlier, then the function must return this value. Otherwise it will go back and call the first argument f() again and repeat the cycle.

This is what I have so far.

call_until = function(f, g) {
    while(1) {
        n = f %>% chunk(3) %>% filter(f, function(v) length(unique(v)) == 1)
        x = n()[1]
        if (x == g()) {
            return(x)
        }
    }
}

For example, if f returns 4 three times in a row, then go to g. If g equals what f returned three times in a row (which in this example is 4); so if g == 4, then call_until should return 4.


Solution

  • Here's another example of how you might do this

    call_until = function(f, g) {
      while(TRUE) {
        value <- f()
        seen <- 1
        while(seen < 3) {
          next_value <- f()
          if (next_value == value) {
            seen <- seen + 1
          } else {
            value <- next_value
            seen <- 1
          }
        }
        if (value == g()) {
            return(value)
        }
      }
    }
    

    Though it kind of matters if you are supposed to draw three new f values if g doesn't match or just one new value.

    Here are some useful testing functions. They just return values from a vector in order, repeating as necessary.

    cycler <- function(vals) {
      i <- 1
      function() {
        i<<-i+1
        vals[(i-2) %% length(vals)+1]
      }
    }
    
    f <- cycler(c(1,2,2,2,3,4,5,5,5,1,1))
    g <- cycler(c(5,4))
    

    With this we get

    call_until(f, g)
    # [1] 5