News for hackers. Stuff that matters.
Re:Mindset Shifts for Functional Programming by willdearden on Wednesday April 05, 2023 @10:30AM (#35454873)
Here's how I would write the loop version of this. It does involve an optimization since we know parity only depends on n - 1.

  #include <algorithm>
  
  template <typename T>
  bool even(T n)
  {
      bool is_even{true}, is_odd{false};
      for (; n--;)
      {
          std::swap(is_even, is_odd);
      }
      return is_even;
  }

There are no comments on this comment yet.