Code Monkey home page Code Monkey logo

Comments (6)

suned avatar suned commented on July 25, 2024

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.

suned avatar suned commented on July 25, 2024

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.

suned avatar suned commented on July 25, 2024

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.

galen104 avatar galen104 commented on July 25, 2024

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.

galen104 avatar galen104 commented on July 25, 2024

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.

suned avatar suned commented on July 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.