Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions examples/denise/test_stack_new_methods.wx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

#Import "<std>"

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<Int>( 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<Int>(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
80 changes: 80 additions & 0 deletions modules/std/collections/stack.wx
Original file line number Diff line number Diff line change
Expand Up @@ -903,4 +903,84 @@ Class Stack<T> Implements IContainer<T>
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>: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
10 changes: 10 additions & 0 deletions modules/wonkey/native/wxarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ template<class T,int D> struct wxArray{
for( int i=0;i<count;++i ) dst.data()[dstOffset+i]=data()[offset+i];
}
}

wxString toString()const{
wxString result=wxDBType<T>()+"[ ";
int n = length();
for ( int i=0;i<n;++i) {
if (i>0) result+=", ";
result+=wxString(_rep->_data[i]);
}
return result+=" ]";
}
};

template<class T,int D> wxString wxDBType( wxArray<T,D> *p ){
Expand Down
6 changes: 5 additions & 1 deletion modules/wonkey/types.wx
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,11 @@ Struct @Array<T>

#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.
Expand Down
Loading