About Monkey 2 › Forums › Monkey 2 Programming Help › RemoveIf example
This topic contains 7 replies, has 3 voices, and was last updated by
degac 1 year, 5 months ago.
-
AuthorPosts
-
November 13, 2017 at 9:14 pm #11683
Hi
I’m trying to understand how to use the method RemoveIf… but without results…Monkey1234567891011121314151617181920212223Namespace myapp#Import "<std>"Using std.collectionsUsing std.stringioFunction Main()Local st:=New Stack<Int>st.Add(10)st.Add(20)st.Add(123)st.RemoveIf(CheckIt(40)) 'I would like to remove all the items with 'value' greater of 40 (for example)EndFunction CheckIt:Bool()EndNovember 14, 2017 at 1:51 am #11689The parameter for RemoveIf should be a function:
Monkey123456789101112131415161718192021222324252627Namespace myapp#Import "<std>"Using std.collectionsUsing std.stringioFunction Main()Local st:=New Stack<Int>st.Add(10)st.Add(20)st.Add(123)st.RemoveIf( CheckIt ) 'I would like to remove all the items with 'value' greater of 40 (for example)For Local i:=Eachin stPrint iNextEndFunction CheckIt:Bool( value:Int )Return value>40End…you can use a lambda to write the function ‘inline’ too:
Monkey1234st.RemoveIf( Lambda:Bool( value:Int )Return value>40End )November 14, 2017 at 12:11 pm #11698Ahhh… many thanks!
So I can’t pass a value to check (ie: RemoveIf(Check(40)), it must be already defined in the function!
I misunderstood the meaning/syntax of it!
I should define a global to change the behavior, same in case of Lambda()November 15, 2017 at 2:48 am #11705Also, you can write & use extension methods, look at example:
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152Namespace myapp#Import "<std>"Using std..Function Main()Local st:=New Stack<Int>st.AddAll( New Int[](-2,0,10,54,3,-7) )Print "source values:"For Local i:=Eachin stPrint iNextst.RemoveIfLess( 0 )Print "filtered values 1:"For Local i:=Eachin stPrint iNextst.RemoveIfGreater( 50 )Print "filtered values 2:"For Local i:=Eachin stPrint iNextEndClass Stack<T> ExtensionMethod RemoveIfGreater( valueToCompare:T )Self.RemoveIf( Lambda:Bool( value:T )Return value>valueToCompareEnd )EndMethod RemoveIfLess( valueToCompare:T )Self.RemoveIf( Lambda:Bool( value:T )Return value<valueToCompareEnd )EndMethod RemoveIfEquals( valueToCompare:T )Self.RemoveIf( Lambda:Bool( value:T )Return value=valueToCompareEnd )EndEndNovember 15, 2017 at 12:25 pm #11713o.O wow! this is really… ingenious!
I would never have though to Extension to resolve the problem! I need to change paradigm… Monkey2 seems have so many interesting way to solve things!Thank you very much!
edit:
this thing about Stack (or list) and RemoveIf() method & solutions *should* be added to some guide/tutorial/help… It’s a simple thing that shows many features of Monkey2!
November 15, 2017 at 11:50 pm #11729…just wanted to see if this would work really (it does!), getting a bit ‘c++’-ish for my liking though…
Monkey12345678910111213141516171819202122232425262728293031#Import "<std>"Using std..Function LessThan<T>:Bool( val:T )( cmp:T )Return Lambda:Bool( val:T )Return val<cmpEndEndFunction Main()Local stack:=New IntStackFor Local i:=0 Until 100stack.Add( Rnd( 100 ) )Endstack.RemoveIf( LessThan( 50 ) )For Local val:=Eachin stackPrint valNextEndNovember 17, 2017 at 4:34 am #11762Function LessThan<T>:Bool( val:T )( cmp:T )
Wow, two pairs of brackets, it’s an undocumented cheat!
And it works. My life will never be the same…November 17, 2017 at 6:24 pm #11780Monkey2 is a … ‘monster’ even for its creator
-
AuthorPosts
You must be logged in to reply to this topic.