API Docs for: 1.0.0
Show:

LinkedList Class

Doubly linked list for storing items. Supports iteration via LinkedListIterator (returned by this.iterator()) or Y.each(). Also supports all the other operations defined in gallery-funcprog.

Direct indexing into the list is not supported, as a reminder that it is an expensive operation. Instead, use find() with a function that checks the index.

Constructor

LinkedList

(
  • [list]
)

Parameters:

  • [list] Mixed optional

    any scalar or iterable list

Methods

append

(
  • value
)
LinkedListItem

Parameters:

  • value Mixed

    value to append

Returns:

LinkedListItem:

appended item

clear

()

Clear the list.

each

(
  • f
  • c
)

Executes the supplied function on each item in the list. The function receives the value, the index, and the list itself as parameters (in that order).

Parameters:

  • f Function

    the function to execute on each item

  • c Object

    optional context object

every

(
  • f
  • c
)
Boolean

Executes the supplied function on each item in the list. Iteration stops if the supplied function does not return a truthy value. The function receives the value, the index, and the list itself as parameters (in that order).

Parameters:

  • f Function

    the function to execute on each item

  • c Object

    optional context object

Returns:

Boolean:

true if every item in the array returns true from the supplied function, false otherwise

filter

(
  • f
  • c
)
Object

Executes the supplied function on each item in the list. Returns a new list containing the items for which the supplied function returned a truthy value. The function receives the value, the index, and the object itself as parameters (in that order).

Parameters:

  • f Function

    the function to execute on each item

  • c Object

    optional context object

Returns:

Object:

list of items for which the supplied function returned a truthy value (empty if it never returned a truthy value)

find

(
  • f
  • c
)
Mixed

Executes the supplied function on each item in the list, searching for the first item that matches the supplied function. The function receives the value, the index, and the object itself as parameters (in that order).

Parameters:

  • f Function

    the function to execute on each item

  • c Object

    optional context object

Returns:

Mixed:

the first item for which the supplied function returns true, or null if it never returns true

head

() LinkedListItem

Returns:

LinkedListItem:

the first item in the list, or null if the list is empty

indexOf

(
  • needle
)
Number

Parameters:

  • needle Mixed

    the item to search for

Returns:

Number:

first index of the needle, or -1 if not found

insertAfter

(
  • item
  • value
)
LinkedListItem

Parameters:

Returns:

LinkedListItem:

inserted item

insertBefore

(
  • value
  • item
)
LinkedListItem

Parameters:

Returns:

LinkedListItem:

inserted item

isEmpty

() Boolean

Returns:

Boolean:

true if the list is empty

lastIndexOf

(
  • needle
)
Number

Parameters:

  • needle Mixed

    the item to search for

Returns:

Number:

last index of the needle, or -1 if not found

map

(
  • f
  • c
)
Object

Executes the supplied function on each item in the list and returns a new list with the results. The function receives the value, the index, and the object itself as parameters (in that order).

Parameters:

  • f String

    the function to invoke

  • c Object

    optional context object

Returns:

Object:

list of all return values

newInstance

() LinkedList

Creates a new, empty LinkedList.

Returns:

partition

(
  • f
  • c
)
Object

Partitions an list into two new list, one with the items for which the supplied function returns true, and one with the items for which the function returns false. The function receives the value, the index, and the object itself as parameters (in that order).

Parameters:

  • f Function

    the function to execute on each item

  • c Object

    optional context object

Returns:

Object:

object with two properties: matches and rejects. Each is a list containing the items that were selected or rejected by the test function (or an empty object if none).

prepend

(
  • value
)
LinkedListItem

Parameters:

  • value Mixed

    value to prepend

Returns:

LinkedListItem:

prepended item

reduce

(
  • init
  • f
  • c
)
Mixed

Executes the supplied function on each item in the list, folding the list into a single value. The function receives the value returned by the previous iteration (or the initial value if this is the first iteration), the value being iterated, the index, and the list itself as parameters (in that order). The function must return the updated value.

Parameters:

  • init Mixed

    the initial value

  • f String

    the function to invoke

  • c Object

    optional context object

Returns:

Mixed:

final result from iteratively applying the given function to each item in the list

reject

(
  • f
  • c
)
Object

Executes the supplied function on each item in the list. Returns a new list containing the items for which the supplied function returned a falsey value. The function receives the value, the index, and the object itself as parameters (in that order).

Parameters:

  • f Function

    the function to execute on each item

  • c Object

    optional context object

Returns:

Object:

array or object of items for which the supplied function returned a falsey value (empty if it never returned a falsey value)

remove

()

Remove the item from the list.

reverse

()

Reverses the items in place.

size

() Number

Warning: This requires traversing the list! Use isEmpty() whenever possible.

Returns:

Number:

the number of items in the list

some

(
  • f
  • c
)
Boolean

Executes the supplied function on each item in the list. Iteration stops if the supplied function returns a truthy value. The function receives the value, the index, and the list itself as parameters (in that order).

Parameters:

  • f Function

    the function to execute on each item

  • c Object

    optional context object

Returns:

Boolean:

true if the function returns a truthy value on any of the items in the array, false otherwise

tail

() LinkedListItem

Returns:

LinkedListItem:

the last item in the list, or null if the list is empty

toArray

() Array

Returns:

Array: