Hello, I am studying maps and would like to convert a string to a map string. What am I doing wrong?
M ← map ["A" "B" "C" "D"] ["1" "2" "3" "4"]
Macros! ← get ^0 M
∵(Macros!) "AABBCCDD"
Hello, I am studying maps and would like to convert a string to a map string. What am I doing wrong?
M ← map ["A" "B" "C" "D"] ["1" "2" "3" "4"]
Macros! ← get ^0 M
∵(Macros!) "AABBCCDD"
The problem here isn’t related to map
, it’s actually the way you’re trying to use the macro. Macros aren’t like functions, they get expanded by just swapping their arguments in at each ^
placeholder, before the code is run. That means that they need their arguments to be directly after them in the source and you can’t use them with modifiers like this.
The main reason to use macros is to create your own modifiers, because they can take other functions as arguments. That doesn’t apply here, so here’s your code rewritten to use a regular function:
M ← map ["A" "B" "C" "D"] ["1" "2" "3" "4"]
Lookup ← get⊙M
∵Lookup "AABBCCDD"
It still doesn’t work though, because ∵ each
will run Lookup
on every character of the input, while your map keys are strings. By defining your map like as map "ABCD" "1234"
, it maps characters to characters, which is what you want here. Here’s the final code on the Uiua Pad.