Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions docs/library/CsvFile.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ points to a live CSV file on the Yahoo finance web site:
*)

// Download the stock prices
let msft =
CsvFile
.Load(__SOURCE_DIRECTORY__ + "/../data/MSFT.csv")
.Cache()
let msft = CsvFile.Load(__SOURCE_DIRECTORY__ + "/../data/MSFT.csv").Cache()

// Print the prices in the HLOC format
for row in msft.Rows |> Seq.truncate 10 do
Expand Down Expand Up @@ -111,10 +108,7 @@ separator and quote characters when saving.
*)

// Saving the first 10 stock prices where the closing price is higher than the opening price in TSV format:
msft
.Filter(fun row -> row?Close.AsFloat() > row?Open.AsFloat())
.Truncate(10)
.SaveToString('\t')
msft.Filter(fun row -> row?Close.AsFloat() > row?Open.AsFloat()).Truncate(10).SaveToString('\t')

(*** include-fsi-merged-output ***)
(**
Expand Down
25 changes: 8 additions & 17 deletions docs/library/CsvProvider.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ The following sample calls the `Load` method with an URL that points to a live C
*)

// Download the stock prices
let msft =
Stocks
.Load(__SOURCE_DIRECTORY__ + "/../data/MSFT.csv")
.Cache()
let msft = Stocks.Load(__SOURCE_DIRECTORY__ + "/../data/MSFT.csv").Cache()

// Look at the most recent row. Note the 'Date' property
// is of type 'DateTime' and 'Open' has a type 'decimal'
Expand Down Expand Up @@ -133,7 +130,7 @@ so we use the `GetSample` method instead of calling `Load` and passing the same
*)

let small =
CsvProvider<"../data/SmallTest.csv", ResolutionFolder=ResolutionFolder>.GetSample ()
CsvProvider<"../data/SmallTest.csv", ResolutionFolder=ResolutionFolder>.GetSample()

(*** include-fsi-merged-output ***)

Expand All @@ -160,7 +157,7 @@ open FSharp.Data.UnitSystems.SI.UnitNames
for row in small.Rows do
let speed = row.Distance / row.Time

if speed > 15.0M<metre/second> then
if speed > 15.0M<metre / second> then
printfn "%s (%A m/s)" row.Name speed

