diff --git a/examples/denise/test_stack_new_methods.wx b/examples/denise/test_stack_new_methods.wx new file mode 100644 index 00000000..06526557 --- /dev/null +++ b/examples/denise/test_stack_new_methods.wx @@ -0,0 +1,111 @@ + +#Import "" + +Using std.. + +Function func_square:Int( it:Int ) + Return it*it +End + +Function Main() + + Local lamb_square := Lambda:Int( it:Int ) + Return it * it + End + + Local lamb_odd := Lambda:Bool( it:Int ) + Return it & 1 + End + + Local init_stack := New Stack( New Int[]( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) ) + + Print init_stack + Print "---------- MapIt -------------" + Local map1 := init_stack.MapIt( func_square ) + Local map2 := init_stack.MapIt( lamb_square ) + Local map3 := init_stack.MapIt( Lambda:String( it:Int ) + Return it * it + End) + + Print map1.ToString() + Print map2.ToString() + Print map3.ToString() + + Print "---------- FilterIt -------------" + Local fil1 := init_stack.FilterIt( lamb_odd ) + Print fil1.ToString() + + Print "---------- AllIt -------------" + Print init_stack.AllIt(Lambda:Bool( it:Int ) + Return it < 5 + End)+": all < 5 ?" + + Print init_stack.AllIt(Lambda:Bool( it:Int ) + Return it < 15 + End)+": all < 15 ?" + + Print init_stack.AllIt(Lambda:Bool( it:Int ) + Return it > 5 + End)+": all > 5 ?" + + Print init_stack.AllIt(Lambda:Bool( it:Int ) + Return it > 15 + End)+": all > 15 ?" + + Print "---------- AnyIt -------------" + Print init_stack.AnyIt(Lambda:Bool( it:Int ) + Return it > 5 + End)+": any > 5 ?" + + Print init_stack.AnyIt(Lambda:Bool( it:Int ) + Return it < 5 + End)+": any < 5 ?" + + Print init_stack.AnyIt(Lambda:Bool( it:Int ) + Return it > 15 + End)+": any > 15 ?" + + Print init_stack.AnyIt(Lambda:Bool( it:Int ) + Return it = 9 + End)+": any = 9 ?" + + Print init_stack.AnyIt(Lambda:Bool( it:Int ) + Return it = 10 + End)+": any = 10 ?" + + Print "---------- FoldItl (left)-------------" + Local fol := New Stack(New Int[]( 2, 3, 666 ) ) + Print fol.FoldItl( Lambda:Int( a:Int, b:Int ) + Return a + b + End) + " = sum " + fol + + Print fol.FoldItl( Lambda:Int( a:Int, b:Int ) + Return a - b + End) + " = sub " + fol + + Print fol.FoldItl( Lambda:Int( a:Int, b:Int ) + Return a * b + End) + " = mul " + fol + + Print fol.FoldItl( Lambda:Int( a:Int, b:Int ) + Return a / b + End) + " = div " + fol + + Print "---------- FoldItr (right)-------------" + Print fol.FoldItr( Lambda:Int( a:Int, b:Int ) + Return a + b + End) + " = sum " + fol + + Print fol.FoldItr( Lambda:Int( a:Int, b:Int ) + Return a - b + End) + " = sub " + fol + + Print fol.FoldItr( Lambda:Int( a:Int, b:Int ) + Return a * b + End) + " = mul " + fol + + Print fol.FoldItr( Lambda:Int( a:Int, b:Int ) + Return a / b + End) + " = div " + fol + +End \ No newline at end of file diff --git a/modules/std/collections/stack.wx b/modules/std/collections/stack.wx index 08b6bd24..347689e9 100644 --- a/modules/std/collections/stack.wx +++ b/modules/std/collections/stack.wx @@ -903,4 +903,84 @@ Class Stack Implements IContainer Sort( compare, x, hi, filter ) End + #rem wonkeydoc Iterates through a container and checks if every item fulfills the predicate. + #end + Method AllIt:Bool( fun:Bool( it:T ) ) + + DebugAssert( _length, "Stack is empty" ) + + For Local i := 0 Until _length + If Not fun( _data[i] ) Return False + Next + Return True + End + + #rem wonkeydoc Iterates through a container and checks if at least one item fulfills the predicate. + #end + Method AnyIt:Bool( fun:Bool( it:T ) ) + + DebugAssert( _length, "Stack is empty" ) + + For Local i := 0 Until _length + If fun( _data[i] ) Return True + Next + Return False + End + + #rem wonkeydoc Returns a new Array with the results of the op func applied to every item in the stack. + #end + Method MapIt:M[]( fun:M( it:T ) ) + + DebugAssert( _length, "Stack is empty" ) + + Local mm := New M[_length] + For Local i := 0 Until _length + mm[i] = fun( _data[i] ) + Next + Return mm + End + + #rem wonkeydoc Returns a new Array with all the items of stack that fulfill the predicate func. + #end + Method FilterIt:T[]( fun:Bool( it:T ) ) + + DebugAssert( _length, "Stack is empty" ) + + Local mm := New T[_length] + Local idx := 0 + For Local i := 0 Until _length + If fun( _data[i] ) + mm[idx] = _data[i] + idx += 1 + Endif + Next + Return mm.Slice( 0, idx ) + End + + #rem wonkeydoc Fold a sequence from left to right, returning the accumulation. + #end + Method FoldItl:T( fun:T( a:T, b:T ) ) + + DebugAssert( _length, "Stack is empty" ) + + Local result:T = _data[0] + For Local i := 1 Until _length + result = fun( result, _data[i] ) + Next + Return result + End + + #rem wonkeydoc Fold a sequence from right to left, returning the accumulation. + #end + Method FoldItr:T( fun:T( a:T, b:T ) ) + + DebugAssert( _length, "Stack is empty" ) + + Local result:T = _data[_length - 1] + For Local i := _length - 2 To 0 Step -1 + result = fun( result, _data[i] ) + Next + Return result + End + End diff --git a/modules/wonkey/native/wxarray.h b/modules/wonkey/native/wxarray.h index bb472455..9c29fd84 100644 --- a/modules/wonkey/native/wxarray.h +++ b/modules/wonkey/native/wxarray.h @@ -225,6 +225,16 @@ template struct wxArray{ for( int i=0;i()+"[ "; + int n = length(); + for ( int i=0;i0) result+=", "; + result+=wxString(_rep->_data[i]); + } + return result+=" ]"; + } }; template wxString wxDBType( wxArray *p ){ diff --git a/modules/wonkey/types.wx b/modules/wonkey/types.wx index 40e9a156..55c257fe 100644 --- a/modules/wonkey/types.wx +++ b/modules/wonkey/types.wx @@ -638,7 +638,11 @@ Struct @Array #end Method CopyTo( dstArray:T[],srcOffset:Int,dstOffset:Int,count:Int )="copyTo" - + + #rem wonkeydoc Return a printable representation of the array. + #end + Method ToString:String()="toString" + End #rem wonkeydoc Base class of all objects.