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

suppose I want to compose two action-do-thingies #60

Open
aryairani opened this issue Feb 18, 2015 · 2 comments
Open

suppose I want to compose two action-do-thingies #60

aryairani opened this issue Feb 18, 2015 · 2 comments

Comments

@aryairani
Copy link

In Play framework (and probably others?) it's common to have a higher-order action that does some processing, and conditionally delegates to another action.

pseudo-code examples:

// reads some parameters from the request and call a delegate
//   with derived parameters or with derived + original parameters
def secure(d: Request => Response): Request => Response
def withUser(d: User => Response): Request => Response
def withUser2(d: User => Request => Response): Request => Response
def withUserOrElse(d: User => Request => Response, orElse: => Response): Request => Response

which check some properties on the Request and either continue with f or return some error.
(Related to #51)

// extracts two parameters from the request and passes them to a delegate, 
//   which may use other parameters from the request
def paged[A:EncodeJson](d: Request => (Offset,MaxResults) => A): Request => Response =
  req => d(req)(req.queryParams[Offset]("paging_offset"), 
                req.queryParams[MaxResults]("paging_max"),
               ).asJson

From what I can tell, in rho, the F needs to accept all of the parameters on the HList stack, making it difficult to modularize things like this.

Can I write an F that pushes / pops some arguments from the stack, and pass the remainder to another F? Or how can I get some kind of modularity here?

@aryairani
Copy link
Author

I guess the Swagger doc generator isn't going to care whether I'm implementing my F in a modular way, and that's fine. But if I still want to?

@bryce-anderson
Copy link
Member

Ultimately you can only get nice function composition with Function1. Besides getting path related values, there is no reason you can't just use a Request => Response and extract the query and header params the neutral way as is done in http4s proper. Of course, whos not going to want path variables?

There is no way to pop parameters off the stack, but it should be doable if we want to put the work into it. That is basically how parboiled2 works. However, I personally don't see this as a high priority for me in the near term.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants