Skip to content
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 update or map function to ease bounded number manipulation #1

Closed
hugooliveirad opened this issue Sep 11, 2018 · 3 comments
Closed

Comments

@hugooliveirad
Copy link
Contributor

hugooliveirad commented Sep 11, 2018

Hey! First of all, great work on elm-bounded-number using it, loving it.

It is very common for having a way to map a value of a data structure. Most data structures allow it, such as Maybe, Result, Dict, and so on. It would be great to have an update or map function so that you can more easily chain bounded number operations.

I have added a function similar to this one to one of the projects I'm working on:

mapBounded fn bounded =
    let
        value = Number.Bounded.value bounded
    in
    Number.Bounded.set (fn value) bounded

This let's me refactor things like this:

-operation : Int -> Int
+operation : Bounded Int -> Int
 operation =
-    (*) 20 >> (+) 10
+    Number.Bounded.update <| (*) 20 >> (+) 10

It also makes it easier to refactor from Number.Bounded.inc. Such as: https://github.com/hugobessaa/nanotimers/pull/11/commits/696171908045a3d369a25dd0855ec8eccf474e0e

I'm not sure if the proper name should be either map or update.

What do you think about adding this function? I could send a PR.

@jschomay
Copy link
Owner

jschomay commented Oct 24, 2018 via email

@hugooliveirad
Copy link
Contributor Author

Never feel sorry for delayed replies on open source work 😄

My use case is that I have timers and the amount of seconds left in the timer should never be below 0.

I'll work on a PR and send it your way soon.

@jschomay
Copy link
Owner

Published to 2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants