Skip to content

Commit

Permalink
Merge dcfccb7 into 79b3b68
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed Sep 21, 2019
2 parents 79b3b68 + dcfccb7 commit b752218
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 213 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
language: dart
dart:
- "2.4.0"
script: pub run grinder start
after_success: pub run grinder finish
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## 0.0.7
- `ZRange` renamed to `NumRange`
- `integers` function added

## 0.0.6
- `ZRange`: firstValue, lastValue, length calculation fixed

Expand Down
32 changes: 25 additions & 7 deletions README.md
@@ -1,18 +1,21 @@
[![Build Status](https://travis-ci.com/gyrdym/xrange.svg?branch=master)](https://travis-ci.com/gyrdym/xrange)
[![Coverage Status](https://coveralls.io/repos/github/gyrdym/xrange/badge.svg?branch=master)](https://coveralls.io/github/gyrdym/xrange?branch=master)
[![pub package](https://img.shields.io/pub/v/xrange.svg)](https://pub.dartlang.org/packages/xrange)
[![Gitter Chat](https://badges.gitter.im/gyrdym/gyrdym.svg)](https://gitter.im/gyrdym/)

This is a fork of [Interval library](https://github.com/seaneagan/interval)

## XRange lib

Provides the `Range` class, a contiguous set of values, and the `ZRange` class, that along with functionality of
`Range` class can also generate values of arithmetic progression in a specific diapason. This range can contain just
integer numbers, that's why `Z` is used as a prefix for the class name (the letter `Z` denotes the set
of all integers in mathematics).
Provides the `Range` class, a contiguous set of values, and the `NumRange` class, that along with functionality of
`Range` class can also generate values of arithmetic progression in a specific diapason.

If a range contains two values, it also contains all values between them. It may have an upper and lower bound,
and those bounds may be open or closed.

Also the library contains `integers` generator function, that is based on `NumRange` class. It produces integer values
from a specific closed diapason.

## Usage

### Range
Expand All @@ -33,13 +36,13 @@ void main() {
}
```

### ZRange
### NumRange

```dart
import 'package:xrange/zrange.dart';
import 'package:xrange/num_range.dart';
void main() {
final range = ZRange.closed(-10, 10);
final range = NumRange.closed(-10, 10);
for (final value in range.values(step: 2)) {
print(value); // it yields numbers from -10 to 10 with step equals 2
Expand All @@ -48,3 +51,18 @@ void main() {
```

Pay attention to `values` method - it is a generator function, so use all the benefits of this.

### integers

````dart
import 'package:xrange/integers.dart';
void main() {
for (final value in integers(-10, 10)) {
print(value); // it yields numbers from -10 to 10 with step equals 2
}
}
````

The `integers` function returns a lazy iterable, thus it consumes little memory, since the whole collection is not
being generated when `integers` is called.
54 changes: 1 addition & 53 deletions analysis_options.yaml
@@ -1,53 +1 @@
include: package:pedantic/analysis_options.yaml

analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
errors:
missing_return: error
dead_code: error
duplicate_import: error
todo: ignore

linter:
rules:
- always_declare_return_types
- avoid_relative_lib_imports
- avoid_types_as_parameter_names
- empty_statements
- hash_and_equals
- iterable_contains_unrelated_type
- no_duplicate_case_values
- unnecessary_statements
- unrelated_type_equality_checks
- always_put_required_named_parameters_first
- avoid_classes_with_only_static_members
- avoid_renaming_method_parameters
- avoid_return_types_on_setters
- constant_identifier_names
- empty_catches
- empty_constructor_bodies
- file_names
- implementation_imports
- non_constant_identifier_names
- parameter_assignments
- prefer_conditional_assignment
- prefer_const_constructors
- prefer_equal_for_default_values
- prefer_expression_function_bodies
- prefer_final_locals
- prefer_is_empty
- prefer_is_not_empty
- prefer_single_quotes
- sort_unnamed_constructors_first
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_this
- camel_case_types
- annotate_overrides
- avoid_init_to_null
- sort_constructors_first
- avoid_empty_else
- unnecessary_const
- unnecessary_new
include: package:ml_tech/analysis_options.yaml
4 changes: 2 additions & 2 deletions example/main.dart
@@ -1,5 +1,5 @@
import 'package:xrange/range.dart';
import 'package:xrange/zrange.dart';
import 'package:xrange/num_range.dart';

void range() {
final date1 = DateTime(2015);
Expand All @@ -14,7 +14,7 @@ void range() {
}

void zrange() {
final range = ZRange.closed(-10, 10);
final range = NumRange.closed(-10, 10);
for (final value in range.values(step: 2)) {
print(value); // it yields numbers from -10 to 10 with step equals 2
}
Expand Down
6 changes: 6 additions & 0 deletions lib/integers.dart
@@ -0,0 +1,6 @@
import 'package:xrange/num_range.dart';

Iterable<int> integers(int start, int end, {int step = 1}) =>
NumRange.closed(start, end)
.values(step: step)
.map((el) => el.toInt());
56 changes: 56 additions & 0 deletions lib/num_range.dart
@@ -0,0 +1,56 @@
import 'package:xrange/range.dart';

class NumRange extends Range<num> {
NumRange({num lower, num upper, bool lowerClosed, bool upperClosed}) :
super(lower: lower, upper: upper, lowerClosed: lowerClosed,
upperClosed: upperClosed);

NumRange.open(num lower, num upper) : super.open(lower, upper);

NumRange.closed(num lower, num upper) : super.closed(lower, upper);

NumRange.openClosed(num lower, num upper) : super.openClosed(lower, upper);

NumRange.closedOpen(num lower, num upper) : super.closedOpen(lower, upper);

NumRange.atLeast(num lower) : super.atLeast(lower);

NumRange.atMost(num upper) : super.atMost(upper);

NumRange.greaterThan(num lower) : super.greaterThan(lower);

NumRange.lessThan(num upper) : super.lessThan(upper);

NumRange.all() : super.all();

NumRange.singleton(num value) : super.singleton(value);

int get length => !bounded ? null : lastValue - firstValue + 1;

int get firstValue {
if (lower == null) {
return null;
}
return (lowerClosed ? lower : lower + 1).toInt();
}

int get lastValue {
if (upper == null) {
return null;
}
return (upperClosed ? upper : upper - 1).toInt();
}

Iterable<num> values({int step = 1}) sync* {
if (step <= 0) {
throw ArgumentError.value(step, 'A step should be greater than 0');
}
if (!bounded) {
throw Exception('There is no bound, '
'${lower == null ? '`lower`' : '`upper`'} is not defined');
}
for (int val = firstValue; val <= lastValue; val += step) {
yield val;
}
}
}
3 changes: 3 additions & 0 deletions lib/xrange.dart
@@ -0,0 +1,3 @@
export 'package:xrange/integers.dart';
export 'package:xrange/num_range.dart';
export 'package:xrange/range.dart';
54 changes: 0 additions & 54 deletions lib/zrange.dart

This file was deleted.

0 comments on commit b752218

Please sign in to comment.