-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #679 from nevalang/lists-at
Add `{lists,strings}.At` flow ops to access elements of a List and runes in a string
- Loading branch information
Showing
7 changed files
with
177 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { lists } | ||
|
||
const lst list<int> = [1, 1, 5, 112, 69, 420] | ||
|
||
flow Main(start) (stop) { | ||
nodes { Index<list<int>>, Println } | ||
nodes { lists.At<int>, Println } | ||
:start -> [ | ||
($lst -> index:data), | ||
(4 -> index:idx) | ||
($lst -> at:data), | ||
(4 -> at:idx) | ||
] | ||
[index:res, index:err] -> println -> :stop | ||
} | ||
[at:res, at:err] -> println -> :stop | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package funcs | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/nevalang/neva/internal/runtime" | ||
) | ||
|
||
var errListIndexOutOfBounds = errors.New("list index out of bounds") | ||
|
||
type listAt struct{} | ||
|
||
func (listAt) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) { | ||
dataIn, err := io.In.Port("data") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
idxIn, err := io.In.Port("idx") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resOut, err := io.Out.Port("res") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
errOut, err := io.Out.Port("err") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return func(ctx context.Context) { | ||
for { | ||
var data []runtime.Msg | ||
var idx int64 | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return | ||
case msg := <-dataIn: | ||
data = msg.List() | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return | ||
case msg := <-idxIn: | ||
idx = msg.Int() | ||
} | ||
|
||
if idx < 0 { | ||
// Support negative indexing: | ||
// $l = [1, 2, 3] | ||
// $l[-1] // 3 | ||
idx += int64(len(data)) | ||
} | ||
|
||
if idx < 0 || idx >= int64(len(data)) { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case errOut <- errorFromString(errListIndexOutOfBounds.Error()): | ||
continue | ||
} | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return | ||
case resOut <- data[idx]: | ||
} | ||
} | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package funcs | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/nevalang/neva/internal/runtime" | ||
) | ||
|
||
var errStrIndexOutOfBounds = errors.New("string index out of bounds") | ||
|
||
type stringAt struct{} | ||
|
||
func (stringAt) Create(io runtime.FuncIO, _ runtime.Msg) (func(context.Context), error) { | ||
dataIn, err := io.In.Port("data") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
idxIn, err := io.In.Port("idx") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resOut, err := io.Out.Port("res") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
errOut, err := io.Out.Port("err") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return func(ctx context.Context) { | ||
for { | ||
var data string | ||
var idx int64 | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return | ||
case msg := <-dataIn: | ||
data = msg.Str() | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return | ||
case msg := <-idxIn: | ||
idx = msg.Int() | ||
} | ||
|
||
if idx < 0 { | ||
// Support negaitve indexing: | ||
// $s = "abc" | ||
// $s[-1] // "c" | ||
idx += int64(len(data)) | ||
} | ||
|
||
if idx >= 0 && idx < int64(len(data)) { | ||
var res rune | ||
var found bool | ||
for i, r := range data { | ||
if int64(i) == idx { | ||
res = r | ||
found = true | ||
break | ||
} | ||
} | ||
if found { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case resOut <- runtime.NewStrMsg(string(res)): | ||
continue | ||
} | ||
} | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return | ||
case errOut <- errorFromString(errStrIndexOutOfBounds.Error()): | ||
} | ||
} | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters