About Monkey 2 › Forums › Monkey 2 Code Library › Range() functions
This topic contains 2 replies, has 1 voice, and was last updated by
nerobot 1 year ago.
-
AuthorPosts
-
February 27, 2018 at 2:44 pm #13754
Set of functions to generate ranges of numbers and chars.
Easy to extending for other numeric types (make it generic for example).
Also there is a helper function Join() that join array values into single string with delimeters.
Functions and demo app (2 in 1):
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172Namespace myapp#Import "<std>"Using std..Function Main()Local arr1:=Range( 20,10 )Print "1: "+Join( arr1 )Local arr2:=Range( 0.0,5.0,.25 )Print "2: "+Join( arr2 )Local arr3:=Range( "a","z" )Print "3: "+Join( arr3 )EndFunction Join<T>:String( arr:T[],delimeter:String="," )Local s:=""For Local i:=Eachin arrIf s Then s+=delimeters+=iNextReturn sEndFunction Range:Int[]( _start:Int,_end:Int,_step:Int=0 )If _step=0_step=(_end>_start) ? 1 Else -1EndifLocal st:=New Stack<Int>For Local i:=_start To _end Step _stepst.Add( i )NextReturn st.ToArray()EndFunction Range:Float[]( _start:Float,_end:Float,_step:Float=0 )If _step=0_step=(_end>_start) ? 1 Else -1EndifLocal st:=New Stack<Float>For Local i:=_start To _end Step _stepst.Add( i )NextReturn st.ToArray()EndFunction Range:String[]( _start:String,_end:String,_step:Int=0 )Local istart:=_start[0],iend:=_end[0]If _step=0_step=(iend>istart) ? 1 Else -1EndifLocal st:=New Stack<String>For Local i:=istart To iend Step _stepst.Add( String.FromChar( i ) )NextReturn st.ToArray()EndIt prints
Monkey1231: 20,19,18,17,16,15,14,13,12,11,102: 0,0.25,0.5,0.75,1,1.25,1.5,1.75,2,2.25,2.5,2.75,3,3.25,3.5,3.75,4,4.25,4.5,4.75,53: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,zMaybe it can be part of std module.
February 28, 2018 at 7:39 am #13757A few new kinds of Range is here.
They generate by expression, where single values or ranges are separated by commas,
and ranges min-max diapasons separated by minus-sign.
RangeExprInt – generates sequence of integers , for example: expr=”1,14,20-27″
RangeExprChars – generates sequence of chars, for example: expr=”a-z,A-Z,0-9″
Also there is a Stack extension with method Merge that allow you to merge values into stack to have unique values only.
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455Function RangeExprInt:Int[]( expr:String,sort:Bool=True )expr = expr.Replace( " ", "" )Local arr:=expr.Split( "," )Local st:=New Stack<Int>For Local part:=Eachin arrLocal arr2:=part.Split( "-" )If arr2.Length = 2st.Merge( Range( Int(arr2[0]),Int(arr2[1]) ) )Elsest.Add( Int(part) )EndifNextIf sortst.Sort()EndifReturn st.ToArray()EndFunction RangeExprChars:String[]( expr:String,sort:Bool=True )expr = expr.Replace( " ", "" )Local arr:=expr.Split( "," )Local st:=New Stack<String>For Local part:=Eachin arrLocal arr2:=part.Split( "-" )If arr2.Length = 2st.Merge( Range( arr2[0],arr2[1] ) )Elsest.Add( Int(part) )EndifNextIf sortst.Sort()EndifReturn st.ToArray()EndClass Stack<T> Extension#Rem monkeydoc Add unique values only.#EndMethod Merge( values:T[] )For Local val:=Eachin valuesIf Not Self.Contains( val )Self.Add( val )EndifNextEndEndMarch 22, 2018 at 10:29 am #14095Additional arrays helpers:
ArrayPad() – Returns a copy of the array padded to size specified by size with value ‘value’.
ArrayFill() – Fills an array with ‘count’ entries of the ‘value’.
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849Namespace myapp#Import "<std>"Using std..Function Main()Print "Hello World"Local arr:=ArrayFill( 3,"ok" )For Local i:=Eachin arrPrint iNextPrint "---"arr=ArrayPad( arr,5,"no" )For Local i:=Eachin arrPrint iNextEnd#Rem monkeydoc Returns a copy of the array padded to size specified by size with value 'value'.If value of size is less than or equal to the length of the array then no padding takes place.#EndFunction ArrayPad<T>:T[]( arr:T[],size:Int,value:T )If size<=arr.Length Return arr.Slice( 0 ) ' make a copyLocal result:=New T[size]arr.CopyTo( result,0,0,arr.Length )For Local i:=arr.Length Until result.Lengthresult[i]=valueNextReturn resultEnd#Rem monkeydoc Fills an array with 'count' entries of the 'value'.#EndFunction ArrayFill<T>:T[]( count:Int,value:T )Local result:=New T[count]For Local i:=0 Until countresult[i]=valueNextReturn resultEnd -
AuthorPosts
You must be logged in to reply to this topic.