-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bytes and bits module #50
Comments
Intriguing! The bits/bytes thing is definitely a bit tricky, might ponder that for a while. What would be the name of the module? Might be cleanest to have separate |
If it was two separate modules with |
Hmm right, good point, I clearly didn't think that one all the way through! Can you think of a name for a single module that would work and be unlikely to conflict with other modules? Something like |
I think just |
Unfortunately |
(which I should have thought of when I proposed it originally...) |
Good point. Another option is Edit: Just to be sure I checked and neither module causes a naming conflict https://klaftertief.github.io/elm-search/?q=module%3AByte |
It occurs to me we might be able to be 'cheat' a bit by exploiting the fact that 1000 is evenly divisible by 8, which means that an integer number of kilobits (or megabits, gigabits etc.) is in fact an integer number of bytes. So we could just use kilobits : number -> Quantity number Bytes
kilobits numKilobits =
bytes (125 * numKilobits)
kibibits : number -> Quantity number Bytes
kibibits numKibibits =
bytes (128 * numKibibits) In that case the only big question might be what the signature of -- Kind of weird but I'm guessing 'number of bits'
-- will usually be an integer
bits : Int -> Quantity Float Bytes
-- A bit more normal but might require some
-- pointless-seeming 'toFloat' calls
bits : Float -> Quantity Float Bytes
-- Not implementable as far as I can see
bits : number -> Quantity Float Bytes |
The issue with not having a |
Hmm yeah, I guess with binary file/network formats it would be pretty common to need to offset things by an integer numbers of bits. Back to the drawing board... |
One more thought: even with the ability to specify an integer number of bits, something like BinarySize.bits 12345 |> Quantity.per Duration.second would still give you you a type alias BitsPerSecond =
Rate Bits Seconds
bitsPerSecond : number -> Quantity number BitsPerSecond |
Currently I've been converting from raw ints to |
Could Obviously it would be a breaking change so it won't be something to do right this moment but maybe at some point in the future? |
Could potentially do that but I don't think it would really help, since BinarySize.bits 5 |> Quantity.per (Duration.seconds 3) to return a Philosophically, I'm also a bit reluctant to have things like |
What about only having a module Bits exposing
( Bits
, bits
, inBits
, bytes
, inBytes
, kilobits
, inKilobits
, ...
)
type Bits
= Bits
bits : number -> Quantity number Bits
inBits : Quantity number Bits -> number
bytes : number -> Quantity number Bits
inBytes : Quantity Float Bits -> Float
kilobits : number -> Quantity number Bits
inKilobits : Quantity Float Bits -> Float
-- etc. I guess the main question is then whether there are cases where it's crucial to track with the type system that a particular ceil (Bits.inBytes messageSize) to get the size in bytes of a particular Protobuf message or something. |
Suppose someone is creating an AWS API and one of the endpoints requires the user to specify how large some resource should be. The API for it would look something like this |
I guess maybe there isn't a good solution and it's better to not have an official Bytes/Bits module. Instead people will need to make a binary unit that suits their use case |
I agree, I think there probably is enough trickiness here that I'd be a bit hesitant to immediately add an 'official' module to
Thoughts? |
I think a package for only this is too small. The risk I see is that people won't bother installing it because it's quick to implement themselves so they won't think to look for it, or won't want to need to install yet another dependency.
This could be a way to go. Especially if there's other stuff in it such as text formatting for units. That said, what does the process look like for moving module from |
Good point! I think the best approach would be to prefix the modules like |
I wanted to represent bitrate, aka bits per second, when I noticed there isn't a module for bits. I guess there would be two units,
Bits
andBytes
, with helper functions to convert between them. The reason for this is that you'll almost always work with integer amounts of either one or the other.The module might look something like this (plus wikipedia links to explain the difference between "kilo" and "kibi")
The text was updated successfully, but these errors were encountered: