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:
Scala lets you add new methods to existing classes that you don’t have the source code for, i.e., classes like
Int, etc. For instance, you can add a method named
hello to the
String class so you can write code like this:
which yields output like this:
Admittedly that’s not the most exciting method in the world, but it demonstrates the end result: You can add methods to a closed class like
String. Properly (tastefully) used, you can create some really nice APIs.
In this article I’ll show how you can create implicit methods (also known as extension methods) in Scala 2 and Scala 3 (Dotty).
This page contains a collection of over 100 Scala
String examples, including strings functions, format specifiers, and more. I don’t provide too many details about how things work in these examples; this is mostly just a collection of examples that can be used as a reference page or cheat sheet. (I do show the output of most examples.)
First, here are some basic uses of the Scala
String class to help get us warmed up:
Scala FAQ: How do I find API documentation (Scaladoc) for the Scala
As a quick note today, if you ever need some examples of how the Kotlin collections methods work, I hope these examples are helpful.
First, here’s some sample data:
val a = listOf(10, 20, 30, 40, 10) val names = listOf("joel", "ed", "chris", "maurice")
This page contains a large collection of examples of how to use the Scala Map class, including most of the methods that are available on a Vector. (Currently well over 100 examples.)
The Scala List class as an immutable, linear, linked-list class. It’s very efficient when it makes sense for your algorithms to (a) prepend all new elements, (b) work with it in terms of its head and tail elements, and (c) use functional methods that traverse the list from beginning to end, such as