zero-functional / zero-functional

A library providing zero-cost chaining for functional abstractions in Nim.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

index out of bounds with map

abijahm opened this issue · comments

import zero_functional

let x = [1,2,3] --> map(it * 2)

echo x

this example gives error

test.nim(3)              test
test.nim(3)              :anonymous
zero_functional.nim      addItemZf
system.nim(2833)         sysFatal
Error: unhandled exception: index out of bounds [IndexError]
Nim Compiler Version 0.18.0 [Windows: amd64]
Copyright (c) 2006-2018 by Andreas Rumpf

git hash: 5ee9e86c87d831d32441db658046fc989a197ac9
active boot switches: -d:release

@abijahm: could you try it again with zero-dsl branch? That is actually the current dev branch that should be merged soon (I guess).
It would work on that branch but the output would be a sequence - as the map function is a function that is considered to change the result type (could also do $it for instance). To force array output the "to" conversion is needed.

  let x = [1,2,3] --> map(it * 2) --> to(array)
  echo x

to(array) works in that case because the size of [1,2,3] is known at compile time. Otherwise it would not work - or the array size and type must be given with to(array[3,int]).
Alternatively you can work with seq or use the iter function to create an iterator.

fix merged