dbrattli/OSlash

List Monad/Functor takes no arguments.

Opened this issue · 2 comments

nlhnt commented

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 commented

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
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)