SHARE
TWEET

Function.andThen

DulcetAirman May 5th, 2018 (edited) 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ch.claude_martin;
  2.  
  3. import java.util.Scanner;
  4. import java.util.function.Function;
  5.  
  6. public class SomeClass {
  7.     public static void main(String[] args) {
  8.         Function<String, String> trim = String::trim;
  9.         Function<CharSequence, Long> length = s -> s.codePoints().count();
  10.  
  11.         Function<String, Number> trimmedLength = trim.andThen(length);
  12.         System.out.println("Type something and hit <enter>.");
  13.         try (Scanner scanner = new Scanner(System.in)) {
  14.             while (true) {
  15.                 String line = scanner.nextLine();
  16.                 Number len = trimmedLength.apply(line);
  17.                 if (0 == len.longValue())
  18.                     break;
  19.                 System.out.format("The trimmed input was %d code points long.%n", len);
  20.             }
  21.         }
  22.         System.out.println("Good Bye");
  23.     }
  24. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top