Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Add Seperators For Foreach Question Block #1020

Open
Bennett-Woolf opened this issue Aug 31, 2023 · 2 comments
Open

Proposal: Add Seperators For Foreach Question Block #1020

Bennett-Woolf opened this issue Aug 31, 2023 · 2 comments

Comments

@Bennett-Woolf
Copy link

Hi all,

I find when I am using [[foreach]] question blocks, I often am using it to shortcut writing out a list of variables. As such, I want to put commas and a space between each run of the foreach loop except the last one, as I don't want an extra comma and space at the end. My solution so far is to either not use the forloop (if there aren't very many elements), or drop out of it one element early so that I can add in the special case manually.

Would it be possible to either add a "seperator" argument that inserts whatever you input between each run of the forloop?

The most recent example of this phenomena is "[[foreach j="makelist(i,i,1,length(complexNumbers))"]] z_{@j@} = {@ComplexNumbers[j]@}, \quad [[/ foreach]]", which, for my case, would output

z_1 = x_1 + i y_1, z_2 = x_2 + i y_2, z_3 = x_3 + y_3, (This is where the quad ends)

I hope others would find this useful too, or if anyone has a workaround, it would be much appreciated,

Jake

@Bennett-Woolf Bennett-Woolf changed the title Add Seperators For Foreach Question Block Proposal: Add Seperators For Foreach Question Block Aug 31, 2023
@aharjula
Copy link
Member

#917 might have an interesting use of [[define]] in one of the suggestions that might work for this as well. But yes someone could possibly add such an extra separator argument to the block. It would only reserve one identifier for something that could not be used as a name of an iteration variable. Not promising to add such a thing though, maybe some newer coder might want to wrap their head around this and add such logic to this. We might at the same time consider if we need to bring other common loop concepts, like loop local flags for first and last executions etc. into the block.

Basically, I would write your example like this, in a world where we would not have such a separator option:

[[define ComplexNumbers="[1,1-i,2+i,2*i]"/]]
[[define _first="true"/]][[foreach j="makelist(i,i,1,length(ComplexNumbers))"]][[if test="_first"]][[define _first="false"/]][[else]], \quad  [[/if]]z_{@j@} = {@ComplexNumbers[j]@}[[/ foreach]]

\quad might not work there though.

Basically, [[define/]] allows one to change the values of variables and define new ones within the text, including in the conditional and loopy bits...

@Bennett-Woolf
Copy link
Author

Hi Matti,

I've been thinking about your response (and had the situation happen again) and a simpler solution to my problem is do exactly what you're saying but without the define blocks and just manually unroll the first loop, giving us

[[define ComplexNumbers="[1,1-i,2+i,2*i]"/]]
[[define loopList="makelist(i,i,1,length(ComplexNumbers))"/]]
z_{@first(loopList)@}[[foreach j="rest(loopList)"]], \quad  z_{@j@} = {@ComplexNumbers[j]@}[[/ foreach]]

which avoids having an extra conditional. The idea of unrolling the first rather than the last item in the loop is exactly what I needed so thank you so much for that.

Unfortunately my PHP knowledge is completely non-existent, otherwise I would love to add foreach constructs myself.

Thank you for all the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants