[Lift] Re: A stupid question
Cans inside Cans says Use a for comprehension to me: for (r - S.request; loc - r.location; link - loc.createDefaultLink) yield link.text It's a few more characters, but IMHO, more readable. On Wed, Oct 15, 2008 at 7:14 PM, Charles F. Munat [EMAIL PROTECTED] wrote: Yes, this does help a bit. I actually understand the Can (and Option) very well, and I think it's a great idea (though the added advantage of the Can doesn't seem to be heavily used yet). The problem I was having was dealing with cans inside cans inside cans. But after reading everything everyone sent, I think I've got it now: val p = S.request.flatMap(_.location).flatMap(_.createDefaultLink).map(_.text) a href={p.openOr()}This page/a Or this: val p = for (req - S.request; loc - req.location; txt - loc.createDefaultLink) yield txt a href={p.openOr().toString}This page/a Of course, now that I know, I'll just use S.uri. Thanks again for all the help! Chas. David Pollak wrote: Charles, A Can is a container... it can contain a thing or be empty. You can transform the contents of a Can from one thing to another using map(). map() on Can, Option, List is exactly the same as map() on Array in Ruby: irb(main):004:0 [1,2,3].map{|v| v.to_s + Cats} = [1 Cats, 2 Cats, 3 Cats] This is just like in Scala: scala List(1,2,3).map(v = v.toString + Cats) res0: List[java.lang.String] = List(1 Cats, 2 Cats, 3 Cats) In Ruby, when you access the first element of an Array that has no elements, you get 'nil' back. In Scala, you get an exception. This allows you to tell the difference between [nil][0] and [][0] which are the same in Ruby. The most syntactically pleasing way of extracting things from List, Can, Option in Scala is the for comprehension: scala for (a - Some(3); |b - Some(4)) yield a * b res1: Option[Int] = Some(12) Does that help? Thanks, David Charles F. Munat wrote: Thanks. I have read everything I could find on this but I think I'm just a bit dense about it. Probably, it's just unfamiliarity with the syntax of Scala as a whole and functional programming in general (or maybe I'm just stupid). Hopefully, at some point the light bulb will come on and this will seem easy. I'll read the blog post. Chas. David Pollak wrote: Please also see: http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html Can[T] is just like Option[T] Marius wrote: to get stuff out of a can you can do: 1. Pattern matching having c a Can[String] c match { case Full(value) = //do something with the value case _ = } 2. call open_!(if you're sure your can is not empty) or openOr Br's, Marius On Oct 15, 3:22 am, Charles F. Munat [EMAIL PROTECTED] wrote: I must be very dense, but these cans are kicking my butt (kicking my can?). No matter what I do, I seem to end up with everything back in the can! I just... want... to get... the goodies... OUT! An example: How do I extract the URI of the current page from S.request? I am currently doing something immensely stupid and wrong like this: S.request.toList.head.location.toList.head.createDefaultLink.toList.head.text I *know* this is way wrong, but I'm not clever enough, apparently, to figure out the puzzle, despite reading through the Can code repeatedly. I figure the above works only because what I'm looking for is there, which sort of defeats the purpose of the cans... Can anyone help? This is driving me insane. Chas. -- Lift, the simply functional web framework http://liftweb.net Collaborative Task Management http://much4.us 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 liftweb@googlegroups.com 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 -~--~~~~--~~--~--~---
[Lift] Re: A stupid question
Please also see: http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html Can[T] is just like Option[T] Marius wrote: to get stuff out of a can you can do: 1. Pattern matching having c a Can[String] c match { case Full(value) = //do something with the value case _ = } 2. call open_!(if you're sure your can is not empty) or openOr Br's, Marius On Oct 15, 3:22 am, Charles F. Munat [EMAIL PROTECTED] wrote: I must be very dense, but these cans are kicking my butt (kicking my can?). No matter what I do, I seem to end up with everything back in the can! I just... want... to get... the goodies... OUT! An example: How do I extract the URI of the current page from S.request? I am currently doing something immensely stupid and wrong like this: S.request.toList.head.location.toList.head.createDefaultLink.toList.head.text I *know* this is way wrong, but I'm not clever enough, apparently, to figure out the puzzle, despite reading through the Can code repeatedly. I figure the above works only because what I'm looking for is there, which sort of defeats the purpose of the cans... Can anyone help? This is driving me insane. Chas. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com 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 -~--~~~~--~~--~--~---
[Lift] Re: A stupid question
Thanks. I have read everything I could find on this but I think I'm just a bit dense about it. Probably, it's just unfamiliarity with the syntax of Scala as a whole and functional programming in general (or maybe I'm just stupid). Hopefully, at some point the light bulb will come on and this will seem easy. I'll read the blog post. Chas. David Pollak wrote: Please also see: http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html Can[T] is just like Option[T] Marius wrote: to get stuff out of a can you can do: 1. Pattern matching having c a Can[String] c match { case Full(value) = //do something with the value case _ = } 2. call open_!(if you're sure your can is not empty) or openOr Br's, Marius On Oct 15, 3:22 am, Charles F. Munat [EMAIL PROTECTED] wrote: I must be very dense, but these cans are kicking my butt (kicking my can?). No matter what I do, I seem to end up with everything back in the can! I just... want... to get... the goodies... OUT! An example: How do I extract the URI of the current page from S.request? I am currently doing something immensely stupid and wrong like this: S.request.toList.head.location.toList.head.createDefaultLink.toList.head.text I *know* this is way wrong, but I'm not clever enough, apparently, to figure out the puzzle, despite reading through the Can code repeatedly. I figure the above works only because what I'm looking for is there, which sort of defeats the purpose of the cans... Can anyone help? This is driving me insane. Chas. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com 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 -~--~~~~--~~--~--~---
[Lift] Re: A stupid question
Charles, A Can is a container... it can contain a thing or be empty. You can transform the contents of a Can from one thing to another using map(). map() on Can, Option, List is exactly the same as map() on Array in Ruby: irb(main):004:0 [1,2,3].map{|v| v.to_s + Cats} = [1 Cats, 2 Cats, 3 Cats] This is just like in Scala: scala List(1,2,3).map(v = v.toString + Cats) res0: List[java.lang.String] = List(1 Cats, 2 Cats, 3 Cats) In Ruby, when you access the first element of an Array that has no elements, you get 'nil' back. In Scala, you get an exception. This allows you to tell the difference between [nil][0] and [][0] which are the same in Ruby. The most syntactically pleasing way of extracting things from List, Can, Option in Scala is the for comprehension: scala for (a - Some(3); |b - Some(4)) yield a * b res1: Option[Int] = Some(12) Does that help? Thanks, David Charles F. Munat wrote: Thanks. I have read everything I could find on this but I think I'm just a bit dense about it. Probably, it's just unfamiliarity with the syntax of Scala as a whole and functional programming in general (or maybe I'm just stupid). Hopefully, at some point the light bulb will come on and this will seem easy. I'll read the blog post. Chas. David Pollak wrote: Please also see: http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html Can[T] is just like Option[T] Marius wrote: to get stuff out of a can you can do: 1. Pattern matching having c a Can[String] c match { case Full(value) = //do something with the value case _ = } 2. call open_!(if you're sure your can is not empty) or openOr Br's, Marius On Oct 15, 3:22 am, Charles F. Munat [EMAIL PROTECTED] wrote: I must be very dense, but these cans are kicking my butt (kicking my can?). No matter what I do, I seem to end up with everything back in the can! I just... want... to get... the goodies... OUT! An example: How do I extract the URI of the current page from S.request? I am currently doing something immensely stupid and wrong like this: S.request.toList.head.location.toList.head.createDefaultLink.toList.head.text I *know* this is way wrong, but I'm not clever enough, apparently, to figure out the puzzle, despite reading through the Can code repeatedly. I figure the above works only because what I'm looking for is there, which sort of defeats the purpose of the cans... Can anyone help? This is driving me insane. Chas. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com 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 -~--~~~~--~~--~--~---
[Lift] Re: A stupid question
Also check out CanSpec.scala to get an idea of how Cans can be used. --j On Wed, Oct 15, 2008 at 9:33 AM, David Pollak [EMAIL PROTECTED] wrote: Charles, A Can is a container... it can contain a thing or be empty. You can transform the contents of a Can from one thing to another using map(). map() on Can, Option, List is exactly the same as map() on Array in Ruby: irb(main):004:0 [1,2,3].map{|v| v.to_s + Cats} = [1 Cats, 2 Cats, 3 Cats] This is just like in Scala: scala List(1,2,3).map(v = v.toString + Cats) res0: List[java.lang.String] = List(1 Cats, 2 Cats, 3 Cats) In Ruby, when you access the first element of an Array that has no elements, you get 'nil' back. In Scala, you get an exception. This allows you to tell the difference between [nil][0] and [][0] which are the same in Ruby. The most syntactically pleasing way of extracting things from List, Can, Option in Scala is the for comprehension: scala for (a - Some(3); |b - Some(4)) yield a * b res1: Option[Int] = Some(12) Does that help? Thanks, David Charles F. Munat wrote: Thanks. I have read everything I could find on this but I think I'm just a bit dense about it. Probably, it's just unfamiliarity with the syntax of Scala as a whole and functional programming in general (or maybe I'm just stupid). Hopefully, at some point the light bulb will come on and this will seem easy. I'll read the blog post. Chas. David Pollak wrote: Please also see:http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html Can[T] is just like Option[T] Marius wrote: to get stuff out of a can you can do: 1. Pattern matching having c a Can[String] c match { case Full(value) = //do something with the value case _ = } 2. call open_!(if you're sure your can is not empty) or openOr Br's, Marius On Oct 15, 3:22 am, Charles F. Munat [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I must be very dense, but these cans are kicking my butt (kicking my can?). No matter what I do, I seem to end up with everything back in the can! I just... want... to get... the goodies... OUT! An example: How do I extract the URI of the current page from S.request? I am currently doing something immensely stupid and wrong like this: S.request.toList.head.location.toList.head.createDefaultLink.toList.head.text I *know* this is way wrong, but I'm not clever enough, apparently, to figure out the puzzle, despite reading through the Can code repeatedly. I figure the above works only because what I'm looking for is there, which sort of defeats the purpose of the cans... Can anyone help? This is driving me insane. Chas. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com 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 -~--~~~~--~~--~--~---
[Lift] Re: A stupid question
Charles, to put this in the context of the JPA stuff I'm working on, here's the pattern I would use for, say, viewing an Author when I have a corresponding RequestVar: object passedAuthor extends RequestVar[Can[Author]](Empty) def view (xhtml : NodeSeq) : NodeSeq = passedAuthor.map({ author = // do bind, etc here and return a NodeSeq }) openOr Text(Invalid author) If the RequestVar contains a Full(author) then the map function will be applied, otherwise the Text node is returned. The map method on Can combined with openOr makes a very nice pattern for safely processing the contents or returning a default value. Can's legacyNullTest also helps when you might get a null back (from a Java library, for instance): val debug = Can.legacyNullTest(System.getenv(DEBUG_FOO)).map(true) openOr false I hope these help. Derek On Wed, Oct 15, 2008 at 10:33 AM, David Pollak [EMAIL PROTECTED] wrote: Charles, A Can is a container... it can contain a thing or be empty. You can transform the contents of a Can from one thing to another using map(). map() on Can, Option, List is exactly the same as map() on Array in Ruby: irb(main):004:0 [1,2,3].map{|v| v.to_s + Cats} = [1 Cats, 2 Cats, 3 Cats] This is just like in Scala: scala List(1,2,3).map(v = v.toString + Cats) res0: List[java.lang.String] = List(1 Cats, 2 Cats, 3 Cats) In Ruby, when you access the first element of an Array that has no elements, you get 'nil' back. In Scala, you get an exception. This allows you to tell the difference between [nil][0] and [][0] which are the same in Ruby. The most syntactically pleasing way of extracting things from List, Can, Option in Scala is the for comprehension: scala for (a - Some(3); |b - Some(4)) yield a * b res1: Option[Int] = Some(12) Does that help? Thanks, David Charles F. Munat wrote: Thanks. I have read everything I could find on this but I think I'm just a bit dense about it. Probably, it's just unfamiliarity with the syntax of Scala as a whole and functional programming in general (or maybe I'm just stupid). Hopefully, at some point the light bulb will come on and this will seem easy. I'll read the blog post. Chas. David Pollak wrote: Please also see:http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html Can[T] is just like Option[T] Marius wrote: to get stuff out of a can you can do: 1. Pattern matching having c a Can[String] c match { case Full(value) = //do something with the value case _ = } 2. call open_!(if you're sure your can is not empty) or openOr Br's, Marius On Oct 15, 3:22 am, Charles F. Munat [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I must be very dense, but these cans are kicking my butt (kicking my can?). No matter what I do, I seem to end up with everything back in the can! I just... want... to get... the goodies... OUT! An example: How do I extract the URI of the current page from S.request? I am currently doing something immensely stupid and wrong like this: S.request.toList.head.location.toList.head.createDefaultLink.toList.head.text I *know* this is way wrong, but I'm not clever enough, apparently, to figure out the puzzle, despite reading through the Can code repeatedly. I figure the above works only because what I'm looking for is there, which sort of defeats the purpose of the cans... Can anyone help? This is driving me insane. Chas. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com 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 -~--~~~~--~~--~--~---
[Lift] Re: A stupid question
Ah, that's a good idea. Didn't think of that. Chas. Jorge Ortiz wrote: Also check out CanSpec.scala to get an idea of how Cans can be used. --j On Wed, Oct 15, 2008 at 9:33 AM, David Pollak [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Charles, A Can is a container... it can contain a thing or be empty. You can transform the contents of a Can from one thing to another using map(). map() on Can, Option, List is exactly the same as map() on Array in Ruby: irb(main):004:0 [1,2,3].map{|v| v.to_s + Cats} = [1 Cats, 2 Cats, 3 Cats] This is just like in Scala: scala List(1,2,3).map(v = v.toString + Cats) res0: List[java.lang.String] = List(1 Cats, 2 Cats, 3 Cats) In Ruby, when you access the first element of an Array that has no elements, you get 'nil' back. In Scala, you get an exception. This allows you to tell the difference between [nil][0] and [][0] which are the same in Ruby. The most syntactically pleasing way of extracting things from List, Can, Option in Scala is the for comprehension: scala for (a - Some(3); |b - Some(4)) yield a * b res1: Option[Int] = Some(12) Does that help? Thanks, David Charles F. Munat wrote: Thanks. I have read everything I could find on this but I think I'm just a bit dense about it. Probably, it's just unfamiliarity with the syntax of Scala as a whole and functional programming in general (or maybe I'm just stupid). Hopefully, at some point the light bulb will come on and this will seem easy. I'll read the blog post. Chas. David Pollak wrote: Please also see: http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html Can[T] is just like Option[T] Marius wrote: to get stuff out of a can you can do: 1. Pattern matching having c a Can[String] c match { case Full(value) = //do something with the value case _ = } 2. call open_!(if you're sure your can is not empty) or openOr Br's, Marius On Oct 15, 3:22 am, Charles F. Munat [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: I must be very dense, but these cans are kicking my butt (kicking my can?). No matter what I do, I seem to end up with everything back in the can! I just... want... to get... the goodies... OUT! An example: How do I extract the URI of the current page from S.request? I am currently doing something immensely stupid and wrong like this: S.request.toList.head.location.toList.head.createDefaultLink.toList.head.text I *know* this is way wrong, but I'm not clever enough, apparently, to figure out the puzzle, despite reading through the Can code repeatedly. I figure the above works only because what I'm looking for is there, which sort of defeats the purpose of the cans... Can anyone help? This is driving me insane. Chas. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com 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 -~--~~~~--~~--~--~---
[Lift] Re: A stupid question
Yes, this does help a bit. I actually understand the Can (and Option) very well, and I think it's a great idea (though the added advantage of the Can doesn't seem to be heavily used yet). The problem I was having was dealing with cans inside cans inside cans. But after reading everything everyone sent, I think I've got it now: val p = S.request.flatMap(_.location).flatMap(_.createDefaultLink).map(_.text) a href={p.openOr()}This page/a Or this: val p = for (req - S.request; loc - req.location; txt - loc.createDefaultLink) yield txt a href={p.openOr().toString}This page/a Of course, now that I know, I'll just use S.uri. Thanks again for all the help! Chas. David Pollak wrote: Charles, A Can is a container... it can contain a thing or be empty. You can transform the contents of a Can from one thing to another using map(). map() on Can, Option, List is exactly the same as map() on Array in Ruby: irb(main):004:0 [1,2,3].map{|v| v.to_s + Cats} = [1 Cats, 2 Cats, 3 Cats] This is just like in Scala: scala List(1,2,3).map(v = v.toString + Cats) res0: List[java.lang.String] = List(1 Cats, 2 Cats, 3 Cats) In Ruby, when you access the first element of an Array that has no elements, you get 'nil' back. In Scala, you get an exception. This allows you to tell the difference between [nil][0] and [][0] which are the same in Ruby. The most syntactically pleasing way of extracting things from List, Can, Option in Scala is the for comprehension: scala for (a - Some(3); |b - Some(4)) yield a * b res1: Option[Int] = Some(12) Does that help? Thanks, David Charles F. Munat wrote: Thanks. I have read everything I could find on this but I think I'm just a bit dense about it. Probably, it's just unfamiliarity with the syntax of Scala as a whole and functional programming in general (or maybe I'm just stupid). Hopefully, at some point the light bulb will come on and this will seem easy. I'll read the blog post. Chas. David Pollak wrote: Please also see: http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html Can[T] is just like Option[T] Marius wrote: to get stuff out of a can you can do: 1. Pattern matching having c a Can[String] c match { case Full(value) = //do something with the value case _ = } 2. call open_!(if you're sure your can is not empty) or openOr Br's, Marius On Oct 15, 3:22 am, Charles F. Munat [EMAIL PROTECTED] wrote: I must be very dense, but these cans are kicking my butt (kicking my can?). No matter what I do, I seem to end up with everything back in the can! I just... want... to get... the goodies... OUT! An example: How do I extract the URI of the current page from S.request? I am currently doing something immensely stupid and wrong like this: S.request.toList.head.location.toList.head.createDefaultLink.toList.head.text I *know* this is way wrong, but I'm not clever enough, apparently, to figure out the puzzle, despite reading through the Can code repeatedly. I figure the above works only because what I'm looking for is there, which sort of defeats the purpose of the cans... Can anyone help? This is driving me insane. Chas. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com 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 -~--~~~~--~~--~--~---