質問するログイン新規登録

回答編集履歴

3

追記

2017/05/18 06:09

投稿

swordone
swordone

スコア20675

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

引き算の順番が逆だった

2017/05/18 06:09

投稿

swordone
swordone

スコア20675

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)).orElse(0);
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のままだった

2017/05/17 14:47

投稿

swordone
swordone

スコア20675

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)