count

Scala: How to use higher-order functions with Option (instead of match expressions)

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

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 alvin October 23, 2018 - 7:39pm

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)

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 alvin April 28, 2018 - 4:03pm

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:

How to use SQL SELECT, GROUP BY, ORDER BY, and COUNT (all in one)

Don’t tell anyone, but my SQL skills are pretty average these days, at best, mostly because I haven’t had to do anything hard in a while. But just now I was happy to write this little SQL SELECT query that does a GROUP BY, an ORDER BY, and a COUNT, yielding the results shown in the image:

select nid, count(nid) from term_node
where tid in (3,1,11,10,9,8,7)
group by nid
order by count(nid) DESC

I’m going to use this query — or one very similar to it — to get a list of nodes (nid) that have the most tag ids (tid) from the list of tid in the query. In theory, the nodes (blog posts) that have the most tags in common should be the most related to each other. So, in my Scrupal6 replacement for Drupal 6, this query is a way to get “related” content for a given blog post. (The tid list shown comes from node id 4, so I need to also exclude nid=4 from the results. I also need to add a limit clause to the query.)

If you ever need to do a group by, order by, and count in one SQL query, I hope this example is helpful.

Scala - How to count the number of occurrences of a character in a String

Scala String FAQ: How can I count the number of times (occurrences) a character appears in a String?

Use the count method on the string, using a simple anonymous function, as shown in this example in the REPL:

scala> "hello world".count(_ == 'o')
res0: Int = 2

There are other ways to count the occurrences of a character in a string, but that's very simple and easy to read.

Ruby command line arguments

Ruby FAQ: How do I read command line arguments in a Ruby script (Ruby command line args)?

To read command line args in a Ruby script, use the special Ruby array ARGV to get the information you need. Here are a few examples.

1) Getting the number of command line args

To get the number of command line arguments passed in to your Ruby script, check ARGV.length, like this:

The Linux wc command (word count)

The Linux word count command is named wc. The wc command counts the number of characters, words, and lines that are contained in a text stream. If that sounds simple or boring, it's anything but; the wc command can be used in Linux command pipelines to do all sorts of interesting things.

Let's take a look at some Linux wc command examples to show the power of this terrific little command.

An AppleScript “list size” example

AppleScript list FAQ: How do I get the AppleScript list size, i.e., the size of an AppleScript list (or number of items in a list)?

To get the count of the number of items in an AppleScript list, use this syntax:

set myList to {"Problem", "There was a problem", "Bummer"}
set listSize to count of myList

The variable listSize will now contain the AppleScript list size (the number of items in the AppleScript list named myList).