Skip to content

digits2integer: inverse digits function #40393

@carstenbauer

Description

@carstenbauer

We have the digits function for integer -> digits:

julia> digits(1234)
4-element Vector{Int64}:
 4
 3
 2
 1

However, we don't have the "inverse function" that implements digits -> integer. A top of the head implementation (ignoring base and pad keyword arguments) could be something like (cc: @ararslan)

digits2integer(digits) = sum(((i, x),) -> x * 10^(i - 1), pairs(digits))

(Very ugly alternative: parse(Int, join(digits)))

such that

julia> digits2integer(digits(1234))
1234

I feel we should have something like this, what do you think?

Potentially issues/difficulties:

  • Type stability: what if the digits don't fit into Int. We would need to dynamically extend to BigInt or similar which is obviously suboptimal.
  • Need to find a good name (as always).
  • More?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureIndicates new feature / enhancement requests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions