Comments (6)
I often find that eliminating "plumbing" parameters with Reader
is most effective when you ask for the reader value close to where you need it. In your example I would probably do something like
from pfun.reader import map_m, Reader, ask
def main():
connection = Connection(URI='testuri')
result = process_all().run(connection)
print(result)
def process_all() -> Reader[Connection, str]:
return map_m(process, range(5)).map(lambda _: 'result')
def process(item: int) -> Reader[Connection, None]:
return ask().map(lambda c: print(item, c))
map_m
is sort of like the built-in map
except it expects a function that returns a Reader
and will combine the results of mapping process
over range(5)
using and_then
.
Reader.map
applies its function argument to the "wrapped" Reader
value, which in the case of the reader returned by ask
happens to be the Connection
instance.
from pfun.
I apologize that I don't talk about map
in the guide on readthedocs :)
I think the documentation still has a way to go before its super beginner friendly.
from pfun.
Maye sequence
is a little more pythonic in this case:
from pfun.reader import sequence, Reader, ask
def process_all() -> Reader[Connection, str]:
return sequence(process(i) for i in range(5)).map(lambda _: 'result')
from pfun.
Thanks, so if I want to do some stuff with a result coming from process
I can do something like:
def process_all() -> Reader[Connection, List[int]]:
def process_results(r):
return list(map(lambda x: x*2, r))
return map_m(process, range(5)).map(process_results)
def process(item: int) -> Reader[Connection, int]:
def _(c):
# do stuff
return item
return ask().map(_)
from pfun.
Another question :), if I want to use the Writer Monad together with Reader Monad:
def process(item: int) -> Writer[Reader[Connection, int], List[str]]:
mylog = []
def _(c):
# do stuff
mylog.append('test log')
return item*2
return Writer(ask().map(_), mylog)
connection = Connection(URI='testuri')
result, log = process(4).map(lambda x: x.run(connection))
print(log)
print(result)
there is a better way to do it? It seems a little ugly to me, and it's a very simple example.
from pfun.
Thanks, so if I want to do some stuff with a result coming from process I can do something like
Exactly!
Another question :), if I want to use the Writer Monad together with Reader Monad
This is normally referred to as effect composition, and no, its not very nice. In other frameworks you'll find special constructs for dealing with this (monad transformers e.g). For pfun I'm working on another approach which will be released beginning of 2020
from pfun.
Related Issues (20)
- ERROR: Could not build wheels for pfun, which is required to install pyproject.toml-based projects HOT 2
- Release 1.0.0 HOT 2
- Satisfy Effect dependencies without performing side effect
- Allow global thread and process pool executors
- aiohttp.ClientSession read_timeout is deprecated HOT 3
- Intersection can't unify type variables
- Effect.map can't handle generators
- TypeError when using Effect.map with async function
- pfun.files.read_bytes opens files with 'b', fails
- tests take far too long to run HOT 4
- Improve documentation
- Distribute hypothesis test strategies
- release gh actions workflow can't publish docs
- Add curried overload to Curry.__call__ type
- Allow to write Curry type without Callable
- Support decoratoring methods with curry in the MyPy plugin
- Support generic types in lens
- SemLock is not implemented when running on AWS Lambda HOT 7
- Cannot infer type argument 1 of Nothing.and_then + other type issues with Maybe HOT 1
- mypy error using effect.combine HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pfun.