Dart Function

Map

  • Iterable

void main() {
  List<String> nj = ['하니', '민지', '혜린', '혜인', '다니엘', '하니'];

  print(nj);
  print(nj.asMap());
  print(nj.toSet());
  
  Map njMap = nj.asMap();
  
  print(njMap.keys);
  print(njMap.values);
}

Cascading

void main() {
  List<String> nj = ['하니', '민지', '혜린', '혜인', '다니엘', '하니'];

  print(nj);
  print(nj.asMap());
  print(nj.toSet());
  
  Map njMap = nj.asMap();
  
  print(njMap.keys);
  print(njMap.values);
  
  print(njMap.keys.toList());
  print(njMap.values.toList());
  
  
  Set njSet = Set.from(nj);
  print(njSet.toList());
  
}

List의 Map method

void main() {
  List<String> nj = ['하니', '민지', '혜린', '혜인', '다니엘'];

  final newNJ = nj.map((x){
    return '뉴진스 $x';
  });
  
  print(nj);
  print(newNJ.toList());
  
  final newNJ2 = nj.map((x) => '뉴진스 $x');
  
  print(newNJ2.toList());
  
  print(nj == nj); // true
  print(newNJ == nj); // false
  print(newNJ == newNJ2); // false
  
  
  String num = '13579';
  final parsed = num.split('').map((x) => '$x.jpg').toList();
  print(parsed);
}

Map의 Map Method

void main() {
  Map<String, String> harryPotter = {
    'Harry' : '해리',
    'Ron' : '론', 
    'Hermione' : '헤르미온느',
  };
  
  final result = harryPotter.map(
    (key, value) => MapEntry(
      'Harry Potter Character $key',
      '해리포터 캐릭터 $value',
    ),
  );
  
  print(harryPotter);
  print(result);
  
  final keys = harryPotter.keys.map((x) => 'Harry Potter Character $x').toList();
  final values = harryPotter.values.map((x) => '해리포터 $x').toList();
  
  print(keys);
  print(values);
}

Set의 Map Method

void main() {
  Set nj = {
    '하니',
    '민지',
    '혜린',
    '혜인',
    '다니엘',
  };
  
  final njSet = nj.map((x) => 'New Jeans $x').toSet();
  
  print(njSet);
}

Where

void main() {
  List<Map<String, String>> people = [
    {
      'name': 'Hanni',
      'group': 'New Jeans',
    },
    {
      'name': 'MinJi',
      'group': 'New Jeans',
    },
    {
      'name': 'Irene',
      'group': 'Red Velvet',
    },
    {
      'name': 'WonYoung',
      'group': 'IVE',
    },
  ];
  
  print(people);
  final nj = people.where((x) => x['group'] == 'New Jeans');

  print(nj);
}

reduce

void main() {
  List<int> nums = [
    1,
    3,
    5,
    7,
    9
  ];
  
  final result = nums.reduce((prev, next) {
    print('--------------------');
    print('previous : $prev');
    print('next : $next');
    print('total : ${prev + next}');
    
    return prev + next;
  });
  
  final result2 = nums.reduce((prev, next) => prev + next);
  
  print(result);
  print(result2);
}
void main() {
  List<int> nums = [1, 3, 5, 7, 9];

  final result2 = nums.reduce((prev, next) => prev + next);

  print(result2);

  List<String> words = [
    'soobin',
    'judy',
    'yerin',
  ];

  final sentence = words.reduce((prev, next) => prev + next);

  print(sentence);
}

fold

void main() {
  List<int> nums = [1, 3, 5, 7, 9];

  final result2 = nums.reduce((prev, next) => prev + next);

  print(result2);

  List<String> words = [
    'soobin',
    'judy',
    'yerin',
  ];

  final sentence = words.reduce((prev, next) => prev + next);

  print(sentence);
  
  // string이 아니라 아래 코드는 에러를 반환함
  // reduce는 같은 타입을 반환해야 함
  // words.reduce((prev, next) => prev.length + next.length)

  final sum = nums.fold<int>(0, (prev, next) => prev + next);
  
  print(sum);
  
  final count = words.fold<int>(0, (prev, next) => prev + next.length);

  print(count);
}

casacading operator

void main() {
  List<int> even = [
    2, 4, 6, 8,
  ];
  
  List<int> odd = [
    1, 3, 5, 7,
  ];
  
  print([even, odd]); // [[2, 4, 6, 8], [1, 3, 5, 7]]
  print([...even, ...odd]); 
  print(even == [...even]);
}

functional programming

  • 새로운 값 생성

  • 체이닝 가능

void main() {
  final List<Map<String, String>> people = [
    {
      'name': 'hanni',
      'group': 'new jeans',
    },
    {
      'name': 'minji',
      'group': 'new jeans',
    },
    {
      'name': 'haerin',
      'group': 'new jeans',
    },
    {
      'name': 'haein',
      'group': 'new jeans',
    },
  ];
  
  print(people);
  
  final parsePeople = people.map(
    (x) => Person(
      name: x['name']!,
      group: x['group']!,
    ),
  ).toList();
  
  print(parsePeople);
  
  for(Person person in parsePeople) {
    print(person.name);
    print(person.group);
  }
  
  final nj = parsePeople.where(
    (x) => x.group == 'new jeans',
  );
  
  print(nj);
  
  final result = people.map(
    (x) => Person(
      name: x['name']!,
      group: x['group']!,
    ),
  ).where((x) => x.group == 'new jeans');
  
  print(result);
}

class Person { // map은 구조화가 안되어있어서 자유도가 너무 높음
  final String name;
  final String group;
  
  Person({
    required this.name,
    required this.group,
  });
  
  @override
  String toString() {
    return 'Person(name: $name, group: $group)';
  }
}

Last updated