Mastering Nim – now available on Amazon(nim-lang.org) |
Mastering Nim – now available on Amazon(nim-lang.org) |
I had added nim std lib to the Techempower benchmarks, and while Nim shines in the plain text and other non db related requests, it comes at the very bottom as far as tests like Fortune go. When I did the work, there were no maintained/active Postgres drivers with async and pipelining support. And IMO, that wa the biggest reason why Nim hasn't had great benchmarking scores.
I am getting ahead of myself since I am still learning the language but I am coming from Python and various other languages so it has not been a complex transition.
I’d be curious to get your Nim highlights.
Pros: Extremely good C and C++ interop Significant indentation Very good performance Quick compilation Terse, but readable Easy cross platform Powerful macros and close to trivial DSL construction Very little code to do a lot of stuff Incoming future features look very promising
Cons: Hard to predict and control program performance, LTO does a lot of heavy lifting Immature ecosystem, because only few users. Many libraries have bugs, missing features or just a very clunky API. Or maybe they are deprecated or abandoned all together. Many different ways to do the same thing everyThingIsCamelCase, ambiguous variable names (subjective) Documentation and tutorials is mostly reading through the code and googling forum posts outside the core language You compile either for C or C++, some libraries simply won't work without C++ compilation
haskell's types are cool
vuetify makes me consider nuxt
but only elixir has otp, immutable data structures, intercluseter message passing, channels, good performance, ease of use and descent ecosystem out of the box.
rust is the only other new language Im looking at but for things outside web development
- Python-style syntax with significant indentation
- Uniform Function Call Syntax: a.len() == a.len == len(a)
- Fully unqualified imports by default: which might seem scary to Python programmers, but works great in practice because of static typing
- All of the above makes code readable and succinct
And from a language features side:
- Compiles to C with all the architectural targets that come with it
- Compilation to C also allows for easy C interop: wrap function signatures and types and you're done
- This, in turn, means that Nim libraries can bootstrap off of the massive C ecosystem, while adding nicer APIs on top
- Extremely performant GC by default: optional Rust-style annotations can further improve performance, and you can remove all overhead and manually manage memory with C-style pointers if you'd like
- Useful compile-time templates and macros that can directly change the AST
The community is also active and helpful on IRC/Matrix/Discord/Gitter (bridged together)
So it's just hard to justify using Rust, for me. Unless you're writing a DB or something where a GC is a no go. But Nim's memory management seems pretty good, and many things are stack allocated.
1. Buying this book supporters the founder and lead developer of Nim.
2. Theres consideration for some major v2.0 changes. Have a look and chime in if you haven’t.
What? Do you have a source on that claim?
For me, I only get ebooks for programming books because I can have the IDE side by side with the PDF, no need to continuously look at the physical book and then back into my IDE, and I can copy paste code examples.
The price of a printed book includes:
- the price of materials
- the price of printing
- the price of transportation
- the price of storage
- the margins and profits of all of the above
- the margins and the profit of the store
And then, and only then, the author making money, if any.
How is e-book going to impact the author's profit, exactly?
Well, if the price is the same, that hardly matters? Or are you implying unpaid/illicit downloads will be worse with a legal digital version as opposed to an illicitly scanned copy?
After buying an e-reader I can't defend buying print books from an environmental standpoint - it may be a sunk cost/resources fallacy - but with all the infrastructure for digital production, distribution and reading in place - print copies are a strict waste of resources.
If so, that was a quick edit.
I have a webpage-backend life in it, re-coded a very CRUD-heavy and medium-sized Django project in nim for funsies. You do notice that the ecosystem is not mature. You may have read another comment on here earlier that libs often are not fully featured or have clunky APIs. ORMs in particular are an area where you'll find a lot of teething issues. I found myself contributing to said libs in order to fix some of that, adding another lib to deal with connection pooling for me while I was at it.
Overall though, the coding experience has been intensely enjoyable and fun for me, which is why I kept going. Pushing logic to compile time is trivial, you don't even really need the macro system to do that since there's a pragma for that, and voila, suddenly you are able to have compile-time evaluation on whether you're trying to query a Many-To-Many relationship in your database correctly, guaranteeing you that you won't face those issues ever at runtime. I love this.
And all of this was without me having done any kind of deep dive into the macro system, I was mostly using generics, templates and compile-time-procedures (which are normal procedures, but annotated with the compile-time-pragma). And said macro-system from what I've seen opens up a whole new world of possibilities.
Another fun tidbit is that python interop is effortless, in both ways, thanks to numpy. As in, it is effortless to use python libraries, as it is effortless to make python use a library written and compiled in nim.
Edit: Deployment with docker needed some figuring out first, but wasn't hard by any means. Could use more documentation though, which I'll likely add.
I'm specifically curious about Crystal vs. Nim. Benchmarks show that crystal is pretty much faster, but I'd also like to know the advantages of why someone would choose Nim over Crystal I'm looking for reasoning that is deeper then just superficial language syntax differences.
Has anyone extensively used both? I would like an unbiased view from someone who has used both as opposed to someone who's only used one.
-----
Syntax wise: Nim is to python what Crystal is to Ruby
Capability wise: both are incredibly powerful general purpose languages. Crystal is fully OOP, while Nim has a more functional/procedural bent. Nim is better for DSLs and has a fascinating macro system.
For system language use, Nim is a better choice as you can use it with its Arc GC or no GC at all.
Perf wise, Crystal seems to be marginably faster in most benchmarks. Though at that level the difference isn't much.
crossplat wise, Crystal has no "official" windows support (and you cannot do webdev on it) while Nim has.
Ecosystem wide: Both are young languages and the ecosystems are tiny. Crystal has more and better options for webdev. Nim, IMO, is better for games, console apps etc.
Tooling wise; crystal IDE tools are far behind that of Nim. But both are terrible when you consider any decent language out there.
Ecosyt
Personally I like Crystal a lot due to its type system and syntax but you can't go wrong with either language.
Julia is the same thing. Data oriented.
After using fast compilers, it’s really painful to go back to slow ones.
That said, Crystal is a really great language, and I plan on tinkering with it again soon.
V8 is likely the fastest interpreter (javascript) for a script style language but these languages should be a step above and beyond v8 in terms of performance.
So you'll see alternative (or new) implementations of core language features like async, threading, DSLs, typeclasses, traits, etc available as external packages, with just about the same user experience as if they were built into the stdlib or compiler.
a.len() == len(a) seems like a good idea, but I'm not so sure about a.len... Isn't that a bit ambiguous?
For reading, I prefer my iPad Pro, although when looking at multiple books at the same time, I also put them up on one of my monitors.
1. https://ramanlabs.in/static/blog/raw_memory_management_patte...
I'll email you regarding what/how to help.
Reading print is still a much more enjoyable experience (except in the tub -- a waterproof Kindle is still great and lightweight for that!), especially with the slow speed of e-ink screens.
I can't defend ebooks compared to the reading, sharing, and the later selling at a used book store my cast-off print books.
I've even gotten O'Reilly books from a decade ago at used book stores that are great to read and look great on my shelf, and I love finding some old novel by some forgotten author instead of whatever the latest Amazon bestseller is.
And I don't have to worry about the Orwellian deletion of my books from my e-reader: https://www.nytimes.com/2009/07/18/technology/companies/18am...
You don't 'waste' electricity, you waste your money. And in many places electricity is (much) cheaper than (good) paper.
>toxic materials used to produce a e-reader.
Surely you realise that different chemicals are used during paper production too? https://en.wikipedia.org/wiki/Sodium_sulfide and https://en.wikipedia.org/wiki/Sodium_hydroxide for example. And this depends on paper type too.
>Reading print is still a much more enjoyable experience
And this is entirely subjective.
>I can't defend ebooks compared to the reading, sharing, and the later selling
While I 100% agree with selling option, sharing is at least as easy with epub. Unless we are talking about a book from Amazon or something. In most cases you can just send a file over your preferred messenger.
As for the deletion I'm with you. That's why I always buy DRM free, or promptly crack anything I buy.
This is generally true for ebooks as well, as I only have electric heating. Both probably require more electricity for lighting anyway.
> and the toxic materials used to produce a e-reader.
I assume an ebook reader uses more resources to produce than a book, but 1) I already have a reader, and 2) I have for example e-books I inherited from my dad - that's approximately a thousand books. Even just moving them into my apartment would require non-trivial resources.
> Reading print is still a much more enjoyable experience
I don't really agree. Especially not for technical books.
> I can't defend ebooks compared to the reading, sharing, and the later selling at a used book store my cast-off print books.
Agree that the Kindle/Amazon drm/marketplace is pretty bad. Not all books have draconic drm, though - and in such cases it's much easier to share a book with a friend that has moved over seas for example.
> I've even gotten O'Reilly books from a decade ago at used book stores that are great to read and look great on my shelf,
Sure, but in the event this is a problem - it's also a drm problem, not a problem inherent to ebooks.
As for books about "programing language x version y, with best practices as of year z" - I fully expect them to be mostly outdated after 5 to 10 years - maybe replaced by a new version.
> and I love finding some old novel by some forgotten author instead of whatever the latest Amazon bestseller is.
I've never discovered books from the AZ bestseller lists, but I've bought a few classics.
There are always going to be books that are hard to get - like the excellent:
Howard McCord book: "The Man Who Walked to The Moon" which was self-published if I'm not mistaken. I found it in a thrift store in Berlin. As it happens, it's now available for the Kindle:
https://www.amazon.com/Man-Who-Walked-Moon-Novella/dp/092970...
> And I don't have to worry about the Orwellian deletion of my books from my e-reader
Again - a market/drm problem (and a real one for sure).
I really do hope we will see more diversity in ebook sales, sharing, re-sale and lending.
I never said Ruby was data oriented.
As it says on nim-lang.org, "Nim is a statically typed compiled systems programming language".
For my contribution to the ecosystem see Nexus mentioned in another comment. If you see bugs please report them. Sometimes libraries are deprecated/abandoned because of new ideas, this happens in every ecosystem.
I personally use camelCase/PascalCase, but Nim is style insensitive, meaning you can call procs written in camelCase using snake case. However this is controversial and under discussion, my personal opinion is to make style sensitivity optional, with insensitivity the default.
Ambiguous variable names I hate, you won't see that in Nexus, if anyone does they should file an issue. That's really developer dependent.
For my part Nexus has some docs and a basic tutorial. But if people want more they need to ask for it. Why write docs nobody wants or will read? Anyway, this book (Mastering Nim) is one more definite step forward for learning Nim.
I think compiling to C is what mostly happens, C++ is probably an outlier. Even then, a C++ compiler isn't difficult to install if needed.
For me missing libraries never stopped me, I could always port/wrap whatever I needed. However with the advent of AI assisted coding, working on libraries is even faster and at some point I hope I wouldn't spend much time rolling decent libraries that are ports of existing github projects.
$ touch foo.nim
$ /usr/bin/time nim c foo.nim
CC: ../usr/lib/nim/lib/std/private/digitsutils.nim
CC: ../usr/lib/nim/lib/system/dollars.nim
CC: ../usr/lib/nim/lib/system.nim
CC: foo.nim
Hint: gc: refc; opt: none (DEBUG BUILD, `-d:release` generates faster code)
36891 lines; 0.154s; 39.434MiB peakmem; proj: /tmp/foo.nim; out: /tmp/foo [SuccessX]
0.24user 0.02system 0:00.27elapsed 101%CPU (0avgtext+0avgdata 44360maxresident)k
0inputs+0outputs (0major+13676minor)pagefaults 0swaps
(I point my default backend compiler to tcc in my $HOME/.config/nim/nim.cfg.) There are some more details in this nim forum thread: https://forum.nim-lang.org/t/8677Unless your standards are "single digit milliseconds per file like tcc itself, please!", 240 ms is not so bad. (EDIT: And this is just on some 2016-era Intel i7-6700k box, not some liquid helium cooled 10 GHz whatever.)
I just compiled my toy project (detecting anomalous parity in integers) in nim 1.6.6 (4.23s), go 1.17.7 (2.43s to create both aarch64 and x86_64 binaries), zig 0.8.0-dev.1140 (2.04s to create aarch64 and x86_64 binaries) and C via clang 13.1.6 (0.14s).
nim's compilation is 175% of Go, 207% of zig, and 3021% of clang.
Nim's compiler is definitely not speedy, and this is why so much effort has been spent on incremental compilation, which tmk, still isn't working - https://github.com/nim-lang/Nim/issues/19757.
Beam really is a marvel.
copy one write and immutable data structures mean you ca have garbage collection reserved for the termination of a thread whihc would be a nontrivial task in languages that encourage mutability cough go cough
Beam processes are a blessing
its also extremely tuned for creating threads that are super lightweight. consequently, scaling strategies that are optimal in elixir would be a TERRIBLE idea in other languages. You could make a dynamic cluster of node/go instances that communicate over rabbitmq and have a supervisor process that can keep track of their state and resurrection in case of a crash. Its going to be error prone and take weeks of fiddling to get right. beam gives you that out of the box. Its not perfect but its more than good enough for your first 10000 customers.
This benefit extends all the way up to Phoenix. because threads are trivial to create and the overhead is low, its a viable strategy to generate a stateful process for every user as they interact with the system, this is what allows liveview to be so powerful.
Each channel connection gets its own thread as well. A single machine can handle thousands of threads this way with full isolation. one thread crashing won't affect your other connections. nodejs cannot provide that. if a web-socket connection crashes a node instance, it ends the connection for every customer currently connected to that node.
Every attempt I've seen outside beam to recreate this revolve around OS level process which have a higher overhead and then they still need to create a mesasge passing protcol and supervision system. since OS threads are epensive, it wouldn't be a. good idea to try creating something like liveview as you'll overload your machine.
So you might say, "ok, I'll craete a multicluster virtual machine optimized for immutable data structures with a built in intra process messaging protocol for communication as well as a higher level super vision process. at that point, I'll say "congratulations, you just reinvented the beam, lets see if we can port other languages to it!"
anyways, if you're looking for something a little smaller for now, I highly recommend really understanding ecto and how it has you interact with databases. its easily the best database library I've worked with. precisely because it isn't a orm. It maps sql rows to elixir records which has much less of an impedance mismatch. its achieves almost zero friction between your database and your api.
Actual work on DB drivers is out-of-scope for me though. I noticed there is an async Postgres driver available: https://github.com/treeform/pg. I don't know how well tested this driver is, and no mention of pipelining support.
It would be great to see Nim's web frameworks higher up on the Techempower benchmarks. Lack of such DB driver features usually comes down to time. The support for those features in stdlib would be a good bounty issue.
https://github.com/nim-lang/Nim/issues/13559
I had much better luck with tiny_sqlite: https://github.com/GULPF/tiny_sqlite
I think the lowest hanging fruit for db drivers would be to just wrap libpq, the default PG driver. Given Nim's c interop, that should be easy. I was planning to do that, but then life happened and I had to skip that project.
I would say, that if you are trying to create a very involved framework, you should look to solve this problem. Otherwise the current perf of db_postgres can be a big deterrent to a lot of potential users.
nim c -d:release --passC:"-flto" --gc:markAndSweep --out:ap ap.nim
I will add that once it's been run a few times, it does go a bit quicker (down to ~0.7s) but the Go also gets quicker (down to 0.28s for two outputs when files are cached.) $ nim -v
Nim Compiler Version 1.6.6 [MacOSX: arm64]
Compiled at 2022-05-05
Copyright (c) 2006-2021 by Andreas Rumpf
active boot switches: -d:release -d:nimUseLinenoise