A Trade goes through 3 state changes – placed, filled, and settled. Let’s solve this simple problem using both OOP & FP approaches.
Place –> Fill –> Settle
OOP approach by mutating the state
State changes are maintained via a variable named “
…
A Trade goes through 3 state changes – placed, filled, and settled. Let’s solve this simple problem using both OOP & FP approaches.
Place –> Fill –> Settle
OOP approach by mutating the state
State changes are maintained via a variable named “
…
Java code to convert given input values from KM to Meter, Meter to Centimetres, etc. OOP approach using the strategy design pattern Step 1: Define an interface. Step 2: Define a generic “MyMetricConverter” that implements the “Converter”. Step 3: Define different strategy classes that return different conversion rates. … Read...
FP – calculate the invoice price Invoice Price = (markedPrice * (100 – discountRate)/100) + deliveryCharge Example “f.andThen(g)” is the composition of functions. This can be described as g(f()) where f() is 270.0 (i.e. 300.00 * (100 – … Read more ›...
Current Balance = Initial Amounts + Credits – Debits OOP approach using the builder design pattern Creating a constructor to take three BigDecimal values is NOT only readable, but also prone to errors as you can inadvertently supply the debit value instead of the credit value, … Read more ›...
int result = input / divideBy will throw an unchecked exception if divideBy = 0. OOP error handling by throwing exceptions In OOP errors are handled by throwing & catching exceptions. Here is a very simple OOP example. Step 1: Define an interface. … Read more ›...
FP error handling – checked exceptions The code below breaks the flow of execution as the input “ABC” throws a checked exception – “NumberFormatException”. Outputs: What if you want to just mark all the inputs that throw a “NumberFormatException” as -1, … Read more ›...
This extends Future Vs. CompletableFuture interview Q&As with Java 8 functional programming (i.e. FP). Q1. Why was CompletableFuture introduced in Java 8 when you already had the Future interface? A1. The CompletableFuture implements both CompletionStage<T> and Future<T> … Read more ›...
This extends Java FP – CompletableFuture monadic chaining with examples – part 1. f and g are processed asynchronously, and then combined A “Supplier” returns a result by taking nothing as an argument. “CompletableFuture.supplyAsync” executes a “Supplier” without blocking. This means executes it asynchronously. … Read more ›...
handle(…) returns a result or an exception A dummy exception is thrown from “f” to demonstrate an exceptional scenario. The “java.util.concurrent.ExecutionException” potentially thrown by f.get(), g.get(), and h.get() is a checked exception. Hence catching it and re throwing as an unchecked (i.e. RuntimeException) exception. Output “Reached here…” … Read more...