File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed
Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ function ArraySeq ( _array ) {
2+ var array = _array . slice ( ) ;
3+ var start = 0 ;
4+ var end = array . length - 1 ;
5+
6+ this . isEmpty = function ( ) {
7+ return end < start ;
8+ }
9+
10+ this . get = function ( idx ) {
11+ if ( idx >= start && idx <= end )
12+ return array [ idx ] ;
13+ else
14+ return "Index invalid!" ;
15+ }
16+
17+ this . indexOfTail = function ( ) {
18+ return end ;
19+ }
20+ }
21+
22+ function RangeSeq ( _st , _ed ) {
23+ var base = _st ;
24+ var start = 0 ;
25+ var end = _ed - _st ;
26+
27+ this . isEmpty = function ( ) {
28+ return end < start ;
29+ }
30+
31+ this . get = function ( idx ) {
32+ if ( idx >= start && idx <= end )
33+ return idx + base ;
34+ else
35+ return "Index invalid!" ;
36+ }
37+
38+ this . indexOfTail = function ( ) {
39+ return end ;
40+ }
41+ }
42+
43+ function logFive ( seq ) {
44+ if ( ! seq . isEmpty ( ) ) {
45+ var cnt = 0 ;
46+ var idx = 0 ;
47+ while ( true ) {
48+ console . log ( seq . get ( idx ) ) ;
49+ cnt ++ ;
50+ if ( cnt == 5 || idx == seq . indexOfTail ( ) ) break ;
51+ idx ++ ;
52+ }
53+ }
54+ }
55+
56+ logFive ( new ArraySeq ( [ 1 , 2 ] ) ) ;
57+ logFive ( new RangeSeq ( 100 , 1000 ) ) ;
You can’t perform that action at this time.
0 commit comments