I have recently waffled between Haskell and Scala in terms of learning.  Well, I'm back on Scala. :) I've found that working with O...

January 27, 2020

My First Taste of Pattern Matching in Scala

I just got a taste of some pattern matching in Scala. It was not really like what I had scene in Elixir or Haskell, but seems straight forward enough. In this case we match on some String attributes:

What made it slightly awkward, I guess, was the have to explicitly use the "match" keyword to say "I'm going to match here", but use the full class name and attributes. I guess it is just a bit "weird" to my eyes. There very well may be slicker ways to do this when I get into the more "hard core" FP techniques that I assume I will see in this book (or another).

All in all, I'm still enjoying Scala and this is just another thing to file away in the Scala portion of my brain.

January 22, 2020

A real quick Scala tip

For my purposes of learning Scala, I generally want to follow the compile, then run model. I didn't want to get into a whole IDE, so I'm still using vim and doing this from the command line. For this to work, however, I needed to extend App, like this:

The key bits are to extend App, and the name you use for the object must be used when running it. So you could name it calc, but then you must also run it with "scala calc", not "scala Calc".

This allows you to catch typos with the compiler and do some simple testing with the asserts. Later, I'll figure out how to integrate a test suite, but not today.

January 20, 2020

More Scala, More Questions

I've started on the Scala Exercises and have learned some tiny tidbits, which lead to more questions.

One thing that was stated in a section titled "Functional Loops", is that you may use '@tailrec' to enforce using a tail call.  I had never seen this, or quite honestly, thought of such a thing myself.  I find this pretty interesting.  The page says "In Scala, only directly recursive calls to the current function are optimized."  So, it is mostly (only) about optimizations, which is more to say memory requirements will stay constant since the stack won't grow out of control.  I suspect I mostly won't notice any issues surrounding this, but ya never know.

Here's an example of the above:

I'm ready to start muddling through a project so I can learn more about how things are tested, and how to start working with Scala in something slightly bigger than tiny little trivial functions. In a section titled "Lexical Scoping" I learned that you can add a 'package' declaration to control which definitions are seen where. So, you might add a 'package foo' to a file forcing all definitions in that file under the 'foo' package and thereby limiting the scope so other packages cannot accidentally see definitions they shouldn't. The example indicated that you might have a function "Bar" in a file "foo/Bar.scala", with a 'package foo' declaration. Ok, but is that file structure enforced? My suspicion is that it is enforced, because this ends up on the JVM.

This brings up another question. Is there a utility to generate a "correct" project structure? Elixir has the 'mix' tool for this (and many other) uses. I'm only aware of the 'sbt' (Scala Build Tool) at the moment. Judging from this old SO question it isn't the solved problem I was hoping for, but I don't know much so it might be once I learn what G8 is, I guess.

Time for some more Scala.

January 18, 2020

Haskell to Scala

I had no real expectations for Haskell when I started to learn it. Now, after going through a few books on it, I've been unable to get to the point where I want to really dive in and attempt my "go to" project of building a ray tracer with it. It's been very good to see other ideas about building software, but I haven't gotten over that "hump".

There is a language that I feel differently about after only a few days: Scala. I've known of it's existence for a long time, but never really looked at it.  Then I started listening to the CoRecursive podcast, and the host, Adam, works in Scala.  Around that same time, I saw some Haskell people posting about people attempting to "de-platform" a Scala person.  Well, that person was John De Goes.  I've found him to be nothing but helpful and pretty nice online.  He's also contributed a concurrent library called ZIO to the Scala world.

So, with all that, and my slight dissatisfaction with the Haskell language (and community) I finally "committed" in my head to learning Scala.

I haven't found "the right book" yet, but I did discover the Scala exercises site.  So far, I like the way they combined reading with answering questions via fields that are checked with Scala.  I started with the track called "Scala Tutorial".  It is providing a little background and "big picture" as well as the more straight forward facts about language features.  I'll need to read a little more before I learn how to organize a project and can begin muddling through The Ray Tracer Challenge, though.

Scala seems to tick all my boxes: it is FP (and OOP FWIW), has a compiler(I just like that step), is probably pretty fast (being on the JVM), but also allows for native binaries, and has browser/Web Assembly targets.  I think the "big ideas" from Haskell are in there, so I'm hopeful and excited about it.