Scala,Liftand theRealTimeWeb DavidPollak BenevolentDictatorforLife - - PowerPoint PPT Presentation

scala lift and the real time web
SMART_READER_LITE
LIVE PREVIEW

Scala,Liftand theRealTimeWeb DavidPollak BenevolentDictatorforLife - - PowerPoint PPT Presentation

Scala,Liftand theRealTimeWeb DavidPollak BenevolentDictatorforLife LiftWebFramework dpp@liftweb.net Allaboutme(DavidPollak) Sometimesstrict,mostlylazy


slide-1
SLIDE 1

Scala,Liftand theRealTimeWeb

DavidPollak

BenevolentDictatorforLife LiftWebFramework dpp@liftweb.net

slide-2
SLIDE 2

Allaboutme(DavidPollak)

Sometimesstrict,mostlylazy LeaddeveloperforLiftWebFramework Spreadsheetjunky(writingmorethan using) Writing

slide-3
SLIDE 3

Oh,thethingsI’llcramintoyourbrain

ScalaisaFunctional/OOhybridlanguage

  • Compiles to JVM byte-code
  • Runs at native speeds
  • Full Java library interoperability

Liftisapowerful,simpleWebFramework

  • Best framework for building interactive web sites
  • More concise than Ruby on Rails
  • More type-safe than anything you’ve ever used

(except Happs)

ScalaleadstoLift

slide-4
SLIDE 4

WorldWideWeb:IntheBeginning

Antisocial Person↔ Machine

  • Shopping
  • Banking
  • CRUD

Browser==GreenScreen

slide-5
SLIDE 5

Web2.0

MoreSocial DifferentFlavors

  • Person ↔ Machine ↔ Machine: Mashups
  • Person ↔ Person: Facebook, Twitter
  • Machine ↔ Machine → Person:

Microformats

InternetbecomesPostman

slide-6
SLIDE 6

RealTimeWeb

WearesocialAnimalsthatlove instantgratification RealTime

  • Games
  • Chat
  • Everything

Nextwave:RealTimeWeb

slide-7
SLIDE 7

PunchLine

Scala→ Lift Lift→ RealTimeWeb RealTimeWeb→ AwesomeUser Experience

slide-8
SLIDE 8

Real8timeChatinLift:Messages

case class Messages(msgs: List[String])

slide-9
SLIDE 9

Real8timeChatinLift:Server

  • bject ChatServer extends Actor with

ListenerManager { private var msgs: List[String] = Nil protected def createUpdate = Messages(msgs)

  • verride def highPriority = {

case s: String if s.length > 0 => msgs ::= s updateListeners() } this.start }

slide-10
SLIDE 10

Real8timeChatinLift:Comet

class Chat extends CometActor with CometListenee { private var msgs: List[String] = Nil def render = <div> <ul>{msgs.reverse.map(m => <li>{m}</li>)}</ul> {ajaxText("", s => {ChatServer ! s; Noop})} </div> protected def registerWith = ChatServer

  • verride def highPriority = {

case Messages(m) => msgs = m ; reRender(false) } }

slide-11
SLIDE 11

Singletons

  • bject ChatServer extends Actor

with ListenerManager ChatServer isasingleton OneinstanceperJVM Canbepassedasparameter… it’san instance CompositionofActorandListenerManager

slide-12
SLIDE 12

Caseclasses

case class Foo(bar: String, baz: List[Foo]) ForFree:

  • bar and baz properties (immutable by default)
  • toString, hashCode, and equals
  • Pattern matching with parameter extraction

20linesofboilerplatereducedto1line

slide-13
SLIDE 13

PatternMatching

case Messages(m) => msgs = m case s: String if s.length > 0 => msgs ::= s Matchagainstcaseclasses

  • Extract parameters
  • Test against parameters: case Person(name, 35) =>
  • Great for message/event handling

Type8safecasting Awesomedeclarativewayofexpressinglogic

slide-14
SLIDE 14

TraitsandComposition

class Chat extends CometActor with CometListenee

Traitsareinterfacesplusdataandlogic Composition

  • object sally extends Person(“Sally”) with

Female with Runner

  • def womansRun(who: Female with Runner) ->

womansRun(sally)

Benefitsofmultipleinheritancew/o diamondproblem

slide-15
SLIDE 15

ImmutableDataTypes

var msgs: List[String] = Nil <div>Hello</div> Immutabilityyourlong8timefriend:String

  • Never have to say synchronized
  • Never have to make a copy “just in case”
  • Great for hash keys

Leadstotransformationalthinking Betterforgarbagecollector

slide-16
SLIDE 16

Functionpassing

msgs.reverse.map(m => <li>{m}</li>) ajaxText("", s => {ChatServer ! s; Noop}) map takesafunctionasaparameter

  • Transforms String to Elem
  • Applied to each String in msgs
  • The function is strongly typed: m is a String

Functionsareinstancesandcanbepassed Functionscanbeputin Mapsforlateruse

slide-17
SLIDE 17

XMLLiteralsandSupport

<ul>{msgs.reverse.map(m => <li>{m}</li>)}</ul>

XMLfirst8classinScala,likeStringsinJava Library8levelXPath8styleoperators

  • xml \ "li" – find all the child <li> tags
  • for {p <- x \\ "p"; ca <- p \ "@class"

c <- ca.text.split(" ")} yield c Find all the classes used by <p> tags

Immutable,likeStrings

slide-18
SLIDE 18

ActorLibrary

RealTimemeansevents Threadless,stackless eventhandlers Withverynicesyntax(Erlangish)

slide-19
SLIDE 19

FeelingRESTful

case Req(ApiPath :: "statuses" :: "public_timeline" :: Nil, this.method, GetRequest) => publicTimeline def publicTimeline(): Box[TwitterResponse] = { val statusList = Message.findAll(OrderBy(Message.id, Descending), MaxRows(20)). map(msgData _) Full(Right(Map("statuses" -> ("status", statusList) ))) }

slide-20
SLIDE 20

Conclusion

Scala’sobjectmodelisasupersetofJava’s Scala’straits:super8powerfulclasscomposition Scalaismoretype8safethanJava ScalaissyntacticallysimplerthanJavaorRuby ScalaisasconciseasRuby ScalahasawesomelibrariesincludingActors WhatifJava,Ruby,andHaskellhasalove8child? Scala’sdesignledtoLift’sdesign Lift’sdesignmakestheRealTimeWebsuper8simple

slide-21
SLIDE 21

Questions