-
-
Save BashkaMen/cb3347278ac3fc1ac2e2b5d721b44a0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
type Id = int | |
type TodoValue = | |
| Text of string | |
| Markdown of string | |
| Html of string | |
//type UnCompleteTodo = | |
// { Id: Id | |
// Value: TodoValue } | |
// | |
//type CompletedTodo = | |
// { Id: Id | |
// Value: TodoValue } | |
type Todo = | |
| UnCompleted of {| Id: Id; Value: TodoValue |} | |
| Completed of {| Id: Id; Value: TodoValue |} | |
type Command = | |
| Create of {| Value: string |} | |
| Remove of {| Id: Id |} | |
| Complete of {| Id: Id |} | |
type State = Todo list | |
let r = Random() | |
let update state cmd = | |
let getId = function | |
| UnCompleted s -> s.Id | |
| Completed s -> s.Id | |
let getValue = function | |
| UnCompleted s -> s.Value | |
| Completed s -> s.Value | |
match cmd with | |
| Create c -> state @ [ UnCompleted {| Id = r.Next(10); Value = Text c.Value |} ] | |
| Remove r -> state |> List.filter (fun s -> getId s <> r.Id) | |
| Complete complete -> | |
let found = state |> List.find (fun s -> getId s = complete.Id) | |
let completed = Completed {| Id = getId found; Value = getValue found |} | |
state | |
|> List.filter (fun s -> getId s <> complete.Id) | |
|> List.append [ completed ] | |
let view state = | |
state |> sprintf "STATE: %A" | |
let rec getCmd() = | |
printf "enter cmd: " | |
let input = Console.ReadLine() | |
match input with | |
| "create" -> | |
printf "enter todo value: " | |
Create {| Value = Console.ReadLine() |} | |
| "remove" -> | |
printf "enter todo id for remove: " | |
Remove {| Id = Int32.Parse(Console.ReadLine()) |} | |
| "complete" -> | |
printf "enter todo id for complete: " | |
Complete {| Id = Int32.Parse(Console.ReadLine()) |} | |
| _ -> | |
printfn "invalid command, try again" | |
getCmd() | |
let rec appLoop state = | |
let newState = getCmd() |> update state | |
printfn "%s" (view newState) | |
appLoop newState | |
let initState = list<Todo>.Empty | |
[<EntryPoint>] | |
let main argv = | |
appLoop initState | |
0 // return an integer exit code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.