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

Implement the Add/Div/Mul/Sub blocks #19

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Conversation

AlexSanches1
Copy link
Contributor

No description provided.

@artob artob changed the title Implement Add/Div/Mul/Sub blocks Implement the Add/Div/Mul/Sub blocks Dec 4, 2024
Copy link
Contributor

@SamuelSarle SamuelSarle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM otherwise but I suggest to change the while self.input.recv { ..., self.output.send(&res) } to while self.input.recv { ... } self.output.send(&result.unwrap_or(<default-value>)) with <default-value> being 0.0 for Add and Sub blocks, and 1.0 for Mul and Div blocks.
Reasoning for the defaults comes from the identity elements of addition or multiplication, e.g. adding/subtracting 0 does nothing and multiplying/diving by 1 does nothing and thus they're used as the values when the input set is empty, adding no numbers together equals 0 and multiplying no numbers together equals 1.

Also noting that as a future improvement, it's quite easy to switch these implementations to support any numerical types (f32,f64,u32,u64, etc.) by defining the blocks with generics as such:

pub struct Add<T: Message + core::ops::Add<Output = T> + num_traits::Zero = f64> { /* ... */ }
pub struct Sub<T: Message + core::ops::Sub<Output = T> + num_traits::Zero = f64> { /* ... */ }
pub struct Mul<T: Message + core::ops::Mul<Output = T> + num_traits::One = f64> { /* ... */ }
pub struct Div<T: Message + core::ops::Div<Output = T> + PartialEq + num_traits::One + num_traits::Zero = f64> { /* ... */ }

In summary what those generics are saying is, taking for example the Add block:

  1. Anything that implements Message (so that we can send it over a port)
  2. And implements core::ops::Add (so that we can call x + y on the types)
  3. And implements num_traits::Zero (so that we can call T::zero() for the identity element as the starting value for the summing)
  4. = f64, by default use f64 if nothing else was provided.

Let me know if you want the patch for those changes for this PR, otherwise I'll open another PR after this is merged.

lib/protoflow-blocks/src/blocks/math/add.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/div.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/mul.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/sub.rs Outdated Show resolved Hide resolved
@SamuelSarle SamuelSarle assigned AlexSanches1 and unassigned artob Dec 9, 2024
lib/protoflow-blocks/src/blocks/math/sub.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/mul.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/div.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/add.rs Outdated Show resolved Hide resolved
AlexSanches1 and others added 4 commits December 9, 2024 22:35
Co-authored-by: Samuel Sarle <[email protected]>
Signed-off-by: Oleksandr Yakovenko <[email protected]>
Co-authored-by: Samuel Sarle <[email protected]>
Signed-off-by: Oleksandr Yakovenko <[email protected]>
Co-authored-by: Samuel Sarle <[email protected]>
Signed-off-by: Oleksandr Yakovenko <[email protected]>
Co-authored-by: Samuel Sarle <[email protected]>
Signed-off-by: Oleksandr Yakovenko <[email protected]>
@AlexSanches1 AlexSanches1 assigned artob and unassigned AlexSanches1 Dec 13, 2024
@artob artob added the enhancement New feature or request label Dec 20, 2024
@artob
Copy link
Member

artob commented Jan 27, 2025

@race-of-sloths invite

@race-of-sloths
Copy link

@artob Thank you for calling!

@AlexSanches1 Thank you for the contribution! Join Race of Sloths by simply mentioning me in your comment/PRs description and start collecting Sloth Points through contributions to open source projects.

What is the Race of Sloths

Race of Sloths is a friendly competition where you can participate in challenges and compete with other open-source contributors within your normal workflow

For contributors:

  • Tag @race-of-sloths inside your pull requests
  • Wait for the maintainer to review and score your pull request
  • Check out your position in the Leaderboard
  • Keep weekly and monthly streaks to reach higher positions
  • Boast your contributions with a dynamic picture of your Profile

For maintainers:

  • Score pull requests that participate in the Race of Sloths and receive a reward
  • Engage contributors with fair scoring and fast responses so they keep their streaks
  • Promote the Race to the point where the Race starts promoting you
  • Grow the community of your contributors

Feel free to check our website for additional details!

Bot commands
  • For contributors
    • Include a PR: @race-of-sloths include to enter the Race with your PR
  • For maintainers:
    • Invite contributor @race-of-sloths invite to invite the contributor to participate in a race or include it, if it's already a runner.
    • Assign points: @race-of-sloths score [1/2/3/5/8/13] to award points based on your assessment.
    • Reject this PR: @race-of-sloths exclude to send this PR back to the drawing board.
    • Exclude repo: @race-of-sloths pause to stop bot activity in this repo until @race-of-sloths unpause command is called

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

Successfully merging this pull request may close these issues.

4 participants