"함수 map()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
4번째 줄: 4번째 줄:
{{참고|JavaScript .map()}}
{{참고|JavaScript .map()}}
[[category: JavaScript]]
[[category: JavaScript]]
<source lang='JavaScript'>
<syntaxhighlight lang='JavaScript'>
var numbers = [1, 4, 9];
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]
// numbers is still [1, 4, 9]
</source>
</syntaxhighlight>
<source lang='JavaScript'>
<syntaxhighlight lang='JavaScript'>
var numbers = [1, 5, 10, 15];
var numbers = [1, 5, 10, 15];
var doubles = numbers.map(function(x) {
var doubles = numbers.map(function(x) {
17번째 줄: 17번째 줄:
// doubles is now [2, 10, 20, 30]
// doubles is now [2, 10, 20, 30]
// numbers is still [1, 5, 10, 15]
// numbers is still [1, 5, 10, 15]
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
{{참고|PHP array_map()}}
{{참고|PHP array_map()}}
<source lang='php'>
<syntaxhighlight lang='php'>
$a = [2, 3, 4];
$a = [2, 3, 4];
$b = array_map( function($n) { return $n*$n; }, $a);
$b = array_map( function($n) { return $n*$n; }, $a);
32번째 줄: 32번째 줄:
#    [2] => 16
#    [2] => 16
# )
# )
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
{{소스헤더|Python 3}}
{{소스헤더|Python 3}}
<source lang='Python'>
<syntaxhighlight lang='Python'>
numbers = [1, 2, 3, 4, 5, 6]
numbers = [1, 2, 3, 4, 5, 6]
print( list(map(lambda x: x * 2 - 1, numbers)) )
print( list(map(lambda x: x * 2 - 1, numbers)) )
# [1, 3, 5, 7, 9, 11]
# [1, 3, 5, 7, 9, 11]
</source>
</syntaxhighlight>
<source lang='Python'>
<syntaxhighlight lang='Python'>
numbers = [1, 2, 3, 4, 5, 6]
numbers = [1, 2, 3, 4, 5, 6]
print( [x * 2 - 1 for x in numbers] )
print( [x * 2 - 1 for x in numbers] )
# [1, 3, 5, 7, 9, 11]
# [1, 3, 5, 7, 9, 11]
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang='Perl'>
<syntaxhighlight lang='Perl'>
my @numbers = (1, 2, 3, 4, 5, 6);
my @numbers = (1, 2, 3, 4, 5, 6);
print map { $_* 2 - 1 } @numbers;
print map { $_* 2 - 1 } @numbers;
# 1357911
# 1357911
</source>
</syntaxhighlight>


==R==
==R==
[[분류: R]]
[[분류: R]]
{{참고|R sapply()}}
{{참고|R sapply()}}
<source lang='r'>
<syntaxhighlight lang='r'>
sapply(c(2,3,4), function(n) { n*n })
sapply(c(2,3,4), function(n) { n*n })
## [1]  4  9 16
## [1]  4  9 16
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:33 판

map

1 JavaScript

var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]
var numbers = [1, 5, 10, 15];
var doubles = numbers.map(function(x) {
   return x * 2;
});
// doubles is now [2, 10, 20, 30]
// numbers is still [1, 5, 10, 15]

2 PHP

$a = [2, 3, 4];
$b = array_map( function($n) { return $n*$n; }, $a);
print_r( $b );
# Array
# (
#     [0] => 4
#     [1] => 9
#     [2] => 16
# )

3 Python

Python 3
numbers = [1, 2, 3, 4, 5, 6]
print( list(map(lambda x: x * 2 - 1, numbers)) )
# [1, 3, 5, 7, 9, 11]
numbers = [1, 2, 3, 4, 5, 6]
print( [x * 2 - 1 for x in numbers] )
# [1, 3, 5, 7, 9, 11]

4 Perl

my @numbers = (1, 2, 3, 4, 5, 6);
print map { $_* 2 - 1 } @numbers;
# 1357911

5 R

sapply(c(2,3,4), function(n) { n*n })
## [1]  4  9 16

6 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}