Skip to content

Commit

Permalink
rename ok operator to some to match SheetEntity case name
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Feb 28, 2023
1 parent 41ea33a commit bf9b8ea
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/FsSpreadsheet/DSL/CellBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ type CellBuilder() =
SheetEntity.NoneOptional []

member inline this.Yield(s : string) : SheetEntity<Value list> =
SheetEntity.ok [DataType.String,s]
SheetEntity.some [DataType.String,s]

member inline this.Yield(value: Value) : SheetEntity<Value list> =
SheetEntity.ok [value]
SheetEntity.some [value]

member inline this.Yield(value: SheetEntity<Value>) : SheetEntity<Value list> =
match value with
Expand All @@ -61,7 +61,7 @@ type CellBuilder() =

member inline this.Yield(n: 'a when 'a :> System.IFormattable) =
let v = DataType.InferCellValue n
SheetEntity.ok [v]
SheetEntity.some [v]

member inline this.Yield(s : string option) : SheetEntity<Value list> =
match s with
Expand Down
16 changes: 8 additions & 8 deletions src/FsSpreadsheet/DSL/ColumnBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ open Expression

type ColumnBuilder() =

static member Empty : SheetEntity<ColumnElement list> = SheetEntity.ok []
static member Empty : SheetEntity<ColumnElement list> = SheetEntity.some []

// -- Computation Expression methods -->

member _.Quote (quotation: Quotations.Expr<'T>) =
quotation

member inline this.Zero() : SheetEntity<ColumnElement list> = SheetEntity.ok []
member inline this.Zero() : SheetEntity<ColumnElement list> = SheetEntity.some []

member this.SignMessages (messages : Message list) : Message list =
messages
|> List.map (fun m -> m.MapText (sprintf "In Column: %s"))

member inline _.Yield(c: ColumnElement) =
SheetEntity.ok [c]
SheetEntity.some [c]

member inline _.Yield(cs: ColumnElement list) =
SheetEntity.ok cs
SheetEntity.some cs

member inline _.Yield(c: SheetEntity<ColumnElement>) =
match c with
Expand Down Expand Up @@ -58,7 +58,7 @@ type ColumnBuilder() =
match c with
| v, Option.Some i -> ColumnElement.IndexedCell (Row i, v)
| v, None -> ColumnElement.UnindexedCell v
SheetEntity.ok [re]
SheetEntity.some [re]

member inline _.Yield(c: SheetEntity<Value>) =
match c with
Expand All @@ -76,7 +76,7 @@ type ColumnBuilder() =
| v, Option.Some i -> ColumnElement.IndexedCell (Row i, v)
| v, None -> ColumnElement.UnindexedCell v
)
SheetEntity.ok res
SheetEntity.some res

member inline this.Yield(cs: seq<SheetEntity<CellElement>>) : SheetEntity<ColumnElement list>=
cs
Expand All @@ -86,7 +86,7 @@ type ColumnBuilder() =

member inline _.Yield(s : string) =
let v = DataType.InferCellValue s
SheetEntity.ok [ColumnElement.UnindexedCell v]
SheetEntity.some [ColumnElement.UnindexedCell v]

member inline this.Yield(n: RequiredSource<unit>) =
n
Expand All @@ -96,7 +96,7 @@ type ColumnBuilder() =

member inline this.Yield(n: 'a when 'a :> System.IFormattable) =
let v = DataType.InferCellValue n
SheetEntity.ok [ColumnElement.UnindexedCell v]
SheetEntity.some [ColumnElement.UnindexedCell v]

member inline this.YieldFrom(ns: SheetEntity<ColumnElement list> seq) =
ns
Expand Down
38 changes: 33 additions & 5 deletions src/FsSpreadsheet/DSL/Operators.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ module Operators =
let inline parseExpression (def : string -> SheetEntity<Value>) (s : Expr<'a>) : SheetEntity<Value> =
try
let value = eval<'a> s |> DataType.InferCellValue
SheetEntity.ok value
SheetEntity.some value
with
| err -> def err.Message

let inline parseOption (def : string -> SheetEntity<Value>) (s : Option<'a>) : SheetEntity<Value> =
match s with
| Option.Some value ->
DataType.InferCellValue value
|> SheetEntity.ok
|> SheetEntity.some
| None -> def "Value was missing"

let inline parseResult (def : string -> SheetEntity<Value>) (s : Result<'a,exn>) : SheetEntity<Value> =
match s with
| Result.Ok value ->
DataType.InferCellValue value
|> SheetEntity.ok
|> SheetEntity.some
| Result.Error exn -> def exn.Message

let inline parseAny (f : string -> SheetEntity<Value>) (v: 'T) : SheetEntity<Value> =
Expand Down Expand Up @@ -56,17 +56,45 @@ module Operators =
/// Required value operator
///
/// If expression does fail, returns a missing required value
let inline (!!) (v : 'T) : SheetEntity<Value> =
let inline (!) (v : 'T) : SheetEntity<Value> =
let f = fun s -> NoneRequired([message s])
parseAny f v

/// Optional value operator
///
/// If expression does fail, returns a missing optional value
let inline (!?) (v : 'T) : SheetEntity<Value> =
let inline (?) (v : 'T) : SheetEntity<Value> =
let f = fun s -> NoneOptional([message s])
parseAny f v

/// Required value operator
///
/// If expression does fail, returns a missing required value
let inline (>!) (f : 'T -> 'U) (v : 'T) : SheetEntity<Value> =
try
f v
|> DataType.InferCellValue
|> SheetEntity.some
with
| err -> NoneRequired([Exception err])

/// Optional value operator
///
/// If expression does fail, returns a missing optional value
let inline (>?) (f : 'T -> 'U) (v : 'T) : SheetEntity<Value> =
try
f v
|> DataType.InferCellValue
|> SheetEntity.some
with
| err -> NoneOptional([Exception err])

let inline (!<) (v : 'T) (f : 'T -> 'U) =
f >! v

let inline (?<) (v : 'T) (f : 'T -> 'U) =
f >? v

/// Optional operators for cell, row, column and sheet expressions
let optional = OptionalSource()

Expand Down
16 changes: 8 additions & 8 deletions src/FsSpreadsheet/DSL/RowBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ open Expression

type RowBuilder() =

static member Empty : SheetEntity<RowElement list> = SheetEntity.ok []
static member Empty : SheetEntity<RowElement list> = SheetEntity.some []

// -- Computation Expression methods -->

member _.Quote (quotation: Quotations.Expr<'T>) =
quotation

member inline this.Zero() : SheetEntity<RowElement list> = SheetEntity.ok []
member inline this.Zero() : SheetEntity<RowElement list> = SheetEntity.some []

member this.SignMessages (messages : Message list) : Message list =
messages
Expand All @@ -28,10 +28,10 @@ type RowBuilder() =
n

member inline _.Yield(c: RowElement) =
SheetEntity.ok [c]
SheetEntity.some [c]

member inline _.Yield(cs: RowElement list) =
SheetEntity.ok cs
SheetEntity.some cs

member inline _.Yield(c: SheetEntity<RowElement>) =
match c with
Expand Down Expand Up @@ -61,7 +61,7 @@ type RowBuilder() =
match c with
| v, Option.Some i -> RowElement.IndexedCell (Col i, v)
| v, None -> RowElement.UnindexedCell v
SheetEntity.ok [re]
SheetEntity.some [re]

member inline _.Yield(c: SheetEntity<Value>) =
match c with
Expand All @@ -78,15 +78,15 @@ type RowBuilder() =
|> Seq.reduce (fun a b -> this.Combine(a,b))

member inline _.Yield(c: Value) =
SheetEntity.ok [RowElement.UnindexedCell c]
SheetEntity.some [RowElement.UnindexedCell c]

member inline this.Yield(n: 'a when 'a :> System.IFormattable) =
let v = DataType.InferCellValue n
SheetEntity.ok [RowElement.UnindexedCell v]
SheetEntity.some [RowElement.UnindexedCell v]

member inline _.Yield(s : string) =
let v = DataType.InferCellValue s
SheetEntity.ok [RowElement.UnindexedCell v]
SheetEntity.some [RowElement.UnindexedCell v]

member inline this.YieldFrom(ns: SheetEntity<RowElement list> seq) =
ns
Expand Down
20 changes: 10 additions & 10 deletions src/FsSpreadsheet/DSL/SheetBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ open Expression

type SheetBuilder(name : string) =

static member Empty : SheetEntity<SheetElement list> = SheetEntity.ok []
static member Empty : SheetEntity<SheetElement list> = SheetEntity.some []

// -- Computation Expression methods -->

member inline this.Zero() : SheetEntity<SheetElement list> = SheetEntity.ok []
member inline this.Zero() : SheetEntity<SheetElement list> = SheetEntity.some []

member this.SignMessages (messages : Message list) : Message list =
messages
|> List.map (fun m -> m.MapText (sprintf "In Sheet %s: %s" name))

member inline _.Yield(se: SheetElement) =
SheetEntity.ok [se]
SheetEntity.some [se]

member inline _.Yield(cs: SheetElement list) =
SheetEntity.ok cs
SheetEntity.some cs

member inline _.Yield(cs: SheetEntity<SheetElement list>) =
cs
Expand Down Expand Up @@ -51,10 +51,10 @@ type SheetBuilder(name : string) =
|> Seq.reduce (fun a b -> this.Combine(a,b))

member inline _.Yield(cs: RowElement list) =
SheetEntity.ok [SheetElement.UnindexedRow cs]
SheetEntity.some [SheetElement.UnindexedRow cs]

member inline _.Yield(cs: RowBuilder) =
SheetEntity.ok [SheetElement.UnindexedRow []]
SheetEntity.some [SheetElement.UnindexedRow []]

member inline _.Yield(t: SheetEntity<string * (TableElement list)>) =
match t with
Expand All @@ -66,10 +66,10 @@ type SheetBuilder(name : string) =
NoneRequired messages

member inline _.Yield(te: string * (TableElement list)) =
SheetEntity.ok [SheetElement.Table te]
SheetEntity.some [SheetElement.Table te]

member inline _.Yield(tb: TableBuilder) =
SheetEntity.ok [SheetElement.Table (tb.Name,[])]
SheetEntity.some [SheetElement.Table (tb.Name,[])]

member inline _.Yield(c: SheetEntity<ColumnElement list>) =
match c with
Expand All @@ -81,10 +81,10 @@ type SheetBuilder(name : string) =
NoneRequired messages

member inline _.Yield(cs: ColumnElement list) =
SheetEntity.ok [SheetElement.UnindexedColumn cs]
SheetEntity.some [SheetElement.UnindexedColumn cs]

member inline _.Yield(cs: ColumnBuilder) =
SheetEntity.ok [SheetElement.UnindexedColumn []]
SheetEntity.some [SheetElement.UnindexedColumn []]


member inline this.YieldFrom(ns: SheetEntity<SheetElement list> seq) =
Expand Down
16 changes: 8 additions & 8 deletions src/FsSpreadsheet/DSL/TableBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ open Expression

type TableBuilder(name : string) =

static member Empty : SheetEntity<TableElement list> = SheetEntity.ok []
static member Empty : SheetEntity<TableElement list> = SheetEntity.some []

// -- Computation Expression methods -->

member inline this.Zero() : SheetEntity<TableElement list> = SheetEntity.ok []
member inline this.Zero() : SheetEntity<TableElement list> = SheetEntity.some []

member this.Name = name

Expand All @@ -21,10 +21,10 @@ type TableBuilder(name : string) =
|> List.map (fun m -> m.MapText (sprintf "In Sheet %s: %s" name))

member inline _.Yield(se: TableElement) =
SheetEntity.ok [se]
SheetEntity.some [se]

member inline _.Yield(cs: TableElement list) =
SheetEntity.ok cs
SheetEntity.some cs

member inline _.Yield(cs: SheetEntity<TableElement list>) =
cs
Expand All @@ -48,10 +48,10 @@ type TableBuilder(name : string) =
NoneRequired messages

member inline _.Yield(cs: RowElement list) =
SheetEntity.ok [TableElement.UnindexedRow cs]
SheetEntity.some [TableElement.UnindexedRow cs]

member inline _.Yield(cs: RowBuilder) =
SheetEntity.ok [TableElement.UnindexedRow []]
SheetEntity.some [TableElement.UnindexedRow []]

member inline _.Yield(c: SheetEntity<ColumnElement list>) =
match c with
Expand All @@ -63,10 +63,10 @@ type TableBuilder(name : string) =
NoneRequired messages

member inline _.Yield(cs: ColumnElement list) =
SheetEntity.ok [TableElement.UnindexedColumn cs]
SheetEntity.some [TableElement.UnindexedColumn cs]

member inline _.Yield(cs: ColumnBuilder) =
SheetEntity.ok [TableElement.UnindexedColumn []]
SheetEntity.some [TableElement.UnindexedColumn []]


member inline this.YieldFrom(ns: SheetEntity<TableElement list> seq) =
Expand Down
2 changes: 1 addition & 1 deletion src/FsSpreadsheet/DSL/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type SheetEntity<'T> =
| NoneOptional of Message list
| NoneRequired of Message list

static member ok (v : 'T) : SheetEntity<'T> = SheetEntity.Some (v,[])
static member some (v : 'T) : SheetEntity<'T> = SheetEntity.Some (v,[])

/// Get messages
member this.Messages =
Expand Down
12 changes: 6 additions & 6 deletions src/FsSpreadsheet/DSL/WorkbookBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ open Expression

type WorkbookBuilder() =

static member Empty : SheetEntity<WorkbookElement list> = SheetEntity.ok []
static member Empty : SheetEntity<WorkbookElement list> = SheetEntity.some []

// -- Computation Expression methods -->

member inline this.Zero() : SheetEntity<WorkbookElement list> = SheetEntity.ok []
member inline this.Zero() : SheetEntity<WorkbookElement list> = SheetEntity.some []

member this.SignMessages (messages : Message list) : Message list =
messages
|> List.map (fun m -> m.MapText (sprintf "In Workbook: %s"))

member inline _.Yield(c: WorkbookElement) =
SheetEntity.ok [c]
SheetEntity.some [c]

member inline _.Yield(w: SheetEntity<WorkbookElement>) =
match w with
Expand All @@ -31,7 +31,7 @@ type WorkbookBuilder() =
NoneRequired messages

member inline _.Yield(cs: WorkbookElement list) =
SheetEntity.ok cs
SheetEntity.some cs

member inline _.Yield(cs: SheetEntity<WorkbookElement list> ) =
cs
Expand All @@ -46,7 +46,7 @@ type WorkbookBuilder() =
NoneRequired messages

member inline _.Yield(cs: SheetElement list) =
SheetEntity.ok [WorkbookElement.UnnamedSheet cs]
SheetEntity.some [WorkbookElement.UnnamedSheet cs]

member inline _.Yield(c: SheetEntity<string * SheetElement list>) =
match c with
Expand All @@ -58,7 +58,7 @@ type WorkbookBuilder() =
NoneRequired messages

member inline _.Yield(cs: string * SheetElement list) =
SheetEntity.ok [WorkbookElement.NamedSheet (cs)]
SheetEntity.some [WorkbookElement.NamedSheet (cs)]


member inline this.YieldFrom(ns: SheetEntity<WorkbookElement list> seq) =
Expand Down

0 comments on commit bf9b8ea

Please sign in to comment.