Scala: Put a long one line operation on collections in it’s own method

Scala collections are extremely powerful and once you get used to all the methods and different collections you don’t want to get back to other programming languages that don’t offer all that power.
Often you can write rather complex operations in just one line instead of many loops and tempory variables thanks to methods like filter, map, foldLeft, reduceLeft, etc.

But while it is easy to chain such method calls for powerful transformations it can take a while for someone new to Scala to understand your code:
Let’s take the following code as an example. It is a contrived example about how to build a URL query string from a Map[String, String].

The code is a “one liner” even though it is better to write it over several lines as I did here to make it more readable.
What the code does is build a query string for a URL from keys and values in a Map.
The code is not really difficult.
In the first line uses the filterKeys method to filter out only the books that belong to the fantasy category. The second line does the URL encoding using UTF-8, the builds the query string using foldLeft and at the end calls dropRight to get rid of the last “&”.

This code is quite easy to understand when you have some experience with Scala. You could come up with a similar code in Ruby or even Java 8 using the new lambdas.

But I recommend putting code like this in it’s own method with a good name. This makes it more readable for people new to your code – and to you too when you get back to the code after few months.

The following code shows how this might look like:

Calling the method makes the code easier to read and everyone who reads your code does not need to know the details of how you build the query string.

Always try to make the code as easy to read as possible.

Note that I prefer to add the return type to the method declaration. This is not necessary because the Scala compiler can figure it out automatically but it makes the code easier to read and when you change it later and make a mistake and return a different data type the compiler will tell you immediately.

Maybe cheap oakleys four out of every five businesses actually paid me. I’d post a review, they’d send money through PayPal, and that’s the end of it. Usually between $10 fake ray ban sunglasses and $25 per review, but you have to space it out. I tried to do three restaurants and two services a week from all of my accounts, but most weeks only managed two in total. You can’t exactly make a living on it, but it brought in nice side money. I got as much as $200 a week, and one car detailing place gave me $50.The 1970s’ decade was an important period for America and the rest of the world. Technological innovations, starting from the introduction of floppy disks to the launch of Microsoft, Sony Walkman and VCR, first test tube baby to the release of the sci fi/fantasy movie Star Wars was witnessed. There was a lot more innovation in the field of science and technology, entertainment, sports, political news, and arts. However, the decade was also cheap oakleys marred by the gruesome incident in Munich where Israeli athletes were brutally killed by terrorists.It’ll take a bit of work, lots of practice, and surely some anxiety filled missteps, but in the end, you’ll be able to transform yourself into the impactful, persuasive, credible, positive, inspiring person you want to be. Of course, as Flip Wilson noted, “You can’t expect to hit the jackpot cheap nfl jerseys if you don’t put a few nickels in the machine.”IntroductionThis study was designed to achieve two objectives simultaneously: 1) to investigate optical coherence tomography (OCT) correlates in the retinal pigment epithelium (RPE); 2) to demonstrate the feasibility of in vivo OCT monitoring of RPE dynamics at sub cellular level. Located posterior to the photoreceptors at the back of the eye, the RPE is a simple cuboidal epithelium attached firmly to the neurosensory retina at the apical aspect and Bruch’s membrane at the basal aspect. BackgroundIn 2013, Kroenke and Stockbridge Capital started discussions on a stadium in the company’s planned development at the Hollywood Park racetrack site. Kroenke purchased 60 acres of nearby land in January 2014 and, a year later, the partners cheap oakleys sunglasses announced the proposed stadium. Part of a 290 acre mixed use development, the land around the stadium would include offices, restaurants, shops and a 6,000 seat performance venue. In February, Inglewood’s City Council unanimously approved a ballot initiative allowing the project to move forward.Abercrombie A cheap jerseys long ralph lauren factory store time, nike huarache who jerseys from china sawed, pondering ralph lauren uk all zapatillas nike know, oakley who coach purses can burberry sale not converse chucks deceive cheap jerseys anyone. vans schuhe Learn ray ban cheap football jerseys sunglasses outlet together, michael kors bags play knicks jerseys together p90x workout schedule also.

Leave a Reply

This blog is kept spam free by WP-SpamFree.