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

Implement List/sum and List/range functions in Bend built-ins #695

Open
Sipher opened this issue Aug 22, 2024 · 0 comments · May be fixed by #696
Open

Implement List/sum and List/range functions in Bend built-ins #695

Sipher opened this issue Aug 22, 2024 · 0 comments · May be fixed by #696
Assignees
Labels
enhancement New feature or request

Comments

@Sipher
Copy link
Member

Sipher commented Aug 22, 2024

1. List/sum(list)

@spec List/sum(List[Number]) -> Number

Calculates the sum of all numbers in the given list.

Parameters

  • list: A list of numbers (integers or floats)

Returns the sum of all numbers in the list.

Possible (but not limited to) errors:

  • InvalidType: If the list contains non-numeric elements

Examples

# Sum an empty list
result = List/sum([])
# 0
# Sum a list of integers
result = List/sum([1, 2, 3, 4, 5])
# 15
# Sum a list of floats
result = List/sum([1.5, 2.7, 3.2])
# 7.4
# Attempt to sum a list with non-numeric elements
result = List/sum([1, 2, "3", 4])
# InvalidType: List contains non-numeric elements

2. List/range(start, stop, step=1)

@spec List/range(int, int, int) -> List[int]

Generates a list of integers from start to stop (exclusive) with a given step.

Parameters

  • start: The starting value of the range (inclusive)
  • stop: The ending value of the range (exclusive)
  • step: The difference between each number in the range (default is 1)

Returns a list of integers.

Possible (but not limited to) errors:

  • InvalidValue: If step is 0, or if the range would never terminate

Examples

# Generate a simple range
result = List/range(0, 5)
# [0, 1, 2, 3, 4]
# Generate a range with a custom step
result = List/range(0, 10, 2)
# [0, 2, 4, 6, 8]
# Generate a descending range
result = List/range(5, 0, -1)
# [5, 4, 3, 2, 1]
# Attempt to create a range with a step 0
result = List/range(0, 5, 0)
# `InvalidValue`: Step cannot be zero
# Create an empty range
result = List/range(0, 0)
# []

Considerations

  • Ensure efficient implementation, especially for List/range with large ranges
  • For List/sum, consider using an appropriate numeric type to avoid overflow for large sums
  • Implement proper error checking and handling
  • Ensure compatibility with both integer and floating-point numbers for List/sum
  • For List/range, consider memory efficiency for large ranges
  • Implement appropriate type checking to ensure arguments are of the correct type

Test cases to implement

  1. Sum a list of positive integers
  2. Sum a list of negative integers
  3. Sum a list of mixed positive and negative integers
  4. Sum a list of floating-point numbers
  5. Attempt to sum an empty list
  6. Attempt to sum a list with non-numeric elements
  7. Generate a range with only positive integers
  8. Generate a range with negative integers
  9. Generate a range with a custom step (both positive and negative)
  10. Generate a range that results in an empty list
  11. Attempt to generate a range with a step of 0
  12. Generate a very large range and check for memory efficiency
  13. Sum a very large list and check for numeric overflow handling
  14. Generate ranges with various combinations of start, stop, and step values
@Sipher Sipher self-assigned this Aug 22, 2024
@Sipher Sipher added the enhancement New feature or request label Aug 22, 2024
@Sipher Sipher linked a pull request Aug 22, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant