Skip to content

Conversation

@julochrobak
Copy link
Contributor

I implemented the "$" operator to allow retrieving the index of a loop. Here are some examples:

$ bin/comp '[ i | i <- ["a", "b", "c"], $i < 1]'
[ "a" ]

$ bin/comp '[ {pos: $i, val: i} | i <- ["a", "b", "c"], $i > 0]'
[ { "pos": 1, "val": "b" }, { "pos": 2, "val": "c" } ]

$ bin/comp '[ {i, j} | i <- ["a", "b"], j <- [-1, -2], $i > 0 && $j < 1 ]'
[ { "i": "b", "j": -1 } ]

There are two things to discuss:

  • I also tried to implement a function which would take the iteration variable and return it's index, for example:

    [ i | i <- [1,2,3], index(i) < 1 ]
    

    but I find this very complex to implement, because the "i" would have to change from an element of a list into kind of an object which contains also "iteration" information and the actual list element, and than the "index" function has to access the iteration part of this object, while everything else must access the element part:

    [ i | i <- [1,2,3], i < 1]
    

    may be there is a simple way to do it...

  • the other question is, why are we actually implementing this? if we only want to allow to take just part of a list we can do it either either the haskell way:

    head([1,2,3])       =>  1
    tail([1,2,3])       =>  3
    take(2, [1,2,3])    =>  [1,2]
    drop(1, [1,2,3])    =>  [2,3] 
    

    or the golang way:

    [1,2,3][:1]     =>  [1]
    [1,2,3][2:]     =>  [3]
    [1,2,3][0:2]    =>  [1,2]
    [1,2,3][1:]     =>  [2,3]
    

I prefer to have both the $ operator together with the golang way of getting a slice of a list.The $ is valuable for all kinds of more complex calculations/filtering, e.g. take the elements only on the every second position in the list, can be put in the result to remember a position of an element in the list, and so on...

@julochrobak
Copy link
Contributor Author

DO NOT merge, there is a bug in this branch. The COMP crashes when using a parallel execution:

$ bin/comp '[a|a<~ [1,2]]'
panic: runtime error: index out of range

goroutine 4 [running]:
main.(*Program).Run(0xc2000de300, 0xc2000fe000, 0xc2000c1c90, 0x5)
    /Users/julo/comp/src/comp/machine.go:146 +0x19a6
main.func·018(0xc2000de300, 0xc2000fe000)
    /Users/julo/comp/src/comp/machine.go:189 +0x38
created by main.(*Program).Run
    /Users/julo/comp/src/comp/machine.go:190 +0x242d

goroutine 1 [chan receive]:
main.(*Program).Run(0xc2000de280, 0xc2000ed000, 0xc2000b4680, 0xc2000de280)
    /Users/julo/comp/src/comp/machine.go:195 +0x24ba
main.Command(0x7fff5fbffae1, 0xd, 0x2b4940, 0x0, 0x24, ...)
    /Users/julo/comp/src/comp/main.go:46 +0x166
main.main()
    /Users/julo/comp/src/comp/main.go:98 +0x1a0

goroutine 2 [syscall]:

goroutine 5 [runnable]:
main.func·018(0xc2000de380, 0xc20010f000)
    /Users/julo/comp/src/comp/machine.go:188
created by main.(*Program).Run
    /Users/julo/comp/src/comp/machine.go:190 +0x242d

@julochrobak
Copy link
Contributor Author

this is READY to be merged, the issue with parallel executions was fixed and the source branch julochrobak:tier was rebased and everything is part of the julochrobak@e230d44 commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant