appinventor-sources: Implement a replace all in text using mapping block

Describe the desired feature

Implement a new text block replace all in text using mapping that replaces substrings in a text with one or more mappings provided by a dictionary or associative list.

Give an example of how this feature would be used

Currently we can use the replace all block to replace a single string with another string. However, if you want to provide potentially many mappings, you would have to implement this in a way that is not commutative with respect to the application of replacements. For example, the string “this is a string” with the mappings {"i": "x", "xs": "y"} should produce “thxs xs a strxng” but with a replace all within a loop approach would result in “thy y a strxng”.

Why doesn’t the current App Inventor system address this use case?

One can implement such a function with a relatively complicated procedure, but this is the type of exercise one would normally do in a AP CS or CS1 college level course, and therefore isn’t necessarily aligned with our goal of computational action:

blocks (34)

Why is this feature beneficial to App Inventor’s educational mission?

People might often want to make multiple substitutions simultaenously and in a predictable way based on the input. A loop using a replace all block doesn’t always provide the most accurate way of doing this. Further, if the replacements are cyclical, a naive implementation could result in an infinite loop (e.g., {"I": "you", "you": "I"}). By providing a robust abstraction we can potentially reduce/eliminate these types of issues.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 33 (22 by maintainers)

Most upvoted comments

Evan’s suggestion (in grey box above) looks reasonable to me.

Another possible naming scheme for the block:

replace all mappings [dict]
             in text [text]
          preferring [{longest string first, dictionary order}]

although it is a bit non-conformant by putting the dropdown into a dummy input at the end rather than attempting to inline it with the rest of the block text.

Also, a third order option might be “earliest occurrence”, which is a function of the position that the keys are found in the text rather than by their length or position in the dictionary iteration.