File: list.go

package info (click to toggle)
golang-github-nlpodyssey-gopickle 0.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 732 kB
  • sloc: python: 32; makefile: 5
file content (80 lines) | stat: -rw-r--r-- 1,714 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2020 NLP Odyssey Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package types

import (
	"fmt"
	"strings"
)

// ListAppender is implemented by any value that exhibits a list-like
// behaviour, allowing arbitrary values to be appended.
type ListAppender interface {
	Append(v interface{})
}

// List represents a Python "list" (builtin type).
type List []interface{}

var _ ListAppender = &List{}

// NewList makes and returns a new empty List.
func NewList() *List {
	l := make(List, 0, 4)
	return &l
}

// NewListFromSlice makes and returns a new List initialized with the elements
// of the given slice.
//
// The new List is a simple type cast of the input slice; the slice is _not_
// copied.
func NewListFromSlice(slice []interface{}) *List {
	l := List(slice)
	return &l
}

// Append appends one element to the end of the List.
func (l *List) Append(v interface{}) {
	*l = append(*l, v)
}

// Get returns the element of the List at the given index.
//
// It panics if the index is out of range.
func (l *List) Get(i int) interface{} {
	return (*l)[i]
}

// Len returns the length of the List.
func (l *List) Len() int {
	return len(*l)
}

func (*List) Call(args ...interface{}) (interface{}, error) {
	if len(args) == 0 {
		return NewList(), nil
	}
	if len(args) == 1 {
		return args[0], nil
	}
	return nil, fmt.Errorf("List: invalid arguments: %#v", args)
}

func (l *List) String() string {
	if l == nil {
		return "nil"
	}
	o := new(strings.Builder)
	o.WriteString("[")
	for i, v := range *l {
		if i > 0 {
			o.WriteString(", ")
		}
		fmt.Fprintf(o, "%v", v)
	}
	o.WriteString("]")
	return o.String()
}