ostap / comp

a tool for querying files in various formats
MIT License
43 stars 2 forks source link

adding $ symbol for loop index, e.g. [i|i<-["a","b"],$i < 1] #23

Open julochrobak opened 11 years ago

julochrobak commented 11 years ago

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 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 commented 10 years ago

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 commented 10 years ago

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 https://github.com/julochrobak/comp/commit/e230d44de30da988fc979261574fb784de6807f4 commit.