Skip to content

Commit

Permalink
corrected homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
WitoldFracek committed Nov 13, 2023
1 parent ec6fce2 commit 892fc07
Show file tree
Hide file tree
Showing 7 changed files with 487 additions and 254 deletions.
119 changes: 107 additions & 12 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,112 @@
# Welcome to MkDocs
# Qwery List
Qwery List is a small library introducing a new way to use higher order functions
with lists, with **lazy evaluation**.

For full documentation visit [mkdocs.org](https://www.mkdocs.org).
### Quick comparison
#### The standard python way
```python
xs = ['1', '2', '3', '4']
s = reduce(lambda acc, x: acc + x, filter(lambda x: x < 3, map(int, xs)), 0)
```
#### Qwery List way
```python
xs = QList(['1', '2', '3', '4'])
s = xs.map(int).filter(lambda x: x < 3).fold(lambda acc, x: acc + x, 0)
```
Chaining methods makes code much more readable and follows the natural flow
of reading left to right.
As a bonus you get `len()` **method**, so no longer will you be forced to wrapp your
lists in this type of code `len(xs)` and simply call `xs.len()` (I understand it is negligibly
slower but look how much nicer it looks!)

## Commands
---

* `mkdocs new [dir-name]` - Create a new project.
* `mkdocs serve` - Start the live-reloading docs server.
* `mkdocs build` - Build the documentation site.
* `mkdocs -h` - Print help message and exit.
# Installation
This package is available on [PyPI](https://pypi.org/project/qwlist/)
```
pip install qwlist
```

---

# Quick tutorial
Let's say we want to read numbers from a file and choose only the even ones. No problem at all!
```python
from qwlist import QList

with open('path/to/file.txt', 'r') as file:
qlist = QList(file.readlines())
even = qlist.map(int).filter(lambda x: x % 2 == 0).collect()
```
Why is there this `collect` at the end? Because all operations on the QList are **lazy evaluated**,
so in order to finally apply all the operations you need to express that.

There is also an eagerly evaluated `EagerQList` in case all the actions performed on the list should
be evaluated instantaneously. This object is in the `qwlist.eager` module, but it is also
possible to transform `QList` into `EagerQList` simply by calling `eager()`
```python
>>> from qwlist import QList
>>> QList(range(3)).eager().map(str)
['0', '1', '2']
```
EagerQList has the same methods that QList has (`filter`, `map`, `foreach`, ...) but not lazy evaluated so
there is no need to call `collect` at the end.

---
# Examples
Making QList from an iterable
```python
>>> QList([1, 2, 3, 4])
[1, 2, 3, 4]
```
Making QList from a generator
```python
>>> QList(range(3))
[0, 1, 2]
```
Making a list of pairs: `int` and `str`
```python
>>> qlist = QList([1, 2, 3])
>>> qlist.zip(qlist.map(str)).collect()
[(1, '1'), (2, '2'), (3, '3')]
```
Summing only the even numbers
```python
>>> QList(range(10)).filter(lambda x: x % 2 == 0).fold(lambda acc, x: acc + x, 0)
20
```
---

## Side note
This syntax resembles **Rust** syntax:

<table>
<tr>
<th>Rust</th>
<th>Python</th>
</tr>
<tr>
<td>

```rust
let xs = vec![1, 2, 3, 4];
let double_xs: Vec<i32> = xs.iter().map(|&x| x * 2).collect();
println!("{double_xs:?}");
// [2, 4, 6, 8]
```

</td>
<td>

```python
xs = QList([1, 2, 3, 4])
double_xs = xs.map(lambda x: x * 2).collect()
print(double_xs)
# [2, 4, 6, 8]
```

</td>
</tr>
</table>

## Project layout

mkdocs.yml # The configuration file.
docs/
index.md # The documentation homepage.
... # Other markdown pages, images and other files.
4 changes: 4 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ theme:
toggle:
icon: material/weather-night
name: Switch to dark mode
icon:
repo: fontawesome/brands/github
repo_url: https://github.com/WitoldFracek/qlist/tree/main


nav:
- Home: index.md
Expand Down
159 changes: 132 additions & 27 deletions site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<div data-md-component="skip">


<a href="#welcome-to-mkdocs" class="md-skip">
<a href="#qwery-list" class="md-skip">
Skip to content
</a>

Expand Down Expand Up @@ -235,17 +235,30 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>

<li class="md-nav__item">
<a href="#commands" class="md-nav__link">
Commands
<a href="#quick-comparison" class="md-nav__link">
Quick comparison
</a>

<nav class="md-nav" aria-label="Quick comparison">
<ul class="md-nav__list">

<li class="md-nav__item">
<a href="#the-standard-python-way" class="md-nav__link">
The standard python way
</a>

</li>

<li class="md-nav__item">
<a href="#project-layout" class="md-nav__link">
Project layout
<li class="md-nav__item">
<a href="#qwery-list-way" class="md-nav__link">
Qwery List way
</a>

</li>

</ul>
</nav>

</li>

</ul>
Expand Down Expand Up @@ -386,17 +399,30 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>

<li class="md-nav__item">
<a href="#commands" class="md-nav__link">
Commands
<a href="#quick-comparison" class="md-nav__link">
Quick comparison
</a>

<nav class="md-nav" aria-label="Quick comparison">
<ul class="md-nav__list">

<li class="md-nav__item">
<a href="#the-standard-python-way" class="md-nav__link">
The standard python way
</a>

</li>

<li class="md-nav__item">
<a href="#project-layout" class="md-nav__link">
Project layout
<li class="md-nav__item">
<a href="#qwery-list-way" class="md-nav__link">
Qwery List way
</a>

</li>

</ul>
</nav>

</li>

</ul>
Expand All @@ -414,21 +440,100 @@



<h1 id="welcome-to-mkdocs">Welcome to MkDocs</h1>
<p>For full documentation visit <a href="https://www.mkdocs.org">mkdocs.org</a>.</p>
<h2 id="commands">Commands</h2>
<ul>
<li><code>mkdocs new [dir-name]</code> - Create a new project.</li>
<li><code>mkdocs serve</code> - Start the live-reloading docs server.</li>
<li><code>mkdocs build</code> - Build the documentation site.</li>
<li><code>mkdocs -h</code> - Print help message and exit.</li>
</ul>
<h2 id="project-layout">Project layout</h2>
<pre><code>mkdocs.yml # The configuration file.
docs/
index.md # The documentation homepage.
... # Other markdown pages, images and other files.
<h1 id="qwery-list">Qwery List</h1>
<p>Qwery List is a small library introducing a new way to use higher order functions
with lists, with <strong>lazy evaluation</strong>. </p>
<h3 id="quick-comparison">Quick comparison</h3>
<h4 id="the-standard-python-way">The standard python way</h4>
<pre><code class="language-python">xs = ['1', '2', '3', '4']
s = reduce(lambda acc, x: acc + x, filter(lambda x: x &lt; 3, map(int, xs)), 0)
</code></pre>
<h4 id="qwery-list-way">Qwery List way</h4>
<pre><code class="language-python">xs = QList(['1', '2', '3', '4'])
s = xs.map(int).filter(lambda x: x &lt; 3).fold(lambda acc, x: acc + x, 0)
</code></pre>
<p>Chaining methods makes code much more readable and follows the natural flow
of reading left to right.
As a bonus you get <code>len()</code> <strong>method</strong>, so no longer will you be forced to wrapp your
lists in this type of code <code>len(xs)</code> and simply call <code>xs.len()</code> (I understand it is negligibly
slower but look how much nicer it looks!)</p>
<hr />
<h1 id="installation">Installation</h1>
<p>This package is available on <a href="https://pypi.org/project/qwlist/">PyPI</a></p>
<pre><code>pip install qwlist
</code></pre>
<hr />
<h1 id="quick-tutorial">Quick tutorial</h1>
<p>Let's say we want to read numbers from a file and choose only the even ones. No problem at all!</p>
<pre><code class="language-python">from qwlist import QList

with open('path/to/file.txt', 'r') as file:
qlist = QList(file.readlines())
even = qlist.map(int).filter(lambda x: x % 2 == 0).collect()
</code></pre>
<p>Why is there this <code>collect</code> at the end? Because all operations on the QList are <strong>lazy evaluated</strong>,
so in order to finally apply all the operations you need to express that.</p>
<p>There is also an eagerly evaluated <code>EagerQList</code> in case all the actions performed on the list should
be evaluated instantaneously. This object is in the <code>qwlist.eager</code> module, but it is also
possible to transform <code>QList</code> into <code>EagerQList</code> simply by calling <code>eager()</code></p>
<pre><code class="language-python">&gt;&gt;&gt; from qwlist import QList
&gt;&gt;&gt; QList(range(3)).eager().map(str)
['0', '1', '2']
</code></pre>
<p>EagerQList has the same methods that QList has (<code>filter</code>, <code>map</code>, <code>foreach</code>, ...) but not lazy evaluated so
there is no need to call <code>collect</code> at the end.</p>
<hr />
<h1 id="examples">Examples</h1>
<p>Making QList from an iterable</p>
<pre><code class="language-python">&gt;&gt;&gt; QList([1, 2, 3, 4])
[1, 2, 3, 4]
</code></pre>
<p>Making QList from a generator</p>
<pre><code class="language-python">&gt;&gt;&gt; QList(range(3))
[0, 1, 2]
</code></pre>
<p>Making a list of pairs: <code>int</code> and <code>str</code></p>
<pre><code class="language-python">&gt;&gt;&gt; qlist = QList([1, 2, 3])
&gt;&gt;&gt; qlist.zip(qlist.map(str)).collect()
[(1, '1'), (2, '2'), (3, '3')]
</code></pre>
<p>Summing only the even numbers</p>
<pre><code class="language-python">&gt;&gt;&gt; QList(range(10)).filter(lambda x: x % 2 == 0).fold(lambda acc, x: acc + x, 0)
20
</code></pre>
<hr />
<h2 id="side-note">Side note</h2>
<p>This syntax resembles <strong>Rust</strong> syntax:</p>
<table>
<tr>
<th>Rust</th>
<th>Python</th>
</tr>
<tr>
<td>


<pre><code class="language-rust">let xs = vec![1, 2, 3, 4];
let double_xs: Vec&lt;i32&gt; = xs.iter().map(|&amp;x| x * 2).collect();
println!(&quot;{double_xs:?}&quot;);
// [2, 4, 6, 8]
</code></pre>


</td>
<td>


<pre><code class="language-python">xs = QList([1, 2, 3, 4])
double_xs = xs.map(lambda x: x * 2).collect()
print(double_xs)
# [2, 4, 6, 8]
</code></pre>


</td>
</tr>
</table>



Expand Down
Loading

0 comments on commit 892fc07

Please sign in to comment.