Skip to content

Instantly share code, notes, and snippets.

@AngelMunoz
Last active April 15, 2022 14:35
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 AngelMunoz/d89b076c2a345baef4a60e0124170be1 to your computer and use it in GitHub Desktop.
Save AngelMunoz/d89b076c2a345baef4a60e0124170be1 to your computer and use it in GitHub Desktop.
a small script to explore reducible from F# (run with: dotnet fsi ./script.fsx)
#r "nuget: Reducible, 0.2.0"
open Morris.Reducible
type UserReducer =
| EmailDelta of string option
| AgeDelta of int option
| NameDelta of string option
type User =
{ name: string
age: int
email: string option }
module User =
let private userReducer =
Reducer
.Given<User, UserReducer>()
.Then(fun state delta ->
match delta with
| EmailDelta delta ->
if state.email = delta then
Reducer.Result(false, state)
else
Reducer.Result(true, { state with email = delta })
| AgeDelta delta ->
match delta with
| Some delta when delta <> state.age -> Reducer.Result(true, { state with age = delta })
| Some _
| None -> Reducer.Result(false, state)
| NameDelta delta ->
match delta with
| Some delta when delta <> state.name -> Reducer.Result(true, { state with name = delta })
| Some _
| None -> Reducer.Result(false, state))
|> FuncConvert.FromFunc
let UpdateUser user delta = (userReducer user delta).Deconstruct()
let user = { name = "Max"; age = 20; email = None }
match User.UpdateUser user (Some "email@emailor.com" |> EmailDelta) with
| (true, updated) -> printfn $"did update %A{updated}"
| (false, _) -> printfn $"did not update :("
match User.UpdateUser user (AgeDelta(Some 20)) with
| (true, updated) -> printfn $"did update %A{updated}"
| (false, _) -> printfn $"did not update :("
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment