如何使用功能接口(也许)将比较运算符传递给while循环?

huangapple go评论70阅读模式
英文:

How to use functional interface (perhaps) to pass comparison operator to a while loop?

问题

I have two almost identical methods, the only difference is than one goes "left" and the second "right" from the spot in the array.

Is there any way how to de-duplicate these tow methods?

Using Functional Interface & function perhaps?

void goLeft(int number, int index, int increment, int to) {
  int localMax = 0;
  int i = index;
  while (i > to) {
    i += increment; // passed increment is negative
    // common body
  }
}

void goRight(int number, int index, increment, int to) {
  int localMax = 0;
  int i = index;
  while (i < to) {
    i += increment; // passed increment is positive
    // common body
  }
}
英文:

I have two almost identical methods, the only difference is than one goes "left" and the second "right" from the spot in the array.

Is there any way how to de-duplicate these tow methods?

Using Functional Interface & function perhaps?

void goLeft(int number, int index, int increment, int to) {
  int localMax = 0;
  int i = index;
  while (i &gt; to) {
    i += increment; // passed increment is negative
    // common body
  }
}

void goRight(int number, int index, int increment, int to) {
  int localMax = 0;
  int i = index;
  while (i &lt; to) {
    i += increment; // passed increment is positive
    // common body
  }
}

答案1

得分: 3

你可以使用三元运算符,如果唯一的不同是 while 循环的条件:

while (increment < 0 ? i > to : i < to) {
    i += increment; 
    // 共同的内容
}
英文:

You can use a ternary operator if the only difference is the condition of the while loop:

while (increment &lt; 0 ? i &gt; to : i &lt; to) {
    i += increment; 
    // common body
}

答案2

得分: 0

我觉得我之前使用枚举做过类似的东西
在你的情况下可能是这样的

    public class Example {
      void go(Direction direction, int number, int index, int to) {
        while (LEFT == direction ? i &gt; to : i &lt; to) {
          i = direction.increment(i);
          // ...
          i = direction.decrement(i);
          // ...
          i = direction.floor(i, to);
          // ...
          var fromIndex = direction.fromIndex(index);
        }
      }
    }
    
    public enum Direction {
      LEFT(" <- left") {
        @Override
        int increment(int value) {
          return --value;
        }
    
        @Override
        int decrement(int value) {
          return ++value;
        }
    
        @Override
        int floor(int value, int limit) {
          return Math.max(value, limit);
        }
    
        @Override
        int fromIndex(int value) {
          return value;
        }
      },
      
      RIGHT(" right ->") {
        @Override
        int increment(int value) {
          return ++value;
        }
    
        @Override
        int decrement(int value) {
          return --value;
        }
    
        @Override
        int floor(int value, int limit) {
          return Math.min(value, limit);
        }
    
        @Override
        int fromIndex(int value) {
          return value + 1;
        }
      },
      ;
    
      final String sign;
    
      Direction(String sign) {
        this.sign = sign;
      }
    
      abstract int increment(int value);
    
      abstract int decrement(int value);
    
      abstract int floor(int value, int limit);
    
      abstract int fromIndex(int value);
    
      @Override
      public String toString() {
        return sign;
      }
    }
英文:

I think I did something similar using an enum,
in your case it might be like this:

public class Example {
void go(Direction direction, int number, int index, int to) {
while (LEFT == direction ? i &gt; to : i &lt; to) {
i = direction.increment(i);
// ...
i = direction.decrement(i);
// ...
i = direction.floor(i, to);
// ...
var fromIndex = direction.fromIndex(index);
}
}
}
public enum Direction {
LEFT(&quot; &lt;- left&quot;) {
@Override
int increment(int value) {
return --value;
}
@Override
int decrement(int value) {
return ++value;
}
@Override
int floor(int value, int limit) {
return Math.max(value, limit);
}
@Override
int fromIndex(int value) {
return value;
}
},
RIGHT(&quot; right -&gt;&quot;) {
@Override
int increment(int value) {
return ++value;
}
@Override
int decrement(int value) {
return --value;
}
@Override
int floor(int value, int limit) {
return Math.min(value, limit);
}
@Override
int fromIndex(int value) {
return value + 1;
}
},
;
final String sign;
Direction(String sign) {
this.sign = sign;
}
abstract int increment(int value);
abstract int decrement(int value);
abstract int floor(int value, int limit);
abstract int fromIndex(int value);
@Override
public String toString() {
return sign;
}
}

huangapple
  • 本文由 发表于 2023年2月19日 03:03:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定