Re: Learning & Using Nim as a C# user + Fan logo Redesign

2020-01-25 Thread kcvinu
Hi, I am a Windows + Nim user. I found VS Code is the best editor for Nim. 
These are the reasons

  1. It's very good IDE
  2. Installing a Nim extension in VS Code is very very easy.
  3. There is an extension named code-runner, which helps us to run our nim 
files in VS Code.



I tried vim, but extension installation process is horrible or i am not smart 
enough to do it. 


Re: Calling =destroy on parent class

2020-01-25 Thread kcvinu
Thanks @Yardanico, A simple example will explain the idea very clearly, Not 
everyone didn't know everything. 


Re: Learning & Using Nim as a C# user + Fan logo Redesign

2020-01-25 Thread zetashift
That is a clean looking logo indeed, I like it and could be an nice alternative 
for people that are looking for other looks.

1\. Not for C# specifically no, BUT if you know C# well learning the necessary 
for Nim is easy, only thing you have to stumble over is the OOP-ness which Nim 
doesn't enforce. The beginner resources in the "Learn" tab should work fine for 
you: [https://nim-lang.org/learn.html](https://nim-lang.org/learn.html)

2\. Sorry not that I know of, but if you have any questions feel free to post 
here or in the gitter/discord/irc.

3\. Godot-nim works fine, it uses GDNative. It is usable albeit some bugs need 
to be ironed out because of Nim 1.0 support. It doesn't support web export. 
Compared to C# (a lot less edge cases) imho it's on par, however they're making 
a lot of progress in improving C# support. If you like a code-only workflow try 
out nimgame2: 
[https://github.com/Vladar4/nimgame2](https://github.com/Vladar4/nimgame2)

4\. Check the 'GUI' section here: 
[https://github.com/nim-lang/Nim/wiki/Curated-Packages](https://github.com/nim-lang/Nim/wiki/Curated-Packages)

5\. I've never used debugging and nim before but this might help: 
[https://nim-lang.org/blog/2017/10/02/documenting-profiling-and-debugging-nim-code.html](https://nim-lang.org/blog/2017/10/02/documenting-profiling-and-debugging-nim-code.html)
 Hoping someone else can chime in on this

6\. For Windows, in my experience, yes. NeoVim is big too but I haven't gotten 
it to work on Windows (yet).

7\. Yes! Multiple even. Personally I use: 
[https://github.com/nimgl/nimgl](https://github.com/nimgl/nimgl) but there are 
alternatives like: 
[https://github.com/jackmott/easygl](https://github.com/jackmott/easygl) and a 
BGFX wrapper 
[https://github.com/zacharycarter/bgfx.nim](https://github.com/zacharycarter/bgfx.nim)


Re: conditional compilation from macro-generated code?

2020-01-25 Thread Yardanico
although thinking about it - what's the difference between if and when in a 
macro? Shouldn't it be the same since macros are evaluated at compile-time?


Re: conditional compilation from macro-generated code?

2020-01-25 Thread juancarlospaco
scope (?). 


Re: conditional compilation from macro-generated code?

2020-01-25 Thread JohnAD
doh! Looking at old code of mine, I should have recalled that.


Re: conditional compilation from macro-generated code?

2020-01-25 Thread dawkot
You need to use 'when' instead of 'if'.


conditional compilation from macro-generated code?

2020-01-25 Thread JohnAD
if I do:


var x:int
if declared(joe):
  x = joe()
else:
  x = 4


Run

Essentially, that is conditional compilation: the existence or non-existence of 
"joe" is checked at compile time. All is good.

If I use a macro to generate some code, that works also. All good.

But if I use a macro to generate code that contains a conditional compilation 
check; not so good. If "joe" is not declared, I get a `Error: undeclared 
identifier: 'joe'`; despite the conditional nature of the check.

Is this correct behavior? If so, that is fine; I can see how the order of 
compilation makes some things not possible.

If it is correct behavior, what are some good workarounds?


Re: conditional compilation from macro-generated code?

2020-01-25 Thread JohnAD
I guess the question would be: is there a good way for the macro itself to 
detect, using a string, if an identifier has been declared?


Re: How can I find the minimum value of given type T?

2020-01-25 Thread adnan
thanks


How can I find the minimum value of given type T?

2020-01-25 Thread adnan
In this code:


proc findMaxCrossingSubArray[T](arr: seq[T]; lo, mid, hi: int64): 
SliceResult[T] =
var leftSum = T.min
# ...


Run

I want leftSum to be the minimum value of the type T. The compiler complains:


Error: type mismatch: got 
but expected one of:
proc min(x, y: int): int
  first type mismatch at position: 1
  required type for x: int
  but expression 'T' is of type: type int
proc min(x, y: int64): int64
  first type mismatch at position: 1
  required type for x: int64
  but expression 'T' is of type: type int
proc min(x, y: float32): float32
  first type mismatch at position: 1
  required type for x: float32
  but expression 'T' is of type: type int
proc min[T: not SomeFloat](x, y: T): T
  first type mismatch at position: 1
  required type for x: T: not SomeFloat
  but expression 'T' is of type: type int
proc min(x, y: int8): int8
  first type mismatch at position: 1
  required type for x: int8
  but expression 'T' is of type: type int
proc min(x, y: int16): int16
  first type mismatch at position: 1
  required type for x: int16
  but expression 'T' is of type: type int
proc min(x, y: int32): int32
  first type mismatch at position: 1
  required type for x: int32
  but expression 'T' is of type: type int
proc min[T](x: openArray[T]): T
  first type mismatch at position: 1
  required type for x: openArray[T]
  but expression 'T' is of type: type int
proc min(x, y: float64): float64
  first type mismatch at position: 1
  required type for x: float64
  but expression 'T' is of type: type int


Run


Re: How can I find the minimum value of given type T?

2020-01-25 Thread Yardanico
T.low or low(T) should work in your case


Re: Will --gc:arc address finalizers for closure iterator variables?

2020-01-25 Thread spip
> Well that's not how it works, when the closure iterator's refcount is zero 
> and thus provably dead the attached resources will be freed.

Like I said, the problem is not only freeing memory but also releasing shared 
resources. How can I intercept such event I write code to release the resources?

It is a bit tricky to exhibit a small example of the problem, showing that 
iterator destruction/finalization not only related to memory.


## Demonstrate that resources leak when allocated in
## closure iterator and the iterator loop is aborted.
## In that example, the resource is a thread.

import os
import strutils

type
  TArgs = object
max: int

var ch: Channel[int]

proc foo(args: TArgs) {. thread .} =
  ## Iterate up to max.
  var
max = args.max
i = 0
  
  while i < max:
ch.send(i)
echo "Sent ", i
# Pause between message so threads play nice
sleep(100)
inc(i)
  
  # Iterator complete
  ch.send(-1)
  echo "Thread completed!"


proc bar(m: int): iterator: int =
  ## Create a closure iterator
  iterator iter: int {.closure.} =
var t: Thread[TArgs]

# Calculate one result at a time
ch.open(1)

# Delegate to counting thread
var args: TArgs = TArgs(max: m)
createThread[TArgs](t, foo, args)

while true:
  let val = recv(ch)
  echo "Received ", val
  sleep(100)
  if val == -1:
# Iterator max reached: break loop
break
  yield val

joinThread(t)
ch.close()
  
  result = iter


proc main =
  let max = parseInt(paramStr(1))
  echo "Counting up to ", max
  let iter = bar(max)
  for i in iter():
echo "i=", i
if i >= 10:
  echo "Aborting iteration in main after 10 items..."
  sleep(1_000)
  break
  echo "Out of iterator in main"


# Not using global variables or iterator
main()
# Force GC collection to be sure that out of scope
# `iter` variable has been claimed by GC.
GC_fullCollect()
echo "GC Stats: ", GC_getStatistics()


Run

The iterator uses a thread to do calculations. You call it with the maximum 
number of calculations, but if `> 10` it will abort the loop and terminate.

I'm using nim '#head' from choosenim: 


$ nim --version
Nim Compiler Version 1.1.1 [Linux: amd64]
Compiled at 2020-01-11
Copyright (c) 2006-2019 by Andreas Rumpf

active boot switches: -d:release


Run

This is compiled with `nim c --gc:arc --threads -d:useMalloc poc.nim`

Running a loop of 5 calculations (the iterator completes) shows that there is 
no memory leak. 


$ valgrind ./poc 5
==5479== Memcheck, a memory error detector
==5479== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5479== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5479== Command: ./poc 5
==5479==
Counting up to 5
Sent 0
Received 0
Sent 1
i=0
Received 1
Sent 2
i=1
Received 2
Sent 3
i=2
Received 3
Sent 4
i=3
Received 4
Thread completed!
i=4
Received -1
Out of iterator in main
GC Stats: [GC] total memory: 0
[GC] occupied memory: 0

==5479==
==5479== HEAP SUMMARY:
==5479== in use at exit: 0 bytes in 0 blocks
==5479==   total heap usage: 51 allocs, 51 frees, 9,841 bytes allocated
==5479==
==5479== All heap blocks were freed -- no leaks are possible
==5479==
==5479== For counts of detected and suppressed errors, rerun with: -v
==5479== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)


Run

If we try with a loop of 15 calculations (it will be aborted at 10!), valgrind 
complains that there is a possible leak: in fact, the thread is still alive 
when the program completes. 


$ valgrind ./poc 15
==5490== Memcheck, a memory error detector
==5490== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5490== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5490== Command: ./poc 15
==5490==
Counting up to 15
Sent 0
Received 0
...
Received 10
Sent 11
i=10
Aborting iteration in main after 10 items...
Out of iterator in main
GC Stats: [GC] total memory: 0
[GC] occupied memory: 0

==5490==
==5490== HEAP SUMMARY:
==5490== in use at exit: 337 bytes in 2 blocks
==5490==   total heap usage: 87 allocs, 85 frees, 11,052 bytes allocated
==5490==
==5490== LEAK SUMMARY:
==5490==definitely lost: 0 bytes in 0 blocks
==5490==indirectly lost: 0 bytes in 0 blocks
==5490==  possibly lost: 288 bytes in 1 

Re: Calling =destroy on parent class

2020-01-25 Thread Yardanico
-r should always be the last argument before the source filename and arguments 
for it gc:arc is a compiler argument, so it should be put before -r


Re: Calling =destroy on parent class

2020-01-25 Thread kcvinu
Thank you for show me an example of how to use "gc:arc". When i first see the 
introductory post by Araq, I didn't understand anything about it. But from this 
post, it's usage is very clear


Re: Code golfing in Nim

2020-01-25 Thread Yardanico
Some examples:

Leap years from 1801 to 2400: 


import times
for x in 1801..2400:
 if x.isleapyear:echo x


Run

Without stdlib - 


for x in 1801..2400:
 if x%%(if x%%25>0:4 else:16)<1:echo x


Run

Fibonacci of numbers from 0 to 30: 


for i in ..30:echo (var(a,b)=(0, 1);(for x in 0..

Code golfing in Nim

2020-01-25 Thread Yardanico
So in [https://forum.nim-lang.org/t/5855](https://forum.nim-lang.org/t/5855) 
@juancarlospaco shared a website [https://code-golf.io](https://code-golf.io)

It's a website for competitive code golfing and it supports Nim!

So if someone considers it fun and has free time, we can try making new 
solutions, and, only if you want to, share the solutions (CodeGolf doesn't 
allow you to see other people's solutions) or the tricks for making Nim code 
smaller here.

Some tricks:

  * I've learned it from 
[https://forum.nim-lang.org/t/5855#36306](https://forum.nim-lang.org/t/5855#36306),
 but you can use a%%b instead of a mod b, %% is actually different from mod (it 
should be used for unsigned ints only, but it works if both arguments are 
positive)
  * <1 is shorter than ==0 if you don't care about negative
  * You don't need spaces around .. or ..< in for a in 0..<5
  * You don't need to specify first argument in case of using .. - for x in ..4 
\- 
[https://nim-lang.org/docs/system.html#..%2CT](https://nim-lang.org/docs/system.html#..%2CT)
  * **You can replace indentation with `;` and parentheses** (although be aware 
that in some cases it doesn't reduce save char count) , for example:



Before: 


proc a(x:int):int=
 result=5
 if x>5:
  if x<3:
   if x==0:
echo x
echo x-1
 elif x==0:
  echo 0
  echo 5
 else:return 5


Run

After: 


proc b(x:int):int=(result=5;(if x>5:(if x<3:(if x==0:(echo x;echo 
x-1)))elif x==0:(echo 0;echo 5) else:return 5))


Run

15 characters less

And of course, **don 't follow these tips in your real projects**


Re: Introducing --gc:arc

2020-01-25 Thread Araq
> Is there a reason why you still used that pragma?

It'll see a comeback.


Re: Karax seems to only update button when text changes. Even other onclick should be bound.

2020-01-25 Thread dawkot
Works for me without changing anything on stable and devel. Maybe try 
reinstalling karax?


Re: Introducing --gc:arc

2020-01-25 Thread Yardanico
Probably because ARC can't really deal with cycles I think


Learning & Using Nim as a C# user + Fan logo Redesign

2020-01-25 Thread doongjohn
I'm not a programmer, but I'm currently using C# and Unity to make games as a 
hobby.

And I recently discovered this fantastic language!

I really want to make games with this lovely language but as an intermediate C# 
user I can't make a Game Engine by myself

* * *

So my Questions are:

  1. Any Learning resource targeted at C# user? (comparison, difference, 
equivalent, etc...)
  2. Any Korean Learning resource? (I'm not very good at English )
  3. Is there any Good Game Engine for Nim? (maybe Godot? 
[https://github.com/pragmagic/godot-nim](https://github.com/pragmagic/godot-nim)
 <\- is it actually usable and better than C# or GD Script?)
  4. Is there any UI app Framework beside using web app?
  5. How do I Debug in VS Code?
  6. Is VS Code the best editor for Nim? (I'm using Windows)
  7. I'm not going to make a Game Engine, but is it possible to use 3d Graphics 
Library? (maybe directX or Vulkan? _just curious :)_ )



# Fan logo Redesign

* * *

I actually don't think Nim needs to change a logo.

I did a logo redesign just for fun! (Inspired by Gitlab Logo)


Re: Introducing --gc:arc

2020-01-25 Thread adnan
Hi, off-topic question, in your 
[https://gist.github.com/Araq/74fc6cfe26e807f7279f65b65500251a#file-binary_trees-nim-L4](https://gist.github.com/Araq/74fc6cfe26e807f7279f65b65500251a#file-binary_trees-nim-L4)
 you use the acyclic pragma. 
[https://nim-lang.org/docs/manual.html#pragmas-acyclic-pragma](https://nim-lang.org/docs/manual.html#pragmas-acyclic-pragma)
 states that this pragma is deprecated and ignored. Is there a reason why you 
still used that pragma?


Re: Karax seems to only update button when text changes. Even other onclick should be bound.

2020-01-25 Thread enthus1ast
I can force redraw with:


# ...
of 0:
  text "Hello World!"
  button(onclick = () => (comp.menu = 1; avoidDomDiffing())):
text "FOO"
of 1:
  text "Other World!"
  button(onclick = () => (comp.menu = 0; avoidDomDiffing())):
text "FOO"
#...


Run


Karax seems to only update button when text changes. Even other onclick should be bound.

2020-01-25 Thread enthus1ast
I have a strange issue with karax. When the text in the buttons are the same 
(so both are "FOO") then i can not toggle back and forth. When the text in one 
of them is changed, lets say 


 = "BAA"

Run

than toggle works.

Is this a bug?


include karax / prelude
import jsffi, future

type Comp = object
  menu: int

proc createDom(comp: var Comp): VNode =
  result = buildHtml(tdiv):
text repr comp
case comp.menu
of 0:
  text "Hello World!"
  button(onclick = () => (comp.menu = 1)):
text "FOO"
of 1:
  text "Other World!"
  button(onclick= () => (comp.menu = 0)):
text "FOO"
else: discard

var comp = Comp()
comp.menu = 0
setRenderer(proc (): VNode = createDom(comp), root="ROOOT")


Run


Re: Fizzbuzz game

2020-01-25 Thread Lantos
This is great! really creative.


Re: Fizzbuzz game

2020-01-25 Thread Yardanico
Yes but that's not the point of this thread, we want to look at each others 
solutions after making them


Re: Fizzbuzz game

2020-01-25 Thread juancarlospaco
[https://github.com/code-golf/code-golf/issues/6#issue-259618460](https://github.com/code-golf/code-golf/issues/6#issue-259618460)
 & 
[https://github.com/code-golf/code-golf/issues/34#issue-322536856](https://github.com/code-golf/code-golf/issues/34#issue-322536856)


Re: Fizzbuzz game

2020-01-25 Thread juancarlospaco
[https://code-golf.io/scores/all-holes/nim](https://code-golf.io/scores/all-holes/nim)
 & [https://code-golf.io/fizz-buzz#nim](https://code-golf.io/fizz-buzz#nim)


Re: Fizzbuzz game

2020-01-25 Thread Yardanico
Sadly you can't actually see other people's solutions there


Re: Fizzbuzz game

2020-01-25 Thread Yardanico
Ok, something a _bit_ more sophisticated - I've used v as a base and created a 
version of it which can represent any Nim code in "fizz", "buzz" and 
"fizzbuzz". This code works like this - we import v and v library "decodes" the 
code written with v's which is a modified (and minified) version of v which can 
translate "fizz", "buzz" and "fizzbuzz" back to original nim code, which then 
runs actual FizzBuzz by @zevv

[https://gist.github.com/Yardanico/c6495e6b8d0776ff07b6feabab0b1d17#file-lol-nim](https://gist.github.com/Yardanico/c6495e6b8d0776ff07b6feabab0b1d17#file-lol-nim)
 (all other files are here too)


Re: Fizzbuzz game

2020-01-25 Thread Yardanico
Well, not really creative but still, just use 
[v](https://github.com/belamenso/v) (used @zevv's solution as a basis): 


import v

 v v v v v v vv vvv vv v v v v 
vv v v  v  v v  vv v v vv  vvv vv vvv
v vv vvv v vv  vvv vv  vv vv  vv vvv v vv
  v   vvv  v vvv v v v vv
v v  vv v vv v v v v  vv vvv vvv vv v vvv
vvv vvv   v  v  v v  v vv v
vvv vv vvv vvv vv v vvv vvv vv v v vvv v v 
v v  v vv v vvv vv vvv vvv vv v vvv vvv vvv  
v  v  v v  v vvv vv v v vvv
v v  v v  v vv v vvv  vvv vv  vv v
 v  v v  v vv v vv vv  v
v  vv vv v vv vv v vv  v vvv v  vv  vvv
vv vv v vv vvv vv vv   vv vvv v  v 
v v  v vv v vv vv  v v  vv vv v
vv vv v vv v vv vvv v  vv  vvv vv vv v 
vvv vv v v  v v 


Run


Re: Why inherit RootObj?

2020-01-25 Thread AyJayKay
> It probably should say "Object Oriented Programming style Classes should 
> inherit from RootObj or one of its descendants."

This wouldn't help really, since it is creating the same confusion. (At least 
for me.) I still don't understand **_why_** one should do it. What do I 
gain/loose putting it there or not?


Re: Fizzbuzz game

2020-01-25 Thread zevv
@miran: I was just hinting at using the `case` as an expression. I wonder 
though why you missed the obvious since you are trying so hard: :)


for n in 1..100:echo 
[$n,"Fizz","Buzz","FizzBuzz"][int(n%%3<1)+2*int(n%%5<1)]


Run

78


Re: Fizzbuzz game

2020-01-25 Thread miran
@zevv, you're not even trying! 43 whitespace characters saved:


for i in 1..100:
 echo case i mod 15
 of 0:"FizzBuzz"
 of 3,6,9,12:"Fizz"
 of 5,10:"Buzz"
 else: $i


Run


Re: Fizzbuzz game

2020-01-25 Thread miran
> one where you try to get the shortest amount of characters


for n in 1..100:echo [$n,"Fizz","Buzz","Fizz Buzz"][(n mod 3==0).int+2*(n 
mod 5==0).int]


Run

89 characters


Re: Fizzbuzz game

2020-01-25 Thread zevv
for i in 1..100:
echo case i mod 15:

of 0:
"FizzBuzz"
of 3,6,9,12:
"Fizz"
of 5,10:
"Buzz"
else:
$i