(*** include-fsi-merged-output ***)
Expand Down Expand Up @@ -204,13 +201,12 @@ are automatically skipped (the sample file ([`data/MortalityNY.csv`](../data/Mor
*)

let mortalityNy =
CsvProvider<"../data/MortalityNY.tsv", IgnoreErrors=true, ResolutionFolder=ResolutionFolder>.GetSample ()
CsvProvider<"../data/MortalityNY.tsv", IgnoreErrors=true, ResolutionFolder=ResolutionFolder>.GetSample()

// Find the name of a cause based on code
// (Pedal cyclist injured in an accident)
let cause =
mortalityNy.Rows
|> Seq.find (fun r -> r.``Cause of death Code`` = "V13.4")
mortalityNy.Rows |> Seq.find (fun r -> r.``Cause of death Code`` = "V13.4")

// Print the number of injured cyclists
printfn "CAUSE: %s" cause.``Cause of death``
Expand Down Expand Up @@ -240,9 +236,7 @@ the `MissingValues` static parameter of `CsvProvider` as a comma-separated strin
For example, to ignore `this` and `that` we could do:
*)

CsvProvider<"X,Y,Z\nthis,that,1.0", MissingValues="this,that">
.GetSample()
.Rows
CsvProvider<"X,Y,Z\nthis,that,1.0", MissingValues="this,that">.GetSample().Rows
(*** include-fsi-merged-output ***)
(**

Expand Down Expand Up @@ -390,9 +384,7 @@ the `Save` method. You can also use the `SaveToString()` to get the output direc

// Saving the first 10 rows that don't have missing values to a new csv file
airQuality
.Filter(fun row ->
not (Double.IsNaN row.Ozone)
&& not (Double.IsNaN row.``Solar.R``))
.Filter(fun row -> not (Double.IsNaN row.Ozone) && not (Double.IsNaN row.``Solar.R``))
.Truncate(10)
.SaveToString()

Expand Down Expand Up @@ -434,8 +426,7 @@ It's even possible to create csv files without parsing at all:
type MyCsvType = CsvProvider<Schema="A (int), B (string), C (date option)", HasHeaders=false>

let myRows =
[ MyCsvType.Row(1, "a", None)
MyCsvType.Row(2, "B", Some System.DateTime.Now) ]
[ MyCsvType.Row(1, "a", None); MyCsvType.Row(2, "B", Some System.DateTime.Now) ]

let myCsv = new MyCsvType(myRows)
myCsv.SaveToString()
Expand Down
6 changes: 1 addition & 5 deletions docs/library/HtmlCssSelectors.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ let links =
doc.CssSelect("div#search > div#ires div.g > div.s div.kv cite")
|> List.map (fun n ->
match n.InnerText() with
| t when
(t.StartsWith("https://")
|| t.StartsWith("http://"))
->
t
| t when (t.StartsWith("https://") || t.StartsWith("http://")) -> t
| t -> "http://" + t)

(*** include-fsi-merged-output ***)
Expand Down
15 changes: 3 additions & 12 deletions docs/library/HtmlParser.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ we are looking at.
*)
let links =
results.Descendants [ "a" ]
|> Seq.choose (fun x ->
x.TryGetAttribute("href")
|> Option.map (fun a -> x.InnerText(), a.Value()))
|> Seq.choose (fun x -> x.TryGetAttribute("href") |> Option.map (fun a -> x.InnerText(), a.Value()))
|> Seq.truncate 10
|> Seq.toList

Expand All @@ -78,14 +76,7 @@ and `Seq.map`.

let searchResults =
links
|> List.filter (fun (name, url) ->
name <> "Cached"
&& name <> "Similar"
&& url.StartsWith("/url?"))
|> List.map (fun (name, url) ->
name,
url
.Substring(0, url.IndexOf("&sa="))
.Replace("/url?q=", ""))
|> List.filter (fun (name, url) -> name <> "Cached" && name <> "Similar" && url.StartsWith("/url?"))
|> List.map (fun (name, url) -> name, url.Substring(0, url.IndexOf("&sa=")).Replace("/url?q=", ""))

(*** include-fsi-merged-output ***)
14 changes: 2 additions & 12 deletions docs/library/HtmlProvider.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,7 @@ let rawStats = NugetStats().Tables.``Version History of FSharp.Data``

// helper function to analyze version numbers from Nuget
let getMinorVersion (v: string) =
System
.Text
.RegularExpressions
.Regex(
@"\d.\d"
)
.Match(
v
)
.Value
System.Text.RegularExpressions.Regex(@"\d.\d").Match(v).Value

// group by minor version and calculate the download count
let stats =
Expand Down Expand Up @@ -150,8 +141,7 @@ let viewersByDoctor =
|> Seq.groupBy (fun season -> season.``Directed by``)
|> Seq.map (fun (doctor, seasons) ->
let averaged =
seasons
|> Seq.averageBy (fun season -> season.``UK viewers (millions)``)
seasons |> Seq.averageBy (fun season -> season.``UK viewers (millions)``)

doctor, averaged)
|> Seq.toArray
Expand Down
4 changes: 1 addition & 3 deletions docs/library/Http.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ let cc = CookieContainer()
// Send a request to switch the language
Http.RequestString(
msdnUrl "system.datetime",
query =
[ "cs-save-lang", "1"
"cs-lang", "fsharp" ],
query = [ "cs-save-lang", "1"; "cs-lang", "fsharp" ],
cookieContainer = cc
)
|> ignore
Expand Down
31 changes: 22 additions & 9 deletions docs/library/JsonProvider.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,12 @@ Applied to the previous example, this would be:
*)

type People2 =
JsonProvider<"""
JsonProvider<
"""
[ { "name":"John", "age":94 },
{ "name":"Tomas" } ] """, SampleIsList=true>
{ "name":"Tomas" } ] """,
SampleIsList=true
>

let person = People2.Parse("""{ "name":"Gustavo" }""")

Expand Down Expand Up @@ -210,11 +213,14 @@ Let's consider an example where this can be useful:
*)

type AmbiguousEntity =
JsonProvider<Sample="""
JsonProvider<
Sample="""
{ "code":"000", "length":"0" }
{ "code":"123", "length":"42" }
{ "code":"4E5", "length":"1.83" }
""", SampleIsList=true>
""",
SampleIsList=true
>

let code = (AmbiguousEntity.GetSamples()[1]).Code
let length = (AmbiguousEntity.GetSamples()[1]).Length
Expand All @@ -232,11 +238,15 @@ Now, let's enable inline schemas:
open FSharp.Data.Runtime.StructuralInference

type AmbiguousEntity2 =
JsonProvider<Sample="""
JsonProvider<
Sample="""
{ "code":"typeof<string>", "length":"typeof< float<metre> >" }
{ "code":"123", "length":"42" }
{ "code":"4E5", "length":"1.83" }
""", SampleIsList=true, InferenceMode=InferenceMode.ValuesAndInlineSchemasOverrides>
""",
SampleIsList=true,
InferenceMode=InferenceMode.ValuesAndInlineSchemasOverrides
>

