-
Notifications
You must be signed in to change notification settings - Fork 46
函数式编程练习:使用函数式重构代码 #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
函数式编程练习:使用函数式重构代码 #138
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,50 +20,30 @@ public static void main(String[] args) { | |
| // 请尝试使用BiConsumer函数式接口重构下列三个方法,消除重复代码,提高可读性 | ||
| // 提示:你可以使用Map.forEach方法 | ||
| // 加分项:如果你能编写一个返回BiConsumer的高阶函数(即"返回函数的函数"),那就更好了 | ||
|
|
||
|
|
||
| public static void printWithConsumer( | ||
| Map<String, String> map1, | ||
| Map<String, String> map2, | ||
| BiConsumer<String, String> consumer) {} | ||
| BiConsumer<String, String> consumer) { | ||
| map1.forEach(consumer); | ||
| map2.forEach(consumer); | ||
| } | ||
|
|
||
| public static void printWithComma(Map<String, String> map1, Map<String, String> map2) { | ||
| for (Map.Entry<String, String> entry : map1.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
| System.out.println(key + "," + value); | ||
| } | ||
|
|
||
| for (Map.Entry<String, String> entry : map2.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
| System.out.println(key + "," + value); | ||
| } | ||
| printWithConsumer(map1,map2,(k,v)->System.out.println(k + "," + v)); | ||
| } | ||
|
|
||
|
|
||
| public static void printWithDash(Map<String, String> map1, Map<String, String> map2) { | ||
| for (Map.Entry<String, String> entry : map1.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
| System.out.println(key + "-" + value); | ||
| } | ||
| printWithConsumer(map1,map2,(k,v)->System.out.println(k + "-" + v)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
|
|
||
| for (Map.Entry<String, String> entry : map2.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
| System.out.println(key + "-" + value); | ||
| } | ||
| } | ||
|
|
||
| public static void printWithColon(Map<String, String> map1, Map<String, String> map2) { | ||
| for (Map.Entry<String, String> entry : map1.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
| System.out.println(key + ":" + value); | ||
| } | ||
| printWithConsumer(map1,map2,(k,v)->System.out.println(k + ":" + v)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| for (Map.Entry<String, String> entry : map2.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
| System.out.println(key + ":" + value); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,42 +16,32 @@ public static void main(String[] args) { | |
|
|
||
| // 请尝试将下列四个方法使用IntBinaryOperator进行重构,减少重复代码 | ||
| public static int[] calculate(int[] a, int[] b, IntBinaryOperator operator) { | ||
| return null; | ||
| int[] result = new int[a.length]; | ||
| for (int i = 0; i < a.length; ++i) { | ||
| result[i] = operator.applyAsInt(a[i], b[i]); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // 将两个数组中的每个数字分别相加,然后返回相加后的数组。你可以假定传入的数组都是等长的 | ||
| // 下列minus/multiply/divide方法同理 | ||
| // 例如,a=[1, 2, 3], b=[4, 5, 6] | ||
| // 返回 [5 (1+4), 7 (2+5), 9 (3+6)] | ||
| public static int[] add(int[] a, int[] b) { | ||
| int[] result = new int[a.length]; | ||
| for (int i = 0; i < a.length; ++i) { | ||
| result[i] = a[i] + b[i]; | ||
| } | ||
| return result; | ||
| return calculate(a, b, Integer::sum); | ||
| } | ||
|
|
||
| public static int[] minus(int[] a, int[] b) { | ||
| int[] result = new int[a.length]; | ||
| for (int i = 0; i < a.length; ++i) { | ||
| result[i] = a[i] - b[i]; | ||
| } | ||
| return result; | ||
| return calculate(a, b, (o1, o2) -> (o1 - o2)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| public static int[] multiply(int[] a, int[] b) { | ||
| int[] result = new int[a.length]; | ||
| for (int i = 0; i < a.length; ++i) { | ||
| result[i] = a[i] * b[i]; | ||
| } | ||
| return result; | ||
| return calculate(a, b, (o1, o2) -> (o1 * o2)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| } | ||
|
|
||
| public static int[] divide(int[] a, int[] b) { | ||
| int[] result = new int[a.length]; | ||
| for (int i = 0; i < a.length; ++i) { | ||
| result[i] = a[i] / b[i]; | ||
| } | ||
| return result; | ||
| return calculate(a, b, (o1, o2) -> (o1 / o2)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
',' 后应有空格。',' 后应有空格。',' 后应有空格。'->' 后应有空格。'->' 前应有空格。