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

RequiresContext container implementation #226

Merged
merged 14 commits into from
Jan 22, 2020
Merged

RequiresContext container implementation #226

merged 14 commits into from
Jan 22, 2020

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Jan 20, 2020

I would be thrilled to get your feedback, guys!

P.S. For some reason I cannot request a review from @pvldruzhinin and @theLastOfCats, but I do request it!

Closes #225
Closes #216
Closes #230
Closes #231

@theLastOfCats
Copy link

LGTM

@paveldroo
Copy link
Contributor

All looks good!

@sobolevn
Copy link
Member Author

sobolevn commented Jan 20, 2020

TODO for later:

  • support join
  • add readme example
  • add docs to webpage
  • add API spec to webpage
  • add typetests for context

@sobolevn
Copy link
Member Author

I would also love to experiment with Reader that can hold both functions and values.

@sobolevn
Copy link
Member Author

Didn't work. Python type system does not allow this.

@paveldroo
Copy link
Contributor

paveldroo commented Jan 21, 2020

Didn't work. Python type system does not allow this.

Maybe you need smth like this?

def curry(aFunction):
"""
Turns a normal python function into a curried function.

Most easily used as a decorator when defining functions:
	@curry
	def add(x, y): return x + y

"""
funcName = aFunction.__code__.co_name
numArgs = aFunction.__code__.co_argcount
def buildReader(argValues, numArgs):
	if (numArgs == 0): return aFunction(*argValues)
	else:
		return lambda x: buildReader(argValues + [x], numArgs - 1)
return Reader(buildReader([], aFunction.__code__.co_argcount))

See here: https://bitbucket.org/jason_delaat/pymonad/src/master/pymonad/Reader.py

@sobolevn
Copy link
Member Author

sobolevn commented Jan 21, 2020

@pvldruzhinin sorry, I didn't get your advice. How should this help me with type inference of python objects and callables?

@paveldroo
Copy link
Contributor

@pvldruzhinin sorry, I didn't get your advice. How should this help me with type inference of python objects and callables?

Yeah, I have no full understanding of the problem. Just tried to find difference between our code and PyMonad implementation.
But try to look at all Reader implementation in PyMonad (edited link in previous answer), it seems they support functions and values.

@sobolevn
Copy link
Member Author

Because they are untyped. Let me show where the problem is.

Current implementation:

class RequiresContext(
    BaseContainer,
    Generic[_EnvType, _ReturnType],
):

    def __init__(
        self, inner_value: Callable[[_EnvType], _ReturnType],
    ) -> None: ...

With this annotation mypy is able to infer correct types of both environment and return type.

When we change the constructor method type to def __init__(self, inner_value: _ReturnType), then we won't have any _EnvType to work with. And it will be set to <nothing>

And this is not what we want.

@paveldroo
Copy link
Contributor

Ah, i see, thanks for the explanation 👌

@sobolevn sobolevn changed the title Quick and dirty Reader monad implementation, no docs yet RequiresContext container implementation Jan 22, 2020
@sobolevn sobolevn merged commit 43b979d into master Jan 22, 2020
@sobolevn sobolevn deleted the issue-216 branch January 22, 2020 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Should we make IO lazy? Use doctests where possible Consider adding Reader monad
4 participants