<commit_before>module BarkSpider.Update (..) where

import BarkSpider.Actions exposing (..)
import BarkSpider.Model exposing (ID, Model, SimulationResults)
import BarkSpider.Comms exposing (runSimulation)
import BarkSpider.Simulation as Sim
import BarkSpider.Util exposing (noFx)
import Effects exposing (Effects)
import Http
import List
import List.Extra exposing (removeWhen)
import Result exposing (Result)
import Task

{-| Update the simulation parameters by ID based on an Action.
 -}
updateModify : ID -> Sim.Action -> Model -> Model
updateModify id action model =
  let
    modifySimulation ( simId, sim ) =
      if simId == id then
        ( simId, Sim.update action sim )
      else
        ( simId, sim )

    matchId ( simId, sim ) =
      simId == id

    sims =
      case action of
        Sim.Delete ->
          removeWhen matchId model.simulations

        _ ->
          List.map modifySimulation model.simulations
  in
    { model | simulations = sims }


addSimulation : Model -> Sim.Simulation -> Model
addSimulation model sim =
  { model
    | simulations = model.simulations ++ [ ( model.next_id, sim ) ]
    , next_id = model.next_id + 1
  }


clearSimulationResults : Model -> Model
clearSimulationResults model =
  { model
    | results = []
  }


runSimulations : Model -> Effects Action
runSimulations model =
  let
    sims =
      List.map snd model.simulations
        |> List.filter .included
  in
    List.map (runSimulation >> Task.toResult) sims
      |> Task.sequence
      |> Task.map NewResults
      |> Effects.task


handleNewResult : Result Http.Error SimulationResults -> Model -> Model
handleNewResult result model =
  case result of
    Ok r ->
      { model
        | results = r :: model.results
      }

    Err error ->
      { model
        | error_messages = (toString error) :: model.error_messages
      }


update : Action -> Model -> ( Model, Effects Action )
update action model =
  case action of
    Modify index action ->
      updateModify index action model
        |> noFx

    AddSimulation sim ->
      addSimulation model sim
        |> noFx

    RunSimulation ->
      ( clearSimulationResults model
      , runSimulations model<commit_msg>Added more docstrings.<commit_after>module BarkSpider.Update (..) where

import BarkSpider.Actions exposing (..)
import BarkSpider.Model exposing (ID, Model, SimulationResults)
import BarkSpider.Comms exposing (runSimulation)
import BarkSpider.Simulation as Sim
import BarkSpider.Util exposing (noFx)
import Effects exposing (Effects)
import Http
import List
import List.Extra exposing (removeWhen)
import Result exposing (Result)
import Task

{-| Update the simulation parameters by ID based on an Action.
 -}
updateModify : ID -> Sim.Action -> Model -> Model
updateModify id action model =
  let
    modifySimulation ( simId, sim ) =
      if simId == id then
        ( simId, Sim.update action sim )
      else
        ( simId, sim )

    matchId ( simId, sim ) =
      simId == id

    sims =
      case action of
        Sim.Delete ->
          removeWhen matchId model.simulations

        _ ->
          List.map modifySimulation model.simulations
  in
    { model | simulations = sims }

{-| Add a simulation to a model using the next available ID.
-}
addSimulation : Model -> Sim.Simulation -> Model
addSimulation model sim =
  { model
    | simulations = model.simulations ++ [ ( model.next_id, sim ) ]
    , next_id = model.next_id + 1
  }

{-| Remove all simulation results from a model.
-}
clearSimulationResults : Model -> Model
clearSimulationResults model =
  { model
    | results = []
  }

{-| Launch all simulations in a model as Effects that come back as NewResults
actions.
-}
runSimulations : Model -> Effects Action
runSimulations model =
  let
    sims =
      List.map snd model.simulations
        |> List.filter .included
  in
    List.map (runSimulation >> Task.toResult) sims
      |> Task.sequence
      |> Task.map NewResults
      |> Effects.task

{-| Append simulation results (or errors) to a model.
-}
handleNewResult : Result Http.Error SimulationResults -> Model -> Model
handleNewResult result model =
  case result of
    Ok r ->
      { model
        | results = r :: model.results
      }

    Err error ->
      { model
        | error_messages = (toString error) :: model.error_messages
      }

{-| Update a model and/or launch effects based on an action.
-}
update : Action -> Model -> ( Model, Effects Action )
update action model =
  case action of
    Modify index action ->
      updateModify index action model
        |> noFx

    AddSimulation sim ->
      addSimulation model sim
        |> noFx

    RunSimulation ->
      ( clearSimulationResults model
      , runSimulations model
{'fraction_non_alphanumeric': 0.08603964571910586, 'fraction_numerical': 0.00042176296921130323, 'mean_word_length': 2.590461771385314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, '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': '1196799', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1329, 'n_words': 443}