Last active
November 24, 2020 00:54
-
-
Save golbin/c6a972235ed67fd13876 to your computer and use it in GitHub Desktop.
JavaScript Function Chain & Lazy Evaluation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
var numbers = [1, 2, 3]; | |
var result = | |
_.map(numbers, function (val) { // numbers를 전부 돌면서 값에 1씩 더해서 새로운 배열을 리턴한다. | |
return val + 1; | |
}) | |
.filter(function (val) { // 위에서 생성된 배열에서 값이 2보다 큰 요소만 배열로 만들어서 리턴한다. | |
return val > 2; | |
}) | |
.map(function (val) { // 위에서 생성된 2보다 큰 요소의 배열을 돌면서 자승 값의 배열을 리턴한다. | |
return val * val; | |
}); | |
console.log(result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
var numbers = [1, 2, 3]; | |
var plusOne = function (val) { | |
return val + 1; | |
}; | |
var biggerThan2 = function (val) { | |
return val > 2; | |
}; | |
var square = function (val) { | |
return val * val; | |
}; | |
var sum = function (total, val) { | |
return total + val; | |
}; | |
var result = | |
_(numbers) | |
.map(plusOne) | |
.filter(biggerThan2) | |
.map(square) | |
.reduce(sum, 0); | |
console.log(result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
var numbers = [1, 2, 3]; | |
var result = | |
_(numbers) | |
.map(function (val) { | |
return val + 1; | |
}) | |
.filter(function (val) { | |
return val > 2; | |
}) | |
.map(function (val) { | |
return val * val; | |
}) | |
.reduce(function (total, val) { | |
return total + val; | |
}, 0); | |
console.log(result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
var numbers = [1, 2, 3]; | |
var result = | |
_(numbers) | |
.map(function (val) { | |
return val + 1; | |
}) | |
.filter(function (val) { | |
return val > 2; | |
}) | |
.map(function (val) { | |
return val * val; | |
}) | |
.value(); | |
console.log(result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numbers = [1, 2, 3]; | |
var result = [], val; | |
for (var i = 0; i < numbers.length; i++) { | |
val = numbers[i] + 1; // 각 요소에 1을 더한다. | |
if (val > 2) { // 2보다 큰 값만 | |
val = val * val; // 자승을 구해서 | |
result.push(val); // 결과 배열에 추가한다. | |
} | |
} | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment