[nim-dev] map/mapIt (now with benchmarks)

  • From: mora <morapeter@xxxxxxxxx>
  • To: nim-dev@xxxxxxxxxxxxx
  • Date: Mon, 7 Sep 2015 07:44:03 +0000

Hi,

Can we remove ``mapIt`` template in the favor of ``mySeq.map(x=>x+1)``? Please
let's discuss. There are so many points here, feel free to question all of them.

I believe that ``map(x=>x+1)`` is more readable, easier to understand and
remember for a newcomer. The ``map(x=>x+1)`` form is not working right now
(first glitch in this proposal), but it is doable (either by fixing ``auto`` or
modify ``=>`` to use generics instead of ``auto``).

Regarding the speed concern: I think that the C compiler should be smart enough
to optimize. I could only test is for ``gcc``, and it was true (with my simple
function). If you have other compilers (e.g., running Windows), please give me
some results by running https://github.com/petermora/nimMapBenchmarks (There
are so many ways to ruin a benchmark, please write me if I did something wrong.)

I believe (not checked) that with the ``--opt:size`` flag the C compiler will
give smaller binary size if we have a ``proc`` and not a ``template``. Nim can
be use on a microcontroller where the memory is limited.

There is a thread about possible lambda forms:
https://github.com/nim-lang/Nim/issues/2179 Maybe we could have something more
clever.

Side note: I also have a secret dream about **nimfmt** will be smart enough
that every time I reformat my source code (by saving in vim), then all the
``mySeq.map(x=>x+1)`` will be replaced by ``mySeq.map(x:int => x+1)``. This way
I don't have to write types, but I could always read them.

Thanks, Peter

Other related posts: