mirror of
https://github.com/correl/typesafe-monads.git
synced 2025-04-02 17:00:25 -09:00
fixed set
This commit is contained in:
parent
c61baf51c1
commit
96efb0c99e
1 changed files with 11 additions and 3 deletions
|
@ -3,6 +3,7 @@ from functools import reduce
|
|||
from itertools import chain
|
||||
from monads import functor, List
|
||||
from typing import (
|
||||
Any,
|
||||
Callable,
|
||||
Iterable,
|
||||
Iterator,
|
||||
|
@ -25,9 +26,16 @@ S = TypeVar("S")
|
|||
class Set(Monad[T], Monoidal[set]):
|
||||
@classmethod
|
||||
def pure(cls, value: T) -> Set[T]:
|
||||
t = set()
|
||||
t.add(value)
|
||||
return Set(t)
|
||||
def unpack(k: T) -> set:
|
||||
s: set = set()
|
||||
if isinstance(k, Iterable):
|
||||
for v in k:
|
||||
s.union(unpack(v))
|
||||
else:
|
||||
s.add(k)
|
||||
return s
|
||||
|
||||
return Set(unpack(value))
|
||||
|
||||
def bind(self, function: Callable[[T], Set[S]]) -> Set[S]:
|
||||
return reduce(Set.mappend, map(function, self.value), Set.mzero())
|
||||
|
|
Loading…
Add table
Reference in a new issue