Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In 'from' clause, allow 'variable = value' #11

Closed
julianhyde opened this issue Feb 27, 2020 · 2 comments
Closed

In 'from' clause, allow 'variable = value' #11

julianhyde opened this issue Feb 27, 2020 · 2 comments

Comments

@julianhyde
Copy link
Collaborator

julianhyde commented Feb 27, 2020

In 'from' clause, allow 'variable = value'. For example,

from e in hr.emps,
 fullName = e.firstName ^ " " ^ e.lastName
yield {e.empno, fullName};

It is syntactic sugar for iterating over a singleton collection:

from e in hr.emps,
 fullName in [e.firstName ^ " " ^ e.lastName]
yield {e.empno, fullName};

But I think the syntax may be useful for users.

In this case you could rewrite to a let:

from e in hr.emps
yield
  let
    val fullName = e.firstName ^ " " ^ e.lastName
  in
    {e.empno, fullName}
  end;

But this would not be possible if fullName were used in a later in or a where clause.

Further, now that #62 has implement the function only: α list → α (which throws if the list is empty or has more than one element), we can write

from e in hr.emps,
  d = only (from d in hr.depts where d.deptno = e.deptno)
yield
  {e, d};

This is important in asserting that a sub-query returns precisely one row. The same effect as a scalar sub-query in SQL:

SELECT e.*,
  (SELECT d.name
    FROM hr.depts AS d
    WHERE d.deptno = e.deptno) AS dname
FROM hr.emps AS e

(SQL is a little more limited, because you are only allowed to return 1 column.)

@julianhyde
Copy link
Collaborator Author

Note the similarity with the 'let' clause in XQuery ('let' is the 'L' in 'FLWOR'). Here is example 6-6 in XQuery by Priscilla Walmsley:

let $doc := doc("catalog.xml")
for $prod in $doc//product
let $prodDept := $prod/@dept
let $prodName := $prod/name
where $prodDept = "ACC" or $prodDept = "WMN"
return $prodName

In Morel, I think it makes sense to intermingle 'in' and '=' in the same from expression. In XQuery if you want a for followed by a let followed by a for you have to create a new query (for), which if nothing else increases the indentation.

@julianhyde
Copy link
Collaborator Author

Fixed in 7fc889a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant