Help refactoring 1 line of code

This code compiles. I need help with making it more elegant via let###.

  { model; 
    inject = fun x -> 
      Effect.bind (inject x) ~f:(fun _ -> Effect.return @@ after x )
  }

Code Context

type t = { model : Model.t; inject : Action.t list -> unit Effect.t }
after: Action.t list -> ()

Informal Context

I’m using Bonsai. I’m trying to allow the component constructor to take an extra argument
(after: Action.t list -> () to be run after every inject call.

Let actions: Action.t list

So we now have something where I need to compile (inject actions) with (after actions).

Thanks!

So I’m trying to write something like:


  { model; 
    inject = fun x -> 
      let open Effect in
      let* _ = inject x in
      f x }



but it is not clear to me how to bind the let* to Effect.bind

You can just bind it :

let (let*) x f = Effect.bind x ~f
1 Like

bind (... return x) is the same as map (... x) (in lawful instances anyway). That’s one refactor, try:

{ model; 
  inject = fun x -> Effect.map (inject x) ~f:(fun _ -> after x)
}
1 Like

A tactic I often use in cases like this is to replace anonymous fun expressions with let bindings to variables named after fields. For example…

let inject x =
  let f _ = after x in
  Effect.map (inject x) ~f
in
{ model; inject }