How about the for comprehension?
for {xml <- req.xml
fooTag <- (xml \ "foo").firstOption
response <- myFunction(fooTag)
} yield response
On Mon, May 4, 2009 at 8:51 AM, Channing Walton <[email protected]>wrote:
>
> Hi,
> I am building a web app which has a rest API. I found that I was
> repeating myself like this:
>
> val reply:Full[XmlResponse] = req.xml match {
> case Full(content) => {
> a block of code that does something with the content
> <ok/>
> }
> case _ => <fail/>
> }
>
> So I first though I would create a function to help a little:
>
> private def handle[T](b:Box[T])(block:T => Unit):Full[XmlResponse] =
> b match {
> case Full(x) => {
> block(x)
> <ok/>
> }
> case _ => <fail/>
> }
>
> which reduces the example to:
>
> val reply:Full[XmlResponse] = handle(req.xml) { content:Elem =>
> do something with content
> }
>
> ok so far so good (I think? Is It?)
>
> But, being REST, the url is pointing at a resource which gets loaded
> as a Box, so the above will not work so nicely. We are back to:
>
> val reply:Full[XmlResponse] = req.xml match {
> case Full(content) => {
> MappedThing.find( By content attributes ) match {
> case Full(obj) => {
> doSomething
> <ok/>
> }
> case _ => <fail/>
> }
> }
> case _ => <fail/>
> }
>
> nasty.
>
> So I was now thinking that I need a crazy function whose signature is
> something like this:
> def crazy[X, T](a:Box[X])(block1:X => Box[T])(block2:T => Unit):Full
> [XmlResponse]
>
> and used something like this:
> crazy(req.xml) (content:Elem => MappedThing.find...) {obj:MappedThing
> =>
> ...
> }
>
> Does any of this makes any sense? Is this just wrong?
>
> Also, as an exercise, I wondered if there was a type-safe way to
> generalise this concept so that a chain of functions could be supplied
> which took an initial Box (or Option) and applied each function to the
> result of the previous function to obtain a final result?
>
> >
>
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---