File: list.go

package info (click to toggle)
golang-github-xiaq-persistent 0.0~git20180301.cd415c6-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 172 kB
  • sloc: makefile: 27
file content (39 lines) | stat: -rw-r--r-- 743 bytes parent folder | download | duplicates (2)
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
// Package list implements persistent list.
package list

// List is a persistent list.
type List interface {
	// Len returns the number of values in the list.
	Len() int
	// Cons returns a new list with an additional value in the front.
	Cons(interface{}) List
	// First returns the first value in the list.
	First() interface{}
	// Rest returns the list after the first value.
	Rest() List
}

// Empty is an empty list.
var Empty List = &list{}

type list struct {
	first interface{}
	rest  *list
	count int
}

func (l *list) Len() int {
	return l.count
}

func (l *list) Cons(val interface{}) List {
	return &list{val, l, l.count + 1}
}

func (l *list) First() interface{} {
	return l.first
}

func (l *list) Rest() List {
	return l.rest
}