Tail Recursion – SCALA

This is a two part series on Recursion, please complete part-1 before you proceed further.

Why do we need a Tail-Recursive functions?

Simple recursion creates a series of stack frames, and for algorithms that require
deep levels of recursion, this creates a StackOverflowError.

How to Overcome this?

Continue reading


Recursion – SCALA

What is a recursive function?

A recursive function is a function that calls itself.

Why do we need to write recursive functions?

The short answer is that algorithms that use for (or any) loops require the use of var fields, and as you know that pure functions should not use any variable fields. so to write a pure function one should use recursive function.

Visualizing lists


Continue reading

Functions Can Have Multiple Parameter Groups

Scala lets you create functions with multiple parameter groups, syntax is quite simple and similar to any regular function like below

//regular function
def sum(x: Int, y: Int, z: Int): Int = x+y+z

//function with multiple parameter groups
def add(x:Int)(y:Int)(z:Int): Int = x+y+z

//difference in function calls
sum(1,2,3) //calling a regular function
add(1)(2)(3) //calling a function which has multiple parameter groups

Benefits of this approach:

  • They let you have both implicit and non-implicit parameters
  • A parameter in one group can use a parameter from a previous group as a default value
  • you will get to know the inside approach of control structures and you can even create one of your own.

we will see all these benefits with examples.

Continue reading

Call By Value vs Call By Name

Programming languages use evaluation strategies to determine when and how the arguments of a function call are evaluated. There are many evaluation strategies, but most of them end up in two categories:

  • Strict evaluation, in which the arguments are evaluated before the function is applied. Examples of programming languages that use strict evaluation strategies are Java, Scheme, JavaScript etc.
  • Non-strict evaluation, which will defer the evaluation of the arguments until they are actually required/used in the function body. Haskell is probably the most popular functional programming language that uses non-strict evaluation.

There are also languages that support both strict and non-strict evaluation strategies and Scala is one of them.

Continue reading

Spark 2 & HIVE Integration

For my recent use case I had a requirement to integrate spark2 with hive and then load the hive table from spark, very first solution I found on Google was to move the existing hive-site.xml file to spark conf directory, but this alone would not beĀ  sufficient for complete integration and yes i had spent some couple of hours to find the exact solution, here are the consolidated steps for you.

Continue reading

About this blog

Hello Everyone, This is Phanidhar Swarna the hadoop enthusiast, In the process of learning hadoop i do faced few hurdles for searching the right content(at-least for a beginner) on the web, one such case is setting up an Apache Hadoop cluster in docker containers. So for the scenarios which ever i feel lack of proper sources on the web i do post here.