Hi,
Does anyone have suggestions about improving upon the following code, to make it more robust and universal? Even suggestions about adding some explanatory comments, or better naming conventions, would be fine.
The purpose is to create a Function that executes a given function on every element in a List, and returns a List with the results. Something like PHP’s array_map(), or Clojure’s map() function.
Does anyone have suggestions about improving upon the following code, to make it more robust and universal? Even suggestions about adding some explanatory comments, or better naming conventions, would be fine.
The purpose is to create a Function that executes a given function on every element in a List, and returns a List with the results. Something like PHP’s array_map(), or Clojure’s map() function.
B4X:
Sub List_Map( s_Component As String, s_Function As String, lst As List, n_Count As Int, lst_Reduced As List ) As List
Private x_Result As Object
If lst_Reduced.IsInitialized == False Then
lst_Reduced.Initialize
End If
'// If function doesn't exist, return an empty list.
If SubExists( s_Component, s_Function ) == False Then
'// Shouldn't be necessary
'// lst_Reduced.Clear
Return lst_Reduced
End If
'// If the count is up to the length of the list, then return final result
If n_Count = lst.Size Then
Return lst_Reduced
Else
'// Is there any way to avoid Adding to the list? It's a forum of assignment, and
'// I'm trying to avoid assignments.
lst_Reduced.Add( CallSub2( s_Component, s_Function, lst.Get( n_Count ) ) )
Return List_Map( s_Component, s_Function, lst, n_Count + 1, lst_Reduced )
End If
End Sub