<commit_before><commit_msg>Even more of a DSL (exercise?)<commit_after>module Errors.Simple.Main4 exposing (..)

import Errors.Simple.Msg exposing (Msg(..))
import Errors.Simple.Model as Model exposing (Model)
import Errors.Simple.View as View
import Date exposing (Date)
import Task
-- import Result.Extra as Result

import Html

type alias ModelState = Result Model Model

justModel : ModelState -> Model
justModel soFar =
  case soFar of 
    Ok model -> model
    Err model -> model

constantly : (Model -> a -> ModelState) -> (Model -> a) -> ModelState -> ModelState
constantly handleResult f soFar =
  let
    either model =
      model |> f |> handleResult model
  in
    case soFar of
      Ok model -> either model |> justModel |> Ok 
      Err model -> either model |> justModel |> Err

whenOk : (Model -> a -> ModelState) -> (Model -> a) -> ModelState -> ModelState
whenOk handleResult f soFar =
  case soFar of
    Ok model ->
      model |> f |> handleResult model
    Err model -> Err model

do : Model -> Model -> ModelState
do fail succeed =
  Ok succeed

try : Model -> Maybe Model -> ModelState
try fail maybe =
  case maybe of
    Just succeed -> Ok succeed
    Nothing -> Err fail

finishWith : Cmd Msg -> ModelState -> (Model, Cmd Msg)
finishWith cmd soFar = 
  case soFar of
    Ok model -> (model, cmd)
    Err model -> (model, Cmd.none)
      
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of 
    Like person index -> 
      Ok model
        |> constantly do  Model.incrementClickCount
        |> whenOk try      (Model.incrementWordCountM person index)
        |> whenOk do       (Model.focusOn person)
        |> finishWith     fetchDateCmd  

    ChoosePerson person ->
      Ok model
        |> whenOk try       (Model.focusOnM person)
        |> constantly do   Model.incrementClickCount
        |> finishWith      fetchDateCmd

    LastChange date ->
      ( Model.noteDate date model
      , Cmd.none
      )

main : Program Never Model Msg
main =
  Html.program
    { init = Model.init
    , view = View.view
    , update = update
    , subscriptions = always Sub.none
    }

fetchDateCmd : Cmd Msg
fetchDateCmd = 
  Task.perform LastChange Date.now
{'fraction_non_alphanumeric': 0.09239130434782608, 'fraction_numerical': 0.0004528985507246377, 'mean_word_length': 2.4515625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5871153', 'n_tokens_mistral': 673, 'n_tokens_neox': 642, 'n_words': 235}