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

[Feature] Read one line at a time from text file #533

Open
hdwalters opened this issue Oct 22, 2024 · 2 comments
Open

[Feature] Read one line at a time from text file #533

hdwalters opened this issue Oct 22, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@hdwalters
Copy link
Contributor

hdwalters commented Oct 22, 2024

Is your feature request related to a problem? Please describe.
Bash allows a text file to be read efficiently one line at a time, so a large file takes no more working memory than a small one:

while IFS= read -r line; do
    echo "[$line]"
done <"foo.txt"

Amber should provide this functionality.

Describe the solution you'd like
I propose the following Amber syntax, where a new lines builtin can be used in the expression slot of an iterative loop:

for line in lines("foo.txt") {
    echo "[{line}]"
}
let path = "foo.txt"
for line in lines(path) {
    echo "[{line}]"
}

The builtin can also be used in an initialisation, set or append statement, in which case it will read all lines from the file into an array, instead of reading one line at a time:

let array = lines("foo.txt")
for line in array {
    echo "[{line}]"
}
let array = [Text]
array = lines("foo.txt")
for line in array {
    echo "[{line}]"
}
let array = [Text]
array += lines("foo.txt")
for line in array {
    echo "[{line}]"
}

Note:

  1. Because lines behaves like a function, it should be called like one, using parentheses; our users will not care how this functionality is implemented.
  2. It will also be necessary to rename standard library function lines as split_lines.

Describe alternatives you've considered
The alternative would be to write a standard library function; but it would then be impossible to read the file efficiently, as the function would have to create and return an array, regardless of how it was called.

Additional context
N/A

@hdwalters hdwalters added the enhancement New feature or request label Oct 22, 2024
@hdwalters hdwalters self-assigned this Oct 22, 2024
@Mte90
Copy link
Member

Mte90 commented Oct 23, 2024

To me is fine but we need to decide about builtins with parenthesis

@hdwalters
Copy link
Contributor Author

hdwalters commented Oct 23, 2024

To me is fine but we need to decide about builtins with parenthesis

I knew this was going to show up again!

I would argue that builtins in expression slots, e.g. for line in lines("foo.txt") and let array = lines("foo.txt"), should expect parentheses, mostly because that's what our users will expect. They won't care if something that looks like a function is actually implemented as a function or a builtin.

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

No branches or pull requests

2 participants