Skip to content

Commit

Permalink
Reverse function now accepts unidrectional unknown-bounds inputs
Browse files Browse the repository at this point in the history
Previously it would always cause an arguments error

Related to #161
  • Loading branch information
pineapplemachine committed Nov 20, 2017
1 parent ebb49f6 commit 8fcc3c8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
17 changes: 17 additions & 0 deletions src/core/expecting.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,23 @@ export const expecting = {
return sequence;
},
}),
notUnidirectionalUnboundedSequence: Expecting({
type: "sequence",
article: "a",
singular: "bidirectional or not-known-unbounded sequence",
plural: "bidirectional or not-known-unbounded sequences",
adjective: "bidirectional or not-known-unbounded",
short: "sequence",
sequence: true,
transforms: true,
validate: value => {
const sequence = asSequence(value);
if(!sequence || (sequence.unbounded() && !sequence.back)){
throw new Error();
}
return sequence;
},
}),
exactly: option => Expecting({
article: "exactly",
singular: String(option),
Expand Down
20 changes: 9 additions & 11 deletions src/functions/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ export const ReverseSequence = defineSequence({
// Implementation for reversing a known-bounded unidirectional sequence.
export const ReverseOnDemandSequence = function(source){
return new OnDemandSequence(ReverseSequence.appliedTo(ArraySequence), {
bounded: () => true,
unbounded: () => false,
bounded: () => source.bounded(),
unbounded: () => source.unbounded(),
done: () => source.done(),
back: () => source.front(),
length: source.nativeLength ? () => source.nativeLength() : undefined,
Expand Down Expand Up @@ -161,15 +161,12 @@ export const reverse = wrap({
ReverseSequence
],
arguments: {
one: wrap.expecting.either(
wrap.expecting.bidirectionalSequence,
wrap.expecting.boundedSequence
),
one: wrap.expecting.notUnidirectionalUnboundedSequence,
},
implementation: function reverse(source){
if(source.back){
return new ReverseSequence(source);
}else{ // Argument validation implies source.bounded()
}else{
return ReverseOnDemandSequence(source);
}
},
Expand Down Expand Up @@ -197,14 +194,15 @@ export const reverse = wrap({
hi.assert(seq.startsWith("olleholleholleh"));
hi.assert(seq.endsWith("olleholleholleh"));
},
"notKnownBoundedUnidirectionalInput": hi => {
const seq = () => hi.recur(i => i + 1).seed(1).until(i => i >= 8);
hi.assertEqual(seq(), [1, 2, 3, 4, 5, 6, 7]);
hi.assertEqual(seq().reverse(), [7, 6, 5, 4, 3, 2, 1]);
},
"unboundedUnidirectionalInput": hi => {
const seq = hi.recur(i => i + "!").seed("hello");
hi.assertFail(() => seq.reverse());
},
"notKnownBoundedUnidirectionalInput": hi => {
const seq = hi.recur(i => i + 1).seed(1).until(i => i >= 8);
hi.assertFail(() => seq.reverse());
},
},
});

Expand Down

0 comments on commit 8fcc3c8

Please sign in to comment.