Magic List is a module that extends the built-in list type.
Project description
Magic List
Magic List is a module that extends the built-in list type.
Documentation · PyPI package · How to install
[!NOTE] Its development is entirely test-driven: it is battery-tested and requires a test coverage of 100%. It also provides typing stub files.
Installation
Pip
pip install magic-list
Package managers
Conda
conda install magic-list
Pipenv
pipenv install magic-list
pipx
pipx install magic-list
Poetry
poetry add magic-list
uv
uv pip install magic-list
Examples
Fibonacci sequence
In the functional programming spirit, let's write a function that given an integer n
, returns the fibonacci sequence up to the n
-th member.
For example, fibonacci_sequence(10)
would return [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
.
import operator
from magic_list import L, list
def fibonacci_sequence(n: int) -> list[int]:
# let's start by creating a list with the first two members, 0 and 1.
base = L[0, 1]
# we define a function that we will use to generate the next members of
# the sequence
def next_member(current: list[int]) -> int:
return current.take_right(2).sum()
return base.fill_right(next_member, n - 1)
[!NOTE]
TheL[0, 1]
notation is a way to construct magic lists nicely.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
magic_list-2.2.1.tar.gz
(12.3 kB
view hashes)
Built Distribution
magic_list-2.2.1-py3-none-any.whl
(12.2 kB
view hashes)
Close
Hashes for magic_list-2.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ca569a25191ed36913b459037473f4f95bf1a20f9a847cd2080403c4057670f |
|
MD5 | f52b35b30ab58be52d4f6c7da3bfa04b |
|
BLAKE2b-256 | 56ac3f77b42ba20f9389aed5b767b385214914421f0c97cf6294d0d3a3ffd34b |