Put simply, projections (also known as list comprehensions) are a way of representing collections. Using a very simple syntax, you can inspect very complex object models inside collections.
Imagine you have a collection of User objects. Each of these objects has a Parent. Now say you want to get a list of all names of the parents (assuming the Parent class has a name field) in the hierarchy of users, you would write something like this:
You can even perform nested operations. Imagine instead, that the User object had a collection member called familyMembers, and we wanted a list of all the family members names:
Filters
You can also filter projections with a constraint with the if operator:
$ serves as the placeholder for the element being filtered. It is actually a regular variable that exists inside the context of the projection. You can also use it to return the current element in the projection to the representative list.
Other Examples
Here are some more examples:
Here is a functional Quicksort algorithm implemented using projections:
1 Comment
Hide/Show CommentsJun 30, 2009
forgeyan
Quicksort algorithm:
line 16 should be " concat(quicksort(($ in list if $ < pivot)),($ in list if $ == pivot), quicksort(($ in list if $ > pivot))); "
line 23 should be " concatList.addAll(pivot);"