option

Creating a drop-down list in Play Framework 2.6

As a brief note to self before I delete this code, this is how you create a drop-down list in Play Framework 2.6:

@* adding 'size to helper.select creates a select/option area (single or multi-select) *@
@helper.select(
    form("category"),
    categories,
    'id -> "category",
    '_help -> "Select one, any one"
)

In that code, categories is passed into the Play template like this:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

Another Scala nested Option + flatMap example

After yesterday’s Scala nested Option + flatMap/for example, here’s another example of plowing through nested Options with flatMap. First, start with some nested options:

val o1 = Option(1)
val oo1 = Option(o1)
val ooo1 = Option(oo1)

Here are those same three lines, with the data type for each instance shown in the comments:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

Functional error handling in Scala

Because functional programming is like algebra, there are no null values or exceptions. But of course you can still have exceptions when you try to access servers that are down or files that are missing, so what can you do? This lesson demonstrates the techniques of functional error handling in Scala.

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

Scala: Handling nested Options with flatMap and for

Summary: In this article I show a couple of ways to extract information from optional fields in your Scala domain models. This example is a little contrived, but if you have a situation where one Option instance contains one or more other Options, this article may be helpful.

There are times when you’re creating your domain model when it makes sense to use optional fields in your case classes. For instance, when you model an Address, the “second street address” isn’t needed for all people, so making it an optional field makes sense:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

How to use `curl` to get headers from a URL

Curl FAQ: How do I use curl to get the headers from a website URL?

Short answer: Use curl's -I option, like this:

$ curl -I URL

Here's a specific example, including a real URL and results:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

How to find regex patterns in Scala strings

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.7, “Finding Patterns in Scala Strings.”

Problem

You need to determine whether a Scala String contains a regular expression pattern.

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

Notes on Scala for expressions, map, flatMap, and Option

Without any explanation, these are some of my working notes from my upcoming book on Scala and Functional Programming about a) for expressions, b) map, c) flatMap, d) Option, and e) nested flatMap and map calls.

These are equivalent (map and for)

this:

val y = x.map(_ * 2)

and this:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.