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

Sugar for collections #445

Merged
merged 9 commits into from
Mar 3, 2022
Merged

Sugar for collections #445

merged 9 commits into from
Mar 3, 2022

Conversation

alari
Copy link
Member

@alari alari commented Mar 2, 2022

Fixes #273

This pull request introduces a new syntax to the Aqua language that helps to create collections.

Make an array

Syntax: [a, b, ...] -> []intersection type of a, b, ...

Creates a stream, writes all values into that stream, and than canonicalizes the stream into a scalar array.

--- old way:
func makeArray(a: string, b: string, c: string) -> []string:
  result: *string

  result <<- a
  result <<- b
  result <<- c

  <- result

-- new way:
func makeArray(a: string, b: string, c: string) -> []string:
  <- [a, b, c]

Make an option

Syntax: ?[a, b, ...] -> ?intersection type of a, b, ...

Tries to yield values one by one, once a value yields, stops calculations. The result can have no more than 1 element.

--- old way:
func makeOption(a: ?u32, b: ?u32) -> ?u32:
  result: *u32
  try:
    result <<- a!
  otherwise:
    try:
      result <<- b!

  <- result

-- new way:
func makeOption(a: ?u32, b: ?u32) -> ?u32:
  <- ?[a!, b!]

Make a stream

Syntax: *[a, b, ...] -> *intersection type of a, b, ...

Creates a stream and fulfills it with values, pushing them one by one.

--- old way:
func makeStream(a: bool, b: bool, c: bool) -> *bool:
  result: *bool

  result <<- a
  result <<- b
  result <<- c
  result <<- true

  <- result

-- new way:
func makeStream(a: bool, b: bool, c: bool) -> *bool:
  <- *[a, b, c, true]

All these expressions can be used in any place where you can put a value. E.g.:

--- Should work
for x <- [1, 2, 3]: ...

--- Should work
z <- foo(?[a, b, c])

--- and so on

@alari alari added enhancement New feature or request syntax Syntax additions & changes sugar Remove boilerplate labels Mar 2, 2022
@alari alari requested a review from DieMyst March 2, 2022 13:07
@alari alari self-assigned this Mar 2, 2022
@DieMyst DieMyst merged commit 6772c1d into main Mar 3, 2022
@DieMyst DieMyst deleted the sugar-for-collections branch March 3, 2022 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request sugar Remove boilerplate syntax Syntax additions & changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sugar to convert value of T into value of ?T
2 participants