Skip to main content

Posts

Showing posts with the label functional programming

Java 8 - Persistent data structure

The following is a series of posts about "functional programming in Java" which is the result of my understanding by reading the book "Java 8 in Action: Lambdas, Streams, and Functional-style Programming, by Alan Mycroft and Mario Fusco".
1. Why functional programming?
2. Functional programming in Java 8
3. Java 8 - Using Functions as Values
4. Java 8 - Persistent data structure
Persistent data structure is also known as a simple technique but it's very important. Its other names are functional data structure and immutable data structure.
Why is it "persistent"? Their values persist and are isolated from changes happening elsewhere. That's it!
This technique is described as below:

If you need a data structure to represent the result of a computation, you should make a new one and not mutable an existing data structure.

Destructive updates version
public static A doSomething(A a){ a.setProp1("new value"); return a; }
Functional versi…

Java 8 - Using Functions as Values

The following is a series of posts about "functional programming in Java" which is the result of my understanding by reading the book "Java 8 in Action: Lambdas, Streams, and Functional-style Programming, by Alan Mycroft and Mario Fusco".
1. Why functional programming?
2. Functional programming in Java 8
3. Java 8 - Using Functions as Values
4. Java 8 - Persistent data structure
In general, the phrase "functional-style programming" means the behavior of functions should be like that of mathematical-style functions - no side effects.

In programmers' point of views, functions may be used like other values: passed as arguments, returned as result, and stored in data structures. That means we can use the :: operator to create a method reference, and lambda expressions to directly express function values. For example:
//method reference Function<String, Integer> strToInt = Integer::parseInt; //lambda expression Comparator<Integer> comparator =…

Functional programming in Java 8

In my previous post, we discussed about why we should consider to use functional programming. Now, let's delve into what functional programming in Java is.
What is pure functional programming?Shortly, functional programming is programming using functions. A function corresponds to a mathematical function such as log, sin. Basically, it takes zero or more arguments, give one or more result, and has no side effects.
We can't completely program in pure functional style in Java Why?  For example, calling Scanner.nextLine twice typically gives different result.

So, it's just called "functional-style programming". How is that?
- There is no mutating structures visible to callers. That means your side effect may not be visible to a program, but it's visible to the programmer in terms of slower execution.
- A function or method shouldn't throw any exceptions (follows the concept "pass arguments, return result"). We can use types like Optional<T> i…

Why Functional Programming Matter

What issues do we concern when implementing and maintaining systems? One of the most concern is debugging during maintenance: "this code crashed because it observed some unexpected value."

Then, it turns out that the ideas of no side effects and immutability, which functional programming promotes, can help.
Shared mutable data is the root cause Shared mutable data are read and updated by more than one of the methods. Share mutable data structures make it harder to track changes in different parts of your program.

An immutable object is an object that can't change its state after it's instantiated so it can't be affected by the actions of a function. It would be a dream to maintain because we wouldn't have any bad surprises about some object somewhere that unexpectedly modifies a data structure.
A new thinking: Declarative programming There are two ways thinking about implementing a system by writing a program.

- Imperative programming: has instructions that …