Skip to content

Commit

Permalink
+2
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Jan 17, 2024
1 parent 6bfd6c8 commit f3107dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
54 changes: 27 additions & 27 deletions range2/range2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace $ {

const list = $mol_range2( index => ( ++ calls , index ) , ()=> 10 )

$mol_assert_ok( list instanceof Array )
$mol_assert_equal( true, list instanceof Array )
$mol_assert_equal( list.length , 10 )

$mol_assert_equal( list[-1] , undefined )
Expand Down Expand Up @@ -81,6 +81,17 @@ namespace $ {

} ,

'reduce'() {

let calls = 0

const list = $mol_range2().slice( 1 , 6 )

$mol_assert_equal( list.reduce( ( s , v )=> s + v ) , 15 )
$mol_assert_equal( list.reduce( ( s , v )=> s + v , 5 ) , 20 )

} ,

'lazy concat'() {

let calls1 = 0
Expand All @@ -91,7 +102,7 @@ namespace $ {
$mol_range2( index => ( ++ calls2 , index ) , ()=> 5 ) ,
)

$mol_assert_ok( list instanceof Array )
$mol_assert_equal( true, list instanceof Array )
$mol_assert_equal( list.length , 15 )

$mol_assert_equal( list[0] , 0 )
Expand All @@ -107,13 +118,13 @@ namespace $ {

} ,

'filter'() {
'lazy filter'() {

let calls = 0

const list = $mol_range2( index => ( ++ calls , index ) , ()=> 15 ).filter( v => v % 2 ).slice( 0 , 3 )

$mol_assert_ok( list instanceof Array )
$mol_assert_equal( true, list instanceof Array )
$mol_assert_equal( list.length , 3 )

$mol_assert_equal( list[0] , 1 )
Expand All @@ -124,12 +135,12 @@ namespace $ {

} ,

'reverse'() {
'lazy reverse'() {
let calls = 0

const list = $mol_range2( index => ( ++ calls , index ) , ()=> 10 ).toReversed().slice(0, 3)

$mol_assert_ok( list instanceof Array )
$mol_assert_equal( true, list instanceof Array )
$mol_assert_equal( list.length , 3 )

$mol_assert_equal( list[0] , 9 )
Expand All @@ -139,17 +150,6 @@ namespace $ {

} ,

'reduce'() {

let calls = 0

const list = $mol_range2().slice( 1 , 6 )

$mol_assert_equal( list.reduce( ( s , v )=> s + v ) , 15 )
$mol_assert_equal( list.reduce( ( s , v )=> s + v , 5 ) , 20 )

} ,

'lazy map'() {

let calls1 = 0
Expand All @@ -165,7 +165,7 @@ namespace $ {
()=> 5 ,
)

$mol_assert_ok( target instanceof Array )
$mol_assert_equal( true, target instanceof Array )
$mol_assert_equal( target.length , 5 )

$mol_assert_equal( target[0] , 10 )
Expand All @@ -183,7 +183,7 @@ namespace $ {

const list = $mol_range2( index => ( ++ calls , index ) , ()=> 10 ).slice( 3 , 7 )

$mol_assert_ok( list instanceof Array )
$mol_assert_equal( true, list instanceof Array )
$mol_assert_equal( list.length , 4 )

$mol_assert_equal( list[0] , 3 )
Expand All @@ -198,33 +198,33 @@ namespace $ {

let calls = 0

$mol_assert_ok( $mol_range2( index => ( ++ calls , index ) , ()=> 5 ).some( v => v >= 2 ) )
$mol_assert_equal( true, $mol_range2( index => ( ++ calls , index ) , ()=> 5 ).some( v => v >= 2 ) )

$mol_assert_equal( calls , 3 )

$mol_assert_not( $mol_range2( i => i , ()=> 0 ).some( v => true ) )
$mol_assert_ok( $mol_range2( i => i ).some( v => v > 5 ) )
$mol_assert_equal( false, $mol_range2( i => i , ()=> 0 ).some( v => true ) )
$mol_assert_equal( true, $mol_range2( i => i ).some( v => v > 5 ) )
} ,

'lazy every'() {

let calls = 0

$mol_assert_not( $mol_range2( index => ( ++ calls , index ) , ()=> 5 ).every( v => v < 2 ) )
$mol_assert_equal( false, $mol_range2( index => ( ++ calls , index ) , ()=> 5 ).every( v => v < 2 ) )

$mol_assert_equal( calls , 3 )

$mol_assert_ok( $mol_range2( i => i , ()=> 0 ).every( v => false ) )
$mol_assert_not( $mol_range2( i => i ).every( v => v < 5 ) )
$mol_assert_equal( true, $mol_range2( i => i , ()=> 0 ).every( v => false ) )
$mol_assert_equal( false, $mol_range2( i => i ).every( v => v < 5 ) )
} ,

'lazyfy'() {

let calls = 0

const list = new $mol_range2_array( ... [ 0 , 1 , 2 , 3 , 4 , 5 ] ).map( i => ( ++ calls , i + 10 ) ).slice( 2 )
const list = $mol_range2([ 0 , 1 , 2 , 3 , 4 , 5 ]).map( i => ( ++ calls , i + 10 ) ).slice( 2 )

$mol_assert_ok( list instanceof Array )
$mol_assert_equal( true, list instanceof Array )
$mol_assert_equal( list.length , 4 )

$mol_assert_equal( calls , 0 )
Expand Down
17 changes: 12 additions & 5 deletions range2/range2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ namespace $ {

/** Lazy computed lists with native Array interface. $mol_range2_array is mutable but all derived ranges are immutable. */
export function $mol_range2< Item = number >(
item : ( index : number )=> Item = index => index as any ,
item: Item[] | ( ( index : number )=> Item ) = index => index as any ,
size = ()=> Number.POSITIVE_INFINITY ,
) : Item[] {
): Item[] {

const source = typeof item === 'function' ? new $mol_range2_array< Item >() : item

if( typeof item !== 'function' ) {
item = index => source[ index ]
size = ()=> source.length
}

return new Proxy( new $mol_range2_array< Item >() , {
return new Proxy( source , {

get( target , field ) {

Expand All @@ -16,10 +23,10 @@ namespace $ {
const index = Number( field )
if( index < 0 ) return undefined
if( index >= size() ) return undefined
if( index === Math.trunc( index ) ) return item( index )
if( index === Math.trunc( index ) ) return ( item as any )( index )
}

return target[ field as any ]
return $mol_range2_array.prototype[ field as any ]
} ,

set( target , field ) {
Expand Down

0 comments on commit f3107dc

Please sign in to comment.