0

F# has Fable, OCaml has Buckle Script. The code will be compiled to JavaScript. I want to use the code within NodeJS/Electron. As far as I know, NodeJS is not recommended for number crunching. I wrote some code in C++ for heavy calculation and imported it as NodeJS modules, but I want to use functional programming languages. If I use F#/Fable, OCaml/Buckle Script, will this problem with number crunching still exist?

JamesFaix
  • 8,050
  • 9
  • 37
  • 73
T.A.
  • 11
  • 1
  • Why would there be a problem with number crunching in JS, when you're not using JS to number crunch? – zfrisch Oct 07 '19 at 17:51
  • @zfrisch When the code written in F# or OCaml is compiled to javascript code, which language performs the task? Isn't it javascript? – T.A. Oct 07 '19 at 19:08
  • The "language" doesn't do anything. JavaScript runs on a virtual machine which does the actual computing. As does F#. OCaml _can_ be compiled to bytecode and be run in a vritual machine, or to machine code to be run directly, just like C++. Performance is determined by a combination of the actual code you write, the optimizations the compiler does to the code, if any, and the optimizations the virtual machine does, if any. "Number crunching" is too vague a description of the problem to really say much at all about what would be appropriate. – glennsl Oct 07 '19 at 19:20
  • Also, OCaml has two compilers targeting JavaScript, neither of which are named Reason. Reason is mostly just an alternative syntax for OCaml. The compilers are called js_of_ocaml and BuckleScript. – glennsl Oct 07 '19 at 19:21
  • Oh, I see. Apologies I misunderstood. I'll be honest and say I'm not well-versed enough in OCaml and F# math to give a direct answer. If it's compiling down to JavaScript and then calculating floating points in JavaScript that is likely an issue. For more information check out https://stackoverflow.com/questions/588004/is-floating-point-math-broken – zfrisch Oct 07 '19 at 19:25
  • @glennsl Thank you for your answer. By number crunching I mean data analysis, portfolio optimization etc. Heavy calculations. I used electron to make a gui and use c++ as node modules to do the heavy work. Now I'm considering to try a functional programming language and I don't understand how fable and bucklescript (thank you for correcting me) can handle the heavy work if they are just f#/ocaml code compiled to javascript code. – T.A. Oct 07 '19 at 19:28
  • @zfrisch Thank you. That's what I mean. – T.A. Oct 07 '19 at 19:29
  • @TolgaAkman kindly find my answer useful related to performing heavy computations – Sudhakar Ramasamy Oct 07 '19 at 19:33
  • While JavaScript can do ok these days I don't think you have access to SIMD instructions. Usually if you want process numbers fast on the CPU you like to use SIMD (if possible). GPU is also a possibility on in the webbrowser as well if your problem can fit into it. – Just another metaprogrammer Oct 09 '19 at 07:41
  • Just to understand the problem better, do you want to crunch the numbers directly in the browser? Because if that's the case the languages themselves won't be your problem - it all boils down to JS. If you really want to do heavy computations in the browser you'll always have to be very specific and use Browser APIs to get to more performance (take a look at tensorfire or ndarray and how they solve those problems). There's also WebAssembly...but I wouldn't count on it right now. What exactly do you try to accomplish? – WalternativE Dec 07 '19 at 18:56
  • 1
    @WalternativE Thank you for your answer. I want to compute portfolios (finance), although it is better to say I want to build a financial analysis platform. All the functions coded in C++, but I wanted to use functional programming languages because you can write code like a mathematical formula. For the gui part my goal was using HTML/CSS/JS and creating an Electron app. With C++ I had the possibility to compile to native modules and use it with JS. Child processes was my solution for Haskell. I wondered if I could skip all the trouble with BuckleScript etc. – T.A. Dec 08 '19 at 12:47
  • @TolgaAkman there's the question if you want to keep the C++ code base or not. If you want to keep it you could still use it from Electron via Fable/Bucklescript. If you want to code all your algorithms in a functional language you could have the UI part being 'dumb' and a 'server' part being smart - having them communicate via an API. have you had a look at the [SAFE stack](https://safe-stack.github.io/) - there is also [Elmish.WPF](https://github.com/elmish/Elmish.WPF) if you want to have everything on the client. – WalternativE Dec 08 '19 at 14:43

0 Answers0