F# on the Server-Side<br />       Tomáš Petříček<br />http://tomasp.net/blog	 | @tomaspetricek<br />
About me<br />Wrote book about F#<br />Introduce functional conceptsand F# to C# developers<br />Worked with the F# team<b...
Server-side specifics<br />Reliability and scalability is essential<br />Many concurrent requests<br />Many I/O bound oper...
Plan of the talk<br />
Event-based style<br />Programming using explicit callbacks<br />Solves part of the problem<br />Inherently concurrent mod...
Limitations of event-based style<br />What we really wanted to write?<br />What we really should write…<br />let data = We...
Plan of the talk<br />
F# asynchronous workflows<br />Write usual sequential code<br />Asynchronous calls using let!and do!<br />Supports all F# ...
F# asynchronous workflows<br />Translated using some primitive operations<br />Looks similar to event-based programs…<br /...
Demo<br />HTTP proxy using asynchronous workflows<br />
Plan of the talk<br />
Agent-based programming<br />Program consists of agents (processes)<br />Lightweight and asynchronous<br />Agents communic...
Simple agent in F#<br />Send Hello to the caller<br />Waiting for message is asynchronous<br />Can perform long-running I/...
Demo<br />Caching web pages using agent<br />
Summary and Q&A<br />Event-based is good starting point<br />F# workflow syntax really helps<br />Asynchronous workflows e...
Upcoming SlideShare
Loading in …5
×

F# on the Server-Side

1,407 views

Published on

Talk about server-side programming in F# from Functional Programming eXchange 2011 at SkillsMatter. The talk progresses from callback-based programming to asynchronous workflows and to agents in F#.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,407
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Agents are lightweight and have some body running inside them
  • F# on the Server-Side

    1. 1. F# on the Server-Side<br /> Tomáš Petříček<br />http://tomasp.net/blog | @tomaspetricek<br />
    2. 2. About me<br />Wrote book about F#<br />Introduce functional conceptsand F# to C# developers<br />Worked with the F# team<br />Contributed to VS 2010 integration<br />Reactive programming research<br />First blog post about F# in May 2006<br />
    3. 3. Server-side specifics<br />Reliability and scalability is essential<br />Many concurrent requests<br />Many I/O bound operations<br />Exist to share state with clients<br />We need different programming style!<br />
    4. 4. Plan of the talk<br />
    5. 5. Event-based style<br />Programming using explicit callbacks<br />Solves part of the problem<br />Inherently concurrent model<br />Avoids blocking of threads<br />Gaining popularity (e.g. Node.js)<br />HttpServer.Start("http://localhost:8080/", fun ctx -> <br />WebClient.DownloadAsync(getProxyUrl(ctx), fun data -><br /> ctx.ResponseStream.WriteAsync(data, fun res -><br /> ctx.ResponseStream.Close())))<br />
    6. 6. Limitations of event-based style<br />What we really wanted to write?<br />What we really should write…<br />let data = WebClient.DownloadAsync(getProxyUrl(ctx))<br />ctx.ResponseStream.WriteAsync(data)<br />ctx.ResponseStream.Close()<br />try <br /> let data = WebClient.DownloadAsync(getProxyUrl(ctx))<br />ctx.ResponseStream.WriteAsync(data)<br />finally<br />ctx.ResponseStream.Close()<br />
    7. 7. Plan of the talk<br />
    8. 8. F# asynchronous workflows<br />Write usual sequential code<br />Asynchronous calls using let!and do!<br />Supports all F# control-flow constructs<br />let copyPageTourloutputStream = async {<br /> try <br /> let! html = WebClient.AsyncDownload(url)<br />do! outputStream.AsyncWrite(html) <br />finally <br />ctx.ResponseStream.Close()}<br />
    9. 9. F# asynchronous workflows<br />Translated using some primitive operations<br />Looks similar to event-based programs…<br />Many thanks to Haskell for Monads™!<br />let copyPageTourloutputStream = <br />async.TryFinally(<br />async.Bind(WebClient.AsyncDownload(url), fun html -> <br />async.Bind( outputStream.AsyncWrite(html),<br />async.Zero() )),<br />fun () ->ctx.ResponseStream.Close())<br />
    10. 10. Demo<br />HTTP proxy using asynchronous workflows<br />
    11. 11. Plan of the talk<br />
    12. 12. Agent-based programming<br />Program consists of agents (processes)<br />Lightweight and asynchronous<br />Agents communicate via messages<br />Thread-safe and queued<br />Enables parallelism<br />Different agents vs. multiple instances<br />Many thanks to Erlang for Agents™!<br />
    13. 13. Simple agent in F#<br />Send Hello to the caller<br />Waiting for message is asynchronous<br />Can perform long-running I/O before replying<br />Calling agent asynchronously<br />letecho = Agent.Start(fun agent -> async {<br /> while true do<br />let!name, rchan= agent.Receive()<br />rchan.Reply("Hello "+ name) })<br />let!str = echo.PostAndAsyncReply(funch-> "Tomas", ch)<br />
    14. 14. Demo<br />Caching web pages using agent<br />
    15. 15. Summary and Q&A<br />Event-based is good starting point<br />F# workflow syntax really helps<br />Asynchronous workflows enable agents<br />Use agents for server-side state<br />

    ×