Skip to content

Commit

Permalink
Fixes for list monad (#25)
Browse files Browse the repository at this point in the history
* Fixes for list monad

* Do not try to instantiate List directly. Use empty instead.
  • Loading branch information
dbrattli authored Oct 8, 2020
1 parent f7283d9 commit 77bd523
Show file tree
Hide file tree
Showing 14 changed files with 362 additions and 352 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[flake8]
ignore = E731 # Do not assign a lambda expression, use a def
max-line-length = 120
32 changes: 15 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

![Python package](https://github.com/dbrattli/OSlash/workflows/Python%20package/badge.svg)

OSlash (Ø) is a library for playing with functional programming in
Python 3.8+. It's an attempt to re-implement some of the code from [Learn You
a Haskell for Great Good!](http://learnyouahaskell.com/) in Python 3.8.
OSlash unifies functional and object oriented paradigms by grouping related
functions within classes. Objects are however never used for storing
values or mutable data, and data only lives within function closures.
OSlash (Ø) is a library for playing with functional programming in Python 3.8+. It's an attempt to re-implement some of
the code from [Learn You a Haskell for Great Good!](http://learnyouahaskell.com/) in Python 3.8. OSlash unifies
functional and object oriented paradigms by grouping related functions within classes. Objects are however never used
for storing values or mutable data, and data only lives within function closures.

OSlash is intended to be a tutorial. For practical functional programming in Python in production environments you
should use [FSlash](https://github.com/dbrattli/fslash) instead.

## Install

Expand Down Expand Up @@ -49,17 +50,14 @@ The project currently contains implementations for:

## But why?

Yes, I know there are other projects out there like
[PyMonad](https://bitbucket.org/jason_delaat/pymonad/),
[fn.py](https://github.com/kachayev/fn.py). I'm simply doing this in order to
better understand the [book](http://learnyouahaskell.com/). It's so much easier to learn when you implement
things yourself. The code may be similar to PyMonad in structure, but is
quite different in implementation.

Why is the project called OSlash? OSlash is the Norwegian character called
[Oslash](http://en.wikipedia.org/wiki/Ø). Initially I wanted to create a
project that used Ø and ø (unicode) for the project name and modules. It didn't
work out well, so I renamed it to OSlash.
Yes, I know there are other projects out there like [PyMonad](https://bitbucket.org/jason_delaat/pymonad/),
[fn.py](https://github.com/kachayev/fn.py). I'm simply doing this in order to better understand the
[book](http://learnyouahaskell.com/). It's so much easier to learn when you implement things yourself. The code may be
similar to PyMonad in structure, but is quite different in implementation.

Why is the project called OSlash? OSlash is the Norwegian character called [Oslash](http://en.wikipedia.org/wiki/Ø).
Initially I wanted to create a project that used Ø and ø (unicode) for the project name and modules. It didn't work out
well, so I renamed it to OSlash.

## Examples

Expand Down
5 changes: 3 additions & 2 deletions oslash/cont.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

from typing import Any, Callable, Generic, TypeVar
from typing import Callable, Generic, TypeVar


from .util import identity, compose
Expand Down Expand Up @@ -83,5 +83,6 @@ def __call__(self, comp: Callable[[T], TResult]) -> TResult:
def __eq__(self, other) -> bool:
return self(identity) == other(identity)


assert isinstance(Cont, Functor)
assert isinstance(Cont, Monad)
assert isinstance(Cont, Monad)
4 changes: 2 additions & 2 deletions oslash/do.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def guard(M, test):
What it does::
return M.pure(Unit) if test else M()
return M.pure(Unit) if test else M.empty()
https://en.wikibooks.org/wiki/Haskell/Alternative_and_MonadPlus#guard
"""
return M.pure(Unit) if test else M()
return M.pure(Unit) if test else M.empty()


# The kwargs syntax forces name to be a valid Python identifier.
Expand Down
2 changes: 1 addition & 1 deletion oslash/either.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from abc import ABCMeta, abstractmethod
from abc import abstractmethod
from functools import partial

from typing import Callable, TypeVar, Generic
Expand Down
2 changes: 1 addition & 1 deletion oslash/identity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from functools import partial # type: ignore
from functools import partial
from typing import TypeVar, Generic, Callable

from .typing import Functor, Monad, Applicative
Expand Down
1 change: 0 additions & 1 deletion oslash/ioaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ def __call__(self, world: int = 0) -> TSource:
return self.run(world)

def __str__(self, m: int = 0, n: int = 0) -> str:
assert self._value is not None
s, io = self._value
a = io.__str__(m + 1, n)
return '%sPut ("%s",\n%s\n%s)' % (ind(m), s, a, ind(m))
Expand Down
Loading

0 comments on commit 77bd523

Please sign in to comment.