oils-for-unix / oils

Oils is our upgrade path from bash to a better language and runtime. It's also for Python and JavaScript users who avoid shell!
http://www.oilshell.org/
Other
2.84k stars 157 forks source link

[ FR ] expose number base conversion to ysh #1921

Open glyh opened 6 months ago

glyh commented 6 months ago

Currently there's no way to do so.

glyh commented 6 months ago

Also it looks like there's no sane way to hack out a solution without using source. This won't work.

func parseBase(num_string, base) {
  shopt --unset ysh:all {
    eval """
      write -n \$(($base#$num_string)) 
    """
  }
}

Related: https://github.com/oilshell/oil/issues/428#issuecomment-2041158822

andychu commented 6 months ago

OK, I guess we can put this in the same place that Python puts it? Not sure what JS does

ysh andy@andy-VirtualBox:~/git/oilshell/oil$ = int('15', 3)
  = int('15', 3)
              ^
[ interactive ]:5: fatal: Expected 1 typed args, but got 2

open to API ideas

glyh commented 6 months ago

JS

Ruby

Note

It depends on what kind of language ysh is, really. For an OOP language, the API could be something like a class instantiation. But for FPs, a function called parseInt seems to be enough. (The elixir thing is a function under a module).

I think overall JS's approach is better if we have this in default namespace, o.w. arguably elixir's naming is better.

andychu commented 3 months ago

FWIW I added a little hack to do this in YSH, using OSH /bash features

https://github.com/oils-for-unix/oils/blob/master/demo/url-search-params.ysh#L53

func strFromTwoHex(two_hex) {
  var result

  printf -v result "\\x$two_hex"
  return (result)
}

OK well I guess this turns 0x1f into a string, not an integer

But printf can probably also do the latter