Re: How check if expression has a type without triggering compilation failure

2017-04-04 Thread LeuGim
> template filter(expr: untyped) =
> 
> 
> when compiles(expr):
> when expr is seq[bool]: echo "bool" elif expr is seq[int]: echo "int" 
> else: echo "something else"
> else:
> echo "can't compile"
> 
> filter(a + b)

It seems code-highliting is broken. 


Re: Amicable numbers in Nim and a few questions

2017-04-04 Thread petevine
I eventually settled for plain aliases: 


alias nim-gen='nim c -r -d:release --passC:-mcpu=cortex-a5 
--passC:-mfpu=neon --passC:- ftree-vectorize --passC:-fprofile-generate 
--passL:-lgcov'



alias nim-use='nim c -d:release --passC:-mcpu=cortex-a5 --passC:-mfpu=neon 
--passC:-ftree-vectorize --passC:-fprofile-use'


so that building a profiled binary is as simple as, e.g.:

$ nim-gen matmul.nim 1500 && nim-use matmul.nim


Re: How to create, destroy, and recreate threads

2017-04-04 Thread bpr
@cdunn2001, it looks like it's running for me with the latest on Ubuntu 14.04 
and 16.04; I haven't tried on OS X yet.


Re: Procedure which returns procedure

2017-04-04 Thread flyx
Your code does not work because you are using `func` as identifier, which is a 
reserved keyword. I demangled it a bit and renamed `func` to `fun`:


type
  MyProc1 = proc()
  MyProc2 = proc(p: MyProc1)
  MyProc3 = proc(p: MyProc1): MyProc1

proc applyAllSync(funcs: seq[MyProc2], onAllDone: MyProc1) =
  fold(funcs,
   proc(onDoneNothing: MyProc1) = onDoneNothing,
   proc(doBefore: MyProc3, fun: MyProc2) =
 return (proc(onDone: MyProc1): MyProc1 =
return doBefore(proc() =
  fun(onDone)
)
 )(onAllDone)()
  )



Re: How check if expression has a type without triggering compilation failure

2017-04-04 Thread cdome
I need something that not going to cause compilation error for the case when 
expr does not have a type


template filter(expr: untyped) =
  when expr is seq[bool]: echo "bool"
  elif expr is seq[int]:  echo "int"
  else:   discard

filter(a + b)  # a and b are not defined, hence no type.
   # Should gracefully go to else statement, but currently 
causes compilation error




Re: Procedure which returns procedure

2017-04-04 Thread dmitrys99
Well, initially it is not JS. I'm working on our internal compiler's Nim 
backend.

Original function source (ML-like language): 


applyAllSync(funcs: [(() -> void) -> void], onAllDone: () -> void) -> void {
fold(funcs, \onDoneNothing -> onDoneNothing,
\doBefore : (() -> void) -> () -> void, func : (() -> void) 
-> void -> {
\onDone : () -> void-> {
doBefore(\ -> func(onDone))
}
}
)(onAllDone)();
}


We have first-class citizen functions, so we can treat them as usual objects, 
which can be returned and used. The syntax here is simple: 


() -> void

is like 


proc(): void

in parameters 


 \name -> void

is like 


proc(name: T): void

, used as lambda (T might be changed to any, depends on context), 


 [] 

is an array.

Direct conversion with types gives next Nim code: 


proc applyAllSync(funcs: seq[proc(t0: proc(): void): void], onAllDone: 
proc(): void): void =.
  fold(funcs, proc(onDoneNothing: any) =
  onDoneNothing, proc(doBefore: (proc(t0: proc(): void): proc(): void), 
func: (proc(t0: proc(): void): void)) =
  proc(onDone: (proc(): void)) =
  return doBefore(proc() =
  func(onDone)))(onAllDone)()


It doesn't compile, error is 


test.nim(3, 77) Error: ')' expected


Position 77 is a letter f in word func at line 3.

I showed Javascript example previously, because it works. I understand JS type 
system and it was just to show syntax.

Again, my question is 'How syntactically nested lambdas and/or closures can be 
written?'


Re: How check if expression has a type without triggering compilation failure

2017-04-04 Thread Arrrrrrrrr
type(expr) -> expr:


