[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-31 Thread ben

Thankyou all for your posts, I now understand whats going on !

Victor : Thanks for the great examples of how to pass functions into
other functions - a very illuminating example, I'm sitting here
thinking about how much that will make a difference to code
conciseness compared with Java !

David : Combined with Victor's example of how to pass a function via
another function, I can now see how the value from the user is applied
to the domain object. Pretty slick, once you grasp whats going on.

Cracking stuff, looking forward to learning more about Lift+Scala, and
hopefully getting a site into production in the future.

Ben

--~--~-~--~~~---~--~~
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 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread Naftoli Gugenheim

I think the difference is only true in a class body. Inside a code block I 
think the def is syntactic sugar for the other syntax.

-
Viktor Klang wrote:

On Thu, Jul 30, 2009 at 11:02 PM, DFectuoso  wrote:

>
> Viktor, that was a great explanation, im sure ben will apraciate that
> kind of teaching =)


Well, thank you.


>
>
> A follow up question that just made me wonder, what is the diference
> between
>
> val square = (x : Int) => x * x
>

This creates a final reference to a new function instance


>
> and
> def square(x:Int):Int = x * x
>

This creates a method

Now, you can transform a method into a function by adding an underscore
after it, like this:

def square(x : Int) = x * x

val sq = square _ //<--- notice the underscore. (I always think of it as
"detaching" the method from an instance)

This is what the Scala REPL says:

scala> def square(x : Int) = x * x
square: (Int)Int

scala> val vsquare = (x : Int) => x * x
vsquare: (Int) => Int = 

scala> val v2square = square _
v2square: (Int) => Int = 

scala> square(2)
res0: Int = 4

scala> vsquare(2)
res1: Int = 4

scala> v2square(2)
res2: Int = 4





>
> ?
>
>
> On Jul 30, 1:49 pm, Viktor Klang  wrote:
> > Hello Ben!
> >
> > the following:
> >
> > val square = (x:Int) => x * x
> >
> > "val square" is the equivalent of a "final" reference in Java.
> >
> > (x : Int) => x * x
> >
> > This constructs a Function instance that takes an Int and returns that
> Int
> > squared.
> >
> > The equivalent Java code would be something like:
> >
> > interface Function //We're ignoring variance here, as to not confuse
> > {
> > public R call(P p);
> >
> > }
> >
> > final Function square = new Function()
> {
> >  public Integer call(Integer i)
> >  {
> >  return i * i;
> >  }
> >
> > }
> >
> > And then you can do:
> >
> > square.call(2) // 4
> >
> > So, back to Scala:
> >
> > val square = (x : Int) => x * x
> >
> > square(2) // 4
> >
> >
> >
> > On Thu, Jul 30, 2009 at 9:37 PM, ben  wrote:
> >
> > > Hi,
> >
> > > I feel I must apologise for this  post ... I've only been doing scala
> > > for a week, and lift for 3 days, but I'm stuck.
> > > I'm coming from an experienced java background, just struggling with
> > > some scala constructs.
> >
> > > I feel I understand the basics of lambda and function literals - but
> > > I'm blown away by some of the syntax in the lift "Getting Started"
> > > tutorial.
> > > For example, given :
> >
> > > val mylist = Array(1,2,3)
> > > mylist..foreach(v => println(v)
> >
> > > Here I understand where "v" comes from - its each int in the list.
> >
> > > In the tutorial (http://liftweb.net/docs/getting_started/
> > > mod_master.html<
> http://liftweb.net/docs/getting_started/%0Amod_master.html>
> > > )
> > > there is a function "desc" (Listing 15) :
> >
> > > private def desc(td: ToDo, reDraw: () => JsCmd) =
> > >  swappable({td.desc},
> > > {ajaxText(td.desc,
> > > v => {td.desc(v).save; reDraw()})}
> > > )
> >
> > > For my own brain to try and "break it down", I have :
> >
> > >  private def desc(td: ToDo, reDraw: () => JsCmd) = {
> > >val myFunctionLiteral = (xxx: String) => {td.desc(xxx).save;
> > > println("!Desc function :" + xxx); reDraw()}
> >
> > >swappable({td.desc},
> > >  {ajaxText(td.desc, myFunctionLiteral)})
> > >  }
> >
> > > This is called from the "doList" method :
> >
> > > private def doList(reDraw: () => JsCmd)(html: NodeSeq): NodeSeq =
> > >  toShow.
> > >  flatMap(td =>
> > >   bind("todo", html,
> > >  "desc" -> desc(td, reDraw)
> > >   ))
> >
> > > I understand where the "ToDo" object "td" is coming from, and how the
> > > "reDraw" thing works, and how they are passed to the "desc" function,
> > > but what I don't understand at all is where the val "xxx" in
> > > "myFunctionLiteral" comes from ?
> > > How is it passed into the function ? Is it currying ?
> >
> > > I'm sorry for such a vague question, I'm totally at sea here.
> >
> > > Thankyou for reading this, and for any help you may provide !
> >
> > > Ben
> >
> > --
> > Viktor Klang
> >
> > Rogue Scala-head
> >
> > Blog: klangism.blogspot.com
> > Twttr: viktorklang
> >
>


-- 
Viktor Klang

Rogue Scala-head

Blog: klangism.blogspot.com
Twttr: viktorklang



--~--~-~--~~~---~--~~
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 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread David Pollak
On Thu, Jul 30, 2009 at 2:02 PM, DFectuoso  wrote:

>
> Viktor, that was a great explanation, im sure ben will apraciate that
> kind of teaching =)
>
> A follow up question that just made me wonder, what is the diference
> between
>
> val square = (x : Int) => x * x
> and
> def square(x:Int):Int = x * x


