2013-12-16 25 views
2

我有categoriesList及其products,我想執行這些操作:如何爲「列表」執行這些操作:映射,組,查找,聚合,排序?

  • 地圖
  • 集團
  • 查找
  • 查詢聚合函數
  • 由多個字段排序

這可以在Dart中完成嗎?

void main() { 
    var books = new Category(0, "Books"); 
    var magazines = new Category(1, "Magazines"); 
    var categories = [books, magazines]; 
    var products = [ 
    new Product(0, "Dr. Dobb's", magazines), 
    new Product(1, "PC Magazine", magazines), 
    new Product(2, "Macworld", magazines), 
    new Product(3, "Introduction To Expert Systems", books), 
    new Product(4, "Compilers: Principles, Techniques, and Tools", books), 
    ];  

    // How to map product list by id? 

    // How to group product list by category? 

    // How to create lookup for product list by category? 

    // How to query aggregate functions? 

    // How to sort product list by category name and product name?  
} 

class Category { 
    int id; 
    String name; 

    Category(this.id, this.name); 

    operator ==(other) { 
    if(other is Category) { 
     return id == other.id; 
    } 

    return false; 
    } 

    String toString() => name; 
} 

class Product { 
    int id; 
    String name; 
    Category category; 

    Product(this.id, this.name, this.category); 

    operator ==(other) { 
    if(other is Product) { 
     return id == other.id; 
    } 

    return false; 
    } 

    String toString() => name; 
} 

這些列表與數據表中的記錄類似。

回答

3

這是我使用queries包的解決方案。

import 'package:queries/queries.dart'; 
import 'package:queries/collections.dart'; 

void main() { 
    var books = new Category(0, "Books"); 
    var magazines = new Category(1, "Magazines"); 
    var categories = [books, magazines]; 
    var products = [ 
    new Product(0, "Dr. Dobb's", magazines), 
    new Product(1, "PC Magazine", magazines), 
    new Product(2, "Macworld", magazines), 
    new Product(3, "Introduction To Expert Systems", books), 
    new Product(4, "Compilers: Principles, Techniques, and Tools", books), 
    ]; 

    var catTable = new Collection(categories); 
    var prodTable = new Collection(products); 

    // How to map product list by id? 
    print("========================"); 
    var result = prodTable.toDictionary((x) => x.id, (x) => x); 

    for(var pair in result) { 
    var key = pair.key; 
    var value = pair.value; 
    print("$key: $value"); 
    } 

    // How to group product list by category? 
    print("========================"); 
    result = prodTable.groupBy((x) => x.category); 

    for(var group in result) { 
    var key = group.key; 
    print(key); 
    for(var element in group) { 
     print(" $element"); 
    } 
    } 

    // How to create lookup for product list by category? 
    print("========================"); 
    result = prodTable.toLookup((x) => x.category); 

    for(var category in categories) { 
    var group = result[category]; 
    var key = group.key; 
    print(key); 
    for(var element in group) { 
     print(" $element"); 
    } 
    } 

    // How to query aggregate functions? 
    print("========================"); 
    result = prodTable.groupBy((x) => x.category) 
    .select((g) => { 
     "category" : g.key, 
     "products" : g.count(), 
     "max name length" : g.max((p) => p.name.length)}); 

    for(var row in result) { 
    print(row); 
    } 

    // How to sort product list by category name and product name? 
    print("========================"); 
    result = prodTable.orderBy((p) => p.category.name) 
    .thenBy((p) => p.name); 

    for(var product in result) { 
    print("${product.category}: $product"); 
    } 
} 

class Category { 
    int id; 
    String name; 

    Category(this.id, this.name); 

    operator ==(other) { 
    if(other is Category) { 
     return id == other.id; 
    } 

    return false; 
    } 

    String toString() => name; 
} 

class Product { 
    int id; 
    String name; 
    Category category; 

    Product(this.id, this.name, this.category); 

    operator ==(other) { 
    if(other is Product) { 
     return id == other.id; 
    } 

    return false; 
    } 

    String toString() => name; 
} 
======================== 
0: Dr. Dobb's 
1: PC Magazine 
2: Macworld 
3: Introduction To Expert Systems 
4: Compilers: Principles, Techniques, and Tools 
======================== 
Magazines 
    Dr. Dobb's 
    PC Magazine 
    Macworld 
Books 
    Introduction To Expert Systems 
    Compilers: Principles, Techniques, and Tools 
======================== 
Books 
    Introduction To Expert Systems 
    Compilers: Principles, Techniques, and Tools 
Magazines 
    Dr. Dobb's 
    PC Magazine 
    Macworld 
======================== 
{category: Magazines, products: 3, max name length: 11} 
{category: Books, products: 2, max name length: 44} 
======================== 
Books: Compilers: Principles, Techniques, and Tools 
Books: Introduction To Expert Systems 
Magazines: Dr. Dobb's 
Magazines: Macworld 
Magazines: PC Magazine 
+0

尼斯。這看起來很有用。 –

相關問題