let code2 = (AmbiguousEntity2.GetSamples().[1]).Code
let length2 = (AmbiguousEntity2.GetSamples().[1]).Length
Expand Down Expand Up @@ -325,8 +335,7 @@ printfn "Showing page %d of %d. Total records %d" info.Page info.Pages info.Tota

// Print all data points
for record in doc.Array do
record.Value
|> Option.iter (fun value -> printfn "%d: %f" record.Date value)
record.Value |> Option.iter (fun value -> printfn "%d: %f" record.Date value)

(*** include-fsi-merged-output ***)

Expand Down Expand Up @@ -448,7 +457,11 @@ project file). If you are building a library `MyLib.dll`, you can write:

*)
type WB =
JsonProvider<"../data/WorldBank.json", EmbeddedResource="MyLib, MyLib.data.worldbank.json", ResolutionFolder=ResolutionFolder>
JsonProvider<
"../data/WorldBank.json",
EmbeddedResource="MyLib, MyLib.data.worldbank.json",
ResolutionFolder=ResolutionFolder
>

(**
You still need to specify the local path, but this is only used when compiling `MyLib.dll`.
Expand Down
41 changes: 28 additions & 13 deletions docs/library/JsonSchema.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#r "../../src/FSharp.Data.Json.Core/bin/Release/netstandard2.0/FSharp.Data.Json.Core.dll"
#r "../../src/FSharp.Data.Runtime.Utilities/bin/Release/netstandard2.0/FSharp.Data.Runtime.Utilities.dll"
#r "../../src/FSharp.Data.Http/bin/Release/netstandard2.0/FSharp.Data.Http.dll"

open System
open System.IO
open FSharp.Data
Expand All @@ -18,7 +19,8 @@ access to JSON documents, similar to how the XML Type Provider supports XML Sche
Let's start with a basic JSON Schema example:
*)

let personSchema = """
let personSchema =
"""
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
Expand Down Expand Up @@ -63,7 +65,8 @@ let personSchema = """

// Create a type based on the schema
[<Literal>]
let PersonSchemaLiteral = """
let PersonSchemaLiteral =
"""
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
Expand Down Expand Up @@ -109,7 +112,9 @@ let PersonSchemaLiteral = """
type Person = JsonProvider<Schema=PersonSchemaLiteral>

// Parse a JSON document that conforms to the schema
let person = Person.Parse("""
let person =
Person.Parse(
"""
{
"firstName": "John",
"lastName": "Smith",
Expand All @@ -126,7 +131,8 @@ let person = Person.Parse("""
}
]
}
""")
"""
)

// Access the strongly typed properties
printfn "Name: %s %s" person.FirstName person.LastName
Expand Down Expand Up @@ -159,19 +165,24 @@ Here's how validation works:
*)

// Valid JSON that conforms to the schema
let validPerson = Person.Parse("""
let validPerson =
Person.Parse(
"""
{
"firstName": "Jane",
"lastName": "Doe",
"age": 35,
"email": "jane.doe@example.com"
}
""")
"""
)

printfn "Valid JSON: %s %s" validPerson.FirstName validPerson.LastName

// Invalid JSON that violates schema rules will cause an exception
// Let's use try-catch to demonstrate validation errors:
let invalidJson = """
let invalidJson =
"""
{
"firstName": "John",
"age": -5
Expand All @@ -190,8 +201,7 @@ if jsonValue.TryGetProperty("lastName").IsNone then
printfn "Schema validation failed: missing required property 'lastName'"

// Check numeric constraints from the schema
if jsonValue.TryGetProperty("age").IsSome &&
jsonValue.["age"].AsInteger() < 0 then
if jsonValue.TryGetProperty("age").IsSome && jsonValue.["age"].AsInteger() < 0 then
printfn "Schema validation failed: 'age' must be non-negative"

(**
Expand Down Expand Up @@ -313,7 +323,8 @@ To use JSON Schema with the JSON Type Provider:
Here's a more complex example with nested objects:
*)

let orderSchema = """
let orderSchema =
"""
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
Expand Down Expand Up @@ -354,7 +365,8 @@ let orderSchema = """

// Create a type based on the order schema
[<Literal>]
let OrderSchemaLiteral = """
let OrderSchemaLiteral =
"""
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
Expand Down Expand Up @@ -395,7 +407,9 @@ let OrderSchemaLiteral = """

type Order = JsonProvider<Schema=OrderSchemaLiteral>

let order = Order.Parse("""
let order =
Order.Parse(
"""
{
"orderId": "ORD-123456",
"customer": {
Expand All @@ -420,7 +434,8 @@ let order = Order.Parse("""
"totalAmount": 1351.97,
"orderDate": "2023-10-01T12:00:00Z"
}
""")
"""
)

printfn "Order: %s" order.OrderId
printfn "Customer: %s" order.Customer.Name
Expand Down
Loading
Loading