Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List Monad/Functor takes no arguments. #34

Open
nlhnt opened this issue Nov 7, 2023 · 2 comments
Open

List Monad/Functor takes no arguments. #34

nlhnt opened this issue Nov 7, 2023 · 2 comments

Comments

@nlhnt
Copy link

nlhnt commented Nov 7, 2023

Hello, I am going through a book by Martin McBride about functional programming in Python.
I know this is not production-class library, but I am trying to instantiate a List functor (from oslash.list) as below and I end up raising an Exception...

from oslash import List as ListFunctor
a = ListFunctor([1,2,3])

Ends up with:

---------------------------------------------------------------------------  
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 ListFunctor([1,2,3])

TypeError: List() takes no arguments

I looked into the code of the library and it seems that we're supposed to use the class method/factory from_iterable right now? Is this true?
While I understand this is not a repository about this book, but it also mentioned possibility of mapping functions using % operator on Functors, which seemed to be working for Just and Nothing Functors, but does not work for this List Functor. I have to use List.from_iterable([1,2,3]).map(operator.neg), the "old way" (if ever supported) neg % List.from_iterable(1,2,3) ends up raising an Exception

from oslash import List as ListFunctor


a = ListFunctor.from_iterable([1,2,3])

neg % a
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[9], line 1
----> 1 neg % a

TypeError: unsupported operand type(s) for %: 'builtin_function_or_method' and 'Cons'
@nlhnt
Copy link
Author

nlhnt commented Nov 7, 2023

According to the wiki the following should work, but it only works when I use apply.

Just(lambda x: x+3) * Just(2) == Just(5)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 Just(lambda x: x+3) * Just(2) == Just(5)

TypeError: unsupported operand type(s) for *: 'Just' and 'Just'

Just(lambda x: x+3).apply(Just(2))
Out[13]: Just 5

@SHJackie
Copy link

SHJackie commented Feb 1, 2024

from operator import neg
from oslash import List, Just

List.__rmod__ = lambda self, fn: self.map(fn)
Just.__mul__ = lambda self, other: self.apply(other)

a = List.from_iterable([1, 2, 3])
b = List.from_iterable([-1, -2, -3])

assert (neg % a) == b

assert Just(lambda x: x+3) * Just(2) == Just(5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants