Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. // 2019-10-02
  2. class A {
  3. A(this.item, this.other, this.next);
  4.  
  5. final String item;
  6. final other;
  7. final int next;
  8. }
  9.  
  10. main(){
  11. // Setup: We have an object A{String item, var other, int next} inside a list (innerList).
  12. // This list<A> is inside another list (outerList).
  13. // Result: The result should be a list of String item. List<String> resultList
  14.  
  15. final List<A> innerList = List.generate(10,(i)=> A('Item $i', 'other $i', i) );
  16. final List<List<A>> outerList = List.generate(10, (_)=> innerList );
  17.  
  18. List<String> resultList;
  19.  
  20. // 1
  21. final List<String> resultL =outerList.map<List<String>>((il)=> il.map<String>((A a)=> a.item).toList()).toList().fold([],(a,b)=> a..addAll(b));
  22.  
  23.  
  24. // print(resultList);
  25. resultList.clear();
  26.  
  27. // 2
  28. resultList = outerList.fold(['kjsdf'], (List<String> a, List<A> b)=> a..addAll(b.map<String>((A i)=>i.item).toList()));
  29.  
  30. //print(resultList);
  31. resultList.clear();
  32.  
  33.  
  34. // 3
  35. for(List<A> listA in outerList){
  36. for(A a in listA){
  37. resultList.add(a.item);
  38. }
  39. }
  40.  
  41. // 4
  42. List<String> resultLister() => outerList.fold(
  43. [],
  44. (newList, outer) => newList
  45. ..addAll(
  46. outer.fold(
  47. [],
  48. (innerNewList, inner) => innerNewList
  49. ..add(
  50. inner.item,
  51. ),
  52. ).toList(),
  53. ),
  54. );
  55.  
  56. print(resultLister());
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement