패키지, 저장소 및 앱 카테고리 검색

패키지 상세v2.1.0

darq

관성 평가 가능한 열거형의 힘을 손에 넣으세요! (.NET 라이브러리의 함수형 LINQ 포트입니다.)

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
2.4만
좋아요
157
Pub 점수
140

패키지 설치

flutter pub add darq

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

Darq는 .NET의 LINQ에서 영감을 받은 지연 평가 가능한 열거형 연산을 제공하는 Dart 패키지로, 효율적이고 가독성 높은 데이터 변환을 가능하게 합니다. 필터링, 매핑, 축소 등의 작업을 즉시 실행하지 않고 필요할 때만 수행하는 유창한 API를 제공합니다. 대규모 데이터셋이나 복잡한 파이프라인 작업에 적합하며, 결과가 실제로 사용될 때까지 계산을 지연시킴으로써 코드의 명확성과 성능을 향상시킵니다.

사용 사례

  • 대규모 데이터 세트를 효율적으로 처리하기
  • 여러 데이터 변환 체인하기
  • 읽기 쉽고 유지보수 가능한 데이터 파이프라인 구축
  • 함수형 스타일의 데이터 조작 구현
  • UI 데이터 처리에서 성능 최적화

주요 기능

  • 성능 향상을 위한 지연 평가
  • 유려하고 체인 가능한 메서드 구문
  • map, filter, reduce와 같은 일반적인 함수적 연산 지원
  • 최소 메모리 오버헤드
  • 조합 가능하고 재사용 가능한 변환 파이프라인

적합한 대상

  • LINQ 또는 함수형 프로그래밍에 익숙한 개발자
  • 효율적인 데이터 처리가 필요한 프로젝트
  • 대규모 또는 스트리밍 데이터를 처리하는 앱
  • 깨끗하고 표현력 있는 코드를 우선시하는 팀

고려 사항

  • 함수형 개념에 익숙하지 않은 개발자에게는 학습 곡선이 있을 수 있습니다
  • 성능 이점은 적절한 사용 패턴에 따라 달라집니다
  • 즉각적인 실행 시나리오에 적합하지 않으며, 급진적 평가가 선호되는 경우

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

pub package github stars license MIT Build Status Code Coverage

A port of .NET's LINQ IEnumerable functions to Dart. This package extends the native Iterable type with all of the LINQ methods that do not exist in native Dart. Starting with version 0.5, this package also contains the extension methods from the MoreLINQ .NET library.

API Reference

Usage

Because this library uses Dart 2.6's new extension methods, any Iterable has access to these methods as though they were native methods. This includes classes that extend from Iterable, such as List and Set.

In addition, this library adds several new types of Iterable classes to make some utility functions easier:

// Creates an iterable containing the numbers from 2 to 6: [2, 3, 4, 5, 6]
var rangeI = RangeIterable(2, 6, inclusive: true);

// Creates an iterable that contains 3 copies of the value 'abc': ['abc', 'abc', 'abc']
var repeatI = RepeatIterable('abc', 3);

// Creates an iterable from a string, iterating over its characters
// This is an extension getter property on String that returns an 
// iterable via `String.codeUnits.map((u) => String.fromCodeUnit(u))`.
// Results in ['a', 'b', 'c', 'd', 'e', 'f']
var stringI = 'abcdef'.iterable;

// Same as above but using `runes` instead of `codeUnits` to respect 
// rune boundaries and maintain surrogate pairs.
var stringIR = 'abcdef'.iterableRunes;

You can call any of 110+ new methods on it to modify or analyze it. For example, the native method map is expanded upon with select, which combines the element with the index at which the element is found within the iterable:

var list = [10, 20, 30];
var mappedList = list.select((i, index) => '$index-$i'); // ['1-10', '2-20', '3-30']

There are "OrDefault" variants on several common iterator value getter methods, such as firstOrDefault, singleOrDefault, and defaultIfEmpty. Instead of throwing an error, these methods will return a default value (or null if left unspecified) if the element(s) cannot be found:

var list = <String>[];

var native = list.first; // Throws a StateError
var orDefault = list.firstOrDefault('abc'); // Returns 'abc'

var list2 = [1, 2, 3];
var importantValue = list2.where((i) => i >= 4)
                          .defaultIfEmpty(-1); // Returns [-1]

You can filter an iterable down to unique instances of elements with the distinct method:

var list = [1, 1, 1, 2, 2, 3, 4, 5, 5, 5, 5, 5];
var uniqueList = myEnum.distinct(); // [1, 2, 3, 4, 5]

There are also set operations with the except, intersect, and union methods:

var listA = [1, 2, 3, 4];
var listB = [3, 4, 5, 6];

var exclusion = listA.except(listB);       // [1, 2]
var intersection = listA.intersect(listB); // [3, 4]
var union = listA.union(listB);            // [1, 2, 3, 4, 5, 6]

And you can group elements together by common features using groupBy:

var list = [1, 2, 3, 4, 5, 6];
var groupedList = list.groupBy((i) => i / 3 == 0); // [[1, 2, 4, 5], [3, 6]]

Or bundle them into groups of a fixed length using segment:

var list = [1, 2, 3, 4, 5, 6];
var segmented = list.segment(2); // [[1, 2], [3, 4], [5, 6]]

You can even perform complex ordering functions using orderBy and thenBy:

var list = ['ab', 'a', 'c', 'aa', ''];
// Sort by string length followed by alphabetical order
var ordered = list.orderBy((c) => c.length)
                  .thenBy((c) => c);
// Result: ['', 'a', 'c', 'aa', 'ab']

Just like in native dart, every method returns a new Iterable, so you can chain methods together to make complex mapping, grouping, and filtering behavior:

var list = [3, 1, 6, 2, 3, 2, 4, 1];
var result = list.select((i, idx) => i * 2 + idx)     // [6, 3, 14, 8, 10, 10, 14, 9]
                 .distinct()                          // [6, 3, 14, 8, 10, 9]
                 .where((i) => i > 4)                 // [6, 14, 8, 10, 9]
                 .orderBy((i) => i)                   // [6, 8, 9, 10, 14]
                 .map((i) => i.toRadixString(16));    // [6, 8, 9, A, E]

NEW in 2.0.0

With record support added in Dart 3, A new method has been added called deconstruct. It's purpose is to easily unwrap iterables of records, resulting in separate iterables containing the corresponding fields of the internal records.

var johns = [
  ("John Doe", 26, "E145326"),
  ("John Wick", 58, "E645091"),
  ("John Teesonter", 15, "E997123"),
];
var (names, ages, ids) = johns.deconstruct();

print(names); // ["John Doe", "John Wick", "John Teesonter"]
print(ages);  // [26, 58, 15]
print(ids);   // ["E145326", "E645091", "E997123"]

This extension method is implemented on lists containing records with up to nine fields.

(Note: Due to apparent current language restrictions, records containing named fields are not supported.)

Tuples

As a necessity for some operations, I needed a Tuple class, and as I was unsatisfied with the current offerings out there right now, I elected to create my own.

For the uninitiated, tuples are similar to lists in concept that they contain multiple values addressable by index. But where every element of a list must resolve to the same type (the type of the list), each element in a tuple can be its own specified type. This results in being able to contain, distribute, and access the items in a tuple in a type-safe way. You could, for example, use a Tuple2<double, String> to return two values from a function and be able to access both the double and the String values without needing to resort to fragile methods such as dynamic or runtime type casting. Another difference between lists and tuples is that tuples are inherently immutable, so they aren't susceptible to side effects stemming from mutation and can even benefit from being declared as constants.

This package exposes tuple classes from Tuple0 up to Tuple9, depending on how many items the tuple contains. (Yes, I agree that Tuple0 and Tuple1 seem largely redundant, but I've seen them exist in the tuple libraries of many programming languages so it must serve some purpose or other, so I included them here all the same for completeness if nothing else.) Each tuple class includes the following features:

  • Constant constructors allow for efficient use of known tuple values.
  • Includes access to the item(s) by getter (tuple.item2) or by indexer(tuple[2]). (Note that access by indexer is not type-safe)
  • Factory constructor fromJson and method toJson means tuples are seralization-ready.
  • Additional factory constructor fromList to generate a tuple from a list (automatically casting when specifying type parameters for the constructor).
  • An asType method allows freely casting the tuple from any assortment of types to any other assortment of types (provided the items are compatible with those types).
    • Additionally, there is an asDynamic convenience method for casting a tuple to dynamic items.
  • Although tuples themselves are immutable, a copyWith method allows easy generation of duplicate tuples, optionally specifying new values for specific items.
    • Additionally, a copyWithout method allows selective filtering of items from a tuple resulting in a lower-order tuple.
  • A mapActions method allows you to iterate over each item with an exhaustive list of type-safe callbacks.
  • Each tuple class extends Iterable<dynamic>, so it can be treated as a normal iterable (and thus combined with any darq extension method).
  • As == and hashCode are both implemented, tuples can be directly compared for equality or used as keys for maps and other hash sets.
  • (2.0.0) Can be converted to and from Dart 3 records.

MoreLINQ Extension Methods

As of version 0.5, this package also contains the extension methods from the MoreLINQ .NET library. This more than triples the available number of extension methods over vanilla LINQ.

Some examples of the new methods from MoreLINQ include:

index:

var list = ['a', 'b', 'c'];
var indexedList = list.index();

// Iterable:
// [ [0, 'a'], [1, 'b'], [2, 'c'] ]

assertAll:

var list = [2, 4, 5];
list.assertAll((x) => x.isEven);

// Throws an assertion error

awaitAll:

var list = [
  Future.delayed(Duration(seconds: 1)),
  Future.delayed(Duration(seconds: 2)),
  Future.delayed(Duration(seconds: 3)),
];
await list.awaitAll();

// Waits for 3 seconds before continuing

subsets:

var list = [1, 2, 3];
var subsets = list.subsets();

// Iterable: 
// [ [], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3] ]

interleave:

var listA = [1, 3, 5];
var listB = [2, 4, 6];
var combined = listA.interleave(listB);

// Iterable:
// [1, 2, 3, 4, 5, 6]

permutations:

var list = [1, 2, 3];
var perms = list.permutations();

// Iterable:
// [ [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] ]

split:

var list = ['a', ' ', 'b', 'c', ' ', 'd'];
var split = list.split(' ');

// Iterable:
// [ ['a'], ['b', 'c'], ['d'] ]

New Iterable Types

String Extension Methods

Map Extension Methods

Iterable Extension Methods

카테고리

소스 저장소

이 패키지를 사용하는 프로젝트