Blog Archives
1 2 3 4 5 6

01: Coding “Java way in Scala” Vs “Scala way in Scala”

Example #1: Read from a list & write to a list

Java Way in Scala

Output: List(Java Programming, Scala Programming, Ruby Programming)

Scala Way in Scala: Using the “map” functional combinator

Immutable code as shown below using the “map” function.

Scala Way in Scala: Using recursion

What does y :: ys mean?Read more ...

01: Scala Functional Programming basics – pure functions, referential transparency & side effects

Q1. What is a pure function?
A1. A pure function is a function where the following conditions are met:

1) The Input solely determines the output.

2) The function does not change its input.

3) The function does not do anything else except computing the output. This means NO I/O, which is no reading from database, file or input console & no writing to a database, file or output console. No modification of a global variable that is outside the function.

A pure function takes the input & computes the output and returns the output. If a function does anything else outside the scope of computing the output value from the input value like writing the output to a console, read from a file or database, change a global variable, etc then these are known as the side effects. A pure function is a function that is free from these side effects.

Side effects are when

1) Method return a unit (e.g. println).

2) Method change the application state.

3) Method that talk to outside world like files, databases, network, web services, etc.

4) and more

Q2. Is below function addOne(….) a pure function?

A2.Read more ...


02: Coding Scala Way – Recursion & Iterator in FP

This extends Coding Scala Way – Part 1 Can you write the following code written in Java the Scala way? Java coding question on recursion and generics Q. Can you write Java code to compute a collection of numbers supplied to it? The computation could be addition, subtraction, etc. Use…

Read more ...

02: Scala FP basics: First-class function, higher order function, statements Vs expressions, immutability & recursion

This extends Scala Functional Programming basics – pure functions, referential transparency, side effects, etc.

Q1. What is a first-class function?
A1. A first-class function is a function that can be treated like a value.

1) Can be assigned to a variable as a value can be assigned to a variable.

Same as below as return types after “:” are inferred in Scala.

2) Can be passed around as arguments to other functions as values can be passed around.

Read more ...

02B: Scala FP basics: Explain the below Scala code

Q. Can you explain the concepts used in the below code?


A. The above code runs the function “(x:Int) =x + 2” given number of times. In the above example it runs 3 times producing (((5+2)+2)+2) = 11. It uses the functional programming concepts like Higher…

Read more ...

03: Coding Scala Way – Lazy Eval

Example #6: Lazy initialization & evaluation of code Scala support “views”, which are collections that can be lazily evaluated. One use for views is when you need to traverse a collection of values which are expensive to compute and you only need one value at a time.

Read more ...

03: Q1 – Q6 Scala interview Q&As on Functional Programming

100+ FAQ Scala interview questions answered with code. Learn more about FP – 6 tips to transforming your thinking from OOP/imperative programming to functional programming (i.e. FP). Scala is very popular in Apache Spark programming to process Big Data. 70+ FAQ Bigdata & Hadoop interview questions & answers with diagrams…

Read more ...

04: Coding Scala Way – Null checks with Option-Some-None

Example #10: Null checks with “Option-Some-None”

Nulls in Scala is a code smell, and a better way to handle null values is with an “Option“, which has 2 sub classes “Some” that has a value and a “None” that does not have a value. Instead of checking for “myVal != null”, you can use “pattern matching” to deal with null values.

You can alse use methods like “isDefined” and “isEmpty“, but pattern matching shown above is cleaner.… Read more ...

04: Currying, partial application & functional composition in Scala

Let’s see how we can wire up functions with & without currying, and then touch on functional composition. Without currying Here are simple functions example that add, add10, multiply and multiplyBy10 without currying.


With currying & partial application with functional composition

Read more ...

04: Q6 – Q12 Scala FP currying, closures & partial functions interview Q&As

This extends Q1 – Q5 Scala Functional Programming basics interview questions & answers Q6. What is a curried function in Scala? A6. Currying is the technique of transforming a function with multiple arguments into a function with just one argument, and the other arguments are curried. Currying is when you…

Read more ...

1 2 3 4 5 6

500+ Enterprise & Core Java programmer & architect Q&As

Java & Big Data Tutorials