Skip to content

Commit

Permalink
refs #2: introduce the new ToOrderable type synonym, use it for sortO…
Browse files Browse the repository at this point in the history
…n, maxOn, minOn, improving maxOn & minOn since you can now max or min by string not only number anymore.
  • Loading branch information
emmanueltouzery committed Mar 21, 2018
1 parent 85c3d5f commit 7ea60a7
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 45 deletions.
6 changes: 3 additions & 3 deletions scripts/make_doc_extra/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const CATEGORIES = Vector.of<[string,Vector<string>]>(
["Control", Vector.of(
"Either", "Option", "Lazy", "Function", "Predicate")],
["Collection", Vector.of(
"Collection", "Foldable",
"Collection", "Foldable",
"IMap","HashMap", "ISet", "HashSet",
"Seq", "LinkedList", "Stream", "Vector",
"Tuple2", "SortOnSpec", "SortBySpec")],
"Seq", "LinkedList", "Stream", "Vector",
"Tuple2", "SortOnSpec", "SortBySpec", "ToOrderable")],
["Core", Vector.of(
"Comparison", "Value", "Contract")]);

Expand Down
5 changes: 3 additions & 2 deletions src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Value } from "./Value";
import { Option } from "./Option";
import { HashMap } from "./HashMap";
import { Foldable } from "./Foldable";
import { ToOrderable } from "./Seq";

