What is Functional Programming from a Student’s Point of View part-II

Hello again, i’m still trying to learn functional programming consepts and Scala. It’s quite challenging to learn new programming paradigm and trying to change my entire mindset. You can read part I in here.

So, this is my notes and what i understand about these consepts. I hope you like it enjoy !:)

Currying

If we don’t know how many argument we need in a function, what should we do ? This is the question and currying the answer.
Currying Function is a function that takes one argument and returns a new function which takes also one argument.

now we can call this function

Both of these function return the same value, but now we can call this function with only one parameter. And we can save the return value.

We can define the currying function like this way too. And if we want to call we can simply type

FUNCTION LITERAL

It’s an alternative syntax for function definition.

We can define a multiply function like this.

Function literals are anonymous,
but we can assign this function to a value and use it later.

This is usefull in High order functions, Like in scala implementation, fold function

op is a function literal in here, and this is an example for high order functions. What is high order functions?
Let’s get into it.

HIGH ORDER FUNCTIONS

High order functions take other functions as parameters or return a function as a result.
I’ve followed the scala-tour example and try to understand what’s going on!

val salaries = Seq(20000, 70000, 40000)
val doubleSalary = (x: Int) => x * 2
val newSalaries = salaries.map(doubleSalary) //

In this example, There is a salaries value which is a sequence.
Double salary is a function that multiplies the element by 2 (double salary is a function literal)
Map function take another function as parameter, and applying this function to salaries => neWSalaries

Map function is an example for high order functions.

In this example, urlBuilder takes two parameter, and Return another (String,String) => String functions.
This is because urlBuilder take the ssl parameter for find out if this site is safe or not. if it’s safe, the url start with the https else http. Also url take the domain name, In this way, we can build any url we want. like https://medium.com/ .
But in medium there are so many url. What if you want to search me in medium ? (https://medium.com/searchq=%C3%A7a%C4%9F%C4%B1l%20ilhan%20s%C3%B6zer , we can see the searching endpoint after .com)
Every request in medium(or any webiste), start with the same part (https://medium.com/) after that every request reach diffrent endpoint.
Thanks to high order functions, getURL value return a function that we can changing our endpoint or query later. This is a great case to understand how high order functions works.

PARTIALLY APPLIED FUNCTIONS

We can define functions with multi parameter group like this

This isn’t unusual thing. But in Functional programming word there is partially applied functions which we can call a function
just give some arguments. Functions keep these argument and return a new function which wait for remaining arguments.

Above example, we called multiply with just one argument and give the other arguments later.
I think output of x and z show how many functions remaining in the x value.
When there is no function left (or we passed all the argument that function needs) multiply works and return the x*y*z.

Also We can use partially applied function in urlBuilder example

Again we passed few argument, first argument domain complete the urlBuilder function and this function return another function which takes 2 parameter.

Thanks who read all of my notes i hope you enjoyed! Please feel free to give me any feedback or ask any question. Let’s see you guys in part 3 :)

Ege University Computer Engineering