From 8fe17d66fa0da5a409dee2d110c3a71476b1c4d7 Mon Sep 17 00:00:00 2001 From: Max Wilson Date: Sun, 3 Mar 2024 18:44:16 -0800 Subject: [PATCH] Allow interactively revising both update and view as we play --- scratch/GuessNumber.fsx | 10 ++++++++-- scratch/SketchUI.fsx | 15 +++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/scratch/GuessNumber.fsx b/scratch/GuessNumber.fsx index 2da6b4f..2cc3ed5 100644 --- a/scratch/GuessNumber.fsx +++ b/scratch/GuessNumber.fsx @@ -19,9 +19,15 @@ let update msg model = { model with guesses = model.guesses @ [feedback] } let view model = $"You've won {model.wins} times. What you know about this next number: {model.guesses}" -let send = connect init update view +let send, state = connect init update view send 10 send 5 send 3 -send 4 \ No newline at end of file +send 4 + +let view2 model = + let fullGuesses = model.guesses |> List.map (sprintf "%A") |> String.concat ", " + $"Score: {model.wins}. Hints: {fullGuesses}" +let send, state = reconnect state update view2 +send 5 diff --git a/scratch/SketchUI.fsx b/scratch/SketchUI.fsx index 3e3d6e1..946626a 100644 --- a/scratch/SketchUI.fsx +++ b/scratch/SketchUI.fsx @@ -3,8 +3,15 @@ #endif let connect init update view = - let mutable current = init() + let state = ref (init()) let dispatch msg = - current <- update msg current - printfn "\n%s" (view current) - dispatch + state.Value <- update msg state.Value + printfn "\n%s" (view state.Value) + dispatch, state + +let reconnect (state: _ ref) update view = + printfn "\n[Reconnecting...]\n%s" (view state.Value) + let dispatch msg = + state.Value <- update msg state.Value + printfn "\n%s" (view state.Value) + dispatch, state