Posts

Scala Lambda Example

Lambdas are just anonymous functions, although typically they are expected to be side-effect free (more of a calculus-style function that does a computation) For instance, in Javascript you might do this: function x() { }   …or….   var y = function() { } The equivalent in Scala might be: val y = () => […]

Scala zipWithIndex example

If you’re new to Scala, the idea of immutable data structures can be a pain at times, especially when you need to replace “for (int x=0; x < arr1.length; x++)". One easy way to handle this is zipWithIndex: val arr1 = List("a", "b", "c")   arr1.zipWithIndex Note that you call this WITHOUT the parens: res14: […]

Scala Zip Example

The zip function combines two lists into tuples. If the lists are of differing lengths, the shorter length is used. If you don’t like this behavior, the zipAll function will keep all elements, filling in specified values for the blanks (compare this to the recycling rule in R, which lets you continuously cycle through the […]

Scala fold example (fold over tuples)

The variations on “fold” let you condense a Scala collection into a single value. This example shows how you might find a named column in a CSV file, by calling “zip” on the list of headers and cells, then doing a lookup with foldLeft: val searchColumn = "Name" val headers = "Name,Quantity,Amount" val row = […]

,

Extracting Dates and Times from Text with Stanford NLP and Scala

Stanford NLP is a library for text manipulation, which can parse and tokenize natural language texts. Typically applications which operate on text first split the text into words, then annotate the words with their part of speech, using a combination of heuristics and statistical rules. Other operations on the text build upon these results with […]

Base64 Encoding and Decoding Example in Scala

This fairly simple example makes use of the core Java APIs to read files, demonstrating how to construct a base-64 encoded version, and then generating the original file again. package com.garysieling { import java.io.{File, FileInputStream, FileOutputStream} import sun.misc.{BASE64Encoder, BASE64Decoder}   object Base64 extends App { val filename = """C:\IdeaProjects\base64\tests.pdf""" Console.println("Testing " + filename)   val […]

Fixing Scala Error: Main method is not static in class X

If you set up a Scala HelloWorld example naively, it is easy to get the following perplexing error message: :runError: Main method is not static in class com.garysieling.HelloWorld, please define the main method as: public static void main(String[] args) This happens if the code looks like the following: package com.garysieling { class HelloWorld extends App […]

Improving Performance of Gradle + Scala Build with a compiler daemon

If you find the build times of Scala programs in gradle unacceptably slow, you can add the following to build.gradle. This took my simple build from 25 seconds to five. compileScala { scalaCompileOptions.useCompileDaemon = true // optionally specify host and port of the daemon: scalaCompileOptions.daemonServer = “localhost:4243” } This doesn’t seem to be stable all […]

Importing Data from Solr to Postgres with Scala

I suspect most people who set up Solr indexes pull data from a second system into Solr; having written a previous example where I pulled git data into a Solr index, I copied this data into Postgres to allow comparing the behavior of two full-text indexers. This is a fairly simple process if you make […]