컴퓨터 프로그램 개발 단계 중에 발생하는 시스템의 논리적인 오류나 비정상적 연산(버그)을 찾아내고 수정하는 작업 과정
프로그래밍 언어에서 사용되는 개념으로 익명 함수를 지칭하는 용어이다
변수에 대입 가능한 객체를 1급 객체(first class object)라고 한다
ex) 값, 인스턴스, 함수
반환형 함수명(매개변수) {
return 반환문 // 반환타입이 void인 경우는 return문 없음
}
함수명(매개변수) => 반환문
// 블록 내부에 반환문만 존재하는 경우 블록은 생략 가능하다
// 함수명은 없어도 된다
void printElement(int element) {
print(element);
}
var list = [1, 2, 3];
1) list.forEach((e){})
2) list.forEach(printElement)
// 입력과 출력이 같은 경우 함수명만 작성할 수 있다. 1, 2 둘다 가능
// forEach는 void형이므로 return문이 올 수 없다!!
함수를 다루는 함수이며, 함수를 파라미터로 받는다는 뜻이다.
final items = [1, 2, 3, 4, 5];
for(var i = 0; i < items.length; i++) {
if(items[i] % 2 == 0) {
print(items[i]); // 짝수인 요소 출력: 2, 4
}
}
items.where((e) => e % 2 == 0).forEach(print);
// where()는 주어진 조건에 맞는 요소만 남긴다.
final items = [1, 2, 3, 4, 5];
for(var i = 0; i < items.length; i++) {
if(items[i] % 2 == 0) {
print('숫자 ${items[i]}'); // 숫자 2, 숫자 4
}
}
items.where((e) => e % 2 == 0).map((e) => '숫자 $e').forEach(print);
// map()은 원하는 형식으로 리스트의 요소를 변경할 수 있다
final items = [1, 2, 3, 4, 5];
var result = false;
for(var i = 0; i < items.length; i++) {
if(items[i] % 2 == 0) {
result = true;
break;
}
}
print(result) // true
items.any((e) => e % 2 == 0); // true
// 정해진 조건에 맞는 요소가 1개라도 존재하는 경우 true를 반환한다
final items = [1, 2, 3, 4, 5];
var maxResult = items[0];
for(var i = 1; i < items.length; i++) {
maxResult = max(items[i], maxResult);
}
print(maxResult) // 5
items.reduce((v, e) => max(v, e)); // 5
// 입출력이 같기 때문에 items.reduce(max)로 축약 가능
"collectionChartDataList": [
{
"collectionName": "collection1",
"collectionSalePrice": [
{
"price": 58.25,
"cvtDatetime": "2023-03-26T08:00:00"
},
{
"price": 58.50,
"cvtDatetime": "2023-03-26T08:00:10"
}
]
},
{
"collectionName": "collection2",
"collectionSalePrice": [
{
"price": 58.75,
"cvtDatetime": "2023-03-26T08:00:20"
},
{
"price": 59.00,
"cvtDatetime": "2023-03-26T08:00:30"
}
]
},
]
class CollectionChartData {
String collectionName;
List<SalePrice>? collectionSalePrice;
CollectionChartData({
required this.collectionName,
required this.collectionSalePrice,
});
Map<String, dynamic> toJson() {
if (collectionSalePrice == null) {
return {'collectionName': collectionName};
}
return {
'collectionName': collectionName,
'collectionSalePrice':
collectionSalePrice?.map((e) => e.toJson()).toList(),
};
}
String toString() {
// TODO: implement toString
return '{collectionName: $collectionName, collectionSalePrice: ${collectionSalePrice?.map((e) => e.toString())}}';
}
}
CollectionChartData.fromJson(Map<String, dynamic> json)
: collectionName = json['collectionName'],
collectionSalePrice =
(json['collectionSalePrice'] as List<dynamic>?)
?.map((e) => SalePrice.fromJson(e))
.toList();