Skip to content

Commit 3fbb922

Browse files
authored
Merge pull request #102 from denise-amiga/develop
Added Method ToString() to Arrays.
2 parents e8b6d7c + 95e476f commit 3fbb922

4 files changed

Lines changed: 206 additions & 1 deletion

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
#Import "<std>"
3+
4+
Using std..
5+
6+
Function func_square:Int( it:Int )
7+
Return it*it
8+
End
9+
10+
Function Main()
11+
12+
Local lamb_square := Lambda:Int( it:Int )
13+
Return it * it
14+
End
15+
16+
Local lamb_odd := Lambda:Bool( it:Int )
17+
Return it & 1
18+
End
19+
20+
Local init_stack := New Stack<Int>( New Int[]( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) )
21+
22+
Print init_stack
23+
Print "---------- MapIt -------------"
24+
Local map1 := init_stack.MapIt( func_square )
25+
Local map2 := init_stack.MapIt( lamb_square )
26+
Local map3 := init_stack.MapIt( Lambda:String( it:Int )
27+
Return it * it
28+
End)
29+
30+
Print map1.ToString()
31+
Print map2.ToString()
32+
Print map3.ToString()
33+
34+
Print "---------- FilterIt -------------"
35+
Local fil1 := init_stack.FilterIt( lamb_odd )
36+
Print fil1.ToString()
37+
38+
Print "---------- AllIt -------------"
39+
Print init_stack.AllIt(Lambda:Bool( it:Int )
40+
Return it < 5
41+
End)+": all < 5 ?"
42+
43+
Print init_stack.AllIt(Lambda:Bool( it:Int )
44+
Return it < 15
45+
End)+": all < 15 ?"
46+
47+
Print init_stack.AllIt(Lambda:Bool( it:Int )
48+
Return it > 5
49+
End)+": all > 5 ?"
50+
51+
Print init_stack.AllIt(Lambda:Bool( it:Int )
52+
Return it > 15
53+
End)+": all > 15 ?"
54+
55+
Print "---------- AnyIt -------------"
56+
Print init_stack.AnyIt(Lambda:Bool( it:Int )
57+
Return it > 5
58+
End)+": any > 5 ?"
59+
60+
Print init_stack.AnyIt(Lambda:Bool( it:Int )
61+
Return it < 5
62+
End)+": any < 5 ?"
63+
64+
Print init_stack.AnyIt(Lambda:Bool( it:Int )
65+
Return it > 15
66+
End)+": any > 15 ?"
67+
68+
Print init_stack.AnyIt(Lambda:Bool( it:Int )
69+
Return it = 9
70+
End)+": any = 9 ?"
71+
72+
Print init_stack.AnyIt(Lambda:Bool( it:Int )
73+
Return it = 10
74+
End)+": any = 10 ?"
75+
76+
Print "---------- FoldItl (left)-------------"
77+
Local fol := New Stack<Int>(New Int[]( 2, 3, 666 ) )
78+
Print fol.FoldItl( Lambda:Int( a:Int, b:Int )
79+
Return a + b
80+
End) + " = sum " + fol
81+
82+
Print fol.FoldItl( Lambda:Int( a:Int, b:Int )
83+
Return a - b
84+
End) + " = sub " + fol
85+
86+
Print fol.FoldItl( Lambda:Int( a:Int, b:Int )
87+
Return a * b
88+
End) + " = mul " + fol
89+
90+
Print fol.FoldItl( Lambda:Int( a:Int, b:Int )
91+
Return a / b
92+
End) + " = div " + fol
93+
94+
Print "---------- FoldItr (right)-------------"
95+
Print fol.FoldItr( Lambda:Int( a:Int, b:Int )
96+
Return a + b
97+
End) + " = sum " + fol
98+
99+
Print fol.FoldItr( Lambda:Int( a:Int, b:Int )
100+
Return a - b
101+
End) + " = sub " + fol
102+
103+
Print fol.FoldItr( Lambda:Int( a:Int, b:Int )
104+
Return a * b
105+
End) + " = mul " + fol
106+
107+
Print fol.FoldItr( Lambda:Int( a:Int, b:Int )
108+
Return a / b
109+
End) + " = div " + fol
110+
111+
End

