As a quick note, if you need some examples of the syntax of how to write a Java method that returns a generic type, I hope these are helpful:
As a quick note, this is how you create a Scala
import scala.collection.mutable.ArrayBuffer var fruits = ArrayBuffer[String]() var ints = ArrayBuffer[Int]()
The key there is that the keyword
new is not required before the
While I’m in the neighborhood, here are some other ways you can work with
Besides having a bad memory, I haven’t been able to work with Scala much recently, so I’ve been putting together this list of
for loop examples.
This page is currently a work in progress, and as of tonight I haven’t tested some of the examples, but ... if you’re looking for some Scala
for loop examples — technically called a
for comprehension — I hope these examples are helpful.
This image shows how to use color syntax highlighting in the Scala REPL. In short, just start the REPL like this:
$ scala -Dscala.color
Markdown FAQ: How do I create comments in Markdown? Especially comments that won’t appear in the generated output.
Part 1 of my answer is that technically there is no way — or at least no standard way — to create comments in Markdown documents, other than to use HTML comments like this:
This is an excerpt from my forthcoming book on Scala and Functional Programming. It’s an appendix that “explains and explores” Scala’s function syntax.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 19.2, “How to write a Scala method that takes a simple generic type.”
You’re not concerned about type variance, and want to create a Scala method (or function) that takes a generic type, such as a method that accepts a
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.4, “How to manage dependencies with SBT (Simple Build Tool).”
You want to use one or more external libraries in your Scala/SBT projects.
You can use both managed and unmanaged dependencies in your SBT projects.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.7, “How to create a method that returns a function in Scala.”
You want to return a function (algorithm) from a function or method.
Define a function that returns an algorithm (an anonymous function), assign that to a new function, and then call that new function.
The following code declares an anonymous function that takes a
String argument and returns a
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.5, “How to use closures in Scala (closure examples, syntax).”Back to top
You want to pass a function around like a variable, and while doing so, you want that function to be able to refer to one or more fields that were in the same scope as the function when it was declared.Back to top
You can demonstrate a closure in Scala with the following simple (but complete) example:Back to top