Friday, November 13, 2020

Finally, C# 9 record, the equivalent of Scala's case class

While C# is a wonderful programming language, there is something that I would like to see to make our life programmer easier. If you are familiar with the Scala programming language you would know about "case class". 

Object Comparison

The case class in Scala is designed to support immutable data modeling. It provides object comparison out of the box without you need to compare each property in the object. For example in C# you have a Person class as below:

Thursday, October 8, 2020

Writing the correct object equality

 While working with scala, object equality is a very important feature that we get for free while in the other programming languages such as C# or Java is quite challenging to implement. In scala, there is the case class, which is just like a normal class but is used to represent data only, it just like POJO in Java or POCO in C#.

Today, I came across an interesting article written by:

Martin Odersky is the creator of the Scala language. As a professor at EPFL in Lausanne, Switzerland, he works on programming languages, more specifically languages for object-oriented and functional programming. His research thesis is that the two paradigms are two sides of the same coin, to be unified as much as possible. To prove this, he has experimented with a number of language designs, from Pizza to GJ to Functional Nets. He has also influenced the development of Java as a co-designer of Java generics and as the original author of the current javac reference compiler. Since 2001 he has concentrated on designing, implementing, and refining the Scala programming language.

Lex Spoon is a software engineer at Google, Inc. He worked on Scala for two years as a post-doc at EPFL. He has a Ph.D. in computer science from Georgia Tech, where he worked on static analysis of dynamic languages. In addition to Scala, he has worked on a wide variety of programming languages, ranging from the dynamic language Smalltalk to the scientific language X10. He and his wife currently live in Atlanta with two cats, a chihuahua, and a turtle.

Bill Venners is president of Artima, Inc., publisher of the Artima Developer website (www.artima.com). He is the author of the book, Inside the Java Virtual Machine, a programmer-oriented survey of the Java platform's architecture and internals. His popular columns in JavaWorld magazine covered Java internals, object-oriented design, and Jini. Active in the Jini Community since its inception, Bill led the Jini Community's ServiceUI project, whose ServiceUI API became the de facto standard way to associate user interfaces to Jini services. Bill is also the lead developer and designer of ScalaTest, an open-source testing tool for Scala and Java developers.

Enjoy the article below:

https://www.artima.com/lejava/articles/equality.html



Tuesday, June 16, 2020

SBT Troubleshoot: Error on SBT Scala Compilation Because of Incompatible Java Version

Have you ever getting this kind of problem when trying to run your SBT command to compile your Scala code?


$ sbt compile
[info] Done updating.
error: error while loading String, class file '/modules/java.base/java/lang/String.class' is broken
(class java.lang.NullPointerException/null)
[error] java.io.IOError: java.lang.RuntimeException: /packages cannot be represented as URI
[error]         at java.base/jdk.internal.jrtfs.JrtPath.toUri(JrtPath.java:176)
[error]         at scala.tools.nsc.classpath.JrtClassPath.asURLs(DirectoryClassPath.scala:204)




Usually, this problem happens the first time you set up your project on your computer. This happens because you're using an incompatible Java version to compile your scala code. You can check the scala to Java compatibility in this page https://docs.scala-lang.org/overviews/jdk-compatibility/overview.html

Now, how can you fix this? You need to find out which Java version is being used by SBT and then set the correct one to use. Do you try to use java -version command?

$ java -version



While the java -version command helps you telling which java is being used by your system, SBT might use a different version. For SBT specific, you can use sbt -J-showversion command as follow:

$ sbt -J-showversion
openjdk version "1.8.0_192"
OpenJDK Runtime Environment (Zulu 8.33.0.1-macosx) (build 1.8.0_192-b01)
OpenJDK 64-Bit Server VM (Zulu 8.33.0.1-macosx) (build 25.192-b01, mixed mode)



After knowing your Scala compatibility and your SBT's Java version you can follow any of these solutions in this page https://users.scala-lang.org/t/telling-sbt-to-use-different-jdk-version/4608/10

I hope this helps anyone having a similar problem.

Wednesday, March 11, 2020

Tips: Dealing with joda time LocalDate comparison in scala using implicit class

If you are coming from C#, working with DateTime is pretty straight forward. You can compare equal, greater than, less than, or getting the day difference from two dates without any hassle. But if you are in Scala, using joda time, it's not easy to get it right at the right for the first time. the LocalData has several functions can be used to compare the date-time. They are isAfter, isEqual, isBefore, and daysBeetween to get the diff of the day.

Here is my favorite approach to deal with joda time LocalDate comparison. First, we need to create an implicit class. In C#, this is comparable to the Extension Method. We can add or extend the functionality of a class without modifying the class implementation. In this case, we will extend LocalData as below:

