-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 1. adding more methods to enum module 2. added ModuleMap.AddMap() 3. added bytes iterator * add builtin functions 'is_enumerable' and 'is_array_like' * builtin function 'is_iterable' * first iteration on 'enum' module * fix 'is_iterable' builtin function
- Loading branch information
Showing
16 changed files
with
417 additions
and
77 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
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,19 @@ | ||
# Module - "enum" | ||
|
||
```golang | ||
enum := import("enum") | ||
``` | ||
|
||
## Functions | ||
|
||
- `all(x, fn) => bool`: returns true if the given function `fn` evaluates to a truthy value on all of the items in `x`. It returns undefined if `x` is not enumerable. | ||
- `any(x, fn) => bool`: returns true if the given function `fn` evaluates to a truthy value on any of the items in `x`. It returns undefined if `x` is not enumerable. | ||
- `chunk(x, size) => [object]`: returns an array of elements split into groups the length of size. If `x` can't be split evenly, the final chunk will be the remaining elements. It returns undefined if `x` is not array. | ||
- `at(x, key) => object`: returns an element at the given index (if `x` is array) or key (if `x` is map). It returns undefined if `x` is not enumerable. | ||
- `each(x, fn)`: iterates over elements of `x` and invokes `fn` for each element. `fn` is invoked with two arguments: `key` and `value`. `key` is an int index if `x` is array. `key` is a string key if `x` is map. It does not iterate and returns undefined if `x` is not enumerable.` | ||
- `filter(x, fn) => [object]`: iterates over elements of `x`, returning an array of all elements `fn` returns truthy for. `fn` is invoked with two arguments: `key` and `value`. `key` is an int index if `x` is array. `key` is a string key if `x` is map. It returns undefined if `x` is not enumerable. | ||
- `find(x, fn) => object`: iterates over elements of `x`, returning value of the first element `fn` returns truthy for. `fn` is invoked with two arguments: `key` and `value`. `key` is an int index if `x` is array. `key` is a string key if `x` is map. It returns undefined if `x` is not enumerable. | ||
- `find_key(x, fn) => int/string`: iterates over elements of `x`, returning key or index of the first element `fn` returns truthy for. `fn` is invoked with two arguments: `key` and `value`. `key` is an int index if `x` is array. `key` is a string key if `x` is map. It returns undefined if `x` is not enumerable. | ||
- `map(x, fn) => [object]`: creates an array of values by running each element in `x` through `fn`. `fn` is invoked with two arguments: `key` and `value`. `key` is an int index if `x` is array. `key` is a string key if `x` is map. It returns undefined if `x` is not enumerable. | ||
- `key(k, _) => object`: returns the first argument. | ||
- `value(_, v) => object`: returns the second argument. |
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
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
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,57 @@ | ||
package objects | ||
|
||
import "github.com/d5/tengo/compiler/token" | ||
|
||
// BytesIterator represents an iterator for a string. | ||
type BytesIterator struct { | ||
v []byte | ||
i int | ||
l int | ||
} | ||
|
||
// TypeName returns the name of the type. | ||
func (i *BytesIterator) TypeName() string { | ||
return "bytes-iterator" | ||
} | ||
|
||
func (i *BytesIterator) String() string { | ||
return "<bytes-iterator>" | ||
} | ||
|
||
// BinaryOp returns another object that is the result of | ||
// a given binary operator and a right-hand side object. | ||
func (i *BytesIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { | ||
return nil, ErrInvalidOperator | ||
} | ||
|
||
// IsFalsy returns true if the value of the type is falsy. | ||
func (i *BytesIterator) IsFalsy() bool { | ||
return true | ||
} | ||
|
||
// Equals returns true if the value of the type | ||
// is equal to the value of another object. | ||
func (i *BytesIterator) Equals(Object) bool { | ||
return false | ||
} | ||
|
||
// Copy returns a copy of the type. | ||
func (i *BytesIterator) Copy() Object { | ||
return &BytesIterator{v: i.v, i: i.i, l: i.l} | ||
} | ||
|
||
// Next returns true if there are more elements to iterate. | ||
func (i *BytesIterator) Next() bool { | ||
i.i++ | ||
return i.i <= i.l | ||
} | ||
|
||
// Key returns the key or index value of the current element. | ||
func (i *BytesIterator) Key() Object { | ||
return &Int{Value: int64(i.i - 1)} | ||
} | ||
|
||
// Value returns the value of the current element. | ||
func (i *BytesIterator) Value() Object { | ||
return &Int{Value: int64(i.v[i.i-1])} | ||
} |
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
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,49 @@ | ||
package runtime_test | ||
|
||
import "testing" | ||
|
||
func TestSrcModEnum(t *testing.T) { | ||
expect(t, ` | ||
x := import("enum") | ||
out = x.all([1, 2, 3], func(_, v) { return v >= 1 }) | ||
`, Opts().Stdlib(), true) | ||
expect(t, ` | ||
x := import("enum") | ||
out = x.all([1, 2, 3], func(_, v) { return v >= 2 }) | ||
`, Opts().Stdlib(), false) | ||
|
||
expect(t, ` | ||
x := import("enum") | ||
out = x.any([1, 2, 3], func(_, v) { return v >= 1 }) | ||
`, Opts().Stdlib(), true) | ||
expect(t, ` | ||
x := import("enum") | ||
out = x.any([1, 2, 3], func(_, v) { return v >= 2 }) | ||
`, Opts().Stdlib(), true) | ||
|
||
expect(t, ` | ||
x := import("enum") | ||
out = x.chunk([1, 2, 3], 1) | ||
`, Opts().Stdlib(), ARR{ARR{1}, ARR{2}, ARR{3}}) | ||
expect(t, ` | ||
x := import("enum") | ||
out = x.chunk([1, 2, 3], 2) | ||
`, Opts().Stdlib(), ARR{ARR{1, 2}, ARR{3}}) | ||
expect(t, ` | ||
x := import("enum") | ||
out = x.chunk([1, 2, 3], 3) | ||
`, Opts().Stdlib(), ARR{ARR{1, 2, 3}}) | ||
expect(t, ` | ||
x := import("enum") | ||
out = x.chunk([1, 2, 3], 4) | ||
`, Opts().Stdlib(), ARR{ARR{1, 2, 3}}) | ||
expect(t, ` | ||
x := import("enum") | ||
out = x.chunk([1, 2, 3, 4, 5, 6], 2) | ||
`, Opts().Stdlib(), ARR{ARR{1, 2}, ARR{3, 4}, ARR{5, 6}}) | ||
|
||
expect(t, ` | ||
x := import("enum") | ||
out = x.at([1, 2, 3], 0) | ||
`, Opts().Stdlib(), 1) | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.