This is a list of Alvin Alexander's source code snippets (simple source code examples).
Scala/SBT FAQ: How do I determine the SBT version?Scala/SBT FAQ: How do I determine the SBT version I have installed? Use the 'sbt about' command, as shown here:
/Users/Al/.sbt/plugins> sbt about
[info] Set current project to default-6315be (in build file:/Users/Al/.sbt/plugins/)
[info] This is sbt 0.12.1
[info] The current project is {file:/Users/Al/.sbt/plugins/}default-6315be
[info] The current project is built against Scala 2.9.2
[info]
[info] sbt, sbt plugins, and build definitions are using Scala 2.9.2
|
Scala tabulate method - Use on List, Array, Vector, Seq, and moreHere are a few examples of using the Scala tabulate method, used to create and populate a List: scala> val xs = List.tabulate(5)(_ + 1) xs: List[Int] = List(1, 2, 3, 4, 5) scala> val xs = List.tabulate(5)(_ + 2) xs: List[Int] = List(2, 3, 4, 5, 6) scala> val xs = List.tabulate(5)(_ * 2) xs: List[Int] = List(0, 2, 4, 6, 8) You can use the tabulate method on other Scala collections, including Array, Vector, Seq, and more: scala> val xs = Array.tabulate(5)(_ * 2) xs: Array[Int] = Array(0, 2, 4, 6, 8) scala> val xs = Vector.tabulate(5)(_ * 2) xs: scala.collection.immutable.Vector[Int] = Vector(0, 2, 4, 6, 8) scala> val xs = Seq.tabulate(5)(_ * 2) xs: Seq[Int] = List(0, 2, 4, 6, 8) If you needed to see some examples of the tabulate method, I hope this has been helpful. |
MySQL and Postgresql dependency strings with Scala, SBT, build.sbt, or the Play FrameworkIf you need to use MySQL or Postgresql dependency strings with Scala, SBT, build.sbt, or the Play Framework, these dependency settings work fine, as of October 9, 2012: "mysql" % "mysql-connector-java" % "5.1.18" "postgresql" % "postgresql" % "9.1-901.jdbc4" |
Play Framework 2 and MySQL on a different port (Using MAMP, not port 3306)I just started using the Play Framework 2 with MySQL, and because I use MAMP in my development environment, MySQL runs on port 8889, and not the MySQL default port of 3306. In short, to get the Play Framework to work with MySQL like this, I had to put these properties in the Play application.conf file: db.default.url="jdbc:mysql://localhost:8889/finance" db.default.driver=com.mysql.jdbc.Driver db.default.user=root db.default.pass=root This is a little different than what the Play documentation looks like at the moment, but I can confirm that it works, letting me connect Play to MySQL on this different port. |
How to add Lift-JSON as a Scala SBT dependencyIn your build.sbt file, add this line to declare the Lift-JSON library to be a dependency in your Scala SBT project: libraryDependencies += "net.liftweb" %% "lift-json" % "2.4-M5" It's important to note that this line will vary depending on what version of Scala you are using. It's currently the correct dependency line for Scala 2.9.1. See the URL for more information, or see this Lift-JSON URL: https://github.com/lift/framework/blob/master/core/json/README.md
|
A curl script to perform a POST to an HTTP web service using JSON dataYou can use the following curl script to POST JSON data to a web service:
curl \
--header "Content-type: application/json" \
--request POST \
--data '{"symbol":"GOOG","price":"600.00"}' \
http://localhost:8080/stocks/saveJsonStock
If you're familiar with the curl command, and JSON web services, this should make sense. Here are links to other curl shell scripts I've written:
For more information, see the (very long) curl man page. |
Scala SBT - How to force/require a specific SBT versionI ran across this code snippet in the SBT docs, and thought it was a good but hard to find tip. To force/require a specific SBT version in a Scala/SBT project, include a line like the following in a file named build.properties in your project/ subdirectory: sbt.version = 0.11.3 So, if your project directory is named /Users/Al/Foo, the file would be here: /Users/Al/Foo/project/build.properties I thought this tip was in the SBT FAQ, but at the moment I can't find it. |
Putting comments in Play Framework templatesQ: How do I put comments in a Play Framework template? What is the syntax for Play Framework template comments? A: Use @********************* * This is a comment * *********************@ You can even use comments as the first line of Play Framework templates (before the function declaration): @************************************* * The 'Hello, world' page * * * * @param msg The message to display * *************************************@ @(msg: String) <h1>Hello, world</h1> |
solution: sbt compile/run - 'deprecation warnings; re-run with -deprecation for details' messageI got a "deprecation warnings; re-run with -deprecation for details" message from SBT when trying to compile or run my project code. How do I add -deprecation to see the deprecation warnings? Add this entry to your SBT build.sbt file: scalacOptions += "-deprecation" For example, a complete build.sbt file might look like this: name := "Test 1" version := "1.0" scalaVersion := "2.9.1" resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/" libraryDependencies += "com.typesafe.akka" % "akka-actor" % "2.0.2" scalacOptions += "-deprecation" |
SBT entry for HTMLCleaner (SBT libraryDependencies syntax)Q: What should an SBT entry for the HTMLCleaner library look like? A: As of August 12, 2012, my entry looks like this: libraryDependencies += "net.sourceforge.htmlcleaner" % "htmlcleaner" % "2.2" |