Foggy day

[Dart] 알고 있으면 좋은 문법들 본문

Flutter/Dart 문법

[Dart] 알고 있으면 좋은 문법들

jinhan38 2023. 3. 2. 16:40

 

 

알고 있으면 좋은 문법들에 대해 예제를 추가했습니다. 

이중에서 spreadOperator는 특히 많이 사용되고, where도 필요에 따라서 많이 사용할 수도 있습니다.

어떠한 형태로 사용하는지 익혀두면 좋습니다. 

import 'dart:math';

// The imported package 'collection' isn't a dependency of the importing package.
// 나올 경우 yaml -> dependencies -> collection: any 추가
import 'package:collection/collection.dart';


class Advanced {
  Advanced() {
    spreadOperator();
    functionalProgramming();
    whereSyntax();
    reduceSyntax();
    sumSyntax();
  }

  void spreadOperator() {
    List<int> addList = [5, 6, 7];
    List<int> list = [1, 2, 3, 4, 5, ...addList];
    print('Advanced.spreadOperator list : $list');
  }

  void functionalProgramming() {
    // int Function(int a, int b) a = add;
    // type : int Function(int a, int b)
    // name : a
    var a = add;
    int result = a(4, 3);
    print('Advanced.functionalProgramming result : $result');
  }

  int add(int a, int b) => a + b;

  void whereSyntax() {
    List<int> list = [1, 2, 3, 4, 5, 6];
    for (var value in list) {
      if (value > 3) {
        print('Advanced.whereSyntax value : $value');
      }
    }
    
	/// element가 3 보다 큰 경우에만 값을 리턴
    List<int> whereToList = list.where((element) => element > 3).toList();
    print('Advanced.whereSyntax whereToList : $whereToList');

    list.where((element) => element > 2).forEach((element) {
      print('Advanced.whereSyntax where forEach element : $element');
    });

	/// element가 2보다 큰 경우, 첫번째 항목은 skip하고, 2개의 값만 취한다.
    list.where((element) => element > 2).skip(1).take(2).forEach((element) {
      print('Advanced.whereSyntax where skip take forEach element : $element');
    });
  }

  void reduceSyntax() {
    List<int> list = [1, 4, 5, 6, 2, 3];

    int maxValue = list[0];
    for (var value in list) {
      print('Advanced.reduceSyntax value : $value');
      maxValue = max(value, maxValue);
    }
    print('Advanced.reduceSyntax maxValue : $maxValue');

    var result = list.reduce((value, e) {
      print('Advanced.reduceSyntax value : $value, element : $e');
      return max(value, e);
    });
    print('Advanced.reduceSyntax result : $result');
  }

  void sumSyntax() {
    List<int> list = [1, 4, 5, 6, 2, 3];

    /// 비교값이 배열의 0, 1번째 값으로 시작
    int reduceSum = list.reduce((a, b) {
      print('Advanced.sumSyntax reduce a : $a, b : $b');
      return a + b;
    });
    print('Advanced.sumSyntax reduceSum : $reduceSum');

    /// 비교값이 입력한 값(50) 부터 시작
    int foldSum2 = list.fold(50, (previous, current) {
      print('Advanced.sumSyntax p : $previous, c : $current');
      return previous + current;
    });
    print('Advanced.sumSyntax foldSum2 : $foldSum2');

    int sum = list.sum;
    print('Advanced.sumSyntax sum : $sum');
  }
}
콘솔 

Advanced.spreadOperator list : [1, 2, 3, 4, 5, 5, 6, 7]
Advanced.functionalProgramming result : 7
Advanced.whereSyntax value : 4
Advanced.whereSyntax value : 5
Advanced.whereSyntax value : 6
Advanced.whereSyntax whereToList : [4, 5, 6]
Advanced.whereSyntax where forEach element : 3
Advanced.whereSyntax where forEach element : 4
Advanced.whereSyntax where forEach element : 5
Advanced.whereSyntax where forEach element : 6
Advanced.whereSyntax where skip take forEach element : 4
Advanced.whereSyntax where skip take forEach element : 5
Advanced.reduceSyntax value : 1
Advanced.reduceSyntax value : 4
Advanced.reduceSyntax value : 5
Advanced.reduceSyntax value : 6
Advanced.reduceSyntax value : 2
Advanced.reduceSyntax value : 3
Advanced.reduceSyntax maxValue : 6
Advanced.reduceSyntax value : 1, element : 4
Advanced.reduceSyntax value : 4, element : 5
Advanced.reduceSyntax value : 5, element : 6
Advanced.reduceSyntax value : 6, element : 2
Advanced.reduceSyntax value : 6, element : 3
Advanced.reduceSyntax result : 6
Advanced.sumSyntax reduce a : 1, b : 4
Advanced.sumSyntax reduce a : 5, b : 5
Advanced.sumSyntax reduce a : 10, b : 6
Advanced.sumSyntax reduce a : 16, b : 2
Advanced.sumSyntax reduce a : 18, b : 3
Advanced.sumSyntax reduceSum : 21
Advanced.sumSyntax p : 50, c : 1
Advanced.sumSyntax p : 51, c : 4
Advanced.sumSyntax p : 55, c : 5
Advanced.sumSyntax p : 60, c : 6
Advanced.sumSyntax p : 66, c : 2
Advanced.sumSyntax p : 68, c : 3
Advanced.sumSyntax foldSum2 : 71
Advanced.sumSyntax sum : 21

'Flutter > Dart 문법' 카테고리의 다른 글

[Dart] 클래스(class)에 대하여 - 2  (0) 2023.03.03
[Dart] 클래스(class)에 대하여 - 1  (0) 2023.03.02
[Dart] 함수  (0) 2023.03.02
[Dart] 반복문(for문)  (0) 2023.03.02
[Dart] Collection - List, Map, Set  (0) 2023.03.02