Giter VIP home page Giter VIP logo

nimprogrammingbook's Introduction

NimProgrammingBook

Computer Programming with the Nim Programming Language -- A gentle Introduction

(C) Dr. Stefan Salewski 2020, 2021, 2022, 2023

This repository contains the english source code of the book.

Electronic versions of the book in HTML and PDF format are available at

https://nimprogrammingbook.com

For a short overview of the Nim Programming language, you may consult

https://nimprogramming.com/

or the official Nim homepage at

https://nim-lang.org/

nimprogrammingbook's People

Contributors

stefansalewski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nimprogrammingbook's Issues

Some thoughts about async

I have serveral concerns about async usage:

  1. I saw some where on the internet that if we want to check future is failed then we should use yield instead of await to execute the async procedure. Would you put some words in the asyn execute section?
  2. The {.multisync.} pragma, a simple example should be good for this pragma
  3. About the {.async.}, Future[T] on the signature of procedure, I find it's quite confusing sometimes, the example in the book as below:
proc tick(t: string): Future[void]  {.async.} =

The async pragma can be removed without any problem. So my current understanding is: await keyword can only be used inside a proc which has async pragma, and we can use yield to replace await if we want to check the future status, and we use await/waitFor/asyncCheck on any procedure with return type Future[T].

I finally found the description in the standard library is more accurate and helpful: https://nim-lang.org/docs/asyncdispatch.html

the only thing I don't quite understand is the description in the discard future:
Note however that this does not wait for completion, and you should use waitFor for that purpose. I don't know for what purpose I should use waitFor

Expand section on templates

Should have some extra examples which more comprehensively present the intricacies and uses around templates in nim such as with untyped code blocks and the do notation.

I would be happy to help write a draft section around this, however I can only think of the importance/use of templates and untyped code blocks when it comes to things like clients and locks.

The tail insert of linked object

In the chapter of "Reference and Pointers", I'm wondering how can I code the "tail insert" instead of the head insert of linked object? The current example insert item as reverse order.

type
  Friend = ref object
    name: string
    next: Friend  

var
  f: Friend           
  node: Friend
  n: string 

new(f)
node = f
while true:
  write(stdout, "Name of friend: ")
  n = readline(stdin)
  if n=="" or n=="quit":
    break
  node.next = Friend(name: n)
  node = node.next

while f!= nil:
  echo f.name
  f = f.next        

License?

What license is the book under? What license are the code snippets under?

executable examples

Just a suggestion Stefan: In asciidoc there is an include directive. So instead of writing the nim source code in the book, write them in a subfolder and include them in the document. This will enable to include the result in the same way, so that the examples can be run and refreshed before generating the final book.

I've seen this approach in a book "Managing Projects with GNU Make" by Robert Mecklemburg, published by O'Reilly (ISBN 978-0-596-00610-5 if you are interested by this book). It is possible to use the same idea as in this book and build a Makefile that compile the examples, run the examples and save their output to .result files and then generate the final .html, .epub, .pdf book format.

Jerome

Debug code left in regex example

In part VI, process execution, parsing data files(in parallel), using regular expressions:

the code has
if match(l,r,m):
when true: #debug
discard

Maybe this was intended to bypass the large amount of output. Maybe writing it to a file would be better.

Minor update to the book for nims 2.0 release as of today (01.08.2023)

This is honestly more of a notification than an actual issue.

