Scala headOption example

The scala collections API provides a function called “headOption”, which returns a Some(value) if there is an item in a list, and a “None” otherwise. Observe: scala> List(1).headOption res35: Option[Int] = Some(1)   scala> List().headOption res36: Option[Nothing] = None It may not be immediately obvious why this is useful, especially if you’re coming from Java, […]

Scala corresponds example

The “corresponds” function lets you match up items between two lists. For a good use of this, imagine you have a list of values, and a list of ranges (e.g. numeric or time windows): val a = List(1, 2, 3, 4) val b = List((1,4), (2,5), (3,7), (3,11)) From that, you can write a function […]

Handling Circular Data Structures in Postgres

Let’s say we’re setting up a data structure for security, with group membership: CREATE TABLE groups ( id INT UNIQUE, name VARCHAR UNIQUE, member_groups INT[] );   INSERT INTO groups VALUES (1, ‘admins’, ‘{}’);   — automatically include any admins as users INSERT INTO groups VALUES (2, ‘users’, ‘{1}’);   — grant admins access to […]

“flatMap” in Postgres

Postgres has an aggregation function for combining values into lists, but it seems to lack one for concatenating arrays in a GROUP BY. Fortunately, this is easy to fix: CREATE AGGREGATE flatMap (anyarray) ( sfunc = array_cat, stype = ANYARRAY, initcond = ‘{}’ ); Example:   WITH testData AS ( SELECT ‘group1’ val, ARRAY [1, […]

Postgres array concatenation example

Array concatenation in Postgres is really simple, although the operator is non-obvious: || This works for several syntactically similar combination of scalars and arrays, as seen here: SELECT ARRAY[’b’, ‘c’, ‘d’] || ARRAY[’e’]; SELECT ARRAY[’b’, ‘c’, ‘d’] || ‘{e}’; SELECT ‘{a}’ || ARRAY[’b’, ‘c’, ‘d’]; SELECT ARRAY[’a’] || ARRAY[’b’, ‘c’, ‘d’]; SELECT array_cat('{a}’::text[], ‘{b,c,d}’::text[]) SELECT […]

Postgres: JOIN on an array field

Say we create two tables (users and groups), where users are in groups: CREATE TABLE users ( id INT UNIQUE, name VARCHAR UNIQUE );   CREATE TABLE groups ( id INT UNIQUE, name VARCHAR UNIQUE, member_users INT[], member_groups INT[] ); The groups own the user memberships, rather than having an intermediate table with the relation. […]

Postgres: Inserting into an array

If you make an array column in Postgres, it’s not completely obvious how you’d insert static data into it. Say you create a table like so (two int array fields): CREATE TABLE groups ( id INT UNIQUE, name VARCHAR UNIQUE, member_users INT[], member_groups INT[] ); You can insert using quoted strings. Note that the quotes […]

Fixing scala type errors in gradle (“incompatible types: inference variable E has incompatible bounds” + “cannot infer type-variable”)

I experienced an error, where gradle failed to build some Scala code using the ZincCompiler, with a Java 8 JDK: FAILURE: Build failed with an exception. (cannot infer type-variable(s) K#1,V#1 (actual and formal argument lists differ in length)) error: incompatible types: inference variable E has incompatible bounds This error can be caused by having an […]

Scala equivalent for java.String.format

A nice, clean substitute for String.format is string interpolation (which will call toString on your values) An example is shown here: val t = Calendar.getInstance.getTime var num = 7 val dec = 3.14 var text = "hello word"   s"""Test 1: $t""" s"""Test 2: $num $dec $text""" Which returns: res21: String = Test 1: Thu […]