Live data from Hacker News

Julia as a CLI Calculator

krasjet.com

21–30 of 134 posts

Re: Julia as a CLI Calculator

#21

Since this is a calculator topic, I'd like request your input on which calculators you use. I use the Emacs calculator (M-x calc) and XCalc ( http://www.tordivel.no/xcalc/ ) as I like to use RPN calculators. The Windows 10 calculator is painful and slow. XCalc has a mini mode, where it will sit as a small, single line without distracting you too much. I like that feature very much. One thing with Emacs is that I forg…

My favorite calculator of all time is units(1). For example, to calculate how long it takes to download a 20 MiB file over a 56 kbit dialup connection:

  $ alias units='units --verbose' # in your shell rc
  $ units
  Currency exchange rates from FloatRates (USD base) on 2019-06-05
  3460 units, 109 prefixes, 109 nonlinear units
  
  You have: 56 kibibit/second
  You want: hour/20 mebibyte
          reciprocal conversion
          1 / (56 kibibit/second) = 0.8126984126984126977077949 hour/20 mebibyte
          1 / (56 kibibit/second) = (1 / 1.23046875) hour/20 mebibyte
Note that if you are on macOS, you want to install a more recent units via homebrew. The one shipping with the base system is positively ancient.

Re: Julia as a CLI Calculator

#22
post #6
post #5

Earlier quoted context omitted.

Simply loading the REPL takes half a second for me. I use it all the time as a simple calculator.

> Simply loading the REPL takes half a second for me. I juse it all the time as a simple calculator. Half a second is precisely what is meant by "excruciatingly slow". If I need a simple calculator, I expect to be able to use it to compute intermediary results in a shell loop. If my loop has one thousand iterations, you are spending about 10 minutes just initializing the interpreter. This is absurd. Of course, you ca…

The strength of using the julia REPL as a "calculator" is not in doing somewhat trivial calculations like dividing two numbers or quickly multiplying two things, and that's fine. That's not a goal of the REPL and julia is indeed overkill for those kinds of things. However, if you're mathematically inclined and sketch out a few matmuls like in the OP article, julia does have the benefit of making those kinds of interactions more bearable from a syntactic point of view. That's where it shines.

And like I've posted above, if you use certain packages frequently and restart the REPL frequently, why not use PackageCompiler to compile your hot paths into your binary, like described in the article (admittedly at the end)? That way, even the first call in a new session will be fast because it has already been compiled. On top of that, the core devs are very much aware that this is a problem and it's actively being worked on.

Re: Julia as a CLI Calculator

#23
post #5
post #3

I love the julia language. I would love to use julia as a command line calculator. Unfortunately, starting the interpreter is excruciatingly slow, an order of magnitude slower than octave or python (and several orders of magnitude slower than bc or dc).

Simply loading the REPL takes half a second for me. I use it all the time as a simple calculator.

This is simply not true. I have a 3 year old mac, and this is doing simple arithmetic (including starting and exiting Julia). 0.14 seconds user time.

  $ time ~/Desktop/Julia_Releases/Julia-1.4.app/Contents/Resources/julia/bin/julia -E "1+1"                    
  2
  ~/Desktop/Julia_Releases/Julia-1.4.app/Contents/Resources/julia/bin/julia -E   0.14s user 0.08s system 65% cpu 0.342 total
In about 0.5 seconds, I can actually do something non-trivial that actually needs non-trivial compilation - like summing the sin of a million random numbers

  $ time ~/Desktop/Julia_Releases/Julia-1.4.app/Contents/Resources/julia/bin/julia -E "sum(sin.(rand(1000000)))" 
  460165.3756259715
  ~/Desktop/Julia_Releases/Julia-1.4.app/Contents/Resources/julia/bin/julia -E   0.59s user 0.13s system 93% cpu 0.769 total

Re: Julia as a CLI Calculator

#24
post #20
post #19

I use python CLI for exactly the same :)

I used to use Python REPL for that until I switched to fish shell. Now I just run $ math "999.999 / 3" 333.333 Granted, I don't really do any complex calculations in my terminal.

> Granted, I don't really do any complex calculations in my terminal.

Same. I use insect — https://github.com/sharkdp/insect

Re: Julia as a CLI Calculator

#25
post #11
post #4

Earlier quoted context omitted.

