Skip to content

Borrow MATLAB's ability to add a scalar to a vector #21

@PoochyEXE

Description

@PoochyEXE

MATLAB will happily let you add a scalar to a vector, or even a scalar to a matrix. If a is a matrix or vector and b is a scalar value, then a + b will return a copy of a with b added to every element, and likewise b + a, instead of doing the sensible thing and raising a type error.

Of course, we can generalize this a bit for BS. If a is an array, b is a non-array type, and op is an operator -- literally any operator in the whole language -- such that a op b would otherwise be invalid, then instead a op b returns an array with the same length of a such that for each element a_i in the original array a, the returned array has a_i op b in the same position. Likewise for b op a returning an array of b op a_i for each a_i in a.

This also implies that if a is an array of arrays, b is recursively applied to every array in a to produce the output array.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions