回答編集履歴
3
追記
answer
CHANGED
|
@@ -6,4 +6,13 @@
|
|
|
6
6
|
return i1.flatMap(x -> i2.map(y -> y - x)).orElse(0);
|
|
7
7
|
}
|
|
8
8
|
```
|
|
9
|
+
異常を伝えるためにもOptionalで返すべきですね。
|
|
10
|
+
```java
|
|
11
|
+
public Optional<Integer> getNum3(Integer num1,Integer num2){
|
|
12
|
+
Optional<Integer> i1 = Optional.ofNullable(num1),
|
|
13
|
+
i2 = Optional.ofNullable(num2);
|
|
14
|
+
return i1.flatMap(x -> i2.map(y -> y - x));
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
9
18
|
参考→[JavaのOptionalのモナド的な使い方 - 複数のOptional](http://qiita.com/koher/items/6f4a8d8b3ad3142bf645#%E8%A4%87%E6%95%B0%E3%81%AEoptional)
|
2
引き算の順番が逆だった
answer
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
public int getNum3(Integer num1,Integer num2){
|
|
4
4
|
Optional<Integer> i1 = Optional.ofNullable(num1),
|
|
5
5
|
i2 = Optional.ofNullable(num2);
|
|
6
|
-
return i1.flatMap(x -> i2.map(y ->
|
|
6
|
+
return i1.flatMap(x -> i2.map(y -> y - x)).orElse(0);
|
|
7
7
|
}
|
|
8
8
|
```
|
|
9
9
|
参考→[JavaのOptionalのモナド的な使い方 - 複数のOptional](http://qiita.com/koher/items/6f4a8d8b3ad3142bf645#%E8%A4%87%E6%95%B0%E3%81%AEoptional)
|
1
返り値がOptionalのままだった
answer
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
public int getNum3(Integer num1,Integer num2){
|
|
4
4
|
Optional<Integer> i1 = Optional.ofNullable(num1),
|
|
5
5
|
i2 = Optional.ofNullable(num2);
|
|
6
|
-
return i1.flatMap(x -> i2.map(y -> x - y));
|
|
6
|
+
return i1.flatMap(x -> i2.map(y -> x - y)).orElse(0);
|
|
7
7
|
}
|
|
8
8
|
```
|
|
9
9
|
参考→[JavaのOptionalのモナド的な使い方 - 複数のOptional](http://qiita.com/koher/items/6f4a8d8b3ad3142bf645#%E8%A4%87%E6%95%B0%E3%81%AEoptional)
|