Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Flight-School/Rate
A type-safe ratio of Foundation unit quantities
https://github.com/Flight-School/Rate
conversion dimensional-analysis foundation measurements numbers rate swift units
Last synced: 3 months ago
JSON representation
A type-safe ratio of Foundation unit quantities
- Host: GitHub
- URL: https://github.com/Flight-School/Rate
- Owner: Flight-School
- License: mit
- Created: 2018-07-28T13:22:27.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-11-09T19:42:44.000Z (about 4 years ago)
- Last Synced: 2024-07-04T22:14:30.047Z (4 months ago)
- Topics: conversion, dimensional-analysis, foundation, measurements, numbers, rate, swift, units
- Language: Swift
- Homepage: https://flight.school/books/numbers
- Size: 24.4 KB
- Stars: 68
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Rate
[![Build Status][build status badge]][build status]
A ratio of two related quantities,
expressed in terms of an amount of numerator unit per single denominator unit.This functionality is discussed in Chapter 5 of
[Flight School Guide to Swift Numbers](https://flight.school/books/numbers).## Requirements
- Swift 4.0+
## Installation
### Swift Package Manager
Add the Rate package to your target dependencies in `Package.swift`:
```swift
import PackageDescriptionlet package = Package(
name: "YourProject",
dependencies: [
.package(
url: "https://github.com/Flight-School/Rate",
from: "1.0.0"
),
]
)
```Then run the `swift build` command to build your project.
### Carthage
To use Rate in your Xcode project using Carthage,
specify it in `Cartfile`:```
github "Flight-School/Rate" ~> 1.0.0
```Then run the `carthage update` command to build the framework,
and drag the built Rate.framework into your Xcode project.## Usage
One of the shortcomings of the Foundation Unit and Measurement APIs
is the inability to dynamically declare compound units.
This can make it difficult to perform dimensional analysis
and other multi-step calculations.The `Rate` structure allows you to express the ratio
between two units in a type-safe manner.
Multiplying a measurement with one unit type by a rate
whose denominator is that same unit type causes those types to cancel out,
resulting in a measurement with the numerator type.For example, volume over time multiplied by time yields volume:
```swift
let flowRate = Rate(value: 84760,
unit: .cubicFeet,
per: .seconds)
let oneDay = Measurement(value: 24, unit: .hours)(flowRate * oneDay).converted(to: .megaliters) // 207371ML
```## License
MIT
## Contact
Mattt ([@mattt](https://twitter.com/mattt))
[build status]: https://github.com/Flight-School/Rate/actions?query=workflow%3ACI
[build status badge]: https://github.com/Flight-School/Rate/workflows/CI/badge.svg