If you need to copy a directory on Unix/Linux and want to preserve the date/time information while copying the directory and files, use the
-p option to save the date/time information, and the
-r option to copy the directory properly. For instance, I just used this
cp command to copy a directory named OldDir to a new directory named NewDir, while retaining all of the date/time file information:
Short source code examples
If you need to copy a directory on Unix/Linux and want to preserve the date/time information while copying the directory and files, use the
As a brief note today, here’s an example of stackable modifications in Scala.
What is ‘super’ in Scala when you mix in traits?
Lately I was curious about what
super means when you mix Scala traits into a class or object. A simplified answer is that
super refers to the last trait that’s mixed in, though I should be careful and note that this is an oversimplification.
This can be demonstrated in an example that uses both inheritance and mixins with traits. Given this combination of traits and classes:
If you ever need to left-justify a multiline Scala string, the solution is to use the
stripMargin method, as shown in this example:
def getWordFormatted(s: String): String = { s""" | |Word of the Day |--------------- |Word: $s """.stripMargin }
The | character has a special meaning when used in conjunction with
stripMargin, so that method results in a string like this being output:
As a note to self, the Scala 3 “Dotty” procedure syntax is going to look like this:
def f() = { ... } def f(): Unit = { ... }
This Scala 2 procedure syntax is being dropped:
def f() { ... }
You can find the official Dotty procedure syntax page at this link.
As for me, I’m adding this page right now because I noticed today that this procedure syntax (without the parentheses) also works with Dotty:
I can never remember how to create a Scala 3 (Dotty) project with SBT (in early 2019), so:
# create a new Dotty project sbt new lampepfl/dotty.g8 # create a dotty project that cross compiles with scala 2 sbt new lampepfl/dotty-cross.g8 # start a dotty reply from within your sbt project $ sbt > console scala> _
Here’s what an example interaction looks like — include SBT renaming my directory name to all lowercase, which brings me no joy:
As a brief note to self, when you need to debug a chain of RxJava Observable method calls, you can use the
doOnNext method to log the current values or print them to STDOUT or STDERR with
println. Here’s an example from RxJava For Android Developers, where the debug output is logged with the Android
Log.d method: for some reason you ever need a list of people’s given names for testing your Java/Scala/Kotlin/JVM code, here’s a Java class with a sorted, static list of over 5,000 male and female given (first) names::
When I tried to open an Android project with Android 3 that I probably created with Android Studio 2 and haven’t worked on in about two years, I got this error message:
As a brief note, here’s an example that shows how to create an RxJava 2 Observable from a Java List::
I have no idea what I was thinking, but today I learned (or was reminded) that if you want the first element from a Scala
Set you should use its
head method, or
headOption. For some reason I kept thinking that
take should do the job, but you can see the results in the Scala REPL, where
take(1) returns a
Set:
As a bit of warning, this is some old Java code, but if you want to create your own Java file utilities (utility methods), this code might help you get started:
I ran into a couple of interesting things today when trying to generate random alphanumeric strings in Scala, which can be summarized like this. I won’t get into the “random” stuff I was working on, but here are a couple of examples of how to generate lists of alphanumeric/ASCII characters in Scala:
scala> val chars = ('a' to 'Z').toList chars: List[Char] = List() scala> val chars = ('A' to 'z').toList chars: List[Char] =) scala> val chars = (' ' to 'z').toList chars: List[Char] = List( , !, ", #, $, %, &, ', (, ), *, +, ,, -, ., /, a brief note today, here’s a Scala method to generate a random string that is random length and also contains blank spaces:
As a little note to self, here are some examples of how to create ArrayList instances in Kotlin while explicitly assigning their types::