Popularized by Google’s foobar (at least for Python), list comprehension is when a language features some form of syntactical sugar to make dealing with lists of values simpler. In general purpose programming languages, this can be useful for code golfing down the number of lines but, in LSD, this is useful for handling lists of values that happen to exist within DOM elements.
Normally, when ASSIGNing to a variable, the selector value will retrieve the first matched element according to the selector. When a |> is introduced to the definition of a variable, it’s assumed that the selector before the |> is one that repeats within the context its being selected in (whether that’s the entire HTML or a GROUP of repeating containers) and the value after the |> is what you’re interested in plucking from the list of matches.
variable <| repeating |> value_of_interest |
Here the value_of_interest will yield the first matching selector (or value) corresponding to each element being matched against the repeating selector.
When retrieving attributes from the repeating container, you do not need to specify the element you’re selecting the attribute from. For instance, the below assignment:
links <| meta |> meta@name |
Is equivalent to the below since, either way, you’re retrieving the name attribute from the matched meta tags.
links <| meta |> @name |
For experimentation, we have some dummy HTML data that statically returns when you request the URL https://example.lsd.so. Contained in it is the following HTML (at the time of writing this doc):
<!DOCTYPE html>
<html>
<head>
<title>This is the title of the page</title>
</head>
<body>
<div class="repeating-container">
<a class="repeating-link" href="a">A</a>
<a class="repeating-link" href="b">B</a>
<a class="repeating-link" href="c">C</a>
</div>
<div class="repeating-container">
<a class="repeating-link" href="d">D</a>
<a class="repeating-link" href="e">E</a>
<a class="repeating-link" href="f">F</a>
</div>
<div class="repeating-container">
<a class="repeating-link" href="g">G</a>
<a class="repeating-link" href="h">H</a>
<a class="repeating-link" href="i">I</a>
</div>
</body>
</html>
There are repeating div elements with the class repeating-container and, in each of them, there are repeating a elements with the class repeating-link. The LSD to retrieve each container as well as lists of links for each of them would look like the one shown below:
url <| https://example.lsd.so |
links <| a.repeating-link |> @href |
labels <| a.repeating-link |> a |
repeating_container <| div.repeating-container |
FROM url
|> GROUP BY repeating_container
|> SELECT links, labels