Skip to content

Commit

Permalink
Merge branch 'topic/selectors_test' into 'master'
Browse files Browse the repository at this point in the history
Add test & doc for general purpose selectors feature

Closes #198, #199, and #200

See merge request eng/libadalang/langkit-query-language!208
  • Loading branch information
raph-amiard committed Apr 24, 2024
2 parents e585fa0 + c554053 commit d428a01
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
18 changes: 18 additions & 0 deletions testsuite/tests/interpreter/general_purpose_selectors/script.lkql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
selector infinite_sequence
|" Infinite sequence generator
| nb => rec(nb + 1, nb)


fun my_map(lst, fn) =
|" User defined map function. Uses an inner selector to return a lazy
|" iterator result
{
selector internal
| idx => rec(idx + 1, fn(lst[idx]));

internal(1)
}

val mpd = my_map(infinite_sequence(0), (x) => x * 4)
print(mpd)
print(mpd[51])
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<LazyList>
200
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
driver: 'interpreter'
34 changes: 34 additions & 0 deletions user_manual/source/language_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,40 @@ Here is for example how the ``super_types`` selector is expressed in Ada:
| BaseTypeDecl => rec(*this.p_base_types())
| * => ()
While selectors are in the vast majority of cases used to express tree
traversals of graph of nodes, you can use selectors to generate or process more
general sequences:

.. code-block:: lkql
selector infinite_sequence
|" Infinite sequence generator
| nb => rec(
nb + 1, # Recurse with value nb + 1
nb # Add nb to the result list
)
fun my_map(lst, fn) =
|" User defined map function. Uses an inner selector to return a lazy
|" iterator result
{
selector internal
| idx => rec(
idx + 1, # Recurse with value idx + 1
fn(lst[idx]) # Add the result of calling fn on list[idx] to the result list
);
internal(1)
}
val mpd = my_map(infinite_sequence(0), (x) => x * 4)
print(mpd)
print(mpd[51])
.. attention:: The user interface for selectors is not optimal at the moment,
so we might change it again soon

Built-in Selectors
^^^^^^^^^^^^^^^^^^

Expand Down

0 comments on commit d428a01

Please sign in to comment.