Functions to Transform Element Types

Transforming element types

You learned earlier that a list can contain elements of the these types that Neo4j supports:

  • Numeric without decimal (Long)

  • Numeric with decimal (Float)

  • String

  • Date

  • Time

  • LocalTime

  • DateTime

  • LocalDateTime

  • Duration

  • Boolean

  • Point

  • Another list

When you create a list, it can contain mixed types, but in most cases your lists will contain elements of the same type.

Cypher has functions to transform elements to Strings, Longs, Floats and Booleans.

toStringList()

This is the simplest element transformation. It will produce elements of type String.

cypher
WITH ["abc", false, 1, 1.5,null, datetime(), date(), LocalDateTime(), point({x: 2.3, y: 4.5})] AS MyList
RETURN MyList, toStringList(MyList)

The only exception for this function is that null elements will remain null in the list.

toIntegerList()

This function produces elements of type Long (also known as Integer).

cypher
WITH ["abc", false, 1, 1.5,null, datetime(), date(), LocalDateTime(), point({x: 2.3, y: 4.5})] AS MyList
RETURN MyList, toIntegerList(MyList)

If an element cannot be transformed, it is set to null.

Only numeric and Boolean type elements will be transformed. Note that if an element is a Float, the decimal value is dropped.

toFloatList()

This function produces elements of type Float.

cypher
WITH ["abc", false, 1, 1.5,null, datetime(), date(), LocalDateTime(), point({x: 2.3, y: 4.5})] AS MyList
RETURN MyList, toFloatList(MyList)

If an element cannot be transformed, it is set to null.

Only numeric type elements will be transformed.

toBooleanList()

This function produces elements of type Boolean.

cypher
WITH ["abc", false, 1, 1.5, 0, null, datetime(), date(), LocalDateTime(), point({x: 2.3, y: 4.5})] AS MyList
RETURN MyList, toBooleanList(MyList)

If an element cannot be transformed, it is set to null.

Numeric elements with a value of 0 will be transformed to false. Numeric elements with a non-zero value will be transformed to true.

Check your understanding

1. Transform list elements

What types can you transform list elements to?

  • ✓ Long

  • ❏ Date

  • ✓ String

  • ✓ Float

  • ✓ Boolean

  • ❏ DateTime

Hint

There are four built-in functions you can use to transform the elements of a list.

Solution

You can transform list elements to Long, Float, Boolean, and String values.

2. Creating list from string

Suppose we have a list that has these values:

["abc", false, 1, 1.5,null, datetime(), date(), LocalDateTime(), point({x: 2.3, y: 4.5})]

If you use `toBooleanList() on this list, how many elements are set to null?

  • ❏ 1

  • ❏ 2

  • ❏ 6

  • ✓ 7

  • ❏ 9

Hint

With the list element transformation functions, null elements remain null.

A numeric value of 0 is transformed to false and a numeric value of 1 is transformed to true.

Solution

toBoolean() for this list will set 7 elements to null.

Summary

In this lesson, you about the Cypher functions that transform list elements. In the next challenge, you will transform the elements of a list.