質問編集履歴

5

2018/05/12 14:10

投稿

murabito
murabito

スコア108

test CHANGED
@@ -1 +1 @@
1
- 関数Aの実行結果がnullかundefinedのみに関数内に変数を新たに作らず、関数Bを実行させるには?
1
+ 関数Aの実行結果がnullかundefinedの時のみに関数内に変数を新たに作らず、関数Bを実行させるには?
test CHANGED
File without changes

4

`foo(v)`をこのように2回呼ぶことも出来るでしょうが、これは非効率なので避けたいところです。

2018/05/12 14:09

投稿

murabito
murabito

スコア108

test CHANGED
File without changes
test CHANGED
@@ -52,6 +52,20 @@
52
52
 
53
53
 
54
54
 
55
+ ```
56
+
57
+ const hoge = (foo, bar) => (v) => {
58
+
59
+ return foo(v) != null ? foo(v) : bar(v);
60
+
61
+ }
62
+
63
+ ```
64
+
65
+ `foo(v)`をこのように2回呼ぶことも出来るでしょうが、これは非効率なので避けたいところです。
66
+
67
+
68
+
55
69
  ```ここに言語を入力
56
70
 
57
71
  const hoge = (foo, bar) => (v) => foo(v) || bar(v);

3

const zero = (v) => 0 * v; const double = (v) => 2 * v; console.log( hoge(zero, double)(10) )

2018/05/11 11:59

投稿

murabito
murabito

スコア108

test CHANGED
File without changes
test CHANGED
@@ -16,23 +16,15 @@
16
16
 
17
17
  ```
18
18
 
19
+ const zero = (v) => 0 * v;
20
+
21
+ const double = (v) => 2 * v;
22
+
23
+
24
+
19
25
  console.log(
20
26
 
21
- hoge(
22
-
23
- function(v) {
24
-
25
- return 0;
26
-
27
- },
28
-
29
- function(v) {
30
-
31
- return v * 2;
27
+ hoge(zero, double)(10)
32
-
33
- }
34
-
35
- )(10)
36
28
 
37
29
  )
38
30
 

2

const hoge = (foo, bar) => (v) => { const result = foo(v) return result != null ? result : bar(v);

2018/05/11 11:49

投稿

murabito
murabito

スコア108

test CHANGED
File without changes
test CHANGED
@@ -44,23 +44,11 @@
44
44
 
45
45
  ```
46
46
 
47
- function hoge(foo, bar) {
47
+ const hoge = (foo, bar) => (v) => {
48
48
 
49
- return function(v) {
49
+ const result = foo(v)
50
50
 
51
- const result = foo(v)
51
+ return result != null ? result : bar(v);
52
-
53
- if (result != null) {
54
-
55
- return result
56
-
57
- } else {
58
-
59
- return bar(v);
60
-
61
- }
62
-
63
- }
64
52
 
65
53
  }
66
54
 

1

const hoge = (foo, bar) => (v) => foo(v) || bar(v);

2018/05/11 11:45

投稿

murabito
murabito

スコア108

test CHANGED
File without changes
test CHANGED
@@ -1,14 +1,6 @@
1
1
  ```ここに言語を入力
2
2
 
3
- function hoge(foo, bar) {
4
-
5
- return function(v) {
6
-
7
- return foo(v) || bar(v);
3
+ const hoge = (foo, bar) => (v) => foo(v) || bar(v);
8
-
9
- }
10
-
11
- }
12
4
 
13
5
  ```
14
6
 
@@ -82,15 +74,7 @@
82
74
 
83
75
  ```ここに言語を入力
84
76
 
85
- function hoge(foo, bar) {
86
-
87
- return function(v) {
88
-
89
- return foo(v) || bar(v);
77
+ const hoge = (foo, bar) => (v) => foo(v) || bar(v);
90
-
91
- }
92
-
93
- }
94
78
 
95
79
  ```
96
80