英文:
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 > 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 < 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 < 0 ? i > to : i < to) {
i += increment;
// common body
}
答案2
得分: 0
我觉得我之前使用枚举做过类似的东西,
在你的情况下可能是这样的:
public class Example {
void go(Direction direction, int number, int index, int to) {
while (LEFT == direction ? i > to : i < 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 > to : i < 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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论