New lambda like functions

I wanted to share a new release of Stardog-extensions at

This new release includes lambda like functions, call, compose, filter, map, memoize, and reduce. This was partially inspired by the work of Atzori M. "Web of Functions"

Call

The call function allows you do dynamically select a function from a bound variable.

Example: SELECT ?result WHERE { BIND(func:call(string:upperCase, "Hello world" ) AS ?result) } returns "HELLO WORLD"

Compose

The compose function allows you to dynamically compose multiple functions. You can accomplish the same thing with multiple nested functions but I was hoping this would be somewhat nicer to write and it would be easier to use if it where used multiple times in a function without having to copy the deeply nested functions. The initial envisioned use case is chaining multiple string functions. It's a bit like the named query functionality except this is unnamed and dynamic. There may be issues here with scoping so please use with caution.

Example:

SELECT ?result WHERE { BIND(func:call(func:compose(string:reverse, string:upperCase), "Hello world") AS ?result) }

returns

DLROW OLLEH

Map

Filter maps a function over an array literal

Example:

SELECT ?result WHERE { BIND(array:toString(func:map(string:upperCase, array:of("star", "dog"))) AS ?result) }

returns

[ "STAR"^^<http://www.w3.org/2001/XMLSchema#string> "DOG"^^<http://www.w3.org/2001/XMLSchema#string> ]

Filter

Map a Boolean function over an array literal and remove false entries.

Example:

SELECT ?result WHERE { BIND(array:toString(func:filter(string:isNumeric, array:of("star", "dog", "1", "2"))) AS ?result) }

returns

[ "1"^^<http://www.w3.org/2001/XMLSchema#string> "2"^^<http://www.w3.org/2001/XMLSchema#string> ]

Memoize

Memoize caches the result of a function. This can be used to either cache an expensive function, especially a function that might make a network call or avoid repeatedly calling a function for a binding with a large number of repeated values. The first argument is the cache size, the second argument the function name, and remaining arguments, arguments to the function. This is the same as the call function with an additional argument for the cache size.

Example:

SELECT ?result WHERE { BIND(func:memoize(10, string:upperCase, "Hello world" ) AS ?result) }

Reduce

Call a function over an array literal and aggregating results

Example:

SELECT ?result WHERE { BIND(func:reduce("http://www.w3.org/2005/xpath-functions#numeric-add", array:of(2, 2, 2)) AS ?result) }

returns

6

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.