Bài giảng F#

1. Introduction

2. Functional Programming

3. Imperative Programming

4. Object – Oriented Programming

5. Organizing, Annotating, and Quoting

6. F# Libraries

7. User Interfaces

8. Data Access

9. Threadings

 

pptx106 trang | Chuyên mục: F# | Chia sẻ: dkS00TYs | Lượt xem: 2136 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng F#, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
ceding code with this to get the same result: Comments and Doc Comments 71 F# provides two kinds of comments. Single-line comments start with two slashes and extend to the end of a line, as in this example: // this is a single-line comment Multiple-line comment (* this is a comment *) Or this one: (* this is a comment *) Comments and Doc Comments 72 Doc comments allow you to extract comments from the source file in the form of XML or HTML. Doc comments start with three slashes instead of two. /// this is an explanation let myString = "this is a string" /// /// divides the given parameter by 10 /// /// the thing to be divided by 10 let divTen x = x / 10 Comments and Doc Comments 73 fsc --doc:doc.xml Test.fs Comments and Doc Comments 74 Quoted code 75 Quotations give you a way to tell the compiler, “Don’t generate code for this section of the source file; turn it into a data structure, or an expression tree, instead.” To quote an expression, place it between the operators: // quote the integer one let quotedInt = // print the quoted integer printfn "%A" quotedInt 6. F# Libraries Microsoft.FSharp.Core.Operators Microsoft.FSharp.Reflection Microsoft.FSharp.Collections Microsoft.FSharp.Text.Printf Microsoft.FSharp.Control.Event 76 Microsoft.FSharp.Core.Operators 77 Contain mathematical operators. open Microsoft.FSharp.Core.Operators abs Absolute value of the given number. pown Overloaded power operator. sin Sine of the given number cos Cosine of the given number tan Tangent of the given number sqrt Square root of the given number let a=abs(-5) let v=pown 2 3 let s=sin 90.0 Microsoft.FSharp.Reflection 78 A module containing functions that supplement the .NET Framework’s reflection classes to give a more accurate view of F# types and values. open Microsoft.FSharp.Reflection let printTupleTypes (x: obj) = let t = x.GetType() if FSharpType.IsTuple t then printfn "IS tuple" else printfn "Not Is tuple" printTupleTypes("a",1) printTupleTypes("a") Microsoft.FSharp.Collections 79 A module containing functions for any type that supports the IEnumerable interface. open Microsoft.FSharp.Collections Array, Array2D, Array3D, Array4D Microsoft.FSharp.Text.Printf 80 A module for formatting strings. open Microsoft.FSharp.Text Printf.printf "Hello %s" "Teo" Printf.printf "An integer: %i" 10 let pi = System.Math.PI Printf.printfn "%f" pi Printf.printfn "%1.1f" pi Printf.printfn "%2.2f" pi Printf.printfn "%2.8f" pi Microsoft.FSharp.Control.Event 81 A module for working with events in F#. Creating and handling events: The basics of creating and handling events using the create and add functions. The filter function: A function to filter the data coming into events. The partition function: A function that splits the data coming into events into two. The map function: A function that maps the data before it reaches the event handler. Microsoft.FSharp.Control.Event 82 Creating and handling events: let event = new Event() event.Publish.Add(fun x -> printfn "%s" x) event.Trigger "hello" > hello val event : Event > Microsoft.FSharp.Control.Event 83 The filter function: let event = new Event() let newEvent = event.Publish |> 	Event.filter (fun x -> x.StartsWith("H")) newEvent.Add(fun x -> printfn "new event: %s" x) event.Trigger "Harry" event.Trigger "Jane" event.Trigger "Hillary" event.Trigger "John" event.Trigger "Henry" > new event: Harry new event: Hillary new event: Henry val event : Event val newEvent : IEvent > Microsoft.FSharp.Control.Event 84 The partition function: let event = new Event() let hData, nonHData = event.Publish |> Event.partition (fun x -> x.StartsWith("H")) let x = Event.partition hData.Add(fun x -> printfn "H data: %s" x) nonHData.Add(fun x -> printfn "None H data: %s" x) event.Trigger "Harry" event.Trigger "Jane" event.Trigger "Hillary" event.Trigger "John" event.Trigger "Henry" > > H data: Harry None H data: Jane H data: Hillary None H data: John H data: Henry > Microsoft.FSharp.Control.Event 85 The map function: let event = new Event() let newEvent = event.Publish |> Event.map (fun x -> "Mapped data: " + x) newEvent.Add(fun x -> printfn "%s" x) event.Trigger "Harry" event.Trigger "Sally" 7. User Interfaces WinForm Controls in WinForm WinForm Events and the Event Module ASP.NET WebForm and IHttpHandler 86 WinForm 87 Need reference System.Drawing.dll & System.Windows.Forms.dll Application.Run method and passing it the form object to create or form.Show() or form.ShowDialog() WinForm 88 open System open System.Drawing open System.Windows.Forms // create a new form let form = new Form(BackColor = Color.Purple, Text = "Introducing WinForms") // show the form [] form.Show() Application.Run(form) form.ShowDialog() Controls in WinForm 89 A control is simply a class that derives from System.Windows.Forms.Control. Label TextBox MaskedTextBox Button LinkLabel CheckBox RadioButton ComboBox ListBox DataGridView TreeView Table 8-3. / page 192 WinForm Events and the Event Module 90 open System open System.Windows.Forms let form = // create a new form let temp = new Form() // subscribe the mouse click event filtering so it only // reacts to the left button temp.MouseClick |> Event.filter (fun e -> e.Button = MouseButtons.Left) |> Event.add (fun _ -> MessageBox.Show("Left button") |> ignore) // return the form temp [] form.Show() WinForm Events and the Event Module 91 Click DoubleClick Enter Leave KeyPress Move MouseHover Paint Resize ASP.NET WebForm and IHttpHandler 92 ASP.NET is a technology intended to simplify creating dynamic web pages Take yourself 8. Data Access 93 ADO.NET Data Binding and the DataGridView Control 94 ADO.NET open System.Data open System.Data.SqlClient // get the connection string let connectionString = "server=fithui;database=Petshop;user id=sa;pwd=hoilamgi" let main() = // create a connection use connection = new SqlConnection(connectionString) // create a command let command = connection.CreateCommand(CommandText = "select * from Account", CommandType = CommandType.Text) // open the connection connection.Open() 95 ADO.NET // open a reader to read data from the DB use reader = command.ExecuteReader() // fetch the ordinals let title = reader.GetOrdinal("FirstName") let firstName = reader.GetOrdinal("LastName") let lastName = reader.GetOrdinal("Address1") // function to read strings from the data reader let getString (r: #IDataReader) x = if r.IsDBNull(x) then "" else r.GetString(x) 96 ADO.NET // read all the items while reader.Read() do printfn "%s %s %s" (getString reader title ) (getString reader firstName) (getString reader lastName) main() > Adam Barr Vertigo Software, Inc. Kim Abercrombie Vertigo Software, Inc. Adam Barr Vertigo Software, Inc. val it : unit = () > F# Interactive: 97 ADO.NET How to Insert? How to Update? How to Delete? Take yourself 98 Data Binding and the DataGridView Control Data binding is the process of mapping a value or set of values to a user interface control. Example 1: Listbox control 99 Data Binding and the DataGridView Control open System open System.Collections.Generic open System.Data open System.Data.SqlClient open System.Windows.Forms let GetDict() = // get the connection string let connectionString = "server=fithui;database=Petshop;user id=sa;pwd=hoilamgi" // create a connection use connection = new SqlConnection(connectionString) // create a command let command = connection.CreateCommand(CommandText = "select * from 	Category", CommandType = CommandType.Text) // open the connection connection.Open() 100 Data Binding and the DataGridView Control // open a reader to read data from the DB use reader = command.ExecuteReader() // fetch the ordinals let CategoryId = reader.GetOrdinal("CategoryId") let Name = reader.GetOrdinal("Name") // function to read strings from the data reader let getString (r: #IDataReader) x = if r.IsDBNull(x) then "" else r.GetString(x) let dict = new Dictionary() // read all the items while reader.Read() do let fn=(getString reader CategoryId) let ln=(getString reader Name) if not ( dict.ContainsKey fn ) then dict.Add(fn, ln) dict 101 Data Binding and the DataGridView Control let form = let temp = new Form() temp.Text ] form.Show() 102 Data Binding and the DataGridView Control Example 2: DataGridView 103 Data Binding and the DataGridView Control open System open System.Collections.Generic open System.Data open System.Data.SqlClient open System.Windows.Forms // creates a connections then executes the given command on it let createDataSet commandString = // read the connection string let connectionString = 	"server=fithui;database=Petshop;user 	id=sa;pwd=hoilamgi" // create a data adapter to fill the dataset let adapter = new SqlDataAdapter 	(commandString, connectionString) // create a new data set and fill it let ds = new DataSet() adapter.Fill(ds) |> ignore ds 104 Data Binding and the DataGridView Control // create the data set that will be bound to the form let dataSet = createDataSet "select * from Product" // create a form containing a data bound data grid view let form = let temp = new Form(Text="Demo Binding DataGridView") let grid = new DataGridView(Dock = DockStyle.Fill) temp.Controls.Add(grid) grid.DataSource ] form.Show() 9. Threadings 105 open System.Threading let main() = // create a new thread passing it a lambda function let thread = new Thread(fun () -> // print a message on the newly created thread printfn "Created thread: %i" Thread.CurrentThread.ManagedThreadId) // start the new thread thread.Start() // print an message on the original thread printfn "Orginal thread: %i" Thread.CurrentThread.ManagedThreadId // wait of the created thread to exit thread.Join() do main() > Created thread: 67 Orginal thread: 1 val it : unit = () > F# Interactive: Full Name: Trần Duy Thanh Blog	 :  Email: tranduythanh@hui.edu.vn Phone: 0987773061 Contact 106 Beginning F# from Robert Pickering Expert F# from Don Syme, Adam Granicz and Antonio Cisternino Books: END 107 

File đính kèm:

  • pptxF#.pptx
Tài liệu liên quan