Nim On AVR

2021-11-26 Thread galaxyDragon
I've uploaded [Nim On AVR](https://github.com/dinau/nimOnAVR) on Github page. 
**Nim On AVR** has some examples to use Arduino Uno/Nano board with Nim 
language.


Macros: why and/or when to use them?

2021-11-26 Thread xigoi
The conventional rule is: use the least powerful tool that does the job.

  * If a function works, use a function.
  * Elif a procedure works, use a procedure.
  * Elif a template works, use a template.
  * Elif a macro works, use a macro.
  * Else, make a problem-specific solution.




Macros: why and/or when to use them?

2021-11-26 Thread mashingan
I remember answering this kind of exact question about a year ago on Nim 
subreddit 
[https://www.reddit.com/r/nim/comments/jg83c9/macros_vs_functions/g9pi2v3/?utm_source=reddit_medium=web2x=3](https://www.reddit.com/r/nim/comments/jg83c9/macros_vs_functions/g9pi2v3/?utm_source=reddit_medium=web2x=3)


Macros: why and/or when to use them?

2021-11-26 Thread PMunch
I tend to use meta-programming quite a bit when I write code. The benefit of a 
macro over a simple procedure is that you can re-write code during compile 
time. This can be used to create a nicer interface for yourself, reducing 
errors and increasing readability. I wrote an article about that here: 


It can also be used to turn high-abstraction code into something more 
low-level. 
[Zero-functional](https://github.com/zero-functional/zero-functional) is a nice 
example of a macro that turns one style of programming which would normally be 
expensive to do in Nim into something that's much cheaper. I also use this 
quite a bit in my custom keyboard firmware (along with the great type-system) 
both for increasing performance, but also the aforementioned read and 
maintainability. You can see my [NimConf 
talk](https://www.youtube.com/watch?v=dcHEhO4J29U) on the subject or my [video 
series](https://www.youtube.com/watch?v=XoOp3UfKXQY) where I show the entire 
implementation and explain what I'm doing and why as I go along.


Macros: why and/or when to use them?

2021-11-26 Thread ElegantBeef
Macros and procs are two vastly different tools. A procedure is for writing 
code that runs(at runtime or compile time). A macro is for writing code that 
expands code at compile time. Macros enable introspection and automation of 
writing code. Due to this you can do many magical things with macros, say you 
have a serialization library and want to only serialized fields of an object 
that are tagged, this can be done with a macro and a pragma. Macros should not 
be used in place of procedures, they should be used since procedures cannot be.

The following is a small example that is hopefully easily understandable to 
showcase what macros bring that procedures cannot do. Unpacking tuples/arrays 
into proc calls for less redundant code. 


import std/macros

macro `<-`(p: proc, args: tuple): untyped =
  ## Takes a proc and varargs, unpacking the varargs to the proc call if 
it's a tuple or array
  result = newCall(p) # Makes result `p()`
  echo args.treeRepr # Shows the input tuple
  for arg in args:
let
  typ = arg.getType # Gets arg typ
  isSym = typ.kind == nnkSym
echo typ.treeRepr # Shows the type we got
if not isSym and typ[0].eqIdent("tuple"): # This is a tuple so unpack 
values from arg
  for i, _ in typ[0..^2]:
result.add nnkBracketExpr.newTree(arg, newLit(i))
elif not isSym and typ[0].eqIdent("array"): # This is a tuple so unpack 
values from arg
  for i in typ[1][1].intVal .. typ[1][2].intVal:
result.add nnkBracketExpr.newTree(arg, newLit(i))
else: # Otherwise we just dumbly add the value
  result.add arg


proc doThing(a, b: int) = echo a, " ", b
proc doOtherThing(a, b: int, c, d: float) =
  doThing <- (a, b)
  echo c, " ", d

doThing <- (10, 20)
doThing <- ([10, 20],)
doThing <- ([10], (20,))

doOtherThing <- ([10, 20], (30d, 40d))
doOtherThing <- (10, 20, 30d, 40d)


Run


Macros: why and/or when to use them?

2021-11-26 Thread Recruit_main707
I use macros to generate nim code from a .flatbuffers file, protobuffers do a 
similar thing too.


Macros: why and/or when to use them?

2021-11-26 Thread Yardanico
To add to @rb3's answer and resolve your confusion a bit: > I understand the 
"rewriting code at runtime"

Nim macros operate on compile-time - they rewrite the code by the logic you've 
written. The resulting program binary doesn't have anything related to macros.


Macros: why and/or when to use them?

2021-11-26 Thread rb3
In Nim, macros aren't really meant to replace functions. They operate on Nim's 
basic constructs (procedures, types, variables) so programmers can make things 
less (or more) complicated, depending on the usage. Nim is designed to be a 
minimal language that can be extended with macros.

A good example of this that is implemented in Nim's standard library is 
[`async`](https://nim-lang.org/docs/asyncdispatch.html). This module implements 
asynchronous IO, and is equivalent to the language feature of the same name in 
Javascript. The cool thing here is that in Nim this is just a library, and 
nothing had to be added to the compiler. (macro definition 
[here](https://github.com/nim-lang/Nim/blob/version-1-6/lib/pure/asyncmacro.nim#L253))

Of course this great power is a double-edged sword, and can be misused like 
most other things. To avoid unneeded complexity, most Nim devs recommend trying 
procs first, then templates, and _then_ macros to solve problems.

Personally I'm a hands-on person, so I dived straight into it and started 
making mistakes, learning as I went. There's a book called "Nim in Action" 
([link](https://www.manning.com/books/nim-in-action)) people recommend though.


Macros: why and/or when to use them?

2021-11-26 Thread Bonesinger
I've been reading about macros, mainly how they're used in LISP (such as 
 
, / and Andy Gavin's explanation of 
GOOL), but the thing that I can't seem to find on any discussion is the _why_ 
or _when_ you should use a macro.

What I mean is, many times, the examples used are too simple, like "I want to 
define a macro for calculating the square of a number", followed by a lengthy 
explanation of how it works, sometimes comparing LISP macros to C macros, or 
showing how to make a macro that is the same as a specific function. Ok, but 
_why_ should I bother with that if I can make a function/procedure that does 
the same thing? Even the example in the [Nim by 
Example](https://nim-by-example.github.io/macros/) talks about "repetition", 
but the macro code only reduces a bit of what's needed to define a new class. 
In my untrained eyes, that doesn't look worth the effort.

I understand the "rewriting code at runtime", but, again, why or when should I 
actually care about that? What are some real use cases where a macro "works 
better" than a function? Are there any good resources (books, courses, posts) 
that explain this in depth?


Nim forum statistics

2021-11-26 Thread dom96
Different colours are Monthly Active Users, Weekly Active Users, Daily Active 
Users (MAU being highest).

May spike was due to  making 
front page


Nim forum statistics

2021-11-26 Thread Pumpus
Thanx man. For sake of clarity: What's on y axis? What are different colours?


Nim forum statistics

2021-11-26 Thread Hlaaftana
What happened in May/June?


Nim forum statistics

2021-11-26 Thread PMunch
Probably [NimConf](https://conf.nim-lang.org/)


Nim forum statistics

2021-11-26 Thread dom96
Sure, here you go. January 2020 until November 26 2021 :)


Limit number of running threads

2021-11-26 Thread Jocker
already tried ARC and ORC


Limit number of running threads

2021-11-26 Thread tsojtsoj
In my experience using threads is least buggy with ARC (don't know about ORC), 
so maybe that might fix your segfault. Though that was always when using 
threadpool, not sure how this translates to system/threads.


Advent of Nim 2021

2021-11-26 Thread miran
## Advent of Code 2021

Wednesday December 1st at 5 a.m. UTC will mark the start of the seventh 
incarnation of [Advent of Code](https://adventofcode.com/), popular programming 
contest started back in 2015. The author describes Advent of Code (AoC) as " _a 
series of small programming puzzles for a variety of skill sets and skill 
levels in any programming language you like_ ".

The rules of AoC are quite simple. Starting from December 1st until Christmas, 
every day at 5 a.m. UTC a new task is released. The tasks consist of two parts, 
where second part is revealed after you solve the first part, and it is a 
continuation and/or variation of the first part. You don't submit your code, 
just the result of your calculation.

The participation in AoC is free (although, if you like it, consider 
[donating](https://adventofcode.com/2021/support)), all you need to do is log 
in with your Github, Google, Twitter, or Reddit account.

If you have never participated in AoC before or you want to prepare yourself 
for the start of the competition by solving some tasks, take a look at [the 
previous events](https://adventofcode.com/2021/events). To bring you up to 
speed, we recommend solving any of first 10 tasks of any year, as those are 
usually easier and can be solved relatively fast.

## Nim leaderboards

Advent of Code offers private leadearboards, and Nim has not only one but two 
of them.

[The original Nim private 
leaderboard](https://adventofcode.com/2021/leaderboard/private/view/40415) has 
filled up last year to the maximum of 200 users. We have opened [a new private 
leaderboard](https://adventofcode.com/2021/leaderboard/private/view/681448), 
which you can join by using `681448-60235f8f` code on [this 
link](https://adventofcode.com/2021/leaderboard/private).

If you have joined one of these leaderboards in previous years, there's no need 
to do it again -- you're already in.

## Sharing solutions and asking for help

In this thread you can post links to your AoC repositories, share your 
solutions, ask for help, discuss the tasks, etc.

People usually share their solutions on [r/adventofcode 
subreddit](https://old.reddit.com/r/adventofcode/) and we encourage you to 
share your Nim solutions there too and showcase the beauty of Nim.

If you're sharing your solutions via Twitter, use `#AdventOfNim` hashtag and/or 
mention @nim_lang.

You can also use Nim IRC/Gitter/Discord channel if you have some Nim-related 
problem, but have in mind that your snippets might contain spoilers for other 
who haven't solved the task yet -- not everybody will be able to solve the 
tasks at 5 a.m. UTC. Consider waiting at least couple of hours before asking 
for help (in that time, try it some more to see if you can solve it by yourself 
:)).

Have fun! 


Advent of Nim 2021

2021-11-26 Thread miran
Previous Advent of Nim threads:

  * [AoC 2018](https://forum.nim-lang.org/t/4416)
  * [AoC 2019](https://forum.nim-lang.org/t/5588)
  * [Aoc 2020](https://forum.nim-lang.org/t/7162)




Limit number of running threads

2021-11-26 Thread PMunch
Sounds like a job for `threadpool`


Limit number of running threads

2021-11-26 Thread Jocker
is there any way to limit how many threads can be created and let new threads 
wait until the running ones are finished? I tried this code: 
 but I got a Segmentation fault


Nim forum statistics

2021-11-26 Thread juancarlospaco
Forum stats should be made always public, it would benefit Nim, and Nim lib 
maintainers. 


implicit surfaces polygonizer

2021-11-26 Thread rforcen
just port a java implementation,

available @:



requires vec3:



includes typical surfaces func's:


NordstarndWeird, DecoCube, Cassini, Orth, Orth3,  Pretzel, Tooth, Pilz, 
Bretzel , BarthDecic, Clebsch0, Clebsch,  Chubs, Chair, Roman, TangleCube, 
Goursat


Run


Malloc issue after upgrading from 1.4.6 to 1.6.0

2021-11-26 Thread JPLRouge
i am using linux and i upgraded 1.6 and with usemalloc and i have no problem I 
even recompiled everything apart from modifying the nil string with "C" I only 
had to put cstring (var_string), well that's not very bad 


Malloc issue after upgrading from 1.4.6 to 1.6.0

2021-11-26 Thread Araq
Does this also run on Linux? If so, ask `valgrind` what it thinks.