<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">So I have some code that utilizes structs, I would like to go through and update some of the values. While I could use map, it seems I should be able to do it using a for in loop without utilizing indexes. The mutate2 method uses enumerate, and the index to write back the copy to the array, but I would prefer to update the struct value itself and avoid having to use indexes as I try to avoid them, if possible.. It seems the structs kind of force you to go back to indexes. I was thinking that it would be useful to be able to apply the & (reference) operator here to allow direct access to the array element struct, an example of this is shown in mutate3. With a class this is not necessary. I know a little like C++ but just want to explore this idea unless there is some other way I missed. <br class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><br class=""></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><span style="font-family: Menlo; color: rgb(187, 44, 162);" class="">struct</span><span style="font-family: Menlo;" class=""> Codon {<br class=""></span><span style="font-family: Menlo;" class=""> </span><span style="font-family: Menlo; color: rgb(187, 44, 162);" class="">var</span><span style="font-family: Menlo;" class=""> val = </span><span style="font-family: Menlo; color: rgb(112, 61, 170);" class="">Float</span><span style="font-family: Menlo;" class="">(</span><span style="font-family: Menlo; color: rgb(39, 42, 216);" class="">0.0</span><span style="font-family: Menlo;" class="">)<br class=""></span><span style="font-family: Menlo;" class=""> </span><span style="font-family: Menlo; color: rgb(187, 44, 162);" class="">var</span><span style="font-family: Menlo;" class=""> mask = </span><span style="font-family: Menlo; color: rgb(187, 44, 162);" class="">false</span><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div></div></blockquote><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> Geno {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> codons : [<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Codon</span>]</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div></div></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> // pure index based approach</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> mutate() {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="color: rgb(187, 44, 162);" class="">for</span> i <span style="color: rgb(187, 44, 162);" class="">in</span> <span style="color: rgb(39, 42, 216);" class="">0</span>..<<span style="color: rgb(79, 129, 135);" class="">codons</span>.<span style="color: rgb(112, 61, 170);" class="">count</span> {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> !<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">codons</span>[i].<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">mask</span> {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">codons</span>[i].<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">val</span> += r.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">gauss</span>() * vari</div><div style="margin: 0px; line-height: normal;" class=""> }</div><div style="margin: 0px; line-height: normal;" class=""> }</div><div style="margin: 0px; line-height: normal;" class=""> }</div><div class=""><br class=""></div><div class=""> // index to write back, less efficient? </div></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> <span style="color: rgb(187, 44, 162);" class="">func</span> mutate2() {</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> <span style="color: rgb(187, 44, 162);" class="">for</span> (index, <span style="color: rgb(187, 44, 162);" class="">var</span> codon) <span style="color: rgb(187, 44, 162);" class="">in</span> <span style="color: rgb(79, 129, 135);" class="">codons</span>.<span style="color: rgb(61, 29, 129);" class="">enumerate</span>() {</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> <span style="color: rgb(187, 44, 162);" class="">if</span> !codon.<span style="color: rgb(79, 129, 135);" class="">mask</span> {</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> codon.<span style="color: rgb(79, 129, 135);" class="">val</span> += r.<span style="color: rgb(49, 89, 93);" class="">gauss</span>()</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> <span style="color: rgb(79, 129, 135);" class="">codons</span>[index] = codon</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 14px;" class=""> } </p><p style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 14px;" class=""><br class=""></p><p style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 14px;" class=""> // suggested reference approach <br class="webkit-block-placeholder"></p><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> mutate3() {</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> <span style="color: rgb(187, 44, 162);" class="">for</span> <span style="color: rgb(187, 44, 162);" class="">var</span> &codon <span style="color: rgb(187, 44, 162);" class="">in</span> codons {</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> !codon.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">mask</span> {</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> codon.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">val</span> += r.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">gauss</span>()</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><br class=""></div></body></html>