src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/persistent/list/list.go (about)

     1  // Package list implements persistent list.
     2  package list
     3  
     4  // List is a persistent list.
     5  type List interface {
     6  	// Len returns the number of values in the list.
     7  	Len() int
     8  	// Conj returns a new list with an additional value in the front.
     9  	Conj(any) List
    10  	// First returns the first value in the list.
    11  	First() any
    12  	// Rest returns the list after the first value.
    13  	Rest() List
    14  }
    15  
    16  // Empty is an empty list.
    17  var Empty List = &list{}
    18  
    19  type list struct {
    20  	first any
    21  	rest  *list
    22  	count int
    23  }
    24  
    25  func (l *list) Len() int {
    26  	return l.count
    27  }
    28  
    29  func (l *list) Conj(val any) List {
    30  	return &list{val, l, l.count + 1}
    31  }
    32  
    33  func (l *list) First() any {
    34  	return l.first
    35  }
    36  
    37  func (l *list) Rest() List {
    38  	return l.rest
    39  }