Save Your Time on Algebraic Data Type (ADT) Creation in Scala

Why ADT?

(If you want to get straight to the solution, just click this.)
If you do Functional Programming in Scala, you probably write lots of Algebraic Data Types (ADT). If you're not sure what it is or why we use or what benefits it offers, here is a good blog post written by Charles O'farrell about The Expression Tradeoff which can give you a good idea of why we use ADT. I strongly recommend reading it.

Be careful when using BigDecimal in Scala 2.13

I was testing a typeclass instance of BigDecimal in my personal FP library using Hedgehog and found some issue in BigDecimal in Scala 2.13.

[info] - just.fp.MonoidSpec.testBigDecimalMonoidLaw: Falsified after 3 passed tests
[info] > a1: A: 1.0000000000000002E+36
[info] > a2: A: -9223372036854775808
[info] > a3: A: -9223372036835934692
[info] > monoidLaw.associativity

It says the test in associativity law failed which means
((a1 |+| a2) |+| a3) is not equal to (a1 |+| (a2 |+| a3)).

That was a Monoid typeclass instance for BigDecimal but it also means the following code returns false.

Improvement in Scala 2.13 and sbt 1.3

There was Scala 2.13.1 release recently and sbt 1.3.0 was release in early September. sbt 1.3.1 was also released just about two weeks after sbt 1.3.0 release.

I'd like to talk about some improvement in Scala 2.13 (not just 2.13.1). Yet, before doing that, I'd like to quickly mention one thing that sbt 1.3.x has much better than the previous versions.

Trying Scala Script - Script to Create a Symbolic Link to JDK on Mac OS X

I used to use Ubuntu Linux most of the time, yet I'm using Mac OS X more these days. I'm quite satisfied with it, but there's one thing inconvenient. That's installing JDK. On Ubuntu, installing Java 8 (technically what I want to install is JDK 8 but since Java has really terrible versioning history, I'll just call it Java 8) is not supported by Canonical, but it can be done easily by the PPA provided by Web Upd8 (Thanks Web Upd8).

On OS X, as we all know, PPA-like solution is Homebrew (hereinafter referred to as 'brew'). I use brew to install Scala and SBT but there's no brew formula for Java. So I need to download the JDK from Oracle's Java website and install it manually or there is a brew-friendly alternative that is 'Homebrew Cask'.

So I can simply do

Java 8 - Lambda Expressions, Method References and Default Methods

Java 8 - Lambda Expressions, Method References and Default Methods

More information will be added later.

Watch "Java 8 - Lambda Expressions, Method References and Default Methods"

1 / 9: Lambda Expressions? (Click to watch)

2 / 9: Details of Lambda Expressions

3 / 9: Closure

4 / 9: Method References

5 / 9: Default Methods

6 / 9: First Class Functions

7 / 9: Target Type Inference

8 / 9: Stream API Coming soon...

9 / 9: Examples Coming soon...

Git: Push to / Pull from Both Github and Bitbucket

  • Last Updated: 2013-08-26 * Fixed: Bug in the gitpullall() script. - 2013-07-23
  • Refactored: gitpullall() - 2013-08-26
  • Updated: gitpullall() - 2015-03-07: Now it can handle branches. So pulling on branch works. The old version always pulled from master. This version uses the name of current branch then get the same one from the remote. (e.g. remote: github, local branch: some-fix <=pull= github/some-fix)
  • Added: gitpullall() for Z shell (zsh) - 2015-03-07

Both Github and Bitbucket are good SCM hosting services. For some reason, you probably want to migrate your project from Github to Bitbucket or vice versa. It's easy because you just need to change the remote repository info. Sometimes, you want to keep your code in both places which means you want to push it to both Github and Bitbucket.

Pushing to both Github and Bitbucket is easy.

Java Generics: Generics in Real Life Programming

It is surprising to see that there are still many people who do not use generics even when they are using JDK 5 or higher. I can often see it especially when I teach some Java programming subjects at uni. Generics can be very useful and I use it a lot in my libraries and projects. So I'd like to talk about generics but not a basic knowledge of it. I will talk about Java generics in real life programming and how it can be useful as well as some unusual cases may occur and annoy you. I am also going to explain some common cases of generics usage in my next post that I have plan to write soon (hopefully). Then I'm going to write about more complex cases in the sense that it's complex when it's designed but rather simple when it's used, and it's also more practical. It means I'll probably write two more posts about generics so stay tuned if you're interested.