de

class pythonic_fp.queues.de.DEQueue

Bases: Generic

DEQueue

Stateful Double-Ended (DE) Queue data structure.

  • has a left and a right side

  • O(1) pops each end

  • O(1) amortized pushes each end

  • O(1) length determination

  • in a Boolean context, truthy if not empty, falsy if empty

  • will automatically increase storage capacity when needed

  • neither indexable nor sliceable by design

__init__(*ds: Iterable) None

Initializer

Initialize DEQueue with 0 or 1 iterables to populate the queue left (front) to right (rear).

Parameters:

ds – Takes 0 or 1 iterable parameters.

Raises:
  • ValueError – When more than one parameter is provided.

  • TypeError – When passed a non-iterable parameter.

__bool__() bool

Truthiness

Queue truthy when non-empty, falsy when empty.

__len__() int

Get length

Return the number of data elements in the DEQueue.

__eq__(other: object) bool

Equality comparison

If other is a DEQueue and the corresponding elements of self and other compare as equal, then return True. Otherwise return False.

Returns:

self == other

__iter__() Iterator

Iteration

Iterate over current state left to right.

Returns:

An iterator of the data.

__reversed__() Iterator

Iteration

Iterate over current state right to left.

Returns:

An iterator of the data.

__repr__() str

String representation

Construct string ‘DEQueue(d₁, d₂, … dₙ)’ where

  • d₁, d₂, … dₙ are the contents displayed with repr()

Returns:

A string to reproduce the DEQueue.

__str__() str

User string

Construct string ‘>< d₁ | d₂ | … | dₙ ><’ where

  • d₁, d₂, …, dₙ are the contents displayed with str()

Returns:

A string meaningful to an end user.

copy() DEQueue[D]

Copy

Shallow copy the DEQueue.

Returns:

New DEQueue instance containing the same references.

pushl(*ds: D) None

Push left

Push data onto left side of DEQueue.

Parameters:

ds – Data to be pushed onto DEQueue from the left.

pushr(*ds: D) None

Push right

Push data onto right side of DEQueue.

Parameters:

ds – Data to be pushed onto DEQueue from the right.

popl() MayBe

Pop left

Pop next item from left side DEQueue, if it exists.

Returns:

MayBe of popped item if queue was not empty, empty MayBe otherwise.

popr() MayBe

Pop right

Pop next item off right side ‘’DEQueue``, if it exists.

Returns:

MayBe of popped item if queue was not empty, empty MayBe otherwise.

peakl() MayBe

Peak left

Peak left side of DEQueue. Does not consume item.

Returns:

MayBe of leftmost item if queue not empty, empty MayBe otherwise.

peakr() MayBe

Peak right

Peak right side of DEQueue. Does not consume item.

Returns:

MayBe of rightmost item if queue not empty, empty MayBe otherwise.

foldl(f: Callable[[D, D], D]) MayBe[D]
foldl(f: Callable[[L, D], L], start: L) MayBe[L]

Fold left

Reduce DEQueue left to right.

Parameters:
  • f – Reducing function, first argument is for accumulator.

  • start – Optional starting value.

Returns:

MayBe of reduced value with f, empty MayBe if queue empty and no starting value given.

foldr(f: Callable[[D, D], D]) MayBe[D]
foldr(f: Callable[[D, R], R], start: R) MayBe[R]

Fold right

Reduce DEQueue right to left.

Parameters:
  • f – Reducing function, second argument is for accumulator.

  • start – Optional starting value.

Returns:

MayBe of reduced value with f, empty MayBe if queue empty and no starting value given.

map(f: Callable[[D], U]) DEQueue

Map

Map left to right.

Tip

Order map done does not matter if f is pure.

Parameters:

f – Function to map over queue.

Returns:

New DEQueue instance, retain original order.

pythonic_fp.queues.de.de_queue(*ds: D) DEQueue

Create DEQueue

Factory function to create a DEQueue instance from the function’s arguments.

Parameters:

ds – Initial data to be pushed on right to left.

Returns:

A new DEQueue instance.