Scala has two core things: Objects and Methods.

In the first example, we create an instance of an object which implements
the Function1[Int, Int].  You can call the apply() method on square either
by using square.apply(2) or square(2).  The latter syntax is expanded to the
former by the compiler.

The second example is a method definition.  The method is defined on a class
and called on an instance of the class.

Methods can take type parameters.  Functions have their type parameter
defined at instantiation time.


>
>
> ?
>
>
> On Jul 30, 1:49 pm, Viktor Klang  wrote:
> > Hello Ben!
> >
> > the following:
> >
> > val square = (x:Int) => x * x
> >
> > "val square" is the equivalent of a "final" reference in Java.
> >
> > (x : Int) => x * x
> >
> > This constructs a Function instance that takes an Int and returns that
> Int
> > squared.
> >
> > The equivalent Java code would be something like:
> >
> > interface Function //We're ignoring variance here, as to not confuse
> > {
> > public R call(P p);
> >
> > }
> >
> > final Function square = new Function()
> {
> >  public Integer call(Integer i)
> >  {
> >  return i * i;
> >  }
> >
> > }
> >
> > And then you can do:
> >
> > square.call(2) // 4
> >
> > So, back to Scala:
> >
> > val square = (x : Int) => x * x
> >
> > square(2) // 4
> >
> >
> >
> > On Thu, Jul 30, 2009 at 9:37 PM, ben  wrote:
> >
> > > Hi,
> >
> > > I feel I must apologise for this  post ... I've only been doing scala
> > > for a week, and lift for 3 days, but I'm stuck.
> > > I'm coming from an experienced java background, just struggling with
> > > some scala constructs.
> >
> > > I feel I understand the basics of lambda and function literals - but
> > > I'm blown away by some of the syntax in the lift "Getting Started"
> > > tutorial.
> > > For example, given :
> >
> > > val mylist = Array(1,2,3)
> > > mylist..foreach(v => println(v)
> >
> > > Here I understand where "v" comes from - its each int in the list.
> >
> > > In the tutorial (http://liftweb.net/docs/getting_started/
> > > mod_master.html<
> http://liftweb.net/docs/getting_started/%0Amod_master.html>
> > > )
> > > there is a function "desc" (Listing 15) :
> >
> > > private def desc(td: ToDo, reDraw: () => JsCmd) =
> > >  swappable({td.desc},
> > > {ajaxText(td.desc,
> > > v => {td.desc(v).save; reDraw()})}
> > > )
> >
> > > For my own brain to try and "break it down", I have :
> >
> > >  private def desc(td: ToDo, reDraw: () => JsCmd) = {
> > >val myFunctionLiteral = (xxx: String) => {td.desc(xxx).save;
> > > println("!Desc function :" + xxx); reDraw()}
> >
> > >swappable({td.desc},
> > >  {ajaxText(td.desc, myFunctionLiteral)})
> > >  }
> >
> > > This is called from the "doList" method :
> >
> > > private def doList(reDraw: () => JsCmd)(html: NodeSeq): NodeSeq =
> > >  toShow.
> > >  flatMap(td =>
> > >   bind("todo", html,
> > >  "desc" -> desc(td, reDraw)
> > >   ))
> >
> > > I understand where the "ToDo" object "td" is coming from, and how the
> > > "reDraw" thing works, and how they are passed to the "desc" function,
> > > but what I don't understand at all is where the val "xxx" in
> > > "myFunctionLiteral" comes from ?
> > > How is it passed into the function ? Is it currying ?
> >
> > > I'm sorry for such a vague question, I'm totally at sea here.
> >
> > > Thankyou for reading this, and for any help you may provide !
> >
> > > Ben
> >
> > --
> > Viktor Klang
> >
> > Rogue Scala-head
> >
> > Blog: klangism.blogspot.com
> > Twttr: viktorklang
> >
>


-- 
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 liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread David Pollak
On Thu, Jul 30, 2009 at 12:37 PM, ben  wrote:

>
> Hi,
>
> I feel I must apologise for this  post ... I've only been doing scala
> for a week, and lift for 3 days, but I'm stuck.
> I'm coming from an experienced java background, just struggling with
> some scala constructs.
>
> I feel I understand the basics of lambda and function literals - but
> I'm blown away by some of the syntax in the lift "Getting Started"
> tutorial.
> For example, given :
>
> val mylist = Array(1,2,3)
> mylist..foreach(v => println(v)
>
> Here I understand where "v" comes from - its each int in the list.
>
> In the tutorial (http://liftweb.net/docs/getting_started/
> mod_master.html
> )
> there is a function "desc" (Listing 15) :
>
> private def desc(td: ToDo, reDraw: () => JsCmd) =
>  swappable({td.desc},
> {ajaxText(td.desc,
> v => {td.desc(v).save; reDraw()})}
> )
>
> For my own brain to try and "break it down", I have :
>
>  private def desc(td: ToDo, reDraw: () => JsCmd) = {
>val myFunctionLiteral = (xxx: String) => {td.desc(xxx).save;
> println("!Desc function :" + xxx); reDraw()}
>
>swappable({td.desc},
>  {ajaxText(td.desc, myFunctionLiteral)})
>  }
>
> This is called from the "doList" method :
>
> private def doList(reDraw: () => JsCmd)(html: NodeSeq): NodeSeq =
>  toShow.
>  flatMap(td =>
>   bind("todo", html,
>  "desc" -> desc(td, reDraw)
>   ))
>
> I understand where the "ToDo" object "td" is coming from, and how the
> "reDraw" thing works, and how they are passed to the "desc" function,
> but what I don't understand at all is where the val "xxx" in
> "myFunctionLiteral" comes from ?


Your "myFuncionLiteral" is passed as a parameter to ajaxText.  When the
ajaxText box is submitted by the user, your function is applied to the
String typed by the user.

This is a common pattern in Lift... associate a function on the server with
an HTML element on the client.  That way, Lift abstracts away the HTTP
request/response cycle and allows you to focus on "what happens when the
user does something?"


>
> How is it passed into the function ? Is it currying ?
>
> I'm sorry for such a vague question, I'm totally at sea here.
>
> Thankyou for reading this, and for any help you may provide !
>
> Ben
>
> >
>


-- 
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 liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread Viktor Klang
But you can also create and pass a function inline:

scala> doMath(1,2,(a : Int, b : Int) => a ^ b)
res5: Int = 3


On Thu, Jul 30, 2009 at 11:43 PM, Viktor Klang wrote:

> Here's a short example:
>
> val add = (a : Int, b : Int) => a + b
> val sub = (a : Int, b : Int) => a - b
> val div = (a : Int, b : Int) => a / b
> val mul = (a : Int, b : Int) => a * b
>
> //This is a method that takes an Int a, an Int b and a function that takes
> 2 Ints and produces an Int
> def doMath(a : Int, b : Int, arith : (Int,Int) => Int) = arith(a,b)
>
> scala> doMath(1,2,add)
> res1: Int = 3
>
> scala> doMath(1,2,sub)
> res2: Int = -1
>
> scala> doMath(2,1,div)
> res3: Int = 2
>
> scala> doMath(1,2,mul)
> res4: Int = 2
>
>
> On Thu, Jul 30, 2009 at 11:36 PM, ben  wrote:
>
>>
>> Hi,
>>
>> Thanks for patience (and for the interesting subpost on the diff
>> between val & def) !
>> OK, the callback thing you suggested is starting to clear the mist ...
>> I found this article :
>> http://www.ibm.com/developerworks/java/library/j-scala01228.html
>> I've not had time to read it fully yet, as its getting late over here
>> in the UK, but it looks like what I'm after. I'll have a proper read
>> tomorrow.
>>
>> Thanks again for your time.
>>
>> Ben
>>
>> >>
>>
>
>
> --
> Viktor Klang
>
> Rogue Scala-head
>
> Blog: klangism.blogspot.com
> Twttr: viktorklang
>



-- 
Viktor Klang

Rogue Scala-head

Blog: klangism.blogspot.com
Twttr: viktorklang

--~--~-~--~~~---~--~~
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 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread Viktor Klang
Here's a short example:

val add = (a : Int, b : Int) => a + b
val sub = (a : Int, b : Int) => a - b
val div = (a : Int, b : Int) => a / b
val mul = (a : Int, b : Int) => a * b

//This is a method that takes an Int a, an Int b and a function that takes 2
Ints and produces an Int
def doMath(a : Int, b : Int, arith : (Int,Int) => Int) = arith(a,b)

scala> doMath(1,2,add)
res1: Int = 3

scala> doMath(1,2,sub)
res2: Int = -1

scala> doMath(2,1,div)
res3: Int = 2

scala> doMath(1,2,mul)
res4: Int = 2

On Thu, Jul 30, 2009 at 11:36 PM, ben  wrote:

>
> Hi,
>
> Thanks for patience (and for the interesting subpost on the diff
> between val & def) !
> OK, the callback thing you suggested is starting to clear the mist ...
> I found this article :
> http://www.ibm.com/developerworks/java/library/j-scala01228.html
> I've not had time to read it fully yet, as its getting late over here
> in the UK, but it looks like what I'm after. I'll have a proper read
> tomorrow.
>
> Thanks again for your time.
>
> Ben
>
> >
>


-- 
Viktor Klang

Rogue Scala-head

Blog: klangism.blogspot.com
Twttr: viktorklang

--~--~-~--~~~---~--~~
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 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread ben

Hi,

Thanks for patience (and for the interesting subpost on the diff
between val & def) !
OK, the callback thing you suggested is starting to clear the mist ...
I found this article : 
http://www.ibm.com/developerworks/java/library/j-scala01228.html
I've not had time to read it fully yet, as its getting late over here
in the UK, but it looks like what I'm after. I'll have a proper read
tomorrow.

Thanks again for your time.

Ben

--~--~-~--~~~---~--~~
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 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread Viktor Klang
(If I've understood your question correctly)

the definition of ajaxText is as follows:

  def ajaxText(value: String, func: String => JsCmd): Elem =
ajaxText_*(value, Empty, SFuncHolder(func))

So what you're doing is that you're sending a callback function to ajaxText,
and it will be called with a String later.

On Thu, Jul 30, 2009 at 11:03 PM, ben  wrote:

>
> Hi Viktor,
>
> Thanks for your reply.
>
> I do understand the simple examples, like the one you were kind enough
> to post.
>
> My problem is that I just can't seem to break through from the simple
> examples to the code in the "Getting Started" tutorial 
>
> To elaborate, given the following in a scala console :
>
> scala> val square = (x : Int) => x * x
> square: (Int) => Int = 
>
> scala> square(2)
> res5: Int = 4
>
> I can easily (in my mind) see how that function literal works - ie
> function square takes an int and apply(s) a square on the value passed
> in. In my mind I can see where x is coming from - its clearly the
> value "2" passed in the operation "square(2)" - but I just don't get
> where the string "xxx" (in the code posted (from the tutorial - see"
> Listing 15") is actually coming from ... there never seems to be the
> string passed to the function !
>
>
> >
>


-- 
Viktor Klang

Rogue Scala-head

Blog: klangism.blogspot.com
Twttr: viktorklang

--~--~-~--~~~---~--~~
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 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread ben

Hi Viktor,

Thanks for your reply.

I do understand the simple examples, like the one you were kind enough
to post.

My problem is that I just can't seem to break through from the simple
examples to the code in the "Getting Started" tutorial 

To elaborate, given the following in a scala console :

scala> val square = (x : Int) => x * x
square: (Int) => Int = 

scala> square(2)
res5: Int = 4

I can easily (in my mind) see how that function literal works - ie
function square takes an int and apply(s) a square on the value passed
in. In my mind I can see where x is coming from - its clearly the
value "2" passed in the operation "square(2)" - but I just don't get
where the string "xxx" (in the code posted (from the tutorial - see"
Listing 15") is actually coming from ... there never seems to be the
string passed to the function !


--~--~-~--~~~---~--~~
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 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread Viktor Klang
On Thu, Jul 30, 2009 at 11:02 PM, DFectuoso  wrote:

>
> Viktor, that was a great explanation, im sure ben will apraciate that
> kind of teaching =)


Well, thank you.


>
>
> A follow up question that just made me wonder, what is the diference
> between
>
> val square = (x : Int) => x * x
>

This creates a final reference to a new function instance


>
> and
> def square(x:Int):Int = x * x
>

This creates a method

Now, you can transform a method into a function by adding an underscore
after it, like this:

def square(x : Int) = x * x

val sq = square _ //<--- notice the underscore. (I always think of it as
"detaching" the method from an instance)

This is what the Scala REPL says:

scala> def square(x : Int) = x * x
square: (Int)Int

scala> val vsquare = (x : Int) => x * x
vsquare: (Int) => Int = 

scala> val v2square = square _
v2square: (Int) => Int = 

scala> square(2)
res0: Int = 4

scala> vsquare(2)
res1: Int = 4

scala> v2square(2)
res2: Int = 4





>
> ?
>
>
> On Jul 30, 1:49 pm, Viktor Klang  wrote:
> > Hello Ben!
> >
> > the following:
> >
> > val square = (x:Int) => x * x
> >
> > "val square" is the equivalent of a "final" reference in Java.
> >
> > (x : Int) => x * x
> >
> > This constructs a Function instance that takes an Int and returns that
> Int
> > squared.
> >
> > The equivalent Java code would be something like:
> >
> > interface Function //We're ignoring variance here, as to not confuse
> > {
> > public R call(P p);
> >
> > }
> >
> > final Function square = new Function()
> {
> >  public Integer call(Integer i)
> >  {
> >  return i * i;
> >  }
> >
> > }
> >
> > And then you can do:
> >
> > square.call(2) // 4
> >
> > So, back to Scala:
> >
> > val square = (x : Int) => x * x
> >
> > square(2) // 4
> >
> >
> >
> > On Thu, Jul 30, 2009 at 9:37 PM, ben  wrote:
> >
> > > Hi,
> >
> > > I feel I must apologise for this  post ... I've only been doing scala
> > > for a week, and lift for 3 days, but I'm stuck.
> > > I'm coming from an experienced java background, just struggling with
> > > some scala constructs.
> >
> > > I feel I understand the basics of lambda and function literals - but
> > > I'm blown away by some of the syntax in the lift "Getting Started"
> > > tutorial.
> > > For example, given :
> >
> > > val mylist = Array(1,2,3)
> > > mylist..foreach(v => println(v)
> >
> > > Here I understand where "v" comes from - its each int in the list.
> >
> > > In the tutorial (http://liftweb.net/docs/getting_started/
> > > mod_master.html<
> http://liftweb.net/docs/getting_started/%0Amod_master.html>
> > > )
> > > there is a function "desc" (Listing 15) :
> >
> > > private def desc(td: ToDo, reDraw: () => JsCmd) =
> > >  swappable({td.desc},
> > > {ajaxText(td.desc,
> > > v => {td.desc(v).save; reDraw()})}
> > > )
> >
> > > For my own brain to try and "break it down", I have :
> >
> > >  private def desc(td: ToDo, reDraw: () => JsCmd) = {
> > >val myFunctionLiteral = (xxx: String) => {td.desc(xxx).save;
> > > println("!Desc function :" + xxx); reDraw()}
> >
> > >swappable({td.desc},
> > >  {ajaxText(td.desc, myFunctionLiteral)})
> > >  }
> >
> > > This is called from the "doList" method :
> >
> > > private def doList(reDraw: () => JsCmd)(html: NodeSeq): NodeSeq =
> > >  toShow.
> > >  flatMap(td =>
> > >   bind("todo", html,
> > >  "desc" -> desc(td, reDraw)
> > >   ))
> >
> > > I understand where the "ToDo" object "td" is coming from, and how the
> > > "reDraw" thing works, and how they are passed to the "desc" function,
> > > but what I don't understand at all is where the val "xxx" in
> > > "myFunctionLiteral" comes from ?
> > > How is it passed into the function ? Is it currying ?
> >
> > > I'm sorry for such a vague question, I'm totally at sea here.
> >
> > > Thankyou for reading this, and for any help you may provide !
> >
> > > Ben
> >
> > --
> > Viktor Klang
> >
> > Rogue Scala-head
> >
> > Blog: klangism.blogspot.com
> > Twttr: viktorklang
> >
>


-- 
Viktor Klang

Rogue Scala-head

Blog: klangism.blogspot.com
Twttr: viktorklang

--~--~-~--~~~---~--~~
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 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread DFectuoso

Viktor, that was a great explanation, im sure ben will apraciate that
kind of teaching =)

A follow up question that just made me wonder, what is the diference
between

val square = (x : Int) => x * x
and
def square(x:Int):Int = x * x

?


On Jul 30, 1:49 pm, Viktor Klang  wrote:
> Hello Ben!
>
> the following:
>
> val square = (x:Int) => x * x
>
> "val square" is the equivalent of a "final" reference in Java.
>
> (x : Int) => x * x
>
> This constructs a Function instance that takes an Int and returns that Int
> squared.
>
> The equivalent Java code would be something like:
>
> interface Function //We're ignoring variance here, as to not confuse
> {
>     public R call(P p);
>
> }
>
> final Function square = new Function() {
>      public Integer call(Integer i)
>      {
>          return i * i;
>      }
>
> }
>
> And then you can do:
>
> square.call(2) // 4
>
> So, back to Scala:
>
> val square = (x : Int) => x * x
>
> square(2) // 4
>
>
>
> On Thu, Jul 30, 2009 at 9:37 PM, ben  wrote:
>
> > Hi,
>
> > I feel I must apologise for this  post ... I've only been doing scala
> > for a week, and lift for 3 days, but I'm stuck.
> > I'm coming from an experienced java background, just struggling with
> > some scala constructs.
>
> > I feel I understand the basics of lambda and function literals - but
> > I'm blown away by some of the syntax in the lift "Getting Started"
> > tutorial.
> > For example, given :
>
> > val mylist = Array(1,2,3)
> > mylist..foreach(v => println(v)
>
> > Here I understand where "v" comes from - its each int in the list.
>
> > In the tutorial (http://liftweb.net/docs/getting_started/
> > mod_master.html
> > )
> > there is a function "desc" (Listing 15) :
>
> > private def desc(td: ToDo, reDraw: () => JsCmd) =
> >  swappable({td.desc},
> >         {ajaxText(td.desc,
> >                     v => {td.desc(v).save; reDraw()})}
> >         )
>
> > For my own brain to try and "break it down", I have :
>
> >  private def desc(td: ToDo, reDraw: () => JsCmd) = {
> >    val myFunctionLiteral = (xxx: String) => {td.desc(xxx).save;
> > println("!Desc function :" + xxx); reDraw()}
>
> >    swappable({td.desc},
> >      {ajaxText(td.desc, myFunctionLiteral)})
> >  }
>
> > This is called from the "doList" method :
>
> > private def doList(reDraw: () => JsCmd)(html: NodeSeq): NodeSeq =
> >  toShow.
> >  flatMap(td =>
> >   bind("todo", html,
> >      "desc" -> desc(td, reDraw)
> >   ))
>
> > I understand where the "ToDo" object "td" is coming from, and how the
> > "reDraw" thing works, and how they are passed to the "desc" function,
> > but what I don't understand at all is where the val "xxx" in
> > "myFunctionLiteral" comes from ?
> > How is it passed into the function ? Is it currying ?
>
> > I'm sorry for such a vague question, I'm totally at sea here.
>
> > Thankyou for reading this, and for any help you may provide !
>
> > Ben
>
> --
> Viktor Klang
>
> Rogue Scala-head
>
> Blog: klangism.blogspot.com
> Twttr: viktorklang
--~--~-~--~~~---~--~~
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 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: Getting Started tutorial - not understanding some syntax

2009-07-30 Thread Viktor Klang
Hello Ben!

the following:

val square = (x:Int) => x * x

"val square" is the equivalent of a "final" reference in Java.

(x : Int) => x * x

This constructs a Function instance that takes an Int and returns that Int
squared.

The equivalent Java code would be something like:

interface Function //We're ignoring variance here, as to not confuse
{
public R call(P p);
}

final Function square = new Function() {
 public Integer call(Integer i)
 {
 return i * i;
 }
}

And then you can do:

square.call(2) // 4

So, back to Scala:

val square = (x : Int) => x * x

square(2) // 4


On Thu, Jul 30, 2009 at 9:37 PM, ben  wrote:

>
> Hi,
>
> I feel I must apologise for this  post ... I've only been doing scala
> for a week, and lift for 3 days, but I'm stuck.
> I'm coming from an experienced java background, just struggling with
> some scala constructs.
>
> I feel I understand the basics of lambda and function literals - but
> I'm blown away by some of the syntax in the lift "Getting Started"
> tutorial.
> For example, given :
>
> val mylist = Array(1,2,3)
> mylist..foreach(v => println(v)
>
> Here I understand where "v" comes from - its each int in the list.
>
> In the tutorial (http://liftweb.net/docs/getting_started/
> mod_master.html
> )
> there is a function "desc" (Listing 15) :
>
> private def desc(td: ToDo, reDraw: () => JsCmd) =
>  swappable({td.desc},
> {ajaxText(td.desc,
> v => {td.desc(v).save; reDraw()})}
> )
>
> For my own brain to try and "break it down", I have :
>
>  private def desc(td: ToDo, reDraw: () => JsCmd) = {
>val myFunctionLiteral = (xxx: String) => {td.desc(xxx).save;
> println("!Desc function :" + xxx); reDraw()}
>
>swappable({td.desc},
>  {ajaxText(td.desc, myFunctionLiteral)})
>  }
>
> This is called from the "doList" method :
>
> private def doList(reDraw: () => JsCmd)(html: NodeSeq): NodeSeq =
>  toShow.
>  flatMap(td =>
>   bind("todo", html,
>  "desc" -> desc(td, reDraw)
>   ))
>
> I understand where the "ToDo" object "td" is coming from, and how the
> "reDraw" thing works, and how they are passed to the "desc" function,
> but what I don't understand at all is where the val "xxx" in
> "myFunctionLiteral" comes from ?
> How is it passed into the function ? Is it currying ?
>
> I'm sorry for such a vague question, I'm totally at sea here.
>
> Thankyou for reading this, and for any help you may provide !
>
> Ben
>
> >
>


-- 
Viktor Klang

Rogue Scala-head

Blog: klangism.blogspot.com
Twttr: viktorklang

--~--~-~--~~~---~--~~
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 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---