template filter(expr: untyped) =
  when expr is seq[bool]: echo "bool"
  elif expr is seq[int]:  echo "int"
  else:   discard

filter newSeq[int]()


I don't know if this is useful for your usecase tho.


Re: Procedure which returns procedure

2017-04-04 Thread flyx
Your JS is horrible to read as it lacks type information (what is funcs, what 
is onAllDone, what is doBefore etc). I tried to interpret it but gave up. You 
should rather explain what you want to do than giving us this piece of code.


How check if expression has a type without triggering compilation failure

2017-04-04 Thread cdome
Hi Hackers,

I need to apply different logic in template if the argument has a particular 
type or is untyped expression. Calling getType() in macro triggers compilation 
failure for untyped expression.

Ideally, something like this. Any ideas are appreciated.


template filter(expr : untyped) =
  when type(expr) is seq[bool]:  # currently causes compilation failure if 
expr is untyped
 .
  elif type(expr) is seq[int]:
 
  else: # expression has no type, inject additional variables to make 
expression typed
 



Thank you 


Re: why is nim install weird?

2017-04-04 Thread Krux02
I can only support the workflow Araq suggested, I do the same. You can then add 
`~/projects/Nim/bin` to your path, or symlink the binaries from there to a 
folder that is already in your path. I have `~/bin` in my path and put the 
symlinks there. `make install` should not be done with any software at all. You 
can't get rid of it anymore.


Re: Procedure which returns procedure

2017-04-04 Thread dmitrys99
I have another question.

There is a procedure


proc fold*[T, S](arr: seq[T], init: S, op: proc(acc: S, v: T): S): S =
  for x in arr:
init = op(init, x)
  return init


How to implement in Nim this function (Javascript): 


function applyAllSync(funcs, onAllDone) {
return fold(funcs, (function(onDoneNothing) {
return onDoneNothing;
}), (function(doBefore, func) {
return (function(onDone) {
return doBefore((function() {
return func(onDone);
}));
});
}))(onAllDone)();
};



Re: why is nim install weird?

2017-04-04 Thread Araq
> The ./install.sh script puts things under standard subdirs for /usr/bin and 
> /usr/local/bin, but then does something different for other dirs.

"Does something different" \-- Like what? But you shouldn't use `install.sh` 
anyway, manual messing with /usr/bin is bad, Linux got package managers for 
that.

Nim's take on this is:

  * If you want /usr/bin installation, use your Linux package manager.
  * If you want bleeding edge, don't install it. This way it's easy to have 
multiple Nim versions on the same machine. (Something that apparently never was 
considered when the "Unix way" to "install" software was set into stone.)




Re: why is nim install weird?

2017-04-04 Thread hcorion
A better installation and version management system similar to rustup is in 
development by @dom96 called choosenim.

It seems like your on Linux? If so, perhaps your distro has a version of Nim in 
the repo? Perhaps some work could be done by creating a Flatpack or AppImage 
for easier cross-distro installation.

If your on Mac you can use homebrew to install Nim. There is also nim-vm 
[https://github.com/ekarlso/nim-vm](https://github.com/ekarlso/nim-vm) but it 
is unmaintained and doesn't support Nimble very well.


why is nim install weird?

2017-04-04 Thread bkerin
Ok so I've been wanting to try nim for a while and finally did.

Right away I hit something I really dislike, and that is that nim has 
apparently decided to do it's own thing about installation, rather than working 
like ./configure --prefix=whatever. The ./install.sh script puts things under 
standard subdirs for /usr/bin and /usr/local/bin, but then does something 
different for other dirs. So no real support for anything like --prefix.

The instructions under 'Installation based on generated C code' are also 
problematic. Assuming you don't want to dump the vim tarball into your prefix, 
you apparently need to copy things into place after the build, but the 
directions don't cover this. The install.sh script is mentioned but not 
described, and re-running it after doing nim c koch && ./koch tools' doesn't 
appear to give you an installed nimble (for example).

I'm really excited to try nim and have some projects in mind for it. In case 
the noob perspective is valued here, I would strongly suggest that it would be 
worth it to clean up the install process and wherever possible make it work 
like other software.


Re: How to create, destroy, and recreate threads

2017-04-04 Thread cdunn2001
With that fix on `devel`, it no longer hangs on OSX, but it still seg-faults on 
Ubuntu.