Switch to module over ring for duration arithmetic

Đang mở
#62 7 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Lĩnh vực
backend

Hướng nghiên cứu

The issue names the Semiring instances for Milliseconds and other duration types, and links the modules-over-rings library. Start by locating those instances and reviewing the linked abstraction. The issue proposes replacing them and creating a vector-spaces-over-fields library for division; done means duration arithmetic follows those abstractions without the wrapping shown in the examples.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

status: needs more info

Currently a Semiring Milliseconds instance exists. However, durations are not a semiring: the closure property of the multiplication operation is not honoured because multiplying two durations gives not a duration but a duration squared.

This is incorrect, and causes awkward code where you need to add/remove nonsensical wrappers just so you can multiply or divide. One example is multiplying a duration by a scalar:

twiceAsLong :: Milliseconds -> Milliseconds
twiceAsLong a = a * Milliseconds 2.0 -- nasty wrap

Another example is where you divide two durations when implementing a multiplication between durations and frequencies where the frequency type is in terms of the reciprocal of the frequency, which is a duration:

newtype Freq = Freq Milliseconds

-- | The reciprocal of a frequency is a duration. (Note how 1/ms = kHz.)
reciprocal :: Freq -> Milliseconds
reciprocal (Freq r) = r

-- | Multiplying a duration (ms) by a frequency (1/ms) gives a number.
multiply :: Milliseconds -> Freq -> Number
multiply d f = un Milliseconds (d / reciprocal f) -- nasty unwrap

With the right abstraction, no wrapping and unwrapping would be necessary, because the types would just work out. We should remove the Semiring Milliseconds instance (and the instances for other duration types) and switch to modules over rings instead, which model this correctly. To add division we should make a library for vector spaces over fields.

Modules over rings and vector spaces over fields are generally useful for types that represent quantities, such as durations, distances, and file sizes.

Ngôn ngữ chính
PureScript
Star
51
Fork
48
Merge trung bình
2 ngày 23 giờ
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của purescript/purescript-datetime

Tất cả issue của purescript/purescript-datetime

Issue tương tự

Thêm issue về Backend & API Design

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.