forth 为Suave Web Scripting提供自包含的入门示例

为Suave Web Scripting提供自包含的入门示例

gistfile1.fs
//==========================================
// Working fully self-contained getting-started example for Suave Web Server scripting 
//
// Note you don't need to have _anything_ installed before starting with this script. Nothing
// but F# Interactive and this script.
//
// This script fetches the Paket.exe component which is referenced later in the script. 
// Initially the #r "paket.exe" reference is shown as unresolved. Once it has been
// downloaded by the user (by executing the first part of the script) the reference 
// shows as resolved and can be used.
//
// Paket is then used to fetch a set of F# packages, which are then used later in the script.
// 

//------------------------------------------
// Step 0. Boilerplate to get the paket.exe tool

open System
open System.IO

Environment.CurrentDirectory <- __SOURCE_DIRECTORY__

if not (File.Exists "paket.exe") then
    let url = "https://github.com/fsprojects/Paket/releases/download/0.26.3/paket.exe"
    use wc = new Net.WebClient()
    let tmp = Path.GetTempFileName() 
    wc.DownloadFile(url, tmp); 
    File.Move(tmp,Path.GetFileName url)

// Step 1. Resolve and install the packages 

#r "paket.exe"

Paket.Scripting.Install """
    source https://nuget.org/api/v2
    nuget Suave 0.16.0
    nuget FSharp.Data 
    nuget FSharp.Charting
""";;

// Step 2. Use the packages 

#r "packages/Suave/lib/Suave.dll"
#r "packages/FSharp.Data/lib/net40/FSharp.Data.dll"
#r "packages/FSharp.Charting/lib/net40/FSharp.Charting.dll"

let ctxt = FSharp.Data.WorldBankData.GetDataContext()

let data = ctxt.Countries.Algeria.Indicators.``GDP (current US$)``

open Suave                 // always open suave
open Suave.Http.Successful // for OK-result
open Suave.Web             // for config

web_server default_config (OK (sprintf "Hello World! In 2010 Algeria earned %f " data.[2010]))


forth 为Suave Web Scripting提供自包含的入门示例

为Suave Web Scripting提供自包含的入门示例

gistfile1.fs
//==========================================
// Working fully self-contained getting-started example for Suave Web Server scripting 
//
// Note you don't need to have _anything_ installed before starting with this script. Nothing
// but F# Interactive and this script.
//
// This script fetches the Paket.exe component which is referenced later in the script. 
// Initially the #r "paket.exe" reference is shown as unresolved. Once it has been
// downloaded by the user (by executing the first part of the script) the reference 
// shows as resolved and can be used.
//
// Paket is then used to fetch a set of F# packages, which are then used later in the script.
// 

//------------------------------------------
// Step 0. Boilerplate to get the paket.exe tool

open System
open System.IO

Environment.CurrentDirectory <- __SOURCE_DIRECTORY__

if not (File.Exists "paket.exe") then
    let url = "https://github.com/fsprojects/Paket/releases/download/0.26.3/paket.exe"
    use wc = new Net.WebClient()
    let tmp = Path.GetTempFileName() 
    wc.DownloadFile(url, tmp); 
    File.Move(tmp,Path.GetFileName url)

// Step 1. Resolve and install the packages 

#r "paket.exe"

Paket.Scripting.Install """
    source https://nuget.org/api/v2
    nuget Suave 0.16.0
    nuget FSharp.Data 
    nuget FSharp.Charting
""";;

// Step 2. Use the packages 

#r "packages/Suave/lib/Suave.dll"
#r "packages/FSharp.Data/lib/net40/FSharp.Data.dll"
#r "packages/FSharp.Charting/lib/net40/FSharp.Charting.dll"

let ctxt = FSharp.Data.WorldBankData.GetDataContext()

let data = ctxt.Countries.Algeria.Indicators.``GDP (current US$)``

open Suave                 // always open suave
open Suave.Http.Successful // for OK-result
open Suave.Web             // for config

web_server default_config (OK (sprintf "Hello World! In 2010 Algeria earned %f " data.[2010]))


forth test.fs

test.fs
module Program() =
  let main argv =
    printfn "%A" argv

forth 企业博客文章中F#的代码示例。

企业博客文章中F#的代码示例。

FSharpUnitOfMeasureError.fs
// This will compile
let dollarsToEuros dollars: float<dollar> =
    dollars * 0.74<dollar>

// Won't compile due to a type mismatch
let dollarsToEuroCompileError dollars: float<dollar> =
    dollars * 0.74
FSharpUnitOfMeasure.fs
open Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols

let meter = 1<m>
FSharpRecordTypeNullReference.fs
let employee2 = {Person = null; IsActive = false }
FSharpRecordType.fs
type Person = { Name: string }

type Employee = { Person: Person; IsActive: bool }

let employee = { Person = { Name = "John" }; IsActive = true }
FSharpPatternMatchingOptions.fs
let hasBalance (balance: float option) = 
  match balance with
    | Some b -> printfn "Balance - %f" b
    | None -> printfn "No balance available"
FSharpCustomUnitOfMeasure.fs
[<Measure>] type dollar
[<Measure>] type euro
FSharpBankAccount.fs
type BankAccount() =
  member val Balance = Option<float>.None with get, set

  member this.openAccount() =
    this.Balance <- Some 0.0
    this.Balance.Value

  member this.closeAccount() =
    this.Balance <- None

  member this.getBalance() =
    this.Balance.Value