One way to alleviate the slowish startup is to use PackageCompiler.jl (discussed at the end of the article https://krasjet.com/rnd.wlk/julia/#packagecompiler.jl ). I like that there's more than just the standard calculator operations available at the REPL, but I can see why you wouldn't use it if you don't need it and bc satisfies your needs.

I remember years ago when caching was about to be integrated in the next version and be here in a few weeks. Now it has been half a decade of people complaining about how slow it is for his to keep recompiling the same thing over and over.

I've been using julia for about three years now and from my interactions with devs and the issues on github, caching more code has only been considered in the past year because before then, the priorities were in getting the language stable, to 1.0, and on other lower-hanging performance fruit. "Just cache more" turns out to be a harder problem than it seems at first sight, if by the very nature of the language loading a package can invalidate your caches. It has to be done with much caution, in order to reduce those invalidations as much as possible, which is why it takes time to figure it out.

Re: Julia as a CLI Calculator

#26
I really wish there was a language based on Swift that would include most of the nice language, REPL and library characteristics of Julia (and perhaps few from Dart). That would get really close to a "perfect language" for me.

Overall I like Julia - and use it as a CLI calculator as well - but there are several things that prevent it from being a good general purpose language:

- OOP support is "meh".

- str.parse(Int).abs is more readable than abs(parse(Int, str)). And this is a very simple case, the readability difference is greater in more complex expressions.

- 1-based indexing.

- Lack of good support for nullability, think of "if let", "guard let", "??", "object?.property".

- There are some more, this is just from the top of my head.

EDIT: A better example of why Julia's syntax is less readable:

`images.first.resize(100, 100).crop(25, 25, 75, 75)`

versus

`crop(resize(first(images), 100, 100), 25, 25, 75, 75)`

Re: Julia as a CLI Calculator

#27

Since this is a calculator topic, I'd like request your input on which calculators you use. I use the Emacs calculator (M-x calc) and XCalc ( http://www.tordivel.no/xcalc/ ) as I like to use RPN calculators. The Windows 10 calculator is painful and slow. XCalc has a mini mode, where it will sit as a small, single line without distracting you too much. I like that feature very much. One thing with Emacs is that I forg…

I use insect almost daily ( https://github.com/sharkdp/insect ). It’s a command line calculator, not sure how well it would integrate with comint on emacs. Unit conversions built-in. I don’t know whether it has an RPN mode.

The other thing I do frequently is fire up a python3 repl for quick hits. I just did it yesterday for a complex mass file rename I couldn’t figure out how to do with shell variable replacement. With python it was easy with glob and os.rename.

Re: Julia as a CLI Calculator

#28
post #10

A problem with Julia is that it does not parse input numbers with arbitrary precision by default. Example: julia> sin(1.461920290375737576933544899379e+31) -0.9468766486679395 julia> sin(parse(BigFloat, "1.461920290375737576933544899379e+31")) 0.6864670207863400975666631018263839509022548965872940746039593018855528710432756 Fricas is better in this regard, some links: https://en.wikipedia.org/wiki/FriCAS https://fric…

It seems like it would be pretty easy to make a REPL mode that did the big conversions automatically. Not sufficient to protect a naive user, but arbitrary precision floating point is never safe.

Re: Julia as a CLI Calculator

#29
post #5

Earlier quoted context omitted.

Simply loading the REPL takes half a second for me. I use it all the time as a simple calculator.

This is simply not true. I have a 3 year old mac, and this is doing simple arithmetic (including starting and exiting Julia). 0.14 seconds user time. $ time ~/Desktop/Julia_Releases/Julia-1.4.app/Contents/Resources/julia/bin/julia -E "1+1" 2 ~/Desktop/Julia_Releases/Julia-1.4.app/Contents/Resources/julia/bin/julia -E 0.14s user 0.08s system 65% cpu 0.342 total In about 0.5 seconds, I can actually do something non-tri…

By half a second I obviously didn't mean 0.5 seconds but rather a very short duration that I didn't bother to measure.

Edit: Now I bothered to measure. You were saying?

      ~ time julia -E "1+1"
    2
    julia -E "1+1"  0,59s user 0,14s system 153% cpu 0,476 total

      ~ time julia -E "sum(sin.(rand(1000000)))" 
    459807.21199623536
    julia -E "sum(sin.(rand(1000000)))"  1,01s user 0,17s system 125% cpu 0,948 total

Re: Julia as a CLI Calculator

#30
post #2

This seems to be a very neat intro into Julia and a practical use case. I’ve been postponing to learn the language for a while now, but from what I heard/seen it seems to be extremely well designed.

You should, it's a fun, useful language. It's starting to replace python for my use cases.
Post reply on HN