# count

Scala: How to use higher-order functions with Option (instead of match expressions) alvin October 27, 2019 - 5:36pm

Table of Contents1 - Sample data2 - From match expressions to higher-order functions3 - Notes4 - Resources

I originally wrote a long introduction to this article about Scala Options, but I decided to keep that introduction for a future second article in this series. For this article I’ll just say:

• idiomatic Scala code involves never using null values
• because you never use nulls, it’s important for you to become an expert at using Option, Some, and None
• initially you may want to use match expressions to handle Option values
• as you become more proficient with Scala and Options, you’ll find that match expressions tend to be verbose
• becoming proficient with higher-order functions (HOFs) like map, filter, fold, and many others are the cure for that verbosity

## A Play Framework Anorm SQL SELECT query that queries for a single value

As a quick note today, here’s an example Play Framework Anorm SQL SELECT query that queries for a single value:

Scala Vector informational and mathematical methods (syntax, examples) alvin May 21, 2018 - 3:11pm

This page contains a collection of examples of how to use Scala Vector class informational and mathematical methods. Note that these same methods will also work with a Scala Seq, including IndexedSeq.

## Informational and mathematical methods

As the name implies, these methods let you get information about the contents of a Vector, or perform mathematical expressions on a Vector.

## A SQL select group by, order by, and count query

As a note to self, this is how I wrote a “group by” and “order by” SQL query that gives me the number of times each country code occurs in a database table named url_clicks:

select country, count(1) as the_count
from url_clicks
where url_id=6
and country != ''
group by country
order by the_count desc

Results of this query look like this:

US, 50
CA, 30
GB, 15

That tells me that 50 people in the US clicked on the link with the id=6, 30 people from CA clicked on the same link, and 15 people from GB clicked on that link as well.

## How to get the count of detail rows in a master/detail SQL SELECT query and subquery

As a brief note to self, I just used this SQL SELECT query to show both (a) master/summary information about each URL row in the urls database table, and also (b) detail information in the form of the number of rows in the url_clicks database table for each row in the urls table: