Skip to content

Commit

Permalink
add seq.zipWithIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanueltouzery committed Oct 6, 2017
1 parent 964100d commit 423f372
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ export abstract class List<T> implements Iterable<T>, Seq<T> {
*/
abstract zip<U>(other: Iterable<U>): List<[T,U]>;

/**
* Combine this collection with the index of the elements
* in it. Handy if you need the index when you map on
* the collection for instance:
*
* List.of("a","b").zipWithIndex().map([v,idx] => ...)
*/
zipWithIndex(): List<[T,number]> {
return <List<[T,number]>>SeqHelpers.zipWithIndex<T>(this);
}

/**
* Reverse the collection. For instance:
*
Expand Down Expand Up @@ -288,7 +299,7 @@ export abstract class List<T> implements Iterable<T>, Seq<T> {
* Randomly reorder the elements of the collection.
*/
shuffle(): List<T> {
return List.ofIterable(SeqHelpers.shuffle(this.toArray()));
return List.ofIterable<T>(SeqHelpers.shuffle(this.toArray()));
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Seq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ export interface Seq<T> extends Collection<T>, Foldable<T> {
*/
zip<U>(other: Iterable<U>): Seq<[T,U]>;

/**
* Combine this collection with the index of the elements
* in it. Handy if you need the index when you map on
* the collection for instance:
*
* Vector.of("a","b").zipWithIndex().map([v,idx] => ...)
*/
zipWithIndex(): Seq<[T,number]>;

/**
* Retrieve the element at index idx.
* Returns an option because the collection may
Expand Down
8 changes: 8 additions & 0 deletions src/SeqHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Option } from "./Option";
import { WithEquality, hasTrueEquality } from "./Comparison";
import { IMap } from "./IMap";
import { Seq } from "./Seq";
import { Stream } from "./Stream";

/**
* @hidden
Expand Down Expand Up @@ -41,3 +42,10 @@ export function arrangeBy<T,K>(seq: Seq<T>, getKey: (v:T)=>K&WithEquality): Opti
export function seqHasTrueEquality<T>(seq: Seq<T>): boolean {
return seq.find(x => x!=null).hasTrueEquality();
}

/**
* @hidden
*/
export function zipWithIndex<T>(seq: Seq<T>): Seq<[T,number]> {
return seq.zip<number>(Stream.iterate(0,i=>i+1));
}
11 changes: 11 additions & 0 deletions src/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@ export abstract class Stream<T> implements Iterable<T>, Seq<T> {
*/
abstract zip<U>(other: Iterable<U>): Stream<[T,U]>;

/**
* Combine this collection with the index of the elements
* in it. Handy if you need the index when you map on
* the collection for instance:
*
* Stream.of("a","b").zipWithIndex().map([v,idx] => ...)
*/
zipWithIndex(): Stream<[T,number]> {
return <Stream<[T,number]>>SeqHelpers.zipWithIndex<T>(this);
}

/**
* Reverse the collection. For instance:
*
Expand Down
11 changes: 11 additions & 0 deletions src/Vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,17 @@ export class Vector<T> implements Seq<T>, Iterable<T> {
}), 0);
}

/**
* Combine this collection with the index of the elements
* in it. Handy if you need the index when you map on
* the collection for instance:
*
* Vector.of("a","b").zipWithIndex().map([v,idx] => ...)
*/
zipWithIndex(): Vector<[T,number]> {
return <Vector<[T,number]>>SeqHelpers.zipWithIndex<T>(this);
}

/**
* Reverse the collection. For instance:
*
Expand Down

0 comments on commit 423f372

Please sign in to comment.