Skip to content

Instantly share code, notes, and snippets.

@Anks
Created November 7, 2016 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anks/51b5a871d8caea54b6fefd25a72d3999 to your computer and use it in GitHub Desktop.
Save Anks/51b5a871d8caea54b6fefd25a72d3999 to your computer and use it in GitHub Desktop.
Code As Data in F#
(* Type definitions *)
// A list of functions that take the current context (TDS return),
// and return true if the column is supposed to be visible right now
type IsColumnVisible = (TdsReturn -> bool) list
// List of functions that take a data row (generic type 'T)
// and return true if the current column is supposed to be editable
type IsColumnEditable<'T> = ('T -> bool) list
// This is a specification for a single column
// A 3-element tuple, containing a expression that will identify the field,
// and visibility / editability selectors
type ColumnSpec<'T> = (Quotations.Expr * IsColumnVisible * IsColumnEditable<'T>)
(* Pre-defined functions *)
// Pre-defined column visibility specifiers
let showAlways = fun (t : TdsReturn) -> true
let showForRevised = fun (t : TdsReturn) -> t.IsRevised
let showForGovt = fun (t : TdsReturn) -> t.IsForGovernment
// Pre-defined column editing specifiers
let editAlways = fun (d : Deduction) -> true
let editWhenNil = fun (d : Deduction) -> d.Amount = 0
let editWhenDateIsPresent = fun (d : Deduction) -> d.Date |> Option.isSome
(* Actual UI Specification *)
// Final UI Specification
let (deductionColumns : ColumnSpec<Deduction> list) = [
<@ fun (d) -> d.Date @> , [ showAlways ] , [ editAlways ]
<@ fun (d) -> d.SectionCode @> , [ showForRevised ] , [ editWhenNil ]
<@ fun (d) -> d.Amount @> , [ showForRevised ; showForGovt ] , [ editWhenNil ; editWhenDateIsPresent ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment