list

package
v0.0.0-...-b3cfef8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2022 License: MIT, LGPL-3.0 Imports: 1 Imported by: 0

README

List

Doubly-linked list in Go

Intro

A doubly-linked list implementation in Go

This library implements doubly-linked list in Go. Since Go doesn't support generics, this library utilize empty interface to emulate this functionality, delegating element comparison tasks to users.

This library implements common higher order functions as well. Same, the library delegates related function objects to users.

Usage

Find specific elements:

package main

import (
    "github.com/cwchentw/algo-golang/list"
    "log"
)

func main() {
    l := list.New()

    for i := 1; i <= 10; i++ {
        l.Push(i)
    }

    data, _ := l.At(3)
    if data != 4 {
        log.Fatalln("Data error")
    }
}

Select only even elements:

package main

import (
    "errors"
    "github.com/cwchentw/algo-golang/list"
    "log"
)

func main() {
    l := list.New()

    for i := 1; i <= 10; i++ {
        l.Push(i)
    }

    evens, _ := l.Select(isEven)
    for e := range evens.Iter() {
        n, _ := e.(int)
	    if n % 2 != 0 {
            log.Fatalln("Error Select result")
        }
    }
}

func isEven(a interface{}) (bool, error) {
    n, ok := a.(int)
    if ok != true {
        return false, errors.New("Failed type assertion on a")
    }

    return n % 2 == 0, nil
}

2017, Michelle Chen

License

MIT

Documentation

Overview

A doubly-linked list implementation in Go

This library implements doubly-linked list in Go. Since Go doesn't support generics, this library utilize empty interface to mimic this functionality and element comparison tasks are delegated to users.

Example:

package main

import (
    "github.com/cwchentw/golist"
    "log"
)

func main() {
    list := list.New()

    for i := 1; i <= 10; i++ {
        list.Push(i)
    }

    data, _ := list.At(3)
    if data != 4 {
        log.Fatalln("Data error")
    }
}

This library implements higher order functions as well. As Go lacks generics, the library delegates related interface implementations to users.

Example:

import (
    "errors"
    "github.com/cwchentw/golist"
    "log"
)

func main() {
    list := list.New()

    for i := 1; i <= 10; i++ {
        list.Push(i)
    }

    evens, _ := list.Select(even)
    for e := range evens.Iter() {
        n, _ := e.(int)
        if n % 2 != 0 {
            log.Fatalln("Error Select result")
        }
    }
}

func even(a interface{}) (bool, error) {
    n, ok := a.(int)
    if ok != true {
        return false, errors.New("Failed type assertion on a")
    }

    return n % 2 == 0, nil
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List struct {
	// contains filtered or unexported fields
}

List holds the internal structure of a doubly-linked list

func FromArgs

func FromArgs(args ...interface{}) *List

Initialize with some values

func FromArray

func FromArray(arr []interface{}) *List

Initialize from array

func New

func New() *List

Initialize a empty List

func (*List) All

func (list *List) All(grep func(interface{}) (bool, error)) (bool, error)

Check whether all items in the list fulfill the predicate. Implement grep function object to use this method.

func (*List) Any

func (list *List) Any(grep func(interface{}) (bool, error)) (bool, error)

Check whether any item in the list fulfills the predicate. Implement grep function object to use this method.

func (*List) At

func (list *List) At(index uint) (interface{}, error)

Get the element at “index“. Returns error if “index“ out of range

func (*List) Clone

func (list *List) Clone() *List

Clone the content of an existing list. Shallow copy implemented.

func (*List) Each

func (list *List) Each() <-chan interface{}

An alias of Iter

func (*List) Equal

func (list *List) Equal(other *List, eq func(interface{}, interface{}) (bool, error)) (bool, error)

Check whether two lists are equal. If both the lengths and data of the two list are identical, the two lists are equal. Implement eq function object to use this method.

func (*List) Find

func (list *List) Find(data interface{}, cmp func(interface{}, interface{}) (bool, error)) (uint, error)

Find an item in the list. Return error if not found. Implement cmp function object to use this method.

func (*List) IsEmpty

func (list *List) IsEmpty() bool

Check whether the list is empty.

func (*List) Iter

func (list *List) Iter() <-chan interface{}

Iterate through the list and get the elements.

func (*List) Len

func (list *List) Len() uint

Get the length of the list.

func (*List) Map

func (list *List) Map(m func(interface{}) (interface{}, error)) (*List, error)

Iterate through the list and transform each datum into new value. Implement m function object to use this method.

func (*List) Pop

func (list *List) Pop() (interface{}, error)

Pop data from the tail of the list. Return error if empty list.

func (*List) Push

func (list *List) Push(data interface{})

Push data into the tail of the list

func (*List) Reduce

func (list *List) Reduce(r func(a interface{}, b interface{}) (interface{}, error)) (interface{}, error)

Reduce the list into single value. Implement Reduce interface to use this method.

func (*List) Remove

func (list *List) Remove(data interface{}, cmp func(interface{}, interface{}) (bool, error)) error

Remove an item from the list. Return error if not found. Implement cmp function object to use this method

func (*List) RemoveAt

func (list *List) RemoveAt(index uint) (interface{}, error)

Remove the element at “index“ from the list. Returns errors if “index“ out of range.

func (*List) Select

func (list *List) Select(grep func(interface{}) (bool, error)) (*List, error)

Select items from the list when item fulfill the predicate. Implement grep function object to use this method.

func (*List) Shift

func (list *List) Shift() (interface{}, error)

Shift data from the head of the list. Return error if empty list.

func (*List) Sort

func (list *List) Sort(pred func(interface{}, interface{}) (bool, error)) (*List, error)

Sort the list. Shallow copy implemented. Implement pred function object to use this method.

func (*List) Unshift

func (list *List) Unshift(data interface{})

Unshift data into the head of the list

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL