Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support listOf, setOf with varargs #60

Merged
merged 24 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
478 changes: 408 additions & 70 deletions README.md

Large diffs are not rendered by default.

46 changes: 25 additions & 21 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@ void main() {
/**
* Lists
*/
final numbers1 = listOf([1, 2, 3, 4]).map((it) => ">$it<");
print(numbers1); // [>1<, >2<, >3<, >4<]
final mapped = listOf(1, 2, 3, 4).map((it) => ">$it<");
print(mapped); // [>1<, >2<, >3<, >4<]

final numbers2 =
listOf([1, 2, 3, 4]).flatMap((it) => listOf([it * 2, it * 3]));
print(numbers2); // [2, 3, 4, 6, 6, 9, 8, 12]
final flatMapped = listOf(1, 2, 3, 4).flatMap((it) => listOf(it * 2, it * 3));
print(flatMapped); // [2, 3, 4, 6, 6, 9, 8, 12]

final numbers3 = numbers2.filter((it) => it % 3 == 0);
print(numbers3); // [3, 6, 6, 9, 12]
final filtered = flatMapped.filter((it) => it % 3 == 0);
print(filtered); // [3, 6, 6, 9, 12]

final numbers4 = numbers3.distinct();
print(numbers4); //[3, 6, 9, 12]
final distinct = listFrom([1, 2, 3, 1, 2, 3]).distinct();
print(distinct); //[1, 2, 3]

/**
* Better equals
*/
final klistEquals = listOf([12, 9, 6, 3]) == listOf([12, 9, 6, 3]);
print(klistEquals); // true
final kListEquals = listOf(12, 9, 6, 3) == listOf(12, 9, 6, 3);
print(kListEquals); // true

final dartListEquals = [12, 9, 6, 3] == [12, 9, 6, 3];
print(dartListEquals); // false

final kMapEquals = mapOf({1: "Bulbasaur", 2: "Ivysaur"}) ==
mapOf({1: "Bulbasaur", 2: "Ivysaur"});
final kMapEquals = mapFrom({1: "Bulbasaur", 2: "Ivysaur"}) ==
mapFrom({1: "Bulbasaur", 2: "Ivysaur"});
print(kMapEquals); // true

final dartMapEquals =
Expand All @@ -37,28 +36,33 @@ void main() {
/**
* Sets
*/
print(setOf([1, 1, 2, 2, 3])); // [1, 2, 3]
print(setOf(1, 2, 3, 1, 2, 3)); // [1, 2, 3]

/**
* Maps
*/
final pokemon = mutableMapOf({
final pokemon = mutableMapFrom({
1: "Bulbasaur",
2: "Ivysaur",
});
pokemon[1] = "Dito";
print(pokemon); // {1=Dito, 2=Ivysaur}
pokemon[1] = "Ditto";
print(pokemon); // {1=Ditto, 2=Ivysaur}

/**
* Tons of useful operators which *should* be part of the dart std lib
*/
final numbers = listOf([1, 2, 3, 4]);
final numbers = listOf(1, 2, 3, 4);
print(numbers.sum()); // 10

final numbers5 = listOf([1, 2, 3, 4]).sortedDescending();
final numbers5 = listOf(1, 2, 3, 4).sortedDescending();
print(numbers5); // [4, 3, 2, 1]

final beatles = setOf(["John", "Paul", "George", "Ringo"]);
final beatles = setOf("John", "Paul", "George", "Ringo");
print(beatles); // [John, Paul, George, Ringo]
print(beatles.joinToString(separator: "/")); // John/Paul/George/Ringo
print(beatles.joinToString(
separator: "/",
transform: (it) => it.toUpperCase())); // JOHN/PAUL/GEORGE/RINGO

final grouped = beatles.groupBy((it) => it.length);
print(grouped); // {4=[John, Paul], 6=[George], 5=[Ringo]}
}
49 changes: 24 additions & 25 deletions example/shop.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import 'package:dart_kollection/dart_kollection.dart';

void main() {
final rekasPrducts = getOrderedProducts(jbCustomers[reka]);
print("reka bought $rekasPrducts");
final rekasProducts = getOrderedProducts(jbCustomers[reka]);
print("reka bought $rekasProducts");

final allOrdersOfJbShop = getAllOrderedProducts(jbShop);
var formattedSales = allOrdersOfJbShop
final formattedSales = allOrdersOfJbShop
.map((it) => "Sold ${it.second}x '${it.first}', revenue ${it.third}\$")
.joinToString(separator: "\n");
print("total jbShop sales:\n${formattedSales}");
print("total jbShop sales:\n$formattedSales");

final revenue = allOrdersOfJbShop.map((it) => it.third).sum();
print("total jbShop revenue ${revenue}\$");
print("total jbShop revenue $revenue\$");
}

KSet<Product> getOrderedProducts(Customer customer) {
Expand Down Expand Up @@ -48,7 +48,7 @@ class Customer {
}

class Order {
Order(this.products, this.isDelivered);
Order(this.products, {this.isDelivered});

final KList<Product> products;
final bool isDelivered;
Expand Down Expand Up @@ -104,13 +104,13 @@ final Ankara = City("Ankara");
final Tokyo = City("Tokyo");

Customer customer(String name, City city, [List<Order> orders = const []]) =>
Customer(name, city, listOf(orders));
Customer(name, city, listFrom(orders));

Order order(List<Product> products, [bool isDelivered = true]) =>
Order(listOf(products), isDelivered);
Order order(List<Product> products, {bool delivered = true}) =>
Order(listFrom(products), isDelivered: delivered);

Shop shop(String name, List<Customer> customers) =>
Shop(name, listOf(customers));
Shop(name, listFrom(customers));

final jbShop = shop("jb test shop", [
customer(lucas, Canberra, [
Expand All @@ -122,8 +122,8 @@ final jbShop = shop("jb test shop", [
order([rubyMine, webStorm])
]),
customer(reka, Budapest, [
order([idea], false),
order([idea], false),
order([idea], delivered: false),
order([idea], delivered: false),
order([idea])
]),
customer(bajram, Ankara, [
Expand All @@ -139,22 +139,21 @@ final jbShop = shop("jb test shop", [
]);

final KMap<String, Customer> jbCustomers =
jbShop.customers.fold(hashMapOf<String, Customer>(), (map, customer) {
jbShop.customers.fold(hashMapFrom<String, Customer>(), (map, customer) {
(map as KMutableMap<String, Customer>)[customer.name] = customer;
return map;
});

final orderedProducts =
setOf([idea, reSharper, dotTrace, dotMemory, rubyMine, webStorm, phpStorm]);

final sortedCustomers =
listOf([cooper, nathan, bajram, asuka, lucas, riku, reka])
.map((it) => jbCustomers[it]);

final groupedByCities = mapOf({
Canberra: listOf([lucas, cooper]),
Vancouver: listOf([nathan]),
Budapest: listOf([reka]),
Ankara: listOf([bajram]),
Tokyo: listOf([asuka, riku]),
setOf(idea, reSharper, dotTrace, dotMemory, rubyMine, webStorm, phpStorm);

final sortedCustomers = listOf(cooper, nathan, bajram, asuka, lucas, riku, reka)
.map((it) => jbCustomers[it]);

final groupedByCities = mapFrom({
Canberra: listOf(lucas, cooper),
Vancouver: listOf(nathan),
Budapest: listOf(reka),
Ankara: listOf(bajram),
Tokyo: listOf(asuka, riku),
}).mapValues((it) => it.value.map((name) => jbCustomers[name]));
96 changes: 96 additions & 0 deletions example/syntax_comparison.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import 'package:dart_kollection/dart_kollection.dart';

void main() {
/**
* Kotlin like, function based syntax
*/

/// List
// Create immutable lists
emptyList<int>();
listOf(1, 2, 3, 4, 5);
listFrom([1, 2, 3, 4, 5]);

// Create mutable lists
mutableListOf(1, 2, 3, 4, 5);
mutableListFrom([1, 2, 3, 4, 5]);

/// Set
// Create immutable sets
emptySet<int>();
setOf(1, 2, 3, 4, 5);
setFrom([1, 2, 3, 4, 5]);

// Create a mutable set which keeps the order of the items
linkedSetOf(1, 2, 3, 4, 5);
linkedSetFrom([1, 2, 3, 4, 5]);

// Create mutable, unordered hash-table based set
hashSetOf(1, 2, 3, 4, 5);
hashSetFrom([1, 2, 3, 4, 5]);

/// Map
// Create immutable maps
emptyMap<int, String>();
mapFrom({1: "a", 2: "b"});

// Create mutable maps
mutableMapFrom({1: "a", 2: "b"});

// Create mutable maps without specified order when iterating over items
hashMapFrom({1: "a", 2: "b"});

// Create mutable maps which keep the order of the items
linkedMapFrom({1: "a", 2: "b"});

/**
* Dart like, constructor based syntax
*/

/// List
// Create immutable lists
KList<int>.empty();
KList.of(1, 2, 3, 4, 5);
KList.from([1, 2, 3, 4, 5]);

// Create mutable lists
KMutableList<int>.empty();
KMutableList.of(1, 2, 3, 4, 5);
KMutableList.from([1, 2, 3, 4, 5]);

/// Set
// Create immutable sets
KSet<int>.empty();
KSet.of(1, 2, 3, 4, 5);
KSet.from([1, 2, 3, 4, 5]);

// Create a mutable set which keeps the order of the items
KMutableSet<int>.empty();
KMutableSet.of(1, 2, 3, 4, 5);
KMutableSet.from([1, 2, 3, 4, 5]);

// Create mutable, unordered hash-table based set
KHashSet<int>.empty();
KHashSet.of(1, 2, 3, 4, 5);
KHashSet.from([1, 2, 3, 4, 5]);

// Create a mutable set which keeps the order of the items
KLinkedSet<int>.empty();
KLinkedSet.of(1, 2, 3, 4, 5);
KLinkedSet.from([1, 2, 3, 4, 5]);

/// Map
// Create mutable maps
KMutableMap<int, String>.empty();
KMutableMap.from({1: "a", 2: "b"});

// Create mutable maps without specified order when iterating over items
KHashMap<int, String>.empty();
KHashMap.from({1: "a", 2: "b"});

// Create mutable maps which keep the order of the items
KLinkedMap<int, String>.empty();
KLinkedMap.from({1: "a", 2: "b"});

print("Everything works!");
}
4 changes: 4 additions & 0 deletions lib/dart_kollection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ export 'src/k_iterator_mutable.dart';
export 'src/k_list.dart';
export 'src/k_list_mutable.dart';
export 'src/k_map.dart';
export 'src/k_map_hash.dart';
export 'src/k_map_linked.dart';
export 'src/k_map_mutable.dart';
export 'src/k_set.dart';
export 'src/k_set_hash.dart';
export 'src/k_set_linked.dart';
export 'src/k_set_mutable.dart';
export 'src/tuples.dart';
export 'src/util/annotations.dart';
9 changes: 5 additions & 4 deletions lib/src/collection/iterator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import 'package:dart_kollection/src/k_iterator_mutable.dart';
class InterOpKIterator<T> implements KIterator<T> {
InterOpKIterator(this.iterator) {
lastReturned = null;
iterator.moveNext();
_hasNext = iterator.moveNext();
nextValue = iterator.current;
}

final Iterator<T> iterator;
T nextValue;
T lastReturned;
var _hasNext = false;

@override
bool hasNext() {
return nextValue != null;
return _hasNext;
}

@override
T next() {
if (!_hasNext) throw NoSuchElementException();
final e = nextValue;
if (e == null) throw NoSuchElementException();
iterator.moveNext();
_hasNext = iterator.moveNext();
nextValue = iterator.current;
lastReturned = e;
return e;
Expand Down
7 changes: 3 additions & 4 deletions lib/src/collection/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import 'package:dart_kollection/src/extension/list_extension_mixin.dart';
import 'package:dart_kollection/src/util/hash.dart';

/**
* [KList] based on a dart [List]
* [KList] implementation based on a dart [List]
*/
class DartList<T>
with
KIterableExtensionsMixin<T>,
KCollectionExtensionMixin<T>,
KListExtensionsMixin<T>
implements KList<T> {
/// Create an immutable [KList] by copying the incoming [iterable] into a [List]
DartList([Iterable<T> iterable = const []])
:
// copy list to prevent external modification
_list = List.from(iterable, growable: false),
: _list = List.from(iterable, growable: false),
super();

final List<T> _list;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/collection/list_empty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class EmptyList<T>
return true;
}());
throw IndexOutOfBoundsException(
"Empty list doesn't contain element at index $index.");
"Empty list doesn't contain element at index: $index.");
}

@override
Expand All @@ -42,7 +42,7 @@ class EmptyList<T>
return true;
}());
throw IndexOutOfBoundsException(
"Empty list doesn't contain element at index $index.");
"Empty list doesn't contain element at index: $index.");
}

@override
Expand Down
3 changes: 2 additions & 1 deletion lib/src/collection/list_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class DartMutableList<T>
return true;
}());
if (index < 0 || index >= size) {
throw IndexOutOfBoundsException("index: $index, size: $size");
throw IndexOutOfBoundsException(
"List doesn't contain element at index: $index, size: $size");
}
return _list[index];
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/collection/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DartMap<K, V> with KMapExtensionsMixin<K, V> implements KMap<K, V> {

@override
KSet<KMapEntry<K, V>> get entries =>
setOf(_map.entries.map((entry) => _Entry.from(entry)));
setFrom(_map.entries.map((entry) => _Entry.from(entry)));

@override
V get(K key) => _map[key];
Expand All @@ -38,13 +38,13 @@ class DartMap<K, V> with KMapExtensionsMixin<K, V> implements KMap<K, V> {
bool isEmpty() => _map.isEmpty;

@override
KSet<K> get keys => setOf(_map.keys);
KSet<K> get keys => setFrom(_map.keys);

@override
int get size => _map.length;

@override
KCollection<V> get values => listOf(_map.values);
KCollection<V> get values => listFrom(_map.values);

@override
bool operator ==(dynamic other) {
Expand Down
Loading