realworldocaml/book

Variants Chapter

Closed this issue · 0 comments

https://github.com/realworldocaml/book/tree/master/book/variants

The code in question is implemented by folding over the list of messages, where the accumulator is a pair of:

  • The set of session identifiers for the user that have been seen thus far
  • The set of messages so far that are associated with the user

Should be:

The code in question is implemented by folding over the list of messages, where the accumulator is a pair of:

- The list of messages so far that are associated with the user
- The set of session identifiers for the user that have been seen thus far
# let messages_for_user user messages =
    let (user_messages,_) =
      List.fold messages ~init:([], Set.empty (module String))
        ~f:(fun ((messages,user_sessions) as acc) message ->
          match message with
          | Logon m ->
            if String.(m.user = user) then
              (message::messages, Set.add user_sessions m.session_id)
            else acc
          | Heartbeat _ | Log_entry _ ->
            let session_id = match message with
              | Logon     m -> m.session_id
              | Heartbeat m -> m.session_id
              | Log_entry m -> m.session_id
            in
            if Set.mem user_sessions session_id then
              (message::messages,user_sessions)
            else acc
        )
    in
    List.rev user_messages;;
val messages_for_user : string -> client_message list -> client_message list =
  <fun>