[Question]
  [
Ruby has a strange operator, `..`, called [flip-flop](https://ruby-doc.org/core-3.1.2/doc/syntax/control_expressions_rdoc.html#label-Flip-Flop) (not to be confused with the range operator, which looks the same). Used in a loop, flip-flop takes two conditions as operands and will return `false` until the first operand is truthy, then return `true` until the second operand is truthy, whereupon it returns `true` one more time and restarts the cycle with `false`.
```
0.upto 10 do |i|
  if (i==2)..(i==8)
    puts i
  end
end
# Prints 2, 3, 4, 5, 6, 7, and 8
```
[Attempt This Online](https://ato.pxeger.com/run?1=m72kqDSpcsGiNNulpSVpuhY3LQ30SgtK8hUMDRRS8hVqMmu4FBQy0xQ0Mm1tjTT19EC0hSZQTEGhoLSkWCETyEzNS-ECYogBC6AUlAYA)
Your task is to implement a higher-order function (or something equivalent in your language) that behaves like flip-flop. It must take two predicate functions as arguments and return a new function that takes an argument and returns falsey until the first predicate returns `true` given the argument, continues to return `true` until the second predicate returns `true`, whereupon the flip-flop returns `true` one more time (think of it like an inclusive range) and thereafter returns `false`, until the first predicate returns `true` again and so on.
Below is an example of such a function called `make_flip_flop`, which takes two predicate lambdas and returns a new lambda, `flip_flop`, to be used in the following loop.
```
flip_flop = make_flip_flop(->i{ i==2 }, ->{ i == 8 })
0.upto 10 do |i|
  if flip_flop.(i)
    puts i
  end
end
# Prints 2, 3, 4, 5, 6, 7, and 8
```
Here's another example with a different set of predicates:
```
flip_flop = make_flip_flop(->i{ i % 5 == 0 }, ->i{ i % 5 == 2 })
0.upto 10 do |i|
  if flip_flop.(i)
    puts i
  end
end
# Prints 0, 1, 2, 5, 6, 7, and 10
```
## Rules
* Please include a brief example or description of how your solution works.
* Since not all languages have first-class functions, the definition of "predicate" is loose. You could take the names of previously-defined functions to be called or a string to be eval'd, for example. Your solution may not, however, just take primitive values like `true` and `false`. It must be your language’s equivalent of a higher-order function.
* Your predicate functions should take at least one argument, of any type that's convenient, as long as it's reasonable. (A predicate that only takes the specific value `1` is not reasonable, for example.)
* Built-ins are allowed, but only if they fit the spec (it must be a function or similar, etc.). You can’t post **Ruby, 2 bytes, `..`**.
* [Default I/O rules](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods/) apply, [standard rules](https://codegolf.meta.stackexchange.com/questions/2419/default-for-code-golf-program-function-or-snippet/2422#2422) apply, and [standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden.
* This is code golf. Shortest answer in bytes wins.
  
[Answer]
# JavaScript (ES6), 26 bytes
Expects `[ first_predicate, second_predicate ]`, where both predicates are functions taking a single parameter.
Returns a flip-flop function which itself returns either 0 or 1.
```
(p,k=0)=>v=>k|(k^=p[k](v))
```
[Try it online!](https://tio.run/##rY7BCsIwEETv@Yq9CBtapRYEoW6P/YlSodRUYmIT2tKL9ttjoijFixcvuzuPnWEu9VQPTS/tuO7MSbhrrUShpS20sUAObawo4ZRPlKs7qiPZUlU4ce60GKH9fMLSiCVIoDwMgjReiD1UPGOsNT0Gv2eQZH4dCLbhiCIONwYgW3xno@QvBtCYbjBabLQ5e5p5NrOZLekz/FepFexClyT@Aun/y7kH "JavaScript (Node.js) – Try It Online")
### Commented
```
(                  // flip-flop generator taking:
  p,               //   p[] = pair of predicate functions
  k = 0            //   k   = flip-flop status, initialized to 0
) =>               //
  v =>             // anonymous function taking a parameter v
    k | (          //   always return 1 if k is already set
                   //   (because we must still return 1 when the 2nd
                   //   predicate is triggered)
      k ^= p[k](v) //   toggle k if the result of p[k](v) is true
    )              //
```
[Answer]
# [Python](https://www.python.org), 59 bytes
```
lambda a,b:lambda x,t={0}:t.add((o:=t.pop()|a(x))>b(x))or o
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72soLIkIz9vweI025ilpSVpuhY3rXMSc5NSEhUSdZKsoMwKnRLbaoNaqxK9xJQUDY18K9sSvYL8Ag3NmkSNCk1NuyQQmV-kkA81YmJBUWZeiYZWbmKBBpChA6LTNDRgpllV2Noaaeqg8C00NXWKEvPSUzUMDTWBgIuQEQqqCqYKtrYKBigGwUSN0IyDOGzBAggNAA)
Anonymous function returning an anonymous function that keeps state in a mutable initialiser.
[Answer]
# [Raku](https://raku.org/), 18 bytes
```
->\f,\g{&{f ff g}}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPtf1y4mTScmvVqtOk0hLU0hvbb2f3FipUJ6UWqBQppGjUq8po6CgZ6eoYFCWn6RgoaWgq2tgpGOApi2AMoBRVQVTEE8A5AolG2k@R8A "Perl 6 – Try It Online")
Raku has a built-in operator `ff` that does exactly what's asked for. My answer is mostly just setting up the functional plumbing around it.
* `-> \f, \g { ... }` is an anonymous subroutine taking two arguments.
* `&{ ... }` is the anonymous subroutine the top-level subroutine returns. Not having an explicit signature, it takes its argument in the topic variable `$_`, which the flip-flop operator implicitly checks against.
* `f ff g` returns false until the topic variable smartmatches against `f`, that is, `$_ ~~ f`, then keeps returning true until `$_ ~~ g`. Since `f` and `g` are functions, those smartmatches are equivalent to `f($_)` and `g($_)`.
[Answer]
# [Factor](https://factorcode.org/), 33 bytes
```
[ '[ _ _ bi on t get swap off ] ]
```
[Try it online!](https://tio.run/##jY9BSwNBDIXv8yvezXrYQ4WCbFGP0ksv4mlZynQ3sx2anRlnUqT@@TXtKqInE0gI4XvJc7aTmKfXl832uYbLZxwpB2KMVg4IdqSSbEcFHDvLBSmTyDllHwQ@Ym022xpDZGdMDaEilWOftMSEBd5OUVBVuDXQWC5R3m3CTYP@lLCDKjIapFluz2h16rOiLbzTQrY7VLqjgTLW8IF9IGMaJjFTcxHaae49YoBgIJkPRHeB26l@VBtH@nlJUdzj4Xrn7tp/7@eP/tgIrNQKY@y/mO/pg3J8@peGMa2ZPgE "Factor – Try It Online")
Expects the false-making predicate first and the true-making predicate second.
* `[ ... ]` A quotation (anonymous function)...
* `'[ ... ]` ...that creates a quotation (anonymous function).
* `_ _` Slot the predicates into the holes.
* `bi` Call both predicates on the inner quotation's input value (integer).
* `on` Here's where it gets tricky. In Factor, you can use any value as a variable (key) to store something. So what I am doing here is using the result of the true-making predicate to store the value `t`. It's a little strange to use a canonical boolean value to store a possibly different boolean value, but this is code golf. It's not supposed to make sense, it's just supposed to be short.
* `t get` Get the value that is stored in `t`. This is the result that will be returned from the quotation. This must be done after storing the result of the true-making predicate but before storing the result of the false-making predicate in order to get the 'inclusive' behavior we are looking for.
* `swap` Bring the result of the false-making predicate to the top of the stack.
* `off` Store `f` in the result of the false-making predicate.
[Answer]
# [R](https://www.r-project.org/), 54 bytes
```
{x=F;function(f,g)function(y){x<<-(z=x|f(y))&!g(y);z}}
```
[Try it online!](https://tio.run/##hY5NC4JAEIbP7a/YFGIHFFIoJN1rv6KLbI5txK7YSpsfv93Wg5FeYg7vPLwwz9Qj8rGz/Jxio4SRWjEMSvjCGzqbZSFrue3REey2pYu0HYbRp/fmaahWBcWHrEKtaK6uP4xIUNdMUqlodIr30JGNROZmPm/Bch4HS06ASYCqlsq4hQyEiNww76I8IMRfuCYHNS8pClrk4vZfxyYf9P2U0QGCVZXM1RHWT4wf "R – Try It Online")
(or [52 bytes](https://tio.run/##hY7LCsIwEEXX5itiC5KBFmxBKdps/Qo3JXZqRJJSU4x9fHtMFxXrRmZx7@EuzjQOucNWCSO1YhhV8IEX9DbPY9ZxO6An2KwrH8duJJafXEhv7cNQrUqKd1nHWtFCXb4YkaBumKRS0eSQbqEnK4nM32ywYDlPoyVnwCRA3UhlfCEjIaIwLDirAAgJF67JQc1TipKWhbj@17HJB8MwZbKD6GfK5mkPv0@4Nw) using a global variable to save the current state)
---
# [R](https://www.r-project.org/), 37 bytes (non-competing)
```
{x=F;function(f,g){x<<-(z=x|f)&!g;z}}
```
[Try it online!](https://tio.run/##bY3LCoJAFIbXzVNMijEHUkooIp1tT9FCmRw7EWfERhIvzz4htkho9V8W31e7LIiiIJOub@Ul0Q0pi4aE3pbQt2kaik62g4bNuky6cXQ@fzQvyw0VXD@xCg3xnG4/W2umTS2QI/H9Od5Bz1aohUApY5hUUzsBVDWSFQhsZEzlVnhX8oAxf4GdcNy@URW8yNX9P3lGD8OU@wPMktnyPY@w8LkP "R – Try It Online")
Doesn't fit the written spec of the challenge (the function `%..%` returns truthy/falsy, rather than a function), but it seems to very well mimic the described behaviour of Ruby's `..`.
Usage example (to mimic the Ruby example in the challenge):
```
for(i in 1:20){
    if((i==2)%..%(i==8))print(i)
}
```
[Answer]
# [C (clang)](http://clang.llvm.org/), 43 bytes
Reusable form as proposed by [AZTECCO](https://codegolf.stackexchange.com/users/84844/aztecco)
```
#define f(x,a,b)static m;m|(m^=m?x b:x a)&&
```
[Try it online!](https://tio.run/##tY1RCsIwEET/e4olYtnQFCz@iDF4EyFNm7LQxNJGLGivbowFvYEDy8CbHcaUpte@i3HTtJZ8CxZnoUXNp6ADGXDSPdFdlDvPUB9n0DzPI/kATpNHDo8Mkux1xA8ktZNAJ1VVEoqCvvH6giSU2qc78B9MGsZUtMi2DTABxOUaLtlqwy1MyFiC/5pZ4svYXndTLO9v "C (clang) – Try It Online")
# [C (clang)](http://clang.llvm.org/), ~~41~~ 40 bytes
-1 byte thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat)!
```
t;f(a(),b(),i,*r){*r=t|(t^=(t?b:a)(i));}
```
[Try it online!](https://tio.run/##dY/BCsIwDIbve4o4UNKtg4EHxVp8E6Wr6yjMKl2Hh7lXt3YU68kfAiH5kvyRleyF6bx3TKFAQpsQmhaWTIXl7oXuzNGdmoMgqAlhs9fGgRqNvAhcUk1gyiDItm60BjRwDluWzVmWyOY/uU/kTWiDX0TdbZzhNQN95DsGZZkWLFq6th3G3rFUUxiN0Xg1/LGJCPkxWgHGIqw41CQ1gh42bFWYr6@Q0@A3Ts3BoX9L1Ytu8NXzAw "C (clang) – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~29~~ 13 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
Ï{T|(T^=YgXgT
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=z3tUfChUXj1ZZ1hnVA&footer=W1h7WD09Mn1Ye1g9PTh9XWdVCjExb0BbWFhnVl0g&input=LVI)
Defines a function which accepts an array of predicates as its input, and returns the flip-flop function. Theoretically should work on strings, numbers, or arrays as the input for the predicate, but I've only tested with numbers.
```
Ï{T|(T^=YgXgT
              # Initialize T = 0
Ï             # Define a function with argument X (an array of predicates):
 {            #  Return a function with argument Y:
  T           #   Current value of T
   |          #   Bitwise OR
    (T^=      #   T XOR-equals the following:
          XgT #    Get the function in X at index T
        Yg    #    Apply it to Y
```
A [16 byte](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=zz0wQXtZfChZXj1BZ1hnWQ&footer=W1h7WD09Mn1Ye1g9PTh9XWdVCjExb0BbWFhnVl0g&input=LVI) alternative doesn't use a global variable, so could be used to define multiple flip flops.
[Answer]
# [Python](https://www.python.org), 76 bytes
*`-1 byte` thanks to `py3_and_c_programmer`*
Takes as input an element `i`, two lambda functions `a` and `b` while using a global variable `t` to toggle between the `true` & `false` cycles.
```
def f(i,a,b):
 global t
 if~t&a(i):t=1
 if t&b(i):t=0;return 1
 return t
t=0
```
[Attempt This Online!](https://ato.pxeger.com/run?1=jZBBCoMwFET3OcVsKgoWVCiIJTfoJRI0NmCjhC_ophfpxo29U2_TpLGLli66mxk-bz5zuw8znXuzLOtIal8-TnWjoGKdilQmFUPb9VJ0IAatrhSJWCcV8dxbUCSDzY62odEauHxTxFy8MVfVW2hoAytM28R57sme4Is6cZG1wFRNnBcfrnydAYPVhlwTY0E58ScROxzAObIfWfFFD8--h3gC)
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~12~~ 10 bytes
```
%2ṁ§≠²o⁰←ḣ
```
[Try it online!](https://tio.run/##yygtzv6f9qip8VFT06Om5oc7FhsZ/Fc1eriz8dDyR50LDm3Kf9S44VHbBKDEf1sjLluL/wA "Husk – Try It Online")
```
%2ṁ§≠²o⁰←ḣ      # 2-argument function, 
                # which yields a partially-applied function
                # requiring 1 further argument:
         ḣ      # 1..further argument   
  ṁ             # map over this and return sum
   §≠           # absolute differences between
     ²          # function arg2 applied to each element, and
      o⁰        # function arg1 applied to 
        ←       # each element plus 1;
%2              # modulo 2.
```
Example usage `f₁₂₃ḣ20`
```
f               # filter
    ḣ20         # 1..20
                # keeping only truthy results of 
 ₁              # function returned by function on line 1
                # called with arguments:    
  ₂₃            # functions on line 2 and line 3
```
[Answer]
# [Rust](https://www.rust-lang.org/), 40 bytes
```
|f|{let mut t=0;move|x|t|{t^=f[t](x);t}}
```
[Try it online!](https://tio.run/##RY7daoQwEIXvfYrZu0xJZdf@idn4GL0RKyITCHVdaibLUuOz2yh0PTAwczh8Z0bveDEDXFo7CIQpgaieOBrf1BhTgBlEFcfn@Fx6Z39JQVbHvQG9BBOmLewZWB/V5XqjcA8cJv7SpuJa3FHxPC9q58aoMaD/C0QVbABhQWvIEFoHW4mE3c53u0aVbKjWORq5oZ@D2O5V4pimpxOmxvZMo1gBkf9kUWvBo6cHBTHtrn1PHRfF@ZO6c1OWAuUDdKPuUGUSXiS8SniT8C7hQ0Jeb4n1hTlZ/gA "Rust – Try It Online")
An expression compatible with `fn([fn(T)->usize; 2])->impl Fn(T)->usize` for any `T`. (Yes I know `impl Trait` can't be used as a return type for `fn` pointers)
[Answer]
# [C++ (clang)](http://clang.llvm.org/), 64 bytes
```
[t=0](auto p){return[=](auto x)mutable{return t|(t^=p[t](x));};}
```
Port of [@Arnauld's JavaScript answer](https://codegolf.stackexchange.com/a/253208/112488).
Instead of `t` being a default parameter, it's captured and initialized to 0. The function returns a mutable lambda which captures this `t` by value along with the predicates container.
This other lambda (the closure) does the heavy lifting, it's declared mutable because it's member `t` will alternate between the values 0 and 1.
We could save a byte by making the predicates take an `int x` but it would no longer be generic.
[Try it online!](https://tio.run/##rZDBboMwDIbveQqr09Rk7SRWadJUEo59CcamFMIUDZIIzETFeHYWEGWreuhlPiSR/9/2F6fOPaaFNB@DbNBCKT/Ve15o5w/rQAwxiiChk@ZYVylsKhOLOdOyskF5LNQsAH5TfBMuxoS2jIV92A8hudMmLZpMAde2xkrJMvqTk1UlTxEhqEpXSFQcT04ZWSo4ROTL6swrNdIDLFSMdAR85Lai2iBoEBCE/uICnvxjs9FsMoyhc6BLJdXsVxmjxmy/T22DwPnYgMMKViHpCfGNSSm1oedpE8XleuhUPn2AH60t6AMbgdgWdlG3zImTibI9bw9aEAJ2Yb@9YXk5W3q/SkKueNevZj3n/xPuHp7H6cENwNm2u4Dshx8 "C++ (clang) – Try It Online")
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform) ~~120~~ ~~117~~ 107 bytes
Here is a C# version based of the Dart and Go examples.
```
Func<Func<int,bool>,Func<int,bool>,Func<int,bool>>f=(l,r)=>{var s=1<0;return v=>l(v)?s=!s:r(v)?!(s=!s):s;};
```
* Shaved off 3 bytes by replacing `false` with `1<0` and a non needed space. Hat tip to [jdt](https://codegolf.stackexchange.com/users/41374/jdt)
* Shaved off 10 bytes by not doing boolean wizardry
Ungolfed main version:
```
var flipflop = new Func<Func<int,bool>, Func<int, bool>, Func<int,bool>> ( (condition1,condition2) => {
    var flipflop = false;
    return new Func<int,bool>(value => {
        if (condition1(value))
        { 
            flipflop = !flipflop;
        } else if (condition2(value)) {
            flipflop = !flipflop;
            return true;
        }
        return flipflop;
    });
});
```
How you use it:
```
var flop = f(v => v == 2, v => v == 8);
for(var x = 0; x< 11; x++) 
{
      if (flop(x)) {
        Console.WriteLine(x);
      }
}
```
[Try it online!](https://tio.run/##fY1BS8QwEIXP9lfM3hK2ytaTbJp4EDwpCB48iIdY0xLIJjKThkrpb6@JF7cgvsPMe8x8Mx1ddgHNOpL1Azx/UTQnUXVOE8EThgH1CebqgqKOtoMU7Ac8ausZRczA6xtoHIjDvN6Pvmt/ivWxfg/BqfrfqHrJXI1cqjlpBJJNexBo4ogeklSOJX5LckdHLG7HiudHEotY4UyF7V34BAk9SyAV5CLhuobfcMNFdc70Id/M3JShg4CphabJbb/nsNmbN6nI9sDKNzZx/se46C54Cs5cvaCN5sF6k3e375dq65ZqWb8B "C# (.NET Core) – Try It Online")
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 91 bytes
Using a delegate as proposed by [jdt](https://codegolf.stackexchange.com/users/41374/jdt)
```
delegate bool d(int i);Func<d,d,d>f=(l,r)=>{var s=1<0;return v=>l(v)?s=!s:r(v)?!(s=!s):s;};
```
[Try it online!](https://tio.run/##bU7BSsQwEL3nK2ZvCVuXrSfZNPUgCIKCoOBBPMQ2LYFsIpm0rpR@e00K3bXgG5iZ5L2ZNxVeVc6rqUNtW3j5waCOnJDKSER49q718kiGqVZGtTIo@HTOQE21DaAZv@9sVdRZjLIR1GSeiXLopQcUebHnXoXOW@hFaWjPblFs8OBTt6GpZwfkI58IRGCQQVfQO13Dk9SWYvDxovcPkL5FNmuGOScki6AwgACrvpc7KeNnRWJ3rzE9hOV7JHOZLRbmn72NcV9xb0N7ECXEJOA6g8vj5o9L4zzQNHSKE3seSwF5Hut2y86iy/oE3QBNFvTE2IpYyxLunEVn1O7N66AetVVxhq9UI1l3IxmnXw "C# (.NET Core) – Try It Online")
[Answer]
# [Go](https://go.dev), 131 bytes, `int`s only
```
func m(l,r func(int)bool)func(int)bool{f:=1<0
return func(t int)bool{if!f&&l(t){f=!f;return f}
if f&&r(t){f=!f;return!f}
return f}}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=dY5NDoIwEIXX9BQDiaaNaDBuCNg7eAX8GdMIlDRlRbryDK7cEBMP4VG8jUNAjCauZjrv9b3vejvq9lFlu1N2PECRqZKpotLGLgIsbHCvLc7j5xnrcgcFz0MD3cpVacVW61x8vRpM5HIdMXOwtSl7p4VRVejjdJpzKxqUPqZvm2MKgRTzo_ikjB43oFx6FALlomEe5rqCRBJbT_JpG34qKVcuhP9q7ATFaAOqy4lSmmsJy26ZzYAqvI6OargS0MDGUAbyYLKHIKSLY55jA1zb9vMF)
# [Go](https://go.dev), 132 bytes, generic
```
func m[T any](l,r func(T)bool)func(T)bool{f:=1<0
return func(t T)bool{if!f&&l(t){f=!f;return f}
if f&&r(t){f=!f;return!f}
return f}}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=dY5NasMwEIXX1inGhgSJuCGhm5BEd8giu9KF8zNhqCUZIS-C0a5X6KobE8ghepTeJuPYLaWQ1QzzvXnvfV5Orv2qiv1bcTqCKcgKMpXzYZqhCdm1Dvi0-H7H2u7BvGyhsOdXWeYeuovcqp1zpfqzN7jU8_VM-GOove1VAQZGmOJ4XMqgGtQprn5EURACE_-PpEx-NXHo8tF34aZSNSLB0lWw1GDkPYuAbOjThk_S-jnm8JguomIb54E6n9mK51rDvFsmE-CIpGvHMZIUNLDx7IEyGx0gy_kSRRLFUK5t-3kD)
This solution is fully generic for any type `T`.
### Ungolfed
```
func makeFlipFlop[T any](l,r func(T)bool) func(T)bool {
	flipped := false
	return func(t T)bool {
		if !flipped && l(t) {flipped = true; return true}
		if flipped && r(t) {flipped = false; return true}
		return flipped
	}
}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=dU87TgMxFKzXp3hZichWFgRUEYnb1BTpEIVJ7OgpXtsy3gKtfBKaFRKH4ChUXAV7P2IFonq_mXkzr28n2304cTiLk4RaoCFYO-vDVanqUL43QV2uP79UYw7pepY7jW6nrXvYgzAvj1RXHvKR7tmTtZrNB2hJoRLeySPccVBCP0tSeBkabwZcgB9kgQoWE3y5BE0Dg3ZacAi-kRsY2XmIA2dG8b8o_cc_nMnAACNFJHGM2Y0x0VDWe7cuG5_npr1vBDShd96Oasj5bazg_-s6sqRoPWCWvN6kuuVwk5vVasqfP1JMGeDeJw1Fy4sjlFXaxJnPrhvqNw)
[Answer]
# Dart, 42 bytes
```
f(f,s,[b=1<0])=>(i)=>b?b|(b=!s(i)):b=f(i);
```
You use it as:
```
void main() {
  var flipflop = f((i)=>i % 3 == 0, (i) => i % 7 == 0);
  for (var i = 1; i < 15; i++) {
    if (flipflop(i)) print(i);
  }
}
```
Which prints 3,4,5,6,7,9,10,11,12,13,14.
It even allows you to pass in an extra Boolean to make it the active state.
In a less horribly mangled and more typed version, it would be:
```
bool Function(T) flipflop<T>(bool Function(T) from, bool Function(T) until) {
  bool b = false;
  return (T value) {
    if (b) {
      b = !until(value);
      return true;
    }
    b = from(value);
    return b;
  }
}
```
[Answer]
# [Nim](https://nim-lang.org/), 100 bytes
```
import sugar
var s,t=off
proc f(x,y:int->bool):proc=(z:int)=>(s=s or z.x;t=s;s=s and not z.y;s or t)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=nVBBDoIwELz3FRtONAGCJiYEsv0LgtUm0iVtMcBXvJAY_-E3_I0U5eTFeJrMTGZnd683rZppundOxtmzVk1LxoHtjqVhl9KAjRySlKw1VIEM-2jIlXax2BOdee5VDEcvcRShRQtkYEz6wqEtPC11DZrcrA3FYjr-6XrImSlQGtIk2aQ5A1DSV6DoEbfRgGKYkYeKew_gUJ0IFFsgCNhP8eyfODRUww4xXYas7GuT9x3r714)
Works with `int`-typed inputs, uses a couple of globals to store the current and previous states.
Unfortunately, Nim doesn't let us omit type annotations, unless we are supplying the lambda directly as an argument to a higher-order function (as in test cases in the footer), so this ends up rather verbose. Importing syntax sugar macros `->` and `=>` allows getting rid of some repetitive annotations and thus, pays off by a few bytes.
As a bonus, Nim also allows defining operators, so that we can mimick Ruby's behavior precisely:
[Attempt This Online!](https://ato.pxeger.com/run?1=m704LzN3wYKlpSVpuhY3PcoSixSKdUps89PSuAqK8pMVEvT0EjQqdCqtkvLzczTBpK1GsW2xQn6RQoV1iW2xNYiTmJeikJdfolBpDZYo0YSatzoNyMtUyMxTMNDTMzSw4lJQyExT0Mi0tTXS1NOD0CBBBYXU5Ix8hUwuMKWkxEVQnwVJ-hRy81MUTG1tDcC6YTxUuyFOhgUFAA)
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 11 bytes
```
L+Z=?Z!PbOb
```
[Try it online!](https://tio.run/##K6gsyfj/38U/JagwxcjaJQBEW1j7aEfZ2kcpBiT5J4UFVvr9/29oCAA)
Pyth doesn't have any way to take functions as arguments, nor any way to return a function. So here we do the next best thing, which is to define a function which will implement the required behavior on the two functions `O` and `P`. That is to say that when we call `y` it will flip-flop on whatever `O` and `P` are defined as. The try it online link above links to a full working example which is the same as the first example in the question, I'll explain below.
```
DOdRqd2;                           define O(d): (d==2)
        DPdRqd8;                   define P(d): (d==8)
                L+Z=?Z!PbOb        implement y as the flip-floperator
                           VQyN    loop N over range(0,input()) and print the output of y(N)
```
And the actual flip-floperator
```
L              define y(b)
 +Z            sum of previous output and
    ?Z         ternary on previous output
      !Pb          not P(b) if Z currently True
         Ob        O(b) if Z currently False
   =           assign result of ternary to Z
```
[Answer]
# [C++ (clang)](http://clang.llvm.org/), ~~123~~ 101 bytes
```
struct F{int r=0;template<class A,class B>int operator()(A a,B b,int t){return r|(r^=r?b(t):a(t));}};
```
[Try it online!](https://tio.run/##fY8xT8MwEIV3/4ojDLXVEIFYUJ0UtUPFwM5QiuQ6l9ZSYkfORS0q@e3BptAJWO6ke3fv3qfb9kbXyu7GsSPfa4LVyVgCX9xKwqatFWEeFroOFum5L@dxwbXoFTnPBV@ASpewTeOYxMkj9d6C/@D@rfCPW05ipkIRchjkeG2srvsSc@PCQ1TNnPWdsTuwqsGuVRqhoxIki26NMpaLE4MVVJJBFd7FsQnhwOR3oU6nJupgKuAVXxebr4XjJcWxKO7lkP4qPATBCKFdT3lu8jyBJDwZGGNwnqEtazi3oD4jTTog/w4qRPQhc5bxJ6xrBy/O1@UVOItABwe094hZJmCJdEC0MHGTVxvMg3NkcZJFFL1XHvQs@c8jiXSm4o6vN98HFwRdFME40v0t6R8@LVkgG9j4CQ "C++ (clang) – Try It Online")
* actually we can go to 92 Bytes if we just implemented a class function  [*h* for example](https://tio.run/##fY8xT8MwEIV3/4ojDHXUEIFYUO0UtUPFwM4ARXKdS2MpsSPnohaV/PZgU2ACljv53vnd@3TXXelG2f009eQHTbA5GUvgi2tB2HaNIpRhoe9hlZ37ehkXar4Cla1hl8UXpSePNHgL/p3718Lf7zilCxVKKsZRTJfG6mYoURoX7qBql2zojd2DVS32ndIIPZUgWHRrlbE8PTHYQCUYVM7zODYhExh5E@p8bqIOpgJe5TV/LrafK8efHMeiuBVj9qtwFwSTptoNJKWRMoEknBkZY3CeoS0bOLegPiLNeiD/BiqE9CF1nvMHbBoHT8435QU4i0AHB1R7xDxPYY10QLQwc7MXG8yDc6RxgkUYXSsPepH855FEPlNxF@m2X19@IHRRBOvI97ekvwm1YIFtZNMH "C++ (clang) – Try It Online") but it's no more exactly a closure.
Assumes predicates take int type, generalize also predicates will cost a few Bytes.  
Due to implicit type conconversion can work with strings.
Struct *F* is a closure with operator () defined to behave flipFlop.
Saved 22 Bytes by templatizing operator () instead of the entire class.
Approach stolen from @Arnauld [answer](https://codegolf.stackexchange.com/a/253208/84844) where *r* keeps the state of flipFlop.
[Answer]
# PHP 67 bytes
```
function f($i,$a,$b){static$k=0;$r=$k;return$r|$k^=($k?$b:$a)($i);}
```
$k must be copied to a temp variable because the `^=` is executed before the `|`,  
although both operators are LTR.
Just define your two functions and give them to `f` as parameters within the loop.  
The functions may be anonymous or named, or even predefined.
e.g.
```
# example 1
$a=function($i){return$i==2;}    $b=function($i){return$i==8;}
for($i=0;$i<=10;$i++) f($i,$a,$b) && print "$i ";
```
or
```
# example 2
function a($i){return$i%5==0;}    function b($i){return$i%5==2;}
for($i=0;$i<=10;$i++) f($i,'a','b') && print "$i ";
# important: function names must be quoted to avoid "undefined constant" error (in PHP 8)
```
or
```
# other example
for($i=0;$i<=10;$i++) f($i,'intval',function($i){return$i<5;) && print "$i ";
# prints 1 2 3 4 5 6 7 8 9 10
# ('$i<5' is irrelevant because testing 'intval' immediately resumes the printing)
```
[Try it online](https://onlinephp.io/c/29cb0)
]