ForArrayIndices class¶
This page explains the ForArrayIndices
class.
Before reading on, maybe it is helpful to read the following page (the apysc uses this class for the same reason for each data type):
What class is this?¶
The ForArrayIndices
class is the for-loop class.
This interface returns Array
’s index (starts with 0) in a loop.
Basic usage¶
This class requires using the with
-statement.
The as
-keyword value becomes the Int
type index.
import apysc as ap
ap.Stage(
stage_width=0,
stage_height=0,
background_color=ap.Color("#333"),
stage_elem_id="stage",
)
arr: ap.Array[ap.Number] = ap.Array([ap.Number(50), ap.Number(150), ap.Number(250)])
indices: ap.Array[ap.Int] = ap.Array([])
with ap.ForArrayIndices(arr=arr) as i:
indices.append(i)
ap.assert_arrays_equal(indices, [0, 1, 2])
ap.save_overall_html(dest_dir_path="for_array_indices_basic_usage_1/")
The following example uses an index and sets a circle center-x coordinate.
import apysc as ap
ap.Stage(
stage_width=350,
stage_height=150,
background_color=ap.Color("#333"),
stage_elem_id="stage",
)
x_arr: ap.Array[ap.Number] = ap.Array([ap.Number(75), ap.Number(175), ap.Number(275)])
with ap.ForArrayIndices(arr=x_arr) as i:
x: ap.Number = x_arr[i]
circle: ap.Circle = ap.Circle(
x=x,
y=75,
radius=25,
fill_color=ap.Color("#0af"),
)
ap.save_overall_html(dest_dir_path="for_array_indices_basic_usage_2/")
See also¶
Each branch instruction class’s scope variables reverting setting
Notes: This class also has the same arguments and behaves in the same way.
ForArrayIndices API¶
Note: the document build script generates and updates this API document section automatically. Maybe this section is duplicated compared with previous sections.
[Interface signature] __init__(self, arr: apysc._type.array.Array, *, locals_: Union[Dict[str, Any], NoneType] = None, globals_: Union[Dict[str, Any], NoneType] = None, variable_name_suffix: str = '') -> None
[Interface summary]
The loop implementation class for the ap.Array
indices.
[Parameters]
arr
: ArrayAn array to iterate.
locals_
: Optional[Dict[str, Any]], optionalCurrent scope’s local variables. Set locals() value to this argument. If specified, this interface reverts all local scope VariableNameMixIn variables (like Int, Sprite) at the end of a with-statement scope. This setting is useful when you don’t want to update each variable.
globals_
: Optional[Dict[str, Any]], optionalCurrent scope’s global variables. Set globals() value to this argument. This setting works the same way as the locals_ argument.
variable_name_suffix
: str, optionalA JavaScript variable name suffix string. This setting is sometimes useful for JavaScript debugging.
[Examples]
>>> import apysc as ap
>>> arr: ap.Array[ap.Number] = ap.Array(
... [ap.Number(50), ap.Number(150), ap.Number(250)]
... )
>>> indices: ap.Array[ap.Int] = ap.Array([])
>>> with ap.ForArrayIndices(arr=arr) as i:
... indices.append(i)
...
>>> _ = ap.assert_arrays_equal(indices, [0, 1, 2])
[References]