Skip to content

Commit 7342ceb

Browse files
committed
feat(functional-interface): add FunctionChaining examples with Function<T,R> and andThen()
What - Introduced two demos under `FunctionChaining` package: 1. `Main`: Demonstrates string transformation functions. - `Function<String,String>` to convert input to uppercase. - `Function<String,String>` to extract substring. - Applied `andThen()` to chain functions. 2. `Main2`: Demonstrates numeric function composition. - `Function<Integer,Integer>` to double input. - `Function<Integer,Integer>` to cube input. - Showcases chaining order (`f.andThen(g)` vs `g.andThen(f)`). Why - `Function<T,R>` is a core functional interface in Java for mapping input → output. - Method `andThen()` enables **function composition**, combining multiple functions into a pipeline. - Understanding chaining order is crucial: - `f.andThen(g)` = apply `f`, then apply `g` to result. - `g.andThen(f)` = apply `g`, then apply `f`. Logic 1. **String Example (Main)** - `function`: maps string to uppercase. - `function2`: extracts first 3 characters. - `function2.andThen(function2)`: applies substring twice in sequence. - Input `"VIP"`: - First `function2` → `"VIP".substring(0,3)` → `"VIP"`. - Second `function2` again extracts substring → still `"VIP"`. - Output: `VIP`. 2. **Integer Example (Main2)** - `function1`: doubles input (`x -> 2*x`). - `function2`: cubes input (`x -> x*x*x`). - `function1.andThen(function2).apply(3)`: - Step 1: `function1(3)` = `6`. - Step 2: `function2(6)` = `216`. - `function2.andThen(function1).apply(1)`: - Step 1: `function2(1)` = `1`. - Step 2: `function1(1)` = `2`. - Demonstrates order sensitivity. Real-life applications - Data processing pipelines (e.g., normalize string → validate → transform). - Mathematical computations (chaining transformations like scaling, squaring). - Input validation and formatting in UI or service layers. - Functional programming constructs in reactive or stream-based APIs. Notes - `compose()` is the reverse of `andThen()`: `f.compose(g)` = `g` applied first, then `f`. - Null-safe: chaining should ensure intermediate results are valid to avoid `NullPointerException`. - Function chaining enables declarative programming, reducing boilerplate transformations. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 2397cb1 commit 7342ceb

File tree

1 file changed

+13
-0
lines changed
  • Java 8 Crash Course/Function/src/FunctionChaining

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package FunctionChaining;
2+
3+
import java.util.function.Function;
4+
5+
public class Main2 {
6+
public static void main(String[] args) {
7+
Function<Integer, Integer> function1 = x -> 2 * x;
8+
Function<Integer, Integer> function2 = x -> x * x *x;
9+
10+
System.out.println(function1.andThen(function2).apply(3));
11+
System.out.println(function2.andThen(function1).apply(1));
12+
}
13+
}

0 commit comments

Comments
 (0)