Posts in the “scala” category

Simple FP book: No monads. No monoids. No category theory. Just FP code.

How about a simple functional programming book written with this style:

No monads.
No monoids.
No functors.
No category theory.
Just some Java & Scala code.

Robert Martin’s tweet about writing a new simple functional programming book (in Clojure) was shared with me here, and I like his initial wording on not worrying about monads, monoids, functors, and category theory. (I believe it was Eric Torreborre who shared a similar sentiment some time prior to this, that functional programming should be taught separate from category theory.)

I said almost the exact same thing five years ago in the beginning of my book, Functional Programming, Simplified, and then in Learn Functional Programming Without Fear I show how to start with Java/OOP code and slowly transition to a functional programming style with Scala/FP examples. Both books focus on simple, pragmatic functional programming source code examples, without getting lost in FP theory.

Notes: What Functional Programming Can Learn From Object-Oriented Programming by John De Goes

These are my abbreviated notes (Cliffsnotes) from watching the talk, What Functional Programming Can Learn From Object-Oriented Programming, by John De Goes. As a warning, the notes are incomplete, and just a reminder to me of some of the best and most important parts of the talk.

Relatively early on he notes that there are multiple ways to do Scala, but only one way to write Go code. This is both a pro and a con.

OOP: How to learn FP the fastest way

If you’re a computer programmer who’s using object-oriented programming (OOP), and:

  • you want to learn functional programming (FP), and
  • you want to learn FP as fast as possible (without functors, monads, and all the other category theory), then
  • the 7-step process shown in this image is the fastest path I know.

If you should also want a guidebook on your journey, my new best-selling book, Learn Functional Programming Without Fear, can help guide the way. At the time of this writing, the eBook is currently just $10 and the paperback is only $20.

(You’re also welcome to respond to my LinkedIn post, which is where this image comes from.)

Combining the best of FP and OOP with Scala

I like what John De Goes shares in this talk about how to combine the best of FP & OOP. The “best of” portion of the talk starts about 36 minutes into the talk, which is where that link starts.

FWIW, I share almost all of the same information in my book, Functional Programming, Simplified, though I will admit that I probably don’t articulate it quite as well as his speech. (That being said, I wrote the book over five years ago, and it also includes some decent examples of domain modeling and code organization with Scala, FP, and OOP.)

Best 2022 holiday programming gift book (for Java/Kotlin/OOP developers)

As of late-November, 2022, my new book, Learn Functional Programming Without Fear, is currently a best-selling programming book in both Amazon’s Java and functional programming categories!

The book is intentionally written for Java, Kotlin, and object-oriented programming (OOP) developers, and I also made the book much less expensive than other FP books to make Scala 3 and functional programming as accessible as possible to everyone. As a result, I hope it makes for a terrific 2022 Christmas/holiday gift for the Java/Kotlin/OOP computer programmer in your life!

Functional error handling in Scala (Martin Odersky’s strawman proposal, 2022)

If you’re interested in functional error handling in Scala (Option, Try, Either, etc.), there’s a nice discussion at this Github pull request.

There are a few quotes that are well worth sharing for anyone interested in functional error handling in Scala. First, all of Martin Odersky’s introduction is terrific background material on the topic, so I’d start with that.

Later, Adam Warski shares this statement, which I think is a terrific summary about functional error handling with ZIO:

Scala 2.13’s ‘pipe’ and ‘tap’ chaining operations

The Scala 2.13 collections’ class updates introduced two new “chaining operations” named pipe and tap. Here’s a quick look at how they work, plus a little extra fun at the end.

pipe

pipe works like a Unix pipe, passing values to functions. For example, given this setup:

import scala.util.chaining._
import scala.language.implicitConversions

def plus1(i: Int) = i + 1
def double(i: Int) = i * 2
def square(i: Int) = i * i   // we’ll use this one later

How to create a Scala 3 infix method (and extension method)

As a brief note, this example shows how to use the Scala 3 infix annotation — @infix. This solution also shows how to use the infix annotation with Scala 3 extension methods, because that’s what I need for my current situation:

import scala.annotation.infix

extension (i: Int)
   @infix def plus(j: Int) = i + j
   @infix def times(j: Int) = i + j

Given those infix + extension method definitions, this is how you use those infix methods:

1 plus 1    // 2
2 times 2   // 4

The REPL confirms this:

scala> 1 plus 1
val res0: Int = 2

scala> 2 times 2
val res1: Int = 4

Before I go, here’s a note from the Dotty / Scala 3 documentation related to infix methods:

A method annotation that suggests that the annotated method should be used as an infix operator. Infix operations with alphanumeric operator names require the operator to be annotated with @infix

In summary, if you wanted to see how to create a Scala 3 infix method, particularly as an extension method, I hope this example is helpful.

How to execute AppleScript from a Java or Scala application

If you ever need to execute AppleScript from a Java or Scala application, this code shows how to solve the programming part of this problem.

Given an AppleScript command as a Scala multiline string, like this:

// press the `fn` key twice
val asCommand = """
tell application "System Events"
    key code 63
    key code 63
end tell
"""

you can execute that command by calling this executeAppleScriptCommand method:

A Scala CLI 'watch' script

In the spirit of giving back whatever I can to the Scala community, here’s a very little shell script that I named scw that lets you run the scala-cli command with its --watch option:

#!/bin/sh

# NAME:    scw
# VERSION: 0.1
# PURPOSE: a script that works like a “Unix alias
#          that requires a command-line argument”.

filename=""
if [ $1 ]
then
    filename="$1"
else
    echo "PURPOSE: Run 'scala-cli <filename> --watch'"
    echo "USAGE:   scw <filename>"
    exit 1
fi

scala-cli $filename --watch

I personally used this shell script with the exercises in my Learn Scala 3 The Fast Way! book, and I’ll include it with that book’s Github repository shortly.

How to use this scala-cli shell script

I created this script because I wanted something like a Unix alias to shorten that scala-cli command. When you’re typing that command for more than 80 lessons, every character counts. :) It works like this:

$ scw Functions.sc
4
6
Watching sources, press Ctrl+C to exit.
Compiling project (Scala 3.1.1, JVM)
Compiled project (Scala 3.1.1, JVM)
4
8

Thanks to Scala CLI, that command runs my script, and when I change the script it automatically runs it again.

How to stop JavaFX clients in SBT (“fork” setting)

As a brief note, you need this “fork in run” setting in your sbt build.sbt file if you want JavaFX client/GUI applications to properly stop:

// see: https://stackoverflow.com/questions/5137460/sbt-stop-run-without-exiting
// OLD SYNTAX:
// fork in run := true

// NEW SYNTAX:
// https://www.scala-sbt.org/1.x/docs/Forking.html
Compile / run / fork := true

Conversely, if you don’t use that setting and then you stop your JavaFX app that you started inside sbt, the app won’t stop properly and it will leave a Java process running, and at that point about all you can do is kill sbt and restart it, which gets annoying after a very short while. That setting causes sbt to create and run the JavaFX app in a new JVM. As noted in the code, I found that setting on SO.