Function inside map resets currentTiddler

I have no idea if this is expected…

The :map filter run prefix sets the <currentTiddler> variable inside a :map filter run, but for a function call inside the same :map-prefixed filter run, the <currentTiddler> is whatever it was outside the :map prefix.

Test code:

\function outer(v) [<v>] :map[function[inner]addsuffix[ / Current tiddler inside map: ]addsuffix<currentTiddler>]

\function inner() [[Current tiddler inside inner function: ]addsuffix<currentTiddler>]

<<outer "Input Value">>

Output:

Current tiddler inside inner function: Draft of 'New Tiddler' / Current tiddler inside map: Input Value

Is this expected behavior, or is it a bug?

IMO this issue should go to GH. It will be forgotten if you leave it here.

So it is a bug? (I don’t want to open a bug at GH if it’s expected and not a bug.)

No atm it’s an issue. There are some known inconsistencies with functions. This may be one too.

But I also cannot see what you want to achieve, other than testing function calls.

:map returns an output array, but functions always only return 1 result. If you want to get several elements returned from a function you will need to add +[format:titlelist[]join[ ]] otherwise you will only get 1 element back.

So your construction as it is does not make too much sense – other than as a test-case

Also outer will need a titlelist as input if you want to run it through :map. So outer will need to look like this [<v>enlist-input[]] to make sure :map has something to do.

As it happens, you’re right–I was just experimenting with function calls. I was actually trying to see if recursion works as I would expect (with the goal of writing a function to search a json string). I was getting results I didn’t understand, so I reduced it piece by piece, down to a non-recursive example, in order to figure out which part was behaving differently from what I expected.

I believe it’s the opposite; :map returns 1 result per input, and functions are run once with all inputs and return all the results.

I don’t find this to be true. Sometimes, I want to use the output of a previous filter run–even if it’s just one title–as a parameter in the next run rather than as an input. And in that case, :map does exactly that for me by setting the output of the previous run to the currentTiddler variable, enabling me to use it as a parameter.

[tag[spam]count[]] :map[[eggs]someOperator<currentTiddler>]

Thanks @bluepenguindeveloper I think this does sound like a bug – the way that currentTiddler reverts to the parent value suggests that this is another variant of [BUG] Filters don't use the variables set by filter run prefixes · Issue #7907 · Jermolene/TiddlyWiki5 · GitHub

Yes, it looks exactly the same. Thank you!

@jeremyruston – I did test the code in the OP with the current TW prerelease. The bug you pointed to is already closed. … But the issue seems to be still there.