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

Object and advanced array comprehensions #439

Closed
edemaine opened this issue Mar 8, 2023 · 4 comments · Fixed by #1656
Closed

Object and advanced array comprehensions #439

edemaine opened this issue Mar 8, 2023 · 4 comments · Fixed by #1656
Labels
enhancement New feature or request partially completed Some of the requested functionality has already been implemented

Comments

@edemaine
Copy link
Collaborator

edemaine commented Mar 8, 2023

DONE: Object comprehensions

(based on Python's dict comprehensions)

obj := {for item of list
   [item.key]: item
   [item.key.toLowerCase()]: item
}
---
const obj = (()=>{const results = {}
  for (const item of list) {
    Object.assign(results, {
      [item.key]: item,
      [item.key.toLowerCase()]: item,
    })
  }
})()

DONE: Array flattening via spreads

array := for item of list
  if Array.isArray item
    ...item
  else
    item.toString()
---
const array = (()=>{const results = []
  for (const item of list) {
    if (Array.isArray(item)) {
      results.push(...item)
    } else {
      results.push(item.toString())
    }
  }
})()

This could also be achieved via for.value.push(...item); continue given #362, but this seems like more natural notation.

@edemaine edemaine added the enhancement New feature or request label Mar 8, 2023
@STRd6
Copy link
Contributor

STRd6 commented Mar 8, 2023

Spreads could also apply to object results as well.

@edemaine
Copy link
Collaborator Author

edemaine commented Apr 14, 2023

Another application would be the ability to flatten the results of nested for loops. Ideally we could simplify the output to not construct an intermediate array, as follows:

array := for sublist of list
  ...for item of sublist
    item ** 2
---
const array = (function(){const results = []
  for (const sublist of list) {
    for (const item of sublist) {
      results.push(item ** 2)
    }
  }
})()

This gives us a very flexible equivalent of Janet's seq primitive.

@gwhitney
Copy link

gwhitney commented Oct 4, 2023

+1 on the object comprehensions: looks/feels totally natural, and with significant Python exposure prior to any Java/TypeScript, something I've been feeling the lack of basically since day 1.

peey added a commit to peey/Civet that referenced this issue Nov 2, 2024
Parses and transpiles object comprehension syntax proposed in issue DanielXMoore#439

Tested only in limited contexts, of the 4 tests added one is failing
peey added a commit to peey/Civet that referenced this issue Nov 2, 2024
Parses and transpiles object comprehension syntax proposed in issue DanielXMoore#439

Tested only in limited contexts, of the 4 tests added one is failing
@edemaine edemaine added the partially completed Some of the requested functionality has already been implemented label Dec 12, 2024
@edemaine
Copy link
Collaborator Author

For anyone following this issue, #1563 gave us object comprehensions! See https://civet.dev/reference#object-comprehensions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request partially completed Some of the requested functionality has already been implemented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants