syntax

The SBT syntax to add multiple library dependencies (Scala)

As a quick note, the Scala Simple Build Tool (SBT) syntax to add multiple library dependencies is this:

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.1.1",
  "com.typesafe.akka" %% "akka-remote" % "2.1.1"
)

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

How to write a Java method that returns a generic type (syntax) alvin February 7, 2017 - 2:45pm

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:

How to create a Scala ArrayBuffer

As a quick note, this is how you create a Scala ArrayBuffer:

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 ArrayBuffer.

While I’m in the neighborhood, here are some other ways you can work with ArrayBuffer:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Scala ‘for’ loop examples and syntax

Table of Contents1 - Example data structures2 - Basic for loop examples3 - Generators in for loops4 - for loop generators with guards5 - Scala for/yield examples6 - Scala for loop counters (and zip, zipWithIndex)7 - Using a for loop with a Map8 - Multiple futures in a for loop9 - foreach examples10 - Summary

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.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Markdown comments syntax: Comments that won’t appear in generated output

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:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Explaining Scala’s `val` function syntax

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.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

How to write a Scala method that takes a simple generic type

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.”

Problem

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 Seq[A] parameter.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

How to manage project dependencies with SBT (Simple Build Tool)

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).”

Problem

You want to use one or more external libraries in your Scala/SBT projects.

Solution

You can use both managed and unmanaged dependencies in your SBT projects.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

How to create a method that returns a function in Scala

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.”

Problem

You want to return a function (algorithm) from a function or method.

Solution

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 String:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.