I'm not sure if you're already aware that nim 2.0 was released today (https://nim-lang.org/blog/2023/08/01/nim-v20-released.html).

With that come some minor changes, but honestly it shouldn't be much given that I'm pretty impressed with how on top you've been with all developments. At least from what I can gather skimming through: https://ssalewski.de/nimprogramming.html. Seems to me like it'd mostly just be a slight wording change in the About this book section?

Use of word "concept" in place of "defer"

Under the section titled "Defer statement", the term "concepts" is mistakenly used in place of "defer".

The concepts statement can be used to ensure that special actions like closing a file or freeing resources are always executed.

where it should have been:

The defer statement can be used to ensure that special actions like closing a file or freeing resources are always executed.

The same mistake continues throughout the mentioned section.

maybe an effecient way to broadcast message in the chat server application

first, i'd like to thank you for this awesome book, i have learnt a lot from it.

while reading the section about implementing a chat server application, i think await asyncfutures.all clientSendFutures will be more effecient than the for ... await.

proc processClient(client: AsyncSocket) {.async.} =
  while true:
    let line = await client.recvLine()
    if line.len == 0: break
    for c in clients:
      await c.send(line & "\c\L")

->

  let sendFuts = collect:
      for c in clients:
        c.send(line & "\c\L")

  await all sendFuts

by using asyncdispatch.all, it turns linear sending to concurrent

[FR] Dark theme option

hi,

It would be nice if this had a button/switch for a dark themed version.

thanks,
ITwrx

Typo

"And some Nim packages can currently not really compare with the libraries of other languages, which where optimized for years by hundreds or thousands of full time developers."
where optimized
s.b.
have been

Permission to translate the book

Thanks for your request.

It's a book, maybe 30 percent finished now, I would guess we have about
200 pages now. Unfortunately the difficult parts macros, async and
parallel processing/threading are still missing.

Do you really think that translation to Japanese Language makes sense?
We had a discussion about a Chinese translation on the Nim forum
already about one year ago, and there was no clear view.

The fact is Nim is a tiny language still. I know that a few people read
my book, and like it. But only a few. Well the book was mentioned
recently at reddit with 50 upvotes, which is not bad. Unfortunately the
Nim core devs seems not to like the book too much, at least they have
not mentioned it somewhere on their home page. Well maybe they want to
sell the Manning book.

My current estimation is: When I finish the book, so that it has about
500 pages, and try to sell it I may be able to sell 50 pieces a year
for a period of maybe 4 years. Price would be 29 Euro per piece as
HTML/PDF. For early buyers, final price may be 39 or 49 Euro. This
would be for people from central Europe, USA, Canada, Japan. For people
in very poor or unfree countries like north Korea, China, Africa and
for children under 16 years old I would offer free download, which of
course other people may misuse. Of course a fully free book would have
some more readers, at least more downloads, but 29 Euro should be
really not too much as its help beginners to learn computer
programming.

So, for 50 readers a year for the English text, do you still think that
an Japanese edition would make sense at all? For China it is different,
they really have a lot young people without good English knowledge, so
indeed I could imagine for a Chinese version as much readers as for the
English one.

Translation is a lot of work. For english german google translate works
not bad, so I would do an automatic translation. Does that work as well
for Japanese language already?

Unfortunately I can not promise when I will finish the book, and if at
all. I spent only 400 hours for it til now, which is not that much (I
spent 1600 hours on the gintro bindings in the last 5 years totally).
But the current content is a direct flow from my head to the text, so
it was fast. Macros, threading, async would be much slower. And I am
still not sure if I should write about macros and async at all, as the
book is for beginners, and macros is advanced stuff.

So you should really think about your idea carefully, maybe ask your
friends, maybe ask at pages like reddit, maybe at the Nim forum. Note
that I am not sure if I will continue using the Nim Forum, the people
are really a bit too unfriendly sometimes, see

https://forum.nim-lang.org/t/7832

"Alternatively you can always phrase your posts in a more civilized
manner."

Generally my feeling is that currently some translation of the official
docs, as the tut1 and tut2 makes more sense and is much less work, and
the official docs are already in a more stable state, while my book is
still work in progress. For people with some computer science
background the official docs are fine.

But I have to admit that I don't know it. I started the book about one
year ago because we had in the last years some beginners without any
experience in computer programming, and they ask if they should learn
Nim as first language. A serious answer was no, better learn Python or
C first, as we have not too much learning resources. Some Nim devs said
yes, just start with Nim. But often then the beginners tried for a few
months, failed and vanished. My hope was to improve the beginner
experience with the book.

Final point: The young people seems to like videos today. We had
someone who made a collection of about 25 beginner videos, while he was
a beginner himself, I guess he has never written actual Nim software. I
tried to watch his videos for some minutes... But people seem to like
it? Videos are one reason why my expected selling rates for the book
are so low. And Nim homepage promotes the videos, they linked it.

The situation would drastically change when teachers would start
teaching Nim at school or as first language in first year at
university. But I don't think this will happen soon.

For the discussion about a Chinese translation see

https://forum.nim-lang.org/t/6170#38113

Re: Permission to use the book in YouTube video series.

Hi Stefan,

I wanted to know the specific requirements in gaining permission to use the book (the HTML or PDF versions of the book at nimprogrammingbook.com) for a video series on the Nim programming language for beginners. If I get your permission, how much of the problems and code can I use for the video series? Can I do a code-along? Or walk-through of the book?

Above all, how should I request for permission?

Wrong word

Part 1
Analog and Digital:

But quantum mechanics has taught us that many quantities in our world really have a granularity. Physically, quantities like energy or momentum are indeed multiplies of the tiny Planck constant.

The word should be "multiples".

image

Broken URL

“Detailed installation instructions are available on the Nim internet homepage at To visit and read that page

Extra characters - “.footnote:” at end of URL breaks the link. Secondly, the URL ends up on the source download page and one needs to select windows or Unix to get the detailed instructions. consider:

“Detailed installation instructions are available on the Nim internet homepage for [windows] (..) and [Unix] (…)”

Result of some code is not expected as point in the book

Hello Stefan!

The book is so great!
Thank you a lot for that work!
I am reading your book now, on chapter Objects, there are code:

Starting with Nim v2.0, object fields can have custom default values, instead of the binary zero. The syntax for the defaults is the same as the assignment for ordinary variables, as shown below:

type
  Computer = object
    freeShipping: bool = true
    manufacturer: string  = "bananas"
    cpu: string
    powerConsumption: float
    ram: int # GB
    ssd: int # GB
    quantity: int
    price: float
var 
    comp: Computer
with comp:  
    cpu = "Intel i7"
    powerConsumption = 17
    ram = 32
    ssd = 1024
    quantity = 3
    price = 499.99

echo comp

But it doesn't work as I am expected.
Two fields have default values, but when I create comp the fields freeShipping and manufacturer is empty.

[Running] nim compile --verbosity:0 --hints:off --run "c:\Users<user>\source\nimProjct\StefanSalewski\main.nim"
c:\Users<user>\source\nimProjct\StefanSalewski\main.nim(3, 11)
(freeShipping: false, manufacturer: "", cpu: "Intel i7", powerConsumption: 17.0, ram: 32, ssd: 1024, quantity: 3, price: 499.99)

I am testing programming via Nim on Windows 10.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.