Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Scala for the Working Class Java Programmer (Part 2)

DZone's Guide to

Scala for the Working Class Java Programmer (Part 2)

Lesson two for Java-based Scala initiates: lambdas and maps and how they play a role in higher order functions, all for a better understanding of functional programming.

· Java Zone
Free Resource

Never build auth again! The Okta Developer Platform makes it simple to implement authentication, authorization, MFA and more in Java applications. Get started with the free API.

Before we dive in, some questions on vocabulary.

  • lambda: Who named lambda lambda? Why did they call it lambda? Is it simple or complex?

  • map: What is it? Why name map... map? Why did they call it map? Is it simple or complex?

  • functor: Why name functor a functor? What is it? Why did they call it a functor? Is it simple or complex?

In Part 1, we covered some background on FP, why we should use, why not, where it's strength is, and where it's weaknesses are. In this part (2), we are going to move on and discuss more FP terminology.

Lambda

Is lambda a function? Just an anonymous function? A subset of mathematics? Both? Let's do some research:

Wikipedia says:

Anonymous functions originate in the work of Alonzo Church in his invention of the lambda calculus in 1936, before electronic computers, in which all functions are anonymous.[2] In several programming languages, anonymous functions are introduced using the keyword lambda, and anonymous functions are often referred to as lambdas or lambda abstractions. Anonymous functions have been a feature of programming languages since Lisp in 1958, and a growing number of modern programming languages support anonymous functions.

So what can we learn from this?

  1. lambda -> anonymous function.
  2. lambda calculus -> give me a computation and I'll represent it in lambda calculus as anonymous functions.
  3. lambda -> a Greek letter Alonzo Church chose to represent those functions or the binding of variables to function.

In addition. According to this StackOverflow answer with 812 upvotes:

Lambda comes from the Lambda Calculus and refers to anonymous functions in programming. Why is this cool? It allows you to write quick throw away functions without naming them. It also provides a nice way to write closures. With that power you can do things like this.

So to sum up:

  1. lambda -> anonymous function.
  2. lambda calculus -> computations expressed as math functions.
  3. lambda -> Alonzo Church represents a function with this Greek letter.

Map

If you tell a non-FP developer, "Hey, I just wrote a function and I named it map, what do you think it's doing?" He might answer, "Maybe something with Google Maps?" Well, actually you could name the map traverse or something like that. The thing is that if FP is closely related to math, then we had better stick with the mathematical terms — as awful and non-descriptive as they are.

In our case, map is actually a good name. It's mapping from one item in our source structure to another item in our destination structure. You cannot really deduce from only the name that the destination structure has the same shape as the source structure, but if you talk to mathematician, they can deduce it. And we love mathematicians' deductions. The great book Scala Design Patterns By Ivan Nikolov says:

Following common conventions would make things much simpler

And this is exactly what the map name is all about.

Do you write loops? You do this all day, right? Do you write loops that convert each item in a list into another? Well, map is exactly that, nothing special here, you do this all day.

map — are you iterating something and applying a function to each item? Are you looping too much? Maybe all the stuff you are looping on can inherit from something? Let them inherit from map.

Martin Fowler, in his great map article, has a great image for maps:

Wikipedia's mathematical declaration of maps is:

In many programming languages, map is the name of a higher-order function that applies a given function to each element of a list, returning a list of results in the same order. It is often called apply-to-all when considered in functional form.

Here is a map for a Scala list:

final def map[B](f: (A) ⇒ B): List[B]


The first question that comes to mind is, we have just defined a map to take a type parameter [B] , but what about type parameter [A]. Why didn't we define it, too?

Well, if you look at the definition of a List trait itself, you would see that [A] is the type of the list, so it's already defined.

To define a map in Haskell on a list, denoted by [] , you do the following, which let's you see the whole picture in a very compact way.

map :: (a -> b) -> [a] -> [b]


So, we have an input function from a to b and we transform list [a] to list [b] — now you see why Haskell is more compact and much easier to learn FP concepts with, as I mentioned in Part 1. But once you get the hang of Scala, it's rather good also.

Now, in almost all languages, you can see the same thing, transforming from one kind of element to another — and the shape stays the same for you. For example, in Ruby:

[1,2,3,4].map {|i| i + 1}
# => [2, 3, 4, 5]


And in Clojure:

(map #(+ % 1) [1 2 3 4])
;; => (2 3 4 5)


So we just take each element and apply the map higher order function, which takes another function and it does its mapping over the list, reeturning a new list or the same object of the same type.

Conclusion

I think we have covered the basics terms here like map and lambda, which give us the basis for functional programming. We have seen our first higher order function map, which takes another function, which is pretty awesome. In the next post, we will continue on with functor and friends.

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
scala ,functional programing ,java ,lambda ,tutorial

Published at DZone with permission of Tomer Ben David, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}