Skip to content

Instantly share code, notes, and snippets.

@NPException
Last active January 27, 2020 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NPException/b21feac066c20d54dcbaed80efb5336b to your computer and use it in GitHub Desktop.
Save NPException/b21feac066c20d54dcbaed80efb5336b to your computer and use it in GitHub Desktop.
implementation of the "infer" method from nipafx' Twitch stream
private Stream<TypedRelation> infer() {
// Clojure's "for" macro works essentially just like this nested loop,
// except that the resulting stream/sequence is lazily generated.
Builder<TypedRelation> relations = Stream.builder();
for (Article a1 : articles) {
for (Article a2 :articles) {
for (Genealogist g : genealogists) {
Articles articlePair = new Articles(a1, a2);
ArticleResearch research = new ArticleResearch(g, articlePair);
relations.accept(research.infer());
}
}
}
return relations.build();
}
;; based on https://puu.sh/F4jKF/d2aaadc17c.jpg
;; I took the freedom to work without most types,
;; except Genealogist since you want to have the option for
;; different implementations.
;; implementations would extend this protocol and implement the function `infer-from-research`
(defprotocol Genealogist
(infer-from-research [this article-1 article-2]))
;; since we don't use classes and fields in clojure, I'll just pass in what we need
(defn infer [articles genealogists]
(for [a1 articles
a2 articles
g genealogists]
(infer-from-research g a1 a2)))
;; silly example implementation
(defrecord SillyGenealogist [name]
Genealogist
(infer-from-research [this article-1 article-2]
(str "I'm " name ", and prefer neither " article-1 " nor " article-2 ".")))
;; example use:
(infer [1 2 3]
[(->SillyGenealogist "A") (->SillyGenealogist "B")])
;; result =>
("I'm A, and prefer neither 1 nor 1."
"I'm B, and prefer neither 1 nor 1."
"I'm A, and prefer neither 1 nor 2."
"I'm B, and prefer neither 1 nor 2."
"I'm A, and prefer neither 1 nor 3."
"I'm B, and prefer neither 1 nor 3."
"I'm A, and prefer neither 2 nor 1."
"I'm B, and prefer neither 2 nor 1."
"I'm A, and prefer neither 2 nor 2."
"I'm B, and prefer neither 2 nor 2."
"I'm A, and prefer neither 2 nor 3."
"I'm B, and prefer neither 2 nor 3."
"I'm A, and prefer neither 3 nor 1."
"I'm B, and prefer neither 3 nor 1."
"I'm A, and prefer neither 3 nor 2."
"I'm B, and prefer neither 3 nor 2."
"I'm A, and prefer neither 3 nor 3."
"I'm B, and prefer neither 3 nor 3.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment