Skip to content

Commit

Permalink
array.min()
Browse files Browse the repository at this point in the history
Finds the lowest number in an array:

```py
[].min() # NULL
[0, 5, -10, 100].min() # -10
```
  • Loading branch information
odino committed Mar 28, 2020
1 parent 5f07373 commit c5bce79
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/types/array.md
Expand Up @@ -441,6 +441,15 @@ Finds the highest number in an array:
[0, 5, -10, 100].max() # 100
```
### min()
Finds the lowest number in an array:
```py
[].min() # NULL
[0, 5, -10, 100].min() # -10
```
## Next
That's about it for this section!
Expand Down
13 changes: 13 additions & 0 deletions evaluator/builtin_functions_test.go
Expand Up @@ -696,12 +696,25 @@ func TestMax(t *testing.T) {
{`[].max()`, nil},
{`[-10].max()`, -10},
{`[-10, 0, 100, 9].max()`, 100},
{`[-10, 0, 100, 9, 100.1].max()`, 100.1},
{`[-10, {}, 100, 9].max()`, "max(...) can only be called on an homogeneous array, got [-10, {}, 100, 9]"},
}

testBuiltinFunction(tests, t)
}

func TestMin(t *testing.T) {
tests := []Tests{
{`[].min()`, nil},
{`[-10].min()`, -10},
{`[-10, 0, 100, 9].min()`, -10},
{`[-10, 0, 100, 9, -10.5].min()`, -10.5},
{`[-10, {}, 100, 9].min()`, "min(...) can only be called on an homogeneous array, got [-10, {}, 100, 9]"},
}

testBuiltinFunction(tests, t)
}

func testBuiltinFunction(tests []Tests, t *testing.T) {
for _, tt := range tests {
evaluated := testEval(tt.input)
Expand Down
38 changes: 38 additions & 0 deletions evaluator/functions.go
Expand Up @@ -198,6 +198,11 @@ func getFns() map[string]*object.Builtin {
Types: []string{object.ARRAY_OBJ},
Fn: maxFn,
},
// min(array:[1, 2, 3])
"min": &object.Builtin{
Types: []string{object.ARRAY_OBJ},
Fn: minFn,
},
// sort(array:[1, 2, 3])
"sort": &object.Builtin{
Types: []string{object.ARRAY_OBJ},
Expand Down Expand Up @@ -1190,6 +1195,39 @@ func maxFn(tok token.Token, env *object.Environment, args ...object.Object) obje
return &object.Number{Token: tok, Value: max}
}

// min(array:[1, 2, 3])
func minFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
err := validateArgs(tok, "min", args, 1, [][]string{{object.ARRAY_OBJ}})
if err != nil {
return err
}

arr := args[0].(*object.Array)
if arr.Empty() {
return object.NULL
}

if !arr.Homogeneous() {
return newError(tok, "min(...) can only be called on an homogeneous array, got %s", arr.Inspect())
}

if arr.Elements[0].Type() != object.NUMBER_OBJ {
return newError(tok, "min(...) can only be called on arrays of numbers, got %s", arr.Inspect())
}

min := arr.Elements[0].(*object.Number).Value

for _, v := range arr.Elements[1:] {
elem := v.(*object.Number)

if elem.Value < min {
min = elem.Value
}
}

return &object.Number{Token: tok, Value: min}
}

// sort(array:[1, 2, 3])
func sortFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
err := validateArgs(tok, "sort", args, 1, [][]string{{object.ARRAY_OBJ}})
Expand Down

0 comments on commit c5bce79

Please sign in to comment.