Swift For Any Measure: Simplified

Related tags

Utility anymeasure
Overview

AnyMeasure

Swift For Any Measure: Simplified A clean, Swift interface for Foundation.Measurement

Read my post about it here.

Requirements: Swift 4.0+

Motivation

The idea of type-safe numeric values, in other words, Measurements sounds great but, alas, the ergonomics are poor. At least until now. Wouldn't it be great to do this

    let m: Mass = 123(.kilograms) + 17(.stones)
    // or let m = Mass(123, .kilograms) + 17(.stones)
    m.converted(to: .pounds) // 509.1688786398349 lb

instead of this?

    let m = Measurement(value: 123, unit: UnitMass.kilograms)
    let m2 = m + Measurement(value: 17, unit: UnitMass.stones)
    print (m2.converted(to: .pounds)) // 509.1688786398349 lb

In addition to its verboseness, the Foundation Unit and Measurement APIs also lack the ability to dynamically declare compound units. This can make it difficult to perform dimensional analysis and other multi-step calculations.

The Ratio 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.

Usage

For example, volume over time multiplied by time yields volume:

    // Ratio of Measures
    typealias FlowRate = Ratio<UnitVolume, UnitDuration>
    
    let rate: FlowRate = 84760(.cubicFeet, per: .seconds)
    let dailyFlow = (rate * 24(.hours)).converted(to: .megaliters)
        
    // -> ~ 207371ML

Installation

Swift Package Manager

Add the Rate package to your target dependencies in Package.swift:

import PackageDescription

let package = Package(
  name: "YourProject",
  dependencies: [
    .package(
        url: "https://github.com/wildthink/anymeasure",
        from: "1.0.0"
    ),
  ]
)

Then run the swift build command to build your project.

Contact

License Information

MIT - See LICENSE.md

Credits

Thanks to

You might also like...
Cross-Platform, Protocol-Oriented Programming base library to complement the Swift Standard Library. (Pure Swift, Supports Linux)

SwiftFoundation Cross-Platform, Protocol-Oriented Programming base library to complement the Swift Standard Library. Goals Provide a cross-platform in

Swift - ✏️Swift 공부 저장소✏️

Swift 스위프트의 기초 1. Swift의 기본 2. 변수와 상수 [3. 데이터 타입 기본] [4. 데이터 타입 고급] 5. 연산자 6. 흐름 제어 7. 함수 8. 옵셔널 객체지향 프로그래밍과 스위프트 9. 구조체와 클래스 10. 프로퍼티와 메서드 11. 인스턴스 생

Swift-ndi - Swift wrapper around NewTek's NDI SDK

swift-ndi Swift wrapper around NewTek's NDI SDK. Make sure you extracted latest

__.swift is a port of Underscore.js to Swift.

__.swift Now, __.swift is version 0.2.0! With the chain of methods, __.swift became more flexible and extensible. Documentation: http://lotz84.github.

SNTabBarDemo-Swift - Cool TabBar With Swift
SNTabBarDemo-Swift - Cool TabBar With Swift

SNTabBarDemo-Swift Cool TabBar How To Use // MARK: - setup private func setu

Swift-when - Expression switch support in Swift

Swift When - supporting switch expressions in Swift! What is it? Basically, it a

Swift-compute-runtime - Swift runtime for Fastly Compute@Edge

swift-compute-runtime Swift runtime for Fastly Compute@Edge Getting Started Crea

Swift-HorizontalPickerView - Customizable horizontal picker view component written in Swift for UIKit/iOS

Horizontal Picker View Customizable horizontal picker view component written in

swift-highlight a pure-Swift data structure library designed for server applications that need to store a lot of styled text

swift-highlight is a pure-Swift data structure library designed for server applications that need to store a lot of styled text. The Highlight module is memory-efficient and uses slab allocations and small-string optimizations to pack large amounts of styled text into a small amount of memory, while still supporting efficient traversal through the Sequence protocol.

Releases(1.0.0)
Owner
Jason Jobe
Jason Jobe
⏲ A tiny package to measure code execution time. Only 20 lines of code.

Measure ⏲ A tiny package to measure code execution time. Measure.start("create-user") let user = User() Measure.finish("create-user") Console // ⏲ Mea

Mezhevikin Alexey 3 Oct 1, 2022
A Swift Encoder for encoding any Encodable value into an array of URLQueryItem.

URLQueryItemEncoder A Swift Encoder for encoding any Encodable value into an array of URLQueryItem. As part of the SE-0166, Swift has a foundation for

Pitiphong Phongpattranont 60 Aug 21, 2022
Project shows how to unit test asynchronous API calls in Swift using Mocking without using any 3rd party software

UnitTestingNetworkCalls-Swift Project shows how to unit test asynchronous API ca

Gary M 0 May 6, 2022
Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

Mathias Quintero 9 Sep 25, 2022
Functional data types and functions for any project

Swiftx Swiftx is a Swift library containing functional abstractions and extensions to the Swift Standard Library. Swiftx is a smaller and simpler way

TypeLift 219 Aug 30, 2022
StoredIn is a simple property wrapper library to store any value in anywhere

StoredIn StoredIn is a simple property wrapper library to store any value in anywhere. Installation Please use the Swift Package Manager. dependencies

Henrique Sasaki Yuya 2 Jul 4, 2022
BCSwiftTor - Opinionated pure Swift controller for Tor, including full support for Swift 5.5 and Swift Concurrency

BCSwiftTor Opinionated pure Swift controller for Tor, including full support for

Blockchain Commons, LLC — A “not-for-profit” benefit corporation 4 Oct 6, 2022
Swift Markdown is a Swift package for parsing, building, editing, and analyzing Markdown documents.

Swift Markdown is a Swift package for parsing, building, editing, and analyzing Markdown documents.

Apple 2k Dec 28, 2022
Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer documentation.

Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer docum

Apple 833 Jan 3, 2023