JavaFPLearning
The Repository is a compendium of Java-based Functional Programming examples aimed at enhancing your comprehension of the concepts and facilitating your eventual implementation of them. The examples in the repo doesn't contains the examples of how to use obvious map, filter, reduce, instead it focuses on writing efficient functional code in Java
Install / Use
/learn @sameershukla/JavaFPLearningREADME
Functional Programming In Java
<img src="https://axisapplications.com/wp-content/uploads/2019/02/functionalprogramming_icon-300x300.png" width="300">Overview
The Repository is a compendium of Java-based Functional Programming examples aimed at enhancing your comprehension of the concepts and facilitating your eventual implementation of them. The examples in this repo doesn't contains the examples of how to use obvious map, filter, reduce, instead it focuses on writing efficient functional code. Throughout this course, you will gain a solid understanding of Functional programming concepts, starting from the fundamentals and progressing to more advanced topics. You will learn how to write Higher Order Functions in Java and how to leverage Function Chaining to produce elegant and efficient code. Additionally, you will explore Function Currying, Partial Functions, and Monads. One noteworthy aspect of this course is that it includes a variety of practical examples, which will be incredibly beneficial for your learning experience.
What you'll learn and how the code is structured.
The repository contains examples that demonstrate the principles of writing elegant functional code.
FunctionCompositionDemo: This class demonstrates function composition in Java by chaining mathematical operations and logging the result. It showcases two methods, compose and chain, to illustrate different execution orders in a function pipeline using compose (right-to-left execution) and andThen (left-to-right execution)..
StringTransformationPipelineDemo: This class demonstrates a string transformation pipeline in Java that performs a series of operations on an input string. The pipeline includes functions to trim whitespace, remove special characters, and convert the string to uppercase, all composed using andThen for sequential processing. The transform method wraps these transformations, returning an Optional to handle null or empty inputs gracefully.
BiFunctionPipelineDemo: This class demonstrates a BiFunction-based transformation pipeline for processing two string inputs. The letterChain method applies a series of transformations—trimming whitespace, removing special characters, and converting to uppercase—returning a Tuple with the final processed results. This pipeline showcases BiFunction chaining with andThen to build a sequential transformation flow.
FunctionalFileReaderPipeline: This class demonstrates a functional pipeline for file I/O in Java, using function composition to open, read, print, and close a file. Each function in the pipeline is modular and composed sequentially, showcasing the power of functional programming for structured resource management.
HigherOrderFunctionDemo: This class demonstrates higher-order functions in Java by using a generic process method that takes a Predicate to count letters, digits, or special characters in a string. It highlights the flexibility of passing behavior as parameters, making it adaptable for various counting operations.
TriFunctionPipelineDemo: This class demonstrates a TriFunction-based transformation pipeline for processing three string inputs, illustrating the limitations of standard functional interfaces with multiple arguments. The letterChain method applies a series of transformations—concatenating the inputs and then capitalizing them—returning a Tuple with the transformed result. This pipeline showcases the use of a custom TriFunction interface and chaining with andThen, highlighting why currying is often needed to handle functions with flexible, sequential parameter application..
FunctionCurryingDemo: This class demonstrates currying and partial application in Java, showcasing how multi-parameter functions can be broken down into a series of single-parameter functions for flexibility. By applying parameters one at a time, it highlights the limitations of BiFunction and TriFunction and shows how currying enables reusable function chains.
PartialFunctionDemo: This class demonstrates function currying and partial application of functions (PAF) by creating Customer objects in Java. It provides flexible functions to progressively set Customer parameters like name, email, and phone number, showcasing how currying and partial application allow for step-by-step function application and object creation.
The "types" package contains types.
Tuple: In functional programming, a tuple is an ordered collection of elements of different types.
Unit: Unit is a class that represents the absence of a value. It is used to indicate that a function returns no useful value, similar to the void type.
What are Functions
In computer programming, a function is a self-contained block of code that performs a specific task. Functions take input, called arguments or parameters, and can return output values, allowing them to be used as building blocks for larger programs.

Understanding Functions in Functional Programming
Functions are a key concept in functional programming, and are used to express computations and transformations on data. In functional programming, functions are treated as first-class citizens, meaning that they can be passed around as values, stored in variables or data structures, and returned as results from other functions. Functions in functional programming are typically pure functions, which means that they don't have any side effects, and their output is solely determined by their input. This makes them very predictable and easy to reason about, since their behavior doesn't depend on any external state or context.
One way to perceive java.util.function.Function is as follows:

BiFunction can be perceive as

Lambda Expression
In Java, a lambda expression is a type of anonymous function that can be used to represent a block of code that can be passed as an argument to a method or stored in a variable. When a Java compiler encounters a lambda expression in the source code, it performs several steps to detect and process it:
Parsing: The Java compiler parses the lambda expression to determine its syntax and identify the variables that are used in the expression.
Type Inference: The compiler infers the types of the lambda parameters based on the context in which the lambda expression is used.
Creation of a Functional Interface:_ A lambda expression is only valid if it can be assigned to a functional interface. A functional interface is an interface with a single abstract method. If the lambda expression matches the signature of the functional interface, the compiler creates an instance of that interface and assigns the lambda expression to it.
Compilation_: Finally, the compiler compiles the lambda expression and generates bytecode that can be executed by the Java Virtual Machine (JVM).
During compilation, the lambda expression is translated into a class file that implements the functional interface. The class file contains a method that implements the lambda expression, as well as any captured variables and their values. When the lambda expression is executed, the JVM creates an instance of this class and invokes the method on that instance.
How Lambda Expressions handled by JVM
When a lambda expression is encountered in Java code, the JVM uses the invokedynamic instruction to create an instance of a functional interface that represents the lambda.
Here's how the process works in more detail:
-
The Java compiler generates an instance of a functional interface that corresponds to the lambda expression. For example, if the lambda expression is of the form x -> x * 2, the compiler generates an instance of the java.util.function.IntUnaryOperator interface.
-
The invokedynamic instruction is used to create a CallSite object, which is responsible for the dynamic invocation of the lambda expression. The CallSite object is associated with the lambda expression and the functional interface instance generated in step 1.
-
When the lambda expression is invoked, the JVM uses the CallSite object to dynamically bind the lambda expression to the appropriate method in the functional interface.
-
The JVM then invokes the method on the functional interface instance using the appropriate arguments, and returns the result to the calling code.
Overall, the use of invokedynamic and functional interfaces enables efficient implementation of lambda expressions in Java, allowing for concise and expressive code. The dynamic binding of the lambda expression to the appropriate method in the functional interface allows for greater flexibility in code composition and enables more powerful abstractions in Java programming.
Function Chaining
In order to create a chain of functions, it is essential to first instantiate a Function or BiFunction object. This marks the beginning of the pipeline. For example, you could create a Function<String, String> object named "pipeline" using the "createReader" method of the "FileOps" class. Once you have created the "pipeline" object, you can use the "andThen" method to chain subsequent functions together. Each function in the chain will take the output of the preceding function as input. Eventually, the final function in the chain should return a String value.
Function<String, String> pipeline = FileOps :: createReader;
pipeline.andThen("Output of Create Reader is input here") and so on, but eventually it should return String.
more details can be found here: https://www.c-sharpcorner.com/article/creating-function-pipelines-in-java/
Function Chaining Use Cases
Data processing: Function chaining can be used to perform a series of data transformations on a collection or stream of data, such as filtering, mapping, sorting, and reducing. By chaining these operations t
Related Skills
qqbot-channel
345.4kQQ 频道管理技能。查询频道列表、子频道、成员、发帖、公告、日程等操作。使用 qqbot_channel_api 工具代理 QQ 开放平台 HTTP 接口,自动处理 Token 鉴权。当用户需要查看频道、管理子频道、查询成员、发布帖子/公告/日程时使用。
docs-writer
100.0k`docs-writer` skill instructions As an expert technical writer and editor for the Gemini CLI project, you produce accurate, clear, and consistent documentation. When asked to write, edit, or revie
model-usage
345.4kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
ddd
Guía de Principios DDD para el Proyecto > 📚 Documento Complementario : Este documento define los principios y reglas de DDD. Para ver templates de código, ejemplos detallados y guías paso