export interface Collection<T> extends Value, Iterable<T>, Foldable<T> {

Expand Down Expand Up @@ -98,7 +99,7 @@ export interface Collection<T> extends Value, Iterable<T>, Foldable<T> {
*
* also see [[Collection.minBy]]
*/
minOn(getNumber: (v:T)=>number): Option<T>;
minOn(getNumber: ToOrderable<T>): Option<T>;

/**
* Compare values in the collection and return the largest element.
Expand All @@ -115,7 +116,7 @@ export interface Collection<T> extends Value, Iterable<T>, Foldable<T> {
*
* also see [[Collection.maxBy]]
*/
maxOn(getNumber: (v:T)=>number): Option<T>;
maxOn(getSortable: ToOrderable<T>): Option<T>;

/**
* Call the function you give for each element in the collection
Expand Down
9 changes: 5 additions & 4 deletions src/HashSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Option } from "./Option";
import { WithEquality, hasEquals, HasEquals,
getHashCode, areEqual, Ordering } from "./Comparison";
import * as SeqHelpers from "./SeqHelpers";
import { ToOrderable } from "./Seq";
import { contractTrueEquality } from "./Contract";
const hamt: any = require("hamt_plus");

Expand Down Expand Up @@ -451,8 +452,8 @@ export class HashSet<T> implements ISet<T> {
*
* also see [[HashSet.minBy]]
*/
minOn(getNumber: (v:T)=>number): Option<T> {
return SeqHelpers.minOn(this, getNumber);
minOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.minOn(this, getOrderable);
}

/**
Expand All @@ -472,8 +473,8 @@ export class HashSet<T> implements ISet<T> {
*
* also see [[HashSet.maxBy]]
*/
maxOn(getNumber: (v:T)=>number): Option<T> {
return SeqHelpers.maxOn(this, getNumber);
maxOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.maxOn(this, getOrderable);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/ISet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { Collection } from "./Collection";
import { Vector } from "./Vector";
import { LinkedList } from "./LinkedList";
import { Option } from "./Option";
import { ToOrderable } from "./Seq";

/**
* Ability to specify a sorting function.
* See [[Seq.sortOn]].
*
* `{sortOn: ((v:T)=>number)|((v:T)=>string)}`
*/
export type SortOnSpec<T> = {sortOn: ((v:T)=>number)|((v:T)=>string)};
export type SortOnSpec<T> = {sortOn: ToOrderable<T>};

/**
* Ability to specify a sorting function.
Expand Down
22 changes: 11 additions & 11 deletions src/LinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { IMap } from "./IMap";
import { HashMap } from "./HashMap";
import { ISet } from "./ISet";
import { HashSet } from "./HashSet";
import { Seq } from "./Seq";
import { Seq, ToOrderable } from "./Seq";
import { Stream } from "./Stream";
import * as SeqHelpers from "./SeqHelpers";

Expand Down Expand Up @@ -535,7 +535,7 @@ export class EmptyLinkedList<T> implements Seq<T> {
*
* also see [[ConsLinkedList.sortBy]]
*/
sortOn(getKey: ((v:T)=>number)|((v:T)=>string)): LinkedList<T> {
sortOn(getKey: ToOrderable<T>): LinkedList<T> {
return this;
}

Expand Down Expand Up @@ -584,8 +584,8 @@ export class EmptyLinkedList<T> implements Seq<T> {
*
* also see [[ConsLinkedList.minBy]]
*/
minOn(getNumber: (v:T)=>number): Option<T> {
return SeqHelpers.minOn(this, getNumber);
minOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.minOn(this, getOrderable);
}

/**
Expand All @@ -605,8 +605,8 @@ export class EmptyLinkedList<T> implements Seq<T> {
*
* also see [[ConsLinkedList.maxBy]]
*/
maxOn(getNumber: (v:T)=>number): Option<T> {
return SeqHelpers.maxOn(this, getNumber);
maxOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.maxOn(this, getOrderable);
}

/**
Expand Down Expand Up @@ -1295,7 +1295,7 @@ export class ConsLinkedList<T> implements Seq<T> {
*
* also see [[ConsLinkedList.sortBy]]
*/
sortOn(getKey: ((v:T)=>number)|((v:T)=>string)): LinkedList<T> {
sortOn(getKey: ToOrderable<T>): LinkedList<T> {
return <LinkedList<T>>SeqHelpers.sortOn<T>(this, getKey);
}

Expand Down Expand Up @@ -1349,8 +1349,8 @@ export class ConsLinkedList<T> implements Seq<T> {
*
* also see [[ConsLinkedList.minBy]]
*/
minOn(getNumber: (v:T)=>number): Option<T> {
return SeqHelpers.minOn(this, getNumber);
minOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.minOn(this, getOrderable);
}

/**
Expand All @@ -1370,8 +1370,8 @@ export class ConsLinkedList<T> implements Seq<T> {
*
* also see [[ConsLinkedList.maxBy]]
*/
maxOn(getNumber: (v:T)=>number): Option<T> {
return SeqHelpers.maxOn(this, getNumber);
maxOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.maxOn(this, getOrderable);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/Seq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { Option } from "./Option";
import { Collection } from "./Collection";
import { Stream } from "./Stream";

/**
* Sorting function for type T: function
* to convert this type to a type which is natively
* sortable in javascript, that is string or number.
* `((v:T)=>number)|((v:T)=>string)`
*/
export type ToOrderable<T> = ((v:T)=>number)|((v:T)=>string);

/**
* A generic interface for list-like implementations.
* @param T the item type
Expand Down Expand Up @@ -103,7 +111,7 @@ export interface Seq<T> extends Collection<T> {
*
* also see [[Seq.sortBy]]
*/
sortOn(getKey: ((v:T)=>number)|((v:T)=>string)): Seq<T>;
sortOn(getKey: ToOrderable<T>): Seq<T>;

/**
* Prepend an element at the beginning of the collection.
Expand Down
16 changes: 8 additions & 8 deletions src/SeqHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Option } from "./Option";
import { WithEquality, hasTrueEquality, Ordering } from "./Comparison";
import { HashMap } from "./HashMap";
import { Seq } from "./Seq";
import { Seq, ToOrderable } from "./Seq";
import { Collection } from "./Collection";
import { Stream, ConsStream } from "./Stream";
import { Lazy } from "./Lazy";
Expand Down Expand Up @@ -56,7 +56,7 @@ export function zipWithIndex<T>(seq: Seq<T>): Seq<[T,number]> {
/**
* @hidden
*/
export function sortOn<T>(seq: Seq<T>, getKey: ((v:T)=>number)|((v:T)=>string)): Seq<T> {
export function sortOn<T>(seq: Seq<T>, getKey: ToOrderable<T>): Seq<T> {
return seq.sortBy((x,y) => {
const a = getKey(x);
const b = getKey(y);
Expand Down Expand Up @@ -113,16 +113,16 @@ export function minBy<T>(coll: Collection<T>, compare: (v1:T,v2:T)=>Ordering): O
/**
* @hidden
*/
export function minOn<T>(coll: Collection<T>, getNumber: (v:T)=>number): Option<T> {
export function minOn<T>(coll: Collection<T>, getSortable: ToOrderable<T>): Option<T> {
if (coll.isEmpty()) {
return Option.none<T>();
}
let iter = coll[Symbol.iterator]();
let step = iter.next();
let val = getNumber(step.value);
let val = getSortable(step.value);
let result = step.value;
while (!(step = iter.next()).done) {
const curVal = getNumber(step.value);
const curVal = getSortable(step.value);
if (curVal < val) {
val = curVal;
result = step.value;
Expand All @@ -141,16 +141,16 @@ export function maxBy<T>(coll: Collection<T>, compare: (v1:T,v2:T)=>Ordering): O
/**
* @hidden
*/
export function maxOn<T>(coll: Collection<T>, getNumber: (v:T)=>number): Option<T> {
export function maxOn<T>(coll: Collection<T>, getSortable: ToOrderable<T>): Option<T> {
if (coll.isEmpty()) {
return Option.none<T>();
}
let iter = coll[Symbol.iterator]();
let step = iter.next();
let val = getNumber(step.value);
let val = getSortable(step.value);
let result = step.value;
while (!(step = iter.next()).done) {
const curVal = getNumber(step.value);
const curVal = getSortable(step.value);
if (curVal > val) {
val = curVal;
result = step.value;
Expand Down
18 changes: 9 additions & 9 deletions src/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { IMap } from "./IMap";
import { HashMap } from "./HashMap";
import { ISet } from "./ISet";
import { HashSet } from "./HashSet";
import { Seq } from "./Seq";
import { Seq, ToOrderable } from "./Seq";
import { Lazy } from "./Lazy";
import { LinkedList } from "./LinkedList";
import * as SeqHelpers from "./SeqHelpers";
Expand Down Expand Up @@ -591,7 +591,7 @@ export class EmptyStream<T> implements Seq<T> {
*
* also see [[ConsStream.sortBy]]
*/
sortOn(getKey: ((v:T)=>number)|((v:T)=>string)): Stream<T> {
sortOn(getKey: ToOrderable<T>): Stream<T> {
return this;
}

Expand Down Expand Up @@ -640,7 +640,7 @@ export class EmptyStream<T> implements Seq<T> {
*
* also see [[ConsStream.minBy]]
*/
minOn(getNumber: (v:T)=>number): Option<T> {
minOn(getOrderable: ToOrderable<T>): Option<T> {
return Option.none<T>();
}

Expand All @@ -661,7 +661,7 @@ export class EmptyStream<T> implements Seq<T> {
*
* also see [[ConsStream.maxBy]]
*/
maxOn(getNumber: (v:T)=>number): Option<T> {
maxOn(getOrderable: ToOrderable<T>): Option<T> {
return Option.none<T>();
}

Expand Down Expand Up @@ -1338,7 +1338,7 @@ export class ConsStream<T> implements Seq<T> {
*
* also see [[ConsStream.sortBy]]
*/
sortOn(getKey: ((v:T)=>number)|((v:T)=>string)): Stream<T> {
sortOn(getKey: ToOrderable<T>): Stream<T> {
return <Stream<T>>SeqHelpers.sortOn<T>(this, getKey);
}

Expand Down Expand Up @@ -1392,8 +1392,8 @@ export class ConsStream<T> implements Seq<T> {
*
* also see [[ConsStream.minBy]]
*/
minOn(getNumber: (v:T)=>number): Option<T> {
return SeqHelpers.minOn(this, getNumber);
minOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.minOn(this, getOrderable);
}

/**
Expand All @@ -1413,8 +1413,8 @@ export class ConsStream<T> implements Seq<T> {
*
* also see [[ConsStream.maxBy]]
*/
maxOn(getNumber: (v:T)=>number): Option<T> {
return SeqHelpers.maxOn(this, getNumber);
maxOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.maxOn(this, getOrderable);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HashMap } from "./HashMap";
import { HashSet } from "./HashSet";
import { IMap } from "./IMap";
import { Stream } from "./Stream";
import { Seq } from "./Seq";
import { Seq, ToOrderable } from "./Seq";
import { WithEquality, areEqual, getHashCode,
Ordering } from "./Comparison";
import { Collection } from "./Collection";
Expand Down Expand Up @@ -836,7 +836,7 @@ export class Vector<T> implements Seq<T> {
*
* also see [[Vector.sortBy]]
*/
sortOn(getKey: ((v:T)=>number)|((v:T)=>string)): Vector<T> {
sortOn(getKey: ToOrderable<T>): Vector<T> {
return <Vector<T>>SeqHelpers.sortOn<T>(this, getKey);
}

Expand Down Expand Up @@ -1132,8 +1132,8 @@ export class Vector<T> implements Seq<T> {
*
* also see [[Vector.minBy]]
*/
minOn(getNumber: (v:T)=>number): Option<T> {
return SeqHelpers.minOn(this, getNumber);
minOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.minOn(this, getOrderable);
}

/**
Expand All @@ -1153,8 +1153,8 @@ export class Vector<T> implements Seq<T> {
*
* also see [[Vector.maxBy]]
*/
maxOn(getNumber: (v:T)=>number): Option<T> {
return SeqHelpers.maxOn(this, getNumber);
maxOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.maxOn(this, getOrderable);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Seq<T>.equals is a lot less type-precise than Vector<T>.equals, so I'd rather
// the users use concrete types.
export * from "./Option";
export { ToOrderable } from "./Seq";
export * from "./Either";
export * from "./Lazy";
export * from "./Vector";
Expand Down

0 comments on commit 7ea60a7

Please sign in to comment.