----------------------
import org.joda.time.{Days, LocalDate}
object LocalDateExtension {   class LocalDateExtension(a: LocalDate) {     def dayDiff(b: LocalDate): Int = {       Days.daysBetween(a, b).getDays.abs     }
    def >(b: LocalDate): Boolean = {       a.isAfter(b)     }
    def >=(b: LocalDate): Boolean = {       a.isAfter(b) || a.isEqual(b)     }
    def <(b: LocalDate): Boolean = {       a.isBefore(b)     }
    def <=(b: LocalDate): Boolean = {       a.isBefore(b) || a.isEqual(b)     }   }
  implicit def extendLocalDate(a: LocalDate) = new LocalDateExtension(a) }
------------------------

What we are doing here? In scala, we are allowed to name a method using any character such as equal, greater than, or less than signs. This way, we can use the same syntax we use in C# to compare dates. For example as below:

----------------------
import org.joda.time.{Days, LocalDate}
val a = LocalDate("2010-04-28") val b = LocalDate("2010-04-29")
print(a > b) // false print(a >= b) // false print(a < b) // true print(a <= b) // true
------------------------


Tuesday, April 2, 2019

Tips: Using SSL locally with Webpack Dev Server (via create-react-app)

I would share this for anyone interested in running a create-react-app generated app with local dev using SSL.
Webpack Dev Server checks the .env file in the root of the project for any environment variables - so you can add the following line to the file:
HTTPS=true
Now when you run the application with npm start a localhost self-signed certificate will be generated and issued.
Chrome will complain that the certificate is not valid - for which you can do either of the following:
1. Enable invalid certificates for localhost by setting the Chrome flag here (paste into the URL box): 
chrome://flags/#allow-insecure-localhost
OR...
2. Import the certificate to you Trusted Root Authorities certificate store...
First, save the certificate:
Visit the localhost dev server in Chrome, click the Not Secure warning and click the Certificate (invalid) button.
Select Details tab, and then Copy to File (I think it is labelled Export on Mac - I am on Windows).
Save the file as localhost.cer , and note the location - maybe in your project folder is a good location.
To import the Certificate (in Windows... I am sure its easy on MacOS too):
Now you need to import the certificate to the Trusted Root Certification Authorities store. For Windows, you can just double click on the localhost.cer file you saved, and choose to import for Local Machine, and then ensure you import to the Trusted Root Certification Authorities store, as mentioned.
Restart Chrome, and now you can test using SSL on localhost :-) So you can now do the PWA tests without needing to upload to a server first.
One last thing - Chrome now has Lighthouse built in so there is no need to install an extra extension - you can just select it from the Audits tab in Chrome DevTools

Sunday, August 19, 2018

Tips: How to ssh to your Digitalocean server without password

If you are tired of being asked for a password when accessing your remote droplet servers in Digitalocean, you might consider adding an rsa public key to your server so you don't need to input password anymore to ssh to your server.

Here are some useful tips that I have:

The hard way

1. Create a pair of rsa private and public key in your computer.


  • To create a pair of rsa private and public key, in a Mac computer,  in your home directory, you can go to .ssh directory by typing:
$ cd ~/.ssh
  • Once you are in the .ssh directory, you can create the pair of rsa private and public key using the following command:
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • You will be given a choice to overwrite your current id_rsa file or you can choose to create a new file. I personally want to create a new file for example id_rsa_do.
  • If you execute ls command in your .ssh directory, you would notice there will be two new files id_rsa_do and id_rsa_do.pub. It means your ssh-keygen command is working fine.

2. Copy your public key and login to your droplet

  • After creating the private and public key, you might want to copy your public key to and login to your droplet.
  • Go to your .ssh directory and execute 
$ cat id_your_rsa.pub
  • You can copy the content of that file and then login to droplet server. You will still need to provide the password at this step
$ ssh root@your_ip_address
  • Once you are in the remote server, you can go to the .ssh directory by using the same command:
$ cd ~/.ssh

3. Add your public key to .ssh/authorized_keys

  • Once you are in the remote server's .ssh directory, you can execute the following command (it will open nano editor):
$ nano authorized_keys
  • You can paste your public key in this file and then exit and save.
  • You can exit your remote server and back to your local computer's terminal.

4. Try ssh without password

  • You can try ssh-ing again to your remote server using the same command:
$ ssh root@your_ip_address
  • If still not working, and if you're having multiple ssh keys, you can specify the private key you want to use by using the following command:
$ ssh -i /path/to/private/key username@your_ip_address

OR

$ ssh-add /path/to/private/key
$ ssh username@your_ip_address

The easy way

You can follow this official tutorial from Digitalocean to add ssh key from their GUI.



Source:


Tuesday, October 31, 2017

Text Editor Tips: Some Useful Brackets Extension

Brackets is an open source and free-to-use text editor and primarily being used by web developers and designer. I mainly use Brackets for doing the web front-end development. I fell in love with the built-in live preview feature.

Here are some extensions that I found really useful:


I will update this post if I find more

Finally, C# 9 record, the equivalent of Scala's case class

While C# is a wonderful programming language, there is something that I would like to see to make our life programmer easier. If you are fam...