-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fromColumn, rowsWhere, rowsAt & Column class
- Loading branch information
Showing
6 changed files
with
155 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
library koala; | ||
|
||
export 'src/dataframe.dart'; | ||
export 'src/column.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import 'package:collection/collection.dart'; | ||
|
||
import 'list_extensions/extended_list_base.dart'; | ||
|
||
typedef Mask = List<bool>; | ||
|
||
extension MaskExtensions on Mask{ | ||
Mask operator&(Mask other) => | ||
IterableZip([this, other]).map((e) => e.first && e.last).toList(); | ||
|
||
Mask operator|(Mask other) => | ||
IterableZip([this, other]).map((e) => e.first | e.last).toList(); | ||
|
||
Mask operator^(Mask other) => | ||
IterableZip([this, other]).map((e) => e.first ^ e.last).toList(); | ||
} | ||
|
||
class Column<E> extends ExtendedListBase<E>{ | ||
Column(List<E> records): super(records); | ||
|
||
Column<T> cast<T>() => | ||
Column(super.cast<T>()); | ||
|
||
// ************* count ***************** | ||
|
||
/// Count number of occurrences of [element] of the column [colName]. | ||
int count(E object) => | ||
where((element) => element == object).length; | ||
|
||
/// Count number of occurrences of values, corresponding to the column [colName], | ||
/// equaling any element contained by [pool]. | ||
int countElementOccurrencesOf(Set<E?> pool) => | ||
where((element) => pool.contains(element)).length; | ||
|
||
// ************* null freeing ************* | ||
|
||
Column<E> nullFreed({E? replaceWith = null}) => | ||
Column<E>(nullFreedIterable(replaceWith: replaceWith).toList()); | ||
|
||
Iterable<E> nullFreedIterable({E? replaceWith = null}) => | ||
replaceWith == null | ||
? where((element) => element != null) | ||
: map((e) => e ?? replaceWith); | ||
|
||
// ****************** transformation ****************** | ||
|
||
List<num> cumSum() => _nullFreedNums().fold( | ||
[], | ||
(sums, element) => | ||
sums..add(sums.isEmpty ? element : sums.last + element)); | ||
|
||
// **************** accumulation **************** | ||
|
||
double mean({bool treatNullsAsZeros = true}) => | ||
_nullFreedNums(treatNullsAsZeros: treatNullsAsZeros).average; | ||
|
||
num max() => | ||
_nullFreedNums().max; | ||
|
||
num min() => | ||
_nullFreedNums().min; | ||
|
||
num sum() => | ||
_nullFreedNums().sum; | ||
|
||
Iterable<num> _nullFreedNums({bool treatNullsAsZeros = false}) => | ||
cast<num?>().nullFreedIterable(replaceWith: treatNullsAsZeros ? 0.0 : null) | ||
.cast<num>(); | ||
|
||
// ***************** masks ******************* | ||
|
||
Mask equals(E reference) => | ||
map((element) => element == reference).toList().cast<bool>(); | ||
|
||
Mask unequals(E reference) => | ||
map((element) => element != reference).toList().cast<bool>(); | ||
|
||
Mask isIn(Set<E> pool) => | ||
map((element) => pool.contains(element)).toList().cast<bool>(); | ||
|
||
Mask isNotIn(Set<E> pool) => | ||
map((element) => !pool.contains(element)).toList().cast<bool>(); | ||
|
||
Mask toMask(bool Function(E) test) => | ||
map(test).toList().cast<bool>(); | ||
|
||
// ****************** numerical column masks ********************* | ||
|
||
Mask operator<(num reference) => | ||
cast<num>().map((element) => element < reference).toList().cast<bool>(); | ||
|
||
Mask operator>(num reference) => | ||
cast<num>().map((element) => element > reference).toList().cast<bool>(); | ||
|
||
Mask operator<=(num reference) => | ||
cast<num>().map((element) => element <= reference).toList().cast<bool>(); | ||
|
||
Mask operator>=(num reference) => | ||
cast<num>().map((element) => element >= reference).toList().cast<bool>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
import 'package:collection/collection.dart'; | ||
|
||
extension IterableExtensions<E> on Iterable<E> { | ||
List<E> toFixedLengthList() => toList(growable: false); | ||
|
||
Iterable<E> applyMask(List<bool> mask) => | ||
whereIndexed((index, _) => mask[index]); | ||
} | ||
|
||
extension IterableIterableExtensions<E> on Iterable<Iterable<E>>{ | ||
List<List<E>> transposed() => | ||
IterableZip(this).map((e) => e.toList()).toList(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'package:koala/src/utils/iterable.dart'; | ||
import 'package:test/expect.dart'; | ||
import 'package:test/scaffolding.dart'; | ||
|
||
void main(){ | ||
test('transposed', (){ | ||
expect([[1, 2, 3], [2, 3, 4]].transposed(), [[1, 2], [2, 3], [3, 4]]); | ||
}); | ||
} |