<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
        assert(!find!("toLower(a) == b")(s, "hello").empty);<br>
<br>
        assert(!find!("toLower(a) == b")(<a href="http://clist.name" target="_blank">clist.name</a>, "name2").empty);<br></blockquote><div><br></div><div>But clist is an array of c's, it has no `.name` field by itself. So, put the `.name` call inside the comparator:</div>
<div><br></div><div> </div><div> assert( find!("toLower(<a href="http://a.name">a.name</a>) == b")(<a href="http://clist.name/" target="_blank">clist</a><u>,</u> "name2").empty);</div><div><br></div><div>
This gives me this code:</div><div><br></div><div><div>import std.algorithm: find;</div><div>import std.array: empty;</div><div>import std.uni: toLower;</div><div><br></div><div>struct C // Use UpperCase for you user-defined types</div>
<div>{</div><div>  int idx;</div><div>  string name;</div><div>}</div><div><br></div><div>C[] clist = [ {1, "name1"}, {2, "name2"}, { 3, "name3" } ];</div><div><br></div><div>void main() // no need to return 0</div>
<div>{</div><div>  auto target = clist.find!((a,b) => toLower(<a href="http://a.name">a.name</a>) == b)("name2");</div><div>  assert(!target.empty);</div><div>}</div></div><div><br></div><div>Using UFCS (Universal Function Call Syntax) to tranform f(a,b) into a.f(b). I used it on `find`.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
I went looking to replace several foreach statements. Can 'find' (in understand it's just a linear search) be used on an array of structures like above.<br></blockquote><div><br></div><div>Sure, as long as you tell it how you will get the info from the range (it defaults to simple equality).</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
Example pulled and modified. Above code gives me (naturally) -<br>
  ... no property 'name' for type 'cp[]'.<br>
<br>
Interestingly, I had accidentally coded the commented out line before and it compiles correctly but will (as you guessed it) fail.<br></blockquote><div><br></div><div>I never use pointers in D. I suppose the `.name` call is propagated to the array elements?</div>
<div><br></div><div><br></div></div><br></div></div>