Ben Hutchison: Direct syntax - Make Functional Programming in Scala Fun Again!

10 views
Skip to first unread message

Ben Hutchison

unread,
Jun 6, 2025, 12:05:55 AMJun 6
to Melbourne Compose Group
The Melbourne Compose meetup this month is Thursday June 19th 6-8pm, at our usual venue the Kathleen Syme Centre, 251 Faraday St, Carlton.

Ben Hutchison will lead a discussion about "Direct syntax - Make Functional Programming in Scala Fun Again!"

We'll study the example below, written as a classic for-comprehension and then again, in direct form.

The for-comprehension is burdened with unnecessary boilerplate code, and the if-statement requires noisy, meaningless else branch. The second version using direct syntax is akin to typical non-functional style, as we might see in an object-oriented or imperative language like C#. Did FP just reinvent imperative programming? ... or is there more to the story?

```
def fetchStoreRecord[F[_]: {Async as A, UUIDGen, Session}](id: Long): F[Unit] =
  for (status, optRecord) <- queryRecordServer(id)
      _ <- if status == 200 && optRecord.isDefined then
          for id <- newUUID
              _ <- saveRecord(id -> optRecord.get)
          yield ()
        else A.unit
  yield ()


def fetchStoreRecordDirect[F[_]: {Async, UUIDGen, Session}](id: Long): F[Unit] =
  async[F]:
    val (status, optRecord) = queryRecordServer(id).await
    if status == 200 && optRecord.isDefined then
      saveRecord(newUUID.await -> optRecord.get).await
```
https://gist.github.com/benhutchison/ef295940a19567360fc09b269a6d011e

We'll spend the session understanding what's going on in the above example.

This will take us down a rabbit hole to meet Effectful Values, a key idea in functional programming, and then on to the question of how to express compositions of them. And the rest is just syntax ;)

Arrive from 530pm for conversation & socialising, we'll begin the presentation around 630. We'll probably go for dinner on Lygon st afterwards .

As always, newcomers most welcome. Reach Ben on 0407 990 094 on the night if you have any issues finding the venue.

Ben Hutchison

unread,
Jun 6, 2025, 12:07:10 AMJun 6
to Melbourne Compose Group
Oh, I forgot - if you're coming, please register with Luma on the event page: https://lu.ma/4ico6x1x
Reply all
Reply to author
Forward
0 new messages