modules/std/collections/stack.wx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,4 +903,84 @@ Class Stack<T> Implements IContainer<T>
903903
Sort( compare, x, hi, filter )
904904
End
905905

906+
#rem wonkeydoc Iterates through a container and checks if every item fulfills the predicate.
907+
#end
908+
Method AllIt:Bool( fun:Bool( it:T ) )
909+
910+
DebugAssert( _length, "Stack is empty" )
911+
912+
For Local i := 0 Until _length
913+
If Not fun( _data[i] ) Return False
914+
Next
915+
Return True
916+
End
917+
918+
#rem wonkeydoc Iterates through a container and checks if at least one item fulfills the predicate.
919+
#end
920+
Method AnyIt:Bool( fun:Bool( it:T ) )
921+
922+
DebugAssert( _length, "Stack is empty" )
923+
924+
For Local i := 0 Until _length
925+
If fun( _data[i] ) Return True
926+
Next
927+
Return False
928+
End
929+
930+
#rem wonkeydoc Returns a new Array with the results of the op func applied to every item in the stack.
931+
#end
932+
Method MapIt<M>:M[]( fun:M( it:T ) )
933+
934+
DebugAssert( _length, "Stack is empty" )
935+
936+
Local mm := New M[_length]
937+
For Local i := 0 Until _length
938+
mm[i] = fun( _data[i] )
939+
Next
940+
Return mm
941+
End
942+
943+
#rem wonkeydoc Returns a new Array with all the items of stack that fulfill the predicate func.
944+
#end
945+
Method FilterIt:T[]( fun:Bool( it:T ) )
946+
947+
DebugAssert( _length, "Stack is empty" )
948+
949+
Local mm := New T[_length]
950+
Local idx := 0
951+
For Local i := 0 Until _length
952+
If fun( _data[i] )
953+
mm[idx] = _data[i]
954+
idx += 1
955+
Endif
956+
Next
957+
Return mm.Slice( 0, idx )
958+
End
959+
960+
#rem wonkeydoc Fold a sequence from left to right, returning the accumulation.
961+
#end
962+
Method FoldItl:T( fun:T( a:T, b:T ) )
963+
964+
DebugAssert( _length, "Stack is empty" )
965+
966+
Local result:T = _data[0]
967+
For Local i := 1 Until _length
968+
result = fun( result, _data[i] )
969+
Next
970+
Return result
971+
End
972+
973+
#rem wonkeydoc Fold a sequence from right to left, returning the accumulation.
974+
#end
975+
Method FoldItr:T( fun:T( a:T, b:T ) )
976+
977+
DebugAssert( _length, "Stack is empty" )
978+
979+
Local result:T = _data[_length - 1]
980+
For Local i := _length - 2 To 0 Step -1
981+
result = fun( result, _data[i] )
982+
Next
983+
Return result
984+
End
985+
906986
End

modules/wonkey/native/wxarray.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ template<class T,int D> struct wxArray{
225225
for( int i=0;i<count;++i ) dst.data()[dstOffset+i]=data()[offset+i];
226226
}
227227
}
228+
229+
wxString toString()const{
230+
wxString result=wxDBType<T>()+"[ ";
231+
int n = length();
232+
for ( int i=0;i<n;++i) {
233+
if (i>0) result+=", ";
234+
result+=wxString(_rep->_data[i]);
235+
}
236+
return result+=" ]";
237+
}
228238
};
229239

230240
template<class T,int D> wxString wxDBType( wxArray<T,D> *p ){

modules/wonkey/types.wx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,11 @@ Struct @Array<T>
638638

639639
#end
640640
Method CopyTo( dstArray:T[],srcOffset:Int,dstOffset:Int,count:Int )="copyTo"
641-
641+
642+
#rem wonkeydoc Return a printable representation of the array.
643+
#end
644+
Method ToString:String()="toString"
645+
642646
End
643647

644648
#rem wonkeydoc Base class of all objects.

0 commit comments

Comments
 (0)