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<R,P> //We're ignoring variance here, as to not confuse { public R call(P p); } final Function<Integer,Integer> square = new Function<Integer,Integer>() { 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 <b...@primrose.org.uk> 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(<span>{td.desc}</span>, > <span>{ajaxText(td.desc, > v => {td.desc(v).save; reDraw()})} > </span>) > > 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(<span>{td.desc}</span>, > <span>{ajaxText(td.desc, myFunctionLiteral)}</span>) > } > > 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 -~----------~----~----~----~------~----~------~--~---