Scala, Java, Unix, MacOS tutorials (page 142)
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:
val o1 = Option(1) // Option[Int] val oo1 = Option(o1) // Option[Option[Int]] val ooo1 = Option(oo1) // Option[Option[Option[Int]]]
With this setup, just like yesterday, I can dig my way through the Options to get to the enclosed value using flatMap calls:
ooo1 flatMap { oo1 =>
oo1 flatMap { o1 =>
o1
}
}
Here’s what that code looks like in the Scala REPL:
scala> val o1 = Option(1)
o1: Option[Int] = Some(1)
scala> val oo1 = Option(o1)
oo1: Option[Option[Int]] = Some(Some(1))
scala> val ooo1 = Option(oo1)
ooo1: Option[Option[Option[Int]]] = Some(Some(Some(1)))
scala> ooo1 flatMap { oo1 =>
| oo1 flatMap { o1 =>
| o1
| }
| }
res0: Option[Int] = Some(1)
As that shows, the final result of the flatMap calls is an Option[Int], specifically a Some(1).
Just like yesterday, you can also use for-expressions to get to the enclosed value, but for today I just wanted to show this as another flatMap example.
I’ll keep trying to make monads, flatMap, and for-expressions as easy as I can, but for now I hope that’s a simple example you can experiment. (At least the setup portion of it is simple.)
Here’s a little fun with Scala functions, including the use of andThen and compose:
scala> val add1 = (i: Int) => i + 1 add1: Int => Int = <function1> scala> val double = (i: Int) => i * 2 double: Int => Int = <function1> scala> val addThenDouble = add1 andThen double addThenDouble: Int => Int = <function1> scala> addThenDouble(1) res0: Int = 4 scala> val doubleThenAdd = add1 compose double doubleThenAdd: Int => Int = <function1> scala> doubleThenAdd(1) res1: Int = 3
(Inspired by the book, Functional and Reactive Domain Modeling, and my own book, Learning Functional Programming in Scala.)
If you come here for the Scala, I’m sorry that I haven’t posted much here lately. I have my next medical procedure coming up on January 29th, and then my body decided to have a cold and then the flu.
“Developing the habit of mastering the multiple models which underlie reality is the best thing you can do.”
~ Charlie Munger
If you own a copy of Functional Programming, Simplified, and would like to report any bugs, I started this Github repo so you can do that. Just report your bug under the Issues tab.
vox.com has a story titled, In Colorado, a glimpse of renewable energy’s insanely cheap future.
Today’s word of the day is reticent. Per Google, it means, “Not revealing one’s thoughts or feelings readily. ‘She was extremely reticent about her personal affairs.’”
As a note to self, I used code like this in a Scala + JavaFX application to add a ContextMenu to a TableView:
defaddContextMenuToTableView(
tableView: TableView[Note],
tableViewContextMenu: ContextMenu,
addNoteMenuItem: MenuItem,
deleteNoteMenuItem: MenuItem,
) = {
tableViewContextMenu.getItems().add(addNoteMenuItem)
tableViewContextMenu.getItems().add(deleteNoteMenuItem)
tableView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler[MouseEvent]() {
override def handle(mouseEvent: MouseEvent): Unit = {
if (mouseEvent.getButton == MouseButton.SECONDARY) {
tableViewContextMenu.show(tableView, mouseEvent.getScreenX, mouseEvent.getScreenY)
}
}
})
}
There wasn’t anything special about the buttons, they were regular JavaFX Buttons:
val addNoteButton = new Button("add note")
val deleteNoteButton = new Button("delete note")
I did this in my Notes application, but later decided not to use a popup menu for this purpose.
LinkedIn’s engineering blog post has this article about how they use the Play Framework and Akka show online “presence indicators.”
I keep running into Maslow’s Hierarchy of Needs lately, so I thought I’d share that Wikipedia link, and this image, which was created wikimedia.org user FireflySixtySeven.
I updated my Scala Flat File Database project so it now handles newline (\n) characters. The solution isn’t perfect, but it’s a start, and makes the approach much more usable. (I didn’t need this functionality until today, so I didn’t know it was a problem.) I also updated it to work with Scala 2.12.
From The Verge, “General Motors plans to mass-produce self-driving cars that lack traditional controls like steering wheels and pedals by 2019.”
“In my whole life, I have known no wise people (over a broad subject matter area) who didn’t read all the time — none, zero. You’d be amazed at how much Warren reads — and at how much I read. My children laugh at me. They think I’m a book with a couple of legs sticking out.”
~ Charlie Munger, talking about Warren Buffett and himself
Here’s a detailed talk on A brief history branch prediction in CPUs.
The Denver Post has an article about how the Broncos are (finally) hiring more coaches, hopefully to teach “technique” to their players. They’ve been horrible at developing players under the Elway regime, and hopefully this is a positive sign.
When I owned my software company I learned how important training was. At first we hired people who were generally experts at what they did, but as we tried to expand we realized that not everyone was an expert, or, if they were an expert at web development using Framework A, they weren’t an expert at Java Swing development, or vice-versa. I’m not saying we always did a good job at training (in large part because some of the initial hires didn’t think it was necessary), but over time we learned and tried.
“Stay away from negative people. They have a problem for every solution.”
~ Albert Einstein
I was just talking to a woman I’ve known for a few years, and she said, “I didn’t know your last name was Alexander. I love that name.” I didn’t have the heart to tell her that my family got it from a government employee at Ellis Island.
“Don’t ask yourself what the world needs. Ask yourself what makes you come alive, and go do that, because what the world needs is people who have come alive.”