Datautils?

Concurnas includes a number of commonly used data related utility typedefs and functions which can be used to make writing programs using the common data structures: list, map and set easier and more succinct. These are defined in: com.concurnas.lang.datautils and are automatically imported. Most of these data related utility functions are generic and so we're able to take advantage of Concurnases advanced usage based type inference functionality to enable us to write concise programs where this generic type information can largely be omitted and left to the compiler to infer.

Lists?

The list functions and typedefs defined within datautils create instances of java.util.ArrayList

alist list = list()//generic type infered as Integer
alist.add([1 2 3]^)
//alist is equvilent to: [1,2,3]

another = list<String>()//generic type information provided

Sets?

The set functions and typedefs defined within datautils create instances of java.util.HashSet

aset set = set()//generic type infered as Integer
aset.add([1 2 3]^)

another = set<String>()//generic type information provided

Maps?

The map functions and typedefs defined within datautils create instances of java.util.HashMap. The map function may consume a lambda which can be used in order to to populate the map with a default value for missing keys.

amap map = map()//generic type infered as String, Integer
amap["auspicious"] = 108
//amap is equvilent to: {"auspicious" -> 108}

another = map<String, Integer>()//generic type information provided

counter = map(a => 0)//with default value mapping
counter[0]++
counter[0]++
counter[10]++

//counter == {0 -> 2, 10 -> 1}

Other utilities?

Concurnas offers a few basic utilities for working with collections of data, these basic utilities are often used as the basis of more complex and specific data related algorithms.

Sorted?

The sorted function may be used to sort an ordered collection (e.g. a list) in place or as a copy. It can consume an optional comparator - itself a SAM class thus permitting the definition of a lambda in its place:

xyz1 = [1, 4, 3, 2, 5, 4, 3, 2]
xyz2 = xyz1@//deep copy of xyz1
xyz3 = xyz1@
xyz4 = xyz1@

xyz1sorted = sorted(xyz1)

sorted(xyz2, inplace=true)

xyz3reversed = sorted(xyz3, java.util.Collections.reverseOrder())
xyz4reversed = sorted(xyz4, (o1 , o2) => o2-o1)

Reversed?

The reversed function reverses an ordered collection and can act in place or return a copy of the input collection:

xyz1 = [1, 4, 3, 2, 5, 4, 3, 2]
xyz2 = xyz1@//deep copy of xyz1

xyz1reversed = reversed(xyz1)
reversed(xyz2, inplace=true)

Enumerations?

The enumerate function adds counter to a java.lang.Iterable<X> instance object (including lists, sets, ranges etc) and returns it as a list of tuples. The returned type is: java.util.List<(Integer, X).

mylist=[1, 2, 400, 4, 5]

res = enumerate(mylist)
//res == [(0, 1), (1, 2), (2, 400), (3, 4), (4, 5)]

Zips?

The zip function creates a java.lang.List<(X, ..., X)> of n dimensional tuples from the provided n java.lang.List<X> instances. The i-th tuple contains the i-th element from each of the input arguments. The length of the returned list equals the length of the first input argument list. The zip function may consume up to 10 input arguments.

x = zip([1, 2, 3, 4], [4, 3, 2, 1])
y = zip([1, 2, 3, 4], [4, 3, 2, 1],[4, 3, 2, 1],[4, 3, 2, 1] )

//x == [(1, 4), (2, 3), (3, 2), (4, 1)]
//y == [(1, 4, 4, 4), (2, 3, 3, 3), (3, 2, 2, 2), (4, 1, 1, 1)]

The print and error functions can be used to pretty print any object to stdout and stderr respectfully. The pretty print goes beyond the standard System.out.println(xyz) and can handle arrays etc:

print([1, 2, 3, 4], [4, 3, 2, 1])
print("hello world")
error("oh no!")

sum?

The sum function will calculate the sum an array of numerical (primitive or boxed) inputs. It may also be used with vararg input:

sum([1 2 3 4])//=>10
sum(1, 2, 3, 4)  //=>10
sum(1.2, 3.0)    //=>4.2

min and max?

The min and max functions will find the min and max value of an array or java.util.Collection<X>. In the case where a collection is provided, an optional comparator argument may be specified. Where only two input arguments of primitive or boxed type are provided an optimized version of the functions are invoked:

min([1 2 3 4])
max(1, 2, 3, 4)  
max(2, 5)
min("a", "z")

abs?

The sum function will return the absolute value of the input primitive or boxed type:

abs(-1)

any and all?

The any and all functions operator upon arrays of primitive or boxed types, and java.util.Collection<X>'s. The functions will return true if any input value is equivalent of true or all values are the equivalent of true respectfully:

any([0 1 0])
any(0, 1, 0)

all(true, true, false, true)
class MyClass(value int){ override toBoolean() => this.value > 10 }

any(MyClass 5, MyClass 88)

forEach?

The forEach function will apply a function to each element of an array or java.util.Collection<X> and will return a new array or collection respectfully:

forEach([1, 2, 3], a => a*10)//returns: [10, 20, 30]

reduce?

The reduce function will apply a reduction function to each element of an array or java.util.Collection<X> in order to return a single value. An initial value may optionally be specified:

reduce([1, 2, 3, 4], a, b => a*b) //returns: 24
reduce([1, 2, 3, 4], 2, a, b => a*b) //returns: 48