Last active
August 29, 2015 14:13
-
-
Save JesseKPhillips/04fab209263f0b70b26d to your computer and use it in GitHub Desktop.
Translation to D of the Java 8 No more loops post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original: http://www.deadcoderising.com/java-8-no-more-loops/ | |
import std.algorithm; | |
import std.range; | |
import std.typecons; | |
private struct Article { | |
private immutable string title; | |
private immutable string author; | |
private immutable string[] _tags; | |
private this(string title, string author, immutable(string)[] tags) pure { | |
this.title = title; | |
this.author = author; | |
this._tags = tags; | |
} | |
@property const(string)[] tags() const { | |
return _tags; | |
} | |
} | |
bool contains(R, T)(R range, T b) { | |
return !range.find(b).empty; | |
} | |
public Nullable!(const(Article)) getFirstJavaArticle(const(Article)[] articles) { | |
foreach (article; articles) { | |
if (article.tags.contains("Java")) { | |
return typeof(return)(article); | |
} | |
} | |
return typeof(return)(); | |
} | |
public auto getFirstJavaArticle2(const(Article)[] articles) { | |
return articles.find!(x => x.tags.contains("Java")); | |
} | |
unittest { | |
immutable arr = makeArticles(); | |
assert(arr.getFirstJavaArticle() is arr[1]); | |
assert(arr.getFirstJavaArticle2().front is arr[1]); | |
} | |
public auto getAllJavaArticle(const(Article)[] articles) { | |
return articles.filter!(x => x.tags.contains("Java")); | |
} | |
unittest { | |
immutable arr = makeArticles(); | |
assert(arr.getAllJavaArticle().equal(arr[1..3])); | |
} | |
version(none) | |
public auto groupByAuthor(Rebindable!(const(Article))[] articles) { | |
auto authorOrder(string op)(const(Article) a, const(Article) b) pure { | |
mixin("return a.author "~ op ~" b.author;"); | |
} | |
return articles.sort!(authorOrder!"<").groupBy!(authorOrder!"=="); | |
} | |
version(none) | |
unittest { | |
auto arr = makeArticles().map!(x=>rebindable(cast(const)x)).array; | |
immutable(Article)[][] ans = [[Article("title", "author", ["tags", "Java"]), | |
Article("title", "author", ["tags"]),], | |
[Article("title", "jacob", ["tags"]), | |
Article("title", "jacob", ["Java", "tags"]),] | |
]; | |
auto verify(const(Article) a, const(Article) b) { | |
writeln(a); | |
writeln("=="); | |
writeln(b); | |
return a == b; | |
} | |
auto m = arr.groupByAuthor().zip(ans); | |
writeln(m.front); | |
m.popFront; | |
writeln(m.front); | |
writeln(arr.groupByAuthor().zip(ans).map!(x=> x[0].equal!verify(x[1]))); | |
version(none) | |
assert(arr.groupByAuthor().zip(ans).map!(x=> x[0].equal!verify(x[1]).all!"a")); | |
} | |
public auto distinctTags(const(Article)[] articles) { | |
return articles.map!(x=>x.tags).joiner.unqualArray.sort.uniq; | |
} | |
import std.traits; | |
@trusted Unqual!(ForeachType!Range)[] unqualArray(Range)(Range r) | |
if (isIterable!Range && !isNarrowString!Range && !isInfinite!Range) | |
{ | |
return cast(typeof(return)) r.array; | |
} | |
unittest { | |
auto arr = makeArticles(); | |
assert(arr.distinctTags.equal(["Java", "tags"])); | |
} | |
auto makeArticles() pure { | |
return [Article("title", "jacob", ["tags"]), | |
Article("title", "author", ["tags", "Java"]), | |
Article("title", "jacob", ["Java", "tags"]), | |
Article("title", "author", ["tags"]), | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment