Scala Currying & Partially Applied Functions

Currying is a concept rooted in mathematics and has also found it is way in computer science. Currying is a process of translating a function taking multiple arguments to a set of functions accepting only one argument. It is named after Haskell Curry! Currying in computer science is also done in a similar manner. You can read more on the background here. h(x)(y)(z) = f(x,y,z) h = curry(f) Let’s see how we can apply currying in Scala. Well, it’s not difficult to curry a function. See Below If you just pass addCurried(10) you would get an error. Which is not good and to be fair the function did require all the parameters to evaluate. Getting around this is simple with partially applied functions, these should not be confused with partial functions. See Below Another example of a partially applied function How about currying an existing function. That is also possible via Function.curried. It … Read more

Scala Partial Functions

As I was learning Scala and first read the phrase Partial Function had a weird thing about it. How can a function be partial??  I started digging into the documentation and online. What came out was a surprisingly very clean solution to a problem which every programmer encountered. Every function or method which is written usually applies some validation rule to make sure that the parameters which are passed to it are valid. It makes the code a bit messy. We had the logic validating the parameters and then the actual business logic which is invoked if the parameters were correct. That means that the function only works on a subset of parameters. Partial functions provide a much better solution. So now let’s look at the definition of a partial function. Quoting from Scala Doc A partial function of type PartialFunction[A, B] is a unary function where the domain does not … Read more

Scala Functions – Higher Order Functions & Function Literals

Higher Order Functions are supported by many languages including Haskell, Scala, F#, Go, Javascript. Higher Order Function is a fancy name for a function which accepts functions as parameters.!! How is that? Well – that is because functions in Scala are treated as first class values – that means in Scala they can be assigned as values, passed as parameters and returned from functions. Functions are said to be higher order functions if they do atleast one of the following Accept functions as parameters Returns functions as a result The map function is one of the most popular higher order function.  Most Scala programmers have come across this function in their lives. Before we get any deeper into functions – let’s look at another type of functions called function literals. They are more famously known as anonymous functions. Function literals are functions which do not have any name and are created, passed and returned … Read more

Scala Functions – Introduction

Introduction Functions in any language can be defined as a group of statements that constitute a logic which is performed repeatedly during the execution of a program. Functions in scala are no different.Scala identifies both functions and methods. They are both treated a bit differently. Finally, scala does support functional programming(FP) paradigms and this blog entry tries to take you through some of the FP paradigms and get you a good brief. They follow the following format def functionName ([list of parameters]) : [return type] = {  function body  return expression} An example of  a function in Scala. See Below In addition to that, scala also simplifies creation of code. In functions,  it removes the need to write boilerplate code. Every programmer likes less code. Speaking of less code some of the stuff scala does away with  is No need of semi-colons Simple functions do not need curly braces. return … Read more