I wanted to work through some of the core FP concepts in a more familiar context, so am going to apply some of the lessons/principles/exercises in Groovy.
Functions:
If you have done any Groovy programming then you will have come across Groovy functions/closures. As Groovy is dynamically typed (compared to Scala's static typing), you can play it fairly fast and loose.For example, if we take the square root function that is demonstrated in the Scala course, it is defined as follows:
As you can see, Scala expects the values to be typed (aside, you don't actually always need to provide a return type in Scala). But in the Groovy function it is:
A groovy function can be defined and assigned to any variable, thereby allowing it to be passed around as a first class object. If we look at the complete solution for calculating the square root of a number (using Newton's method - To compute the square root of "x" we start with an estimate of the square root, "y" and continue to improve the the guess by taking the mean of x and x/y )
So that's in Scala, if we try Groovy we will see we can achieve pretty much the same thing easily:
Recursion (and tail-recursion):
As FP avoids having mutable state, the most common approach to solve problems is to break the problem down in to simple functions and call them recursively - This avoids having to maintain state whilst iterating through loops, and each function call is given its input and produces an output.
If we again consider an example from the Scala course, with the example of a simple function that calculates the factorial for a given number.
This simple function recursively calculates the factorial, continuing to call itself until all numbers to zero have been considered. As you can see, there is no mutable state - every call to the factorial function simply takes the input and returns an output (the value of n is never changed or re-assigned, n is simply used to calculate output values)
There is a problem here, and that is as soon as you attempt to calculate the factorial of a significantly large enough number you will encounter a StackOverflow exception - this is because in the JVM every time a function is called, a frame is added to the stack, so working recursively its pretty easy to hit upon the limit of the stack and encounter this problem. The common way to solve this is by using Tail-Call recursion. This trick is simply to have the last code that is evaluated in the function to be the recursive call - normally in FP languages the compiler/interpreter will recognise this pattern and under the hood, it will really just run the code as a loop (e.g. if we know the very last piece of code in the block of code is calling itself, its really not that different to just having the block of code/function inside a loop construct)
In the previous factorial example, it might look like the last code to be executed is the recursive call factorial(n-1) - however, the value of that call is actually returned to the function and THEN multiplied by n - so actually the last piece of code to be evaluated in the function call is actually n * return value of factorial(n-1).
Let's have a look at re-writing the function so it is tail-recursive.
Now, using an accumulator, the last code to be evaluated in the function is our recursive function call. In most FP languages, including Scala, this is enough - however, the JVM doesn't automatically support tail-call recursion, so you actually need to use a rather clunkier approach in Groovy:
The use of the trampoline() method means that the function will now be called using tail-call recursion, so there should never be a StackOverflow exception. It's not as nice as in Scala or other languages, but the support is there so we can continue.
Currying:
This is like function composition - the idea being you take a generic function, and then you curry it with some value to make a more specific application of the function. For example, if we look at a function that given values x and y, it returns z which is the value x percent of y (e.g. given x=10, y=100, it returns the 10 percent of 100, z=10)The above simple function is a generic mechanism to get a percentage value of another, but if we consider that we wanted a common application of this function was to always calculate 10% of a given value - rather than write a slightly modified version of the function we can simply curry the function as follows:
Now, if the function tenPercent(x) is called, it uses the original percentage() function, but curries the value 10 as the first argument. (If you need to curry other argument positions you can also use the rcurry() function to curry the right most argument, or ncurry() which also takes an argument position - check the Groovy docs on currying for more info)
Immutability:
Immutability is partially supported in Java normally with use of the final keyword (meaning variables can't be changed after being initially set on object instantiation). Groovy also provides a quick and easy @Immutable annotation that can be added to a class to easily make it immutable. But really, there is more to avoiding immutable state than just having classes as immutable - As we have functions as first class objects, we can easily assign variables and mutate them within a function - so this is more of a mindset or philosophy that you have to get used to. For example:The first example is probably more like the Groovy/Java code we are used to writing, but that is mutating the state of the list - where as the second approach leaves the original list unchanged.
Map Reduce:
As a final note, there are some functions in FP that are pretty common techniques - the most famous of which these days (in part thanks to Google) is Map-Reduce, but the trio of functions are actually Map, Reduce(also known as Fold) & Filter - you can read more about the functions here (or just google them!), but these functions actually correlate pretty nicely to core Groovy functions that you probably use a lot of (assuming you are groovy programmers).
Map
map is the easiest to understand of the three. It takes in two inputs - a function, and a list. It then applies this function to every element in the list. You can basically do the same thing with a list comprehension however.Sound familiar? This is basically the .collect{} function in Groovy
Reduce/Fold
fold takes in a function and folds it in between the elements of a list. It's a bit hard to understand at first
This one is a bit more complicated to descibe, but is the same as the .inject{} function in groovy
Filter
filter is easy. It takes in a 'test' and a list, and it chucks out any elements of the list which don't satisfy that test.
And another simple one - filtering out a list for desired elements, this is Groovy's .findAll{} function
As I said at the start, I am new to FP and coming from an OO background, but hopefully the above isn't too far from the truth! As I get further through the Coursera course I will try to post again, maybe with some of the assignments attempted in Groovy to see how it really stands up.
Some useful references:
Groovy docs on FP: http://groovy.codehaus.org/Functional+Programming+with+Groovy
Coursera Scala course: https://www.coursera.org/course/progfun
Hey there,thank you so much for your writing.Functional programming is a style of programming that emphasizes the application of functions to take care of computing issues.This is in contrast with imperative programming,which emphasizes changes in state and the execution of sequential commands.I think this is an useful writing on functional programming with groovy.Thanks again for your decent post.
ReplyDelete~~~~~~~~~>>>>>>>>
hidden objects games
This is a best post to learn basic things of Groovy programming. As a developer at web app development agency, I learn many things from this page and it is very helpful in my job activities.
ReplyDeleteI learn lots of things from these Groovy functions. This is best post for developers. iOS event app
ReplyDeleteTo check your electric bill, you must have the reference number for your connection. This is the only requirement to verify your bill. It is not possible to check it using CNIC or by name. If you aren't sure of the reference number, you may go to the closest mepco Customer Service Center, where you will be able to get your bill , by providing your CNIC number or the meter's number. To view your online mepco bill, find the reference number on the bill. It's 14 digits, placed in a box, typically at the bottom edge of your bill.
ReplyDeleteIf you are a basketball lover, owning the best portable basketball hoop will be like a dream. With it, your basketball practice will be more accessible. You can even play with your child for emotional bonding. basketballnieuws will accompany you to learn about the top-rated portable basketball hoops on the market today. Through that, you will better understand your needs to make a reasonable choice https://basketballnieuws.com/best-portable-basketball-hoop/.
ReplyDeleteHere we see a black jumpsuit completed with a white blazer and nude heeled sandals. A nice combination for working hours. Update this combo by adding a gold chain necklace and spacious black leather bag. Believe me, you have never seen such amazing creations before shoes for jumpsuit. This onesie design can be worn to pool parties, to the parks, beaches, disco clubs, as well as to work and formal meetings. Indeed, a jumpsuit is an extremely versatile piece of clothing that can be completed with different accessories and jewelry.
ReplyDeleteDidn't get your recent WAPDA electric bill or lost it? Don't worry about it and you can obtain the same duplicate copy from your mepco bill online without visiting the WAPDA office. You can easly verify the bill amount by simply entering the 14 digit code and then click on Get Your online mepco bill or Get the mepco Duplicate bill" link. Pay your bill by printing a duplicate of the bill at any bank.
ReplyDeleteAre you looking for lesco bill, just visit lesco bill
ReplyDeletepesco bill calculator can be used for pesco bill calculator
ReplyDeleteCalculate your lesco bill through the lesco bill calculator & Manage your budget with this calculation
ReplyDeleteYour post is like a guiding star in the night sky, helping us navigate through uncertainty. Telenor Sim Lagao
ReplyDelete