Javascript

Started by windman, December 05, 2022, 21:41:28

Previous topic - Next topic

windman

This is probably a stupid question but is there an algorithm for the map(x,y, . . ) command for the basic language?

Pakz

Do you mean what the use cases for the javascript maps are?

It is a sort of dictionary iirc.

Scaremonger

Not in vanilla basic, but in BlitzMax you could do something like this (Untested code) to call a function on each array element:


Function Map:int[]( source:int[], callback:int(value:int) )
  Local Result:Int[]
  For local n:Int = EachIn source
    Result :+ [ callback( n ) ]
  Next
  Return Result
End Function

Local mydata:int[] =[65,22,33,48,90]
Local results:int[] = Map( mydata, myfunc )

Function myfunc:Int( value:int )
  Return value * 2
End Function


windman

Would this be the same in BB?
DIM A(10)
FOR I =1 TO 4:A(I)=2*I:NEXT
REM A(1)=2:A(2)=4:A(3)=6:A(4)=8
FUNCTION MAP(N)
FOR I=1 TO 4:A(I)=A(I)/2:NEXT
END FUNCTION
REM A(1)=1:A(2)=2:A(3)=3:A(4)=4