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

Converting between numbers and strings #500

Closed
Nihal-Srivastava05 opened this issue May 25, 2024 · 2 comments
Closed

Converting between numbers and strings #500

Nihal-Srivastava05 opened this issue May 25, 2024 · 2 comments
Labels
question Further information is requested syntax About Bend's syntax

Comments

@Nihal-Srivastava05
Copy link

Is your feature request related to a problem? Please describe.
So the problem I was facing was I made a function to index a string and it return a u24 int, is there a way to get the actual letter back or do I need to do the conversion manually (ASCII to letter). Also adding a functionality to type cast for example string number to int is also needed I feel. I couldn't find it anywhere.

Describe the solution you'd like
Function wrappers like Number/Cast ("123") -> returns 123 or String/Cast (101) -> return 'e' can be helpful.

Thanks.

@developedby developedby added question Further information is requested syntax About Bend's syntax labels May 25, 2024
@developedby
Copy link
Member

If you want to see the character as a string, just create a string with a single element.

# This is your String/Cast function
def Number/show(n):
  return String/Cons(n, String/Nil)

To read a string as a number, you have to write a number parser

def Number/parse_digit(digit):
  return digit - '0'

# This is your Number/Cast function
def Number/parse_decimal(str):
  acc = 0
  fold str with acc:
    case String/Nil:
      return acc
    case String/Cons:
      return str.tail(10*acc + Number/parse_digit(str.head))

I think this works, but it's only to give you an idea.

We don't a builtin way to do either at the moment

@developedby developedby changed the title Type Casting Converting between numbers and strings May 25, 2024
@Nihal-Srivastava05
Copy link
Author

Thanks, will use this syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested syntax About Bend's syntax
Projects
None yet
Development

No branches or pull requests

2 participants