Created
November 9, 2012 08:46
-
-
Save rednaxelafx/4044530 to your computer and use it in GitHub Desktop.
Make typoglycemia text with Java 8/lambda. Excercise from Jeffrey Zhao's blog: http://blog.zhaojie.me/2012/11/how-to-generate-typoglycemia-text.html
This file contains 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
import java.util.*; | |
import java.util.concurrent.*; | |
import static java.util.Comparators.comparing; | |
public class TestRandomizeString { | |
public static void main(String[] args) { | |
ThreadLocalRandom random = ThreadLocalRandom.current(); | |
String text = args[0]; | |
String result = String.join(" ", | |
text.splitAsStream(" ") | |
.map( | |
s -> s.length() <= 3 | |
? s | |
: s.charAt(0) | |
+ s.substring(1, s.length() - 1) | |
.asChars() | |
.sorted(comparing((Character _) -> random.nextInt())) | |
.fold( | |
() -> new StringBuilder(s.length() - 2), | |
(sb, c) -> sb.append(c.charValue()), | |
(a, b) -> a) | |
+ s.charAt(s.length() - 1) | |
).into(new ArrayList<String>())); | |
System.out.println(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment