koka-lang / koka

Koka language compiler and interpreter
http://koka-lang.org
Other
3.26k stars 162 forks source link

Is foreach-indexed broken? #220

Closed aaron-jack-manning closed 2 years ago

aaron-jack-manning commented 2 years ago

Creating the simplest version of the error I am dealing with, if I run the following:

val vect = vector(6, 0)

foreach-indexed (vect, fn (i, element) ->
        print (i)
    )

I get as output:

000000

Not sure what I am doing wrong here, or if there is an error in the implementation of foreach-indexed but the value of i doesn't seem to change.

ArbilGit commented 2 years ago

Th order of index and element parameters is reverse:

  val vect = vector(6, 0)
  vect.foreach-indexed fn(element, i)
    i.print
aaron-jack-manning commented 2 years ago

Thank you. Seems like a strange decision that it would be backwards compared to foreach-indexed for lists...

anfelor commented 2 years ago

Ah yes, it would be good if the order of arguments was consistent. In particular, we have

fun foreach-indexed( xs : list<a>, action : (int, a) -> e types/() ) : e types/()
fun foreach-indexed( v : vector<a>, f : (a, int) -> e types/() ) : e types/()

So the order of arguments for f/action is currently inconsistent.

daanx commented 2 years ago

Thanks for spotting this -- fixed by passing the index first.