Published on

Dart functional programming

Introduction

Functional programming creates new forms of values using type conversion and built-in functions.

It is possible to chain the results and ultimately make the code more concise.

Usage

type casting

// List <-> Map <-> Set

// Cast in List
List<String> blackPink = ['a', 'b', 'c', 'd',];
final Map blackPinkMap = blackPink.asMap(); // {0: a, 1: b, 2: c, 3: d}
final Set blackPinkSet = blackPink.toSet(); // {a, b, c, d}

// Case in Map
final Map blackPinkAsMap = blackPink.asMap();
List<dynamic> keys = blackPinkAsMap.keys.toList(); // [0, 1, 2, 3]
List<dynamic> values = blackPinkAsMap.values.toList(); // [a, b, c, d]

// Cast in Set
Set blackPinkFronSet = Set.from(blackPink); // {a, b, c, d}
List balckSet = blackPinkFronSet.toList(); // [a, b, c, d]

map()

  • Methods of List
  • Returns a new list after changing the format of all member variables.
  • The list created by map() has a different address value even if the member variables are the same

iterable:

A collection that can be iterated over (list, array)

Use the .toList() method to convert to a list

List<String> blackPink = ['a', 'b', 'c', 'd',]

blackPink.map((item){
	return 'this it $item'
});

// with arrow function
final blackPink2 = blackPink.map((item) => 'black pink $item')

mapping Map

Map<String, String> harryPotter = {
  'harry potter': '해리포터',
  'ron': '론',
  'hermione': '헤르미온느'
};

// When mapping a map, you receive both key and value.
final result = harryPotter.map(
    (key, value) => MapEntry('harry potter character $key', '해리포터 캐릭터 $value'));

void main() {
  // key
  print(harryPotter.keys.map((item) => 'key is $item').toList());
	// value
  print(harryPotter.values.map((item) => 'value is $item').toList());
}

mapping Set

Set blackPinkSet = {"rose",'jisu','jenny','lisa'};

final newSet = blackPinkSet.map((item) => 'this is $item').toSet();

where()

Purpose of filtering

List<Map<String, String>> people = [
  {
    'name': '제니',
    'group': '블랙핑크',
  },
  {
    'name': '로제',
    'group': '블랙핑크',
  },
  {
    'name': 'RM',
    'group': 'BTS',
  }
];

final result = people.where((item) => item['group'] == 'BTS').toList();

reduce()

  • Only the first values of the previous and next are entered in the first time.
  • After that, the return value of the callback function becomes the previous value of the next iteration.
  • Reduce can only return results of the same type as instance variables.
  • This problem can be solved with fold()
List<int> numbers = [1, 2, 3, 4, 5];

int resultInt = numbers.reduce((prev, next) {
  return prev + next;
});

// arrow function
numbers.reduce((prev, next) => prev + next);

fold()

  • Like reduce(), it allows you to set the return data type while setting an initial value
  • The first parameter goes into prev, and the first member variable goes into next
List<int> numbers = [1, 2, 3, 4, 5];

// Specify the return type as Generic
numbers.fold<int>(0, (prev, next) => prev + next);

List<String> words = ['Hi', 'Hello'];

words.fold<String>('', (prev, next) => prev + next); // HiHello
words.fold<int>(0, (prev, next) => prev + next.length); // 7

…cascade operator

  • When combining lists
  • The member variables are unpacked and added to the new list
  • A new list is created
List<int> even = [2, 4, 6];
List<int> odd = [1, 3, 5];
List newArr = [...even, ...odd];

// `[...even]` and `even` are different.