Skip to content

Instantly share code, notes, and snippets.

@jaydg
Created March 4, 2018 12:41
Show Gist options
  • Save jaydg/871b7c5fdc21928071828903c9a57b94 to your computer and use it in GitHub Desktop.
Save jaydg/871b7c5fdc21928071828903c9a57b94 to your computer and use it in GitHub Desktop.
Using std.algorithm.iteration.map to retrieve an attribute from each member struct of an array
import std.algorithm.iteration : map;
struct Foo {
int id;
string text;
private static Foo[] foos = [
{ 0, "Blah" },
{ 1, "Text" },
{ 2, "Moar Text" },
];
static auto texts() {
return map!(f => f.text)(foos);
}
}
void main() {
import std.stdio;
foreach (text; Foo.texts) {
writeln(text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment