java - Nested flatmap observable rxJava -
say have blog post api returns list<post>
i'd translate else using rxjava. overall i'd break long string paragraphs , words, each word, combine paragraphs , combine blog post. each post can broken multiple paragraphs broken multiple words. logically i'd following , can't find async manner in.
- create observable list
- split each observable
observable<string>
on\n
give me paragraphs - split paragraphs
observable<string>
on' '
segment words - do kind of transformation each word (some words have puncuation part of them fine)
- combine words paragraph putting space between each one
- combine paragraphs post putting carriage return between each one.
i'm having trouble figuring out how multiple levels of splitting , combining on particular term. if needed split pages can flatmap(observable::from).tolist() won't seem work. appreciated.
i think should work fine, code pretty self-explanatory.
i make nested calls observable#from
split posts paragraphs , paragraphs words. use observable#reduce
concatenate words obtain single paragraph, , reduce
again concatenate paragraphs single post.
observable<list<string>> blogpostswithreversedwords(list<string> blogposts) { return observable.from(blogposts) .flatmap(this::blogposttoparagraphs) .tolist(); } observable<string> blogposttoparagraphs(string post) { return observable.from(post.split("\n")) .flatmap(this::paragraphtowords) .reduce((prevtext, nextparagraph) -> prevtext + "\n" + nextparagraph); } observable<string> paragraphtowords(string paragraph) { return observable.from(paragraph.split(" ")) .map(this::reverseword) .reduce((prevtext, nextword) -> prevtext + "\n" + nextword); } string reverseword(string word) { return new stringbuilder(word).reverse().tostring(); }
Comments
Post a Comment