Can I have a devstruct with computed values?

Hey everyone.
I was wondering about how I should initialize a struct where I have some values and some computed values.

For now I came up with:

defmodule Stats do
  defstruct annotations: 0, true_positive: 0,
    false_positive: 0, false_negative: 0,
    precision: 0, recall: 0

  def set_precision(stats \\ %AnnotationStats{}) do
    Map.put(stats, :precision, (stats.true_positive / (stats.true_positive + stats.false_positive)))
  end

  def set_recall(stats \\ %AnnotationStats{}) do
    Map.put(stats, :recall, (stats.true_positive / (stats.true_positive + stats.false_negative)))
  end
end

What I the initialize with

my_stats = %Stats{annotations: 3, true_positive: 1, false_positive: 2, false_negative: 1}
|> set_precision()
|> set_recall()

But is there a cleaner way ? Or the way to do it?

Thank you all :slight_smile: