Skip to content
This repository has been archived by the owner on May 23, 2021. It is now read-only.

Invoke on List type #29

Open
issadarkthing opened this issue Sep 9, 2020 · 0 comments
Open

Invoke on List type #29

issadarkthing opened this issue Sep 9, 2020 · 0 comments

Comments

@issadarkthing
Copy link
Contributor

issadarkthing commented Sep 9, 2020

Hi, how do I invoke an Invokable with List as its argument without the List is being evaluated? Here's a simple implementation to create a reduce function which requires the callback function to accept List and Int64 type.

package main

import (
	"fmt"

	"github.com/spy16/sabre"
)

const program = `
(print (reduce (fn [acc x] (acc.Cons x)) '() '(1 2 3)))
`

func main() {
	scope := sabre.New()
	scope.BindGo("sum", sum)
	scope.BindGo("print", fmt.Println)
	scope.BindGo("fn", sabre.Lambda)
	scope.BindGo("reduce", reduce)

	_, err := sabre.ReadEvalStr(scope, program)
	if err != nil {
		panic(err)
	}
}

func reduce(fn sabre.MultiFn, acc sabre.Value, list *sabre.List) (sabre.Value, error) {

	for _, v := range list.Values {
		applied, err := fn.Invoke(nil, acc, v)
		if err != nil {
			return nil, err
		}
		acc = applied
	}

	return acc, nil
}

This will produce this error.

$ go run main.go
panic: eval-error in '' (at line 0:0): cannot invoke value of type 'sabre.Int64'

goroutine 1 [running]:
main.main()
	/home/terra/Documents/system33/go/sabre/examples/simple/main.go:22 +0x194
exit status 2
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant