Skip to content

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

@Sipher

Description

@Sipher

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions