1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
use melodium_core::*;
use melodium_macro::{check, mel_function, mel_treatment};
/// Return absolute value.
///
/// If the absolute value cannot fit into the type itself (such as `i8` `-128` that cannot be turned into `128`), none value is returned.
#[mel_function(
generic N (Signed)
)]
pub fn abs(value: N) -> Option<N> {
value.signed_abs()
}
/// Give the absolute values of a stream.
///
/// If the absolute value cannot fit into the type itself (such as `i8` `-128` that cannot be turned into `128`), none value is returned.
#[mel_treatment(
generic N (Signed)
input value Stream<N>
output abs Stream<Option<N>>
)]
pub async fn abs() {
while let Ok(values) = value
.recv_many()
.await
.map(|values| Into::<VecDeque<Value>>::into(values))
{
check!(
abs.send_many(TransmissionValue::Other(
values
.into_iter()
.map(|val| val.signed_abs().into())
.collect()
))
.await
)
}
}
/// Return number representing sign of given value.
///
/// - `0` if number is 0,
/// - `-1` if number is negative,
/// - `1` if number is positive.
///
/// ℹ️ For floating types (`f32` and `f64`), `NaN` values gives `NaN` output, `+INFINITY` and `+0.0` gives `1`, `-INFINITY` and `-0.0` gives `-1`.
#[mel_function(
generic N (Signed)
)]
pub fn signum(value: N) -> N {
value.signed_signum()
}
/// Gives numeric sign of a stream.
///
/// - `0` if number is 0,
/// - `-1` if number is negative,
/// - `1` if number is positive.
///
/// ℹ️ For floating types (`f32` and `f64`), `NaN` values gives `NaN` output, `+INFINITY` and `+0.0` gives `1`, `-INFINITY` and `-0.0` gives `-1`.
#[mel_treatment(
generic N (Signed)
input value Stream<N>
output sign Stream<N>
)]
pub async fn signum() {
while let Ok(values) = value
.recv_many()
.await
.map(|values| Into::<VecDeque<Value>>::into(values))
{
check!(
sign.send_many(TransmissionValue::Other(
values.into_iter().map(|val| val.signed_signum()).collect()
))
.await
)
}
}
/// Tells if a value is positive.
///
/// Returns `true` for strictly positive integers, and `false` for `0` and negative ones.
/// ℹ️ For floating types (`f32` and `f64`), `NaN` values gives `false`, `+INFINITY` and `+0.0` gives `true`.
#[mel_function(
generic N (Signed)
)]
pub fn is_positive(value: N) -> bool {
value.signed_is_positive()
}
/// Tells if a stream contains positive values.
///
/// Output `true` for strictly positive integers, and `false` for `0` and negative ones.
/// ℹ️ For floating types (`f32` and `f64`), `NaN` values gives `false`, `+INFINITY` and `+0.0` gives `true`.
#[mel_treatment(
generic N (Signed)
input value Stream<N>
output positive Stream<bool>
)]
pub async fn is_positive() {
while let Ok(values) = value
.recv_many()
.await
.map(|values| Into::<VecDeque<Value>>::into(values))
{
check!(
positive
.send_many(TransmissionValue::Bool(
values
.into_iter()
.map(|val| val.signed_is_positive().into())
.collect()
))
.await
)
}
}
/// Tells if a value is negative.
///
/// Returns `true` for strictly negative integers, and `false` for `0` and positive ones.
/// ℹ️ For floating types (`f32` and `f64`), `NaN` values gives `false`, `-INFINITY` and `-0.0` gives `true`.
#[mel_function(
generic N (Signed)
)]
pub fn is_negative(value: N) -> bool {
value.signed_is_negative()
}
/// Tells if a stream contains negative values.
///
/// Output `true` for strictly negative integers, and `false` for `0` and positive ones.
/// ℹ️ For floating types (`f32` and `f64`), `NaN` values gives `false`, `-INFINITY` and `-0.0` gives `true`.
#[mel_treatment(
generic N (Signed)
input value Stream<N>
output negative Stream<bool>
)]
pub async fn is_negative() {
while let Ok(values) = value
.recv_many()
.await
.map(|values| Into::<VecDeque<Value>>::into(values))
{
check!(
negative
.send_many(TransmissionValue::Bool(
values
.into_iter()
.map(|val| val.signed_is_negative().into())
.collect()
))
.await
)
}
}
/// Add `a` and `b`
///
/// This function is infaillible but may overflow if `a + b` is out of bounds for the data type.
#[mel_function(
generic N (Add)
)]
pub fn add(a: N, b: N) -> N {
a.add(&b)
}
/// Add values from two streams.
///
/// Values passed through `a` & `b` are added and send in `sum`.
/// This treatment is infaillible but output may overflow if `a + b` is out of bounds for the data type.
#[mel_treatment(
generic N (Add)
input a Stream<N>
input b Stream<N>
output sum Stream<N>
)]
pub async fn add() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(sum.send_one(a.add(&b)).await)
}
}
/// Add `a` and `b`, checking if overflow occurs
///
/// This function returns an option containing `a + b`, or none if result cause overflow in data type.
#[mel_function(
generic N (CheckedAdd)
)]
pub fn checked_add(a: N, b: N) -> Option<N> {
a.checked_add(&b)
}
/// Add values from two streams, checking if overflow occurs.
///
/// Values passed through `a` & `b` are added and send in `sum`.
/// This treatment outputs an option containing `a + b`, or none if result cause overflow in data type.
#[mel_treatment(
generic N (CheckedAdd)
input a Stream<N>
input b Stream<N>
output sum Stream<Option<N>>
)]
pub async fn checked_add() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(sum.send_one(a.checked_add(&b).into()).await)
}
}
/// Add `a` and `b`, saturating to bounds.
///
/// This function is infaillible and saturate to the closest bound, minimal or maximal, if `a + b` is out of bounds for the data type.
#[mel_function(
generic N (SaturatingAdd)
)]
pub fn saturating_add(a: N, b: N) -> N {
a.saturating_add(&b)
}
/// Add values from two streams, saturating to bounds.
///
/// Values passed through `a` & `b` are added and send in `sum`.
/// This treatment is infaillible and saturate to the closest bound, minimal or maximal, if `a + b` is out of bounds for the data type.
#[mel_treatment(
generic N (SaturatingAdd)
input a Stream<N>
input b Stream<N>
output sum Stream<N>
)]
pub async fn saturating_add() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(sum.send_one(a.saturating_add(&b)).await)
}
}
/// Add `a` and `b`, wrapping on bounds.
///
/// This function is infaillible and wrap if `a + b` reach boundary of the data type.
#[mel_function(
generic N (WrappingAdd)
)]
pub fn wrapping_add(a: N, b: N) -> N {
a.wrapping_add(&b)
}
/// Add values from two streams, wrapping on bounds.
///
/// Values passed through `a` & `b` are added and send in `sum`.
/// This treatment is infaillible and wrap if `a + b` reach boundary of the data type.
#[mel_treatment(
generic N (WrappingAdd)
input a Stream<N>
input b Stream<N>
output sum Stream<N>
)]
pub async fn wrapping_add() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(sum.send_one(a.wrapping_add(&b)).await)
}
}
/// Sustract `b` from `a`
///
/// This function is infaillible but may overflow if `a - b` is out of bounds for the data type.
#[mel_function(
generic N (Sub)
)]
pub fn sub(a: N, b: N) -> N {
a.sub(&b)
}
/// Substract values from two streams.
///
/// Values passed through `b` are substracted to `a` and send in `diff`.
/// This treatment is infaillible but output may overflow if `a - b` is out of bounds for the data type.
#[mel_treatment(
generic N (Add)
input a Stream<N>
input b Stream<N>
output diff Stream<N>
)]
pub async fn sub() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(diff.send_one(a.sub(&b)).await)
}
}
/// Sustract `b` from `a`, checking if overflow occurs
///
/// This function returns an option containing `a - b`, or none if result cause overflow in data type.
#[mel_function(
generic N (CheckedSub)
)]
pub fn checked_sub(a: N, b: N) -> Option<N> {
a.checked_sub(&b)
}
/// Substract values from two streams, checking if overflow occurs.
///
/// Values passed through `b` are substracted to `a` and send in `diff`.
/// This treatment outputs an option containing `a - b`, or none if result cause overflow in data type.
#[mel_treatment(
generic N (CheckedSub)
input a Stream<N>
input b Stream<N>
output diff Stream<Option<N>>
)]
pub async fn checked_sub() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(diff.send_one(a.checked_sub(&b).into()).await)
}
}
/// Sustract `b` from `a`, saturating to bounds.
///
/// This function is infaillible and saturate to the closest bound, minimal or maximal, if `a - b` is out of bounds for the data type.
#[mel_function(
generic N (SaturatingSub)
)]
pub fn saturating_sub(a: N, b: N) -> N {
a.saturating_sub(&b)
}
/// Substract values from two streams, saturating to bounds.
///
/// Values passed through `b` are substracted to `a` and send in `diff`.
/// This treatment is infaillible and saturate to the closest bound, minimal or maximal, if `a - b` is out of bounds for the data type.
#[mel_treatment(
generic N (SaturatingSub)
input a Stream<N>
input b Stream<N>
output diff Stream<N>
)]
pub async fn saturating_sub() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(diff.send_one(a.saturating_sub(&b)).await)
}
}
/// Sustract `b` from `a`, wrapping on bounds.
///
/// This function is infaillible and wrap if `a - b` reach boundary of the data type.
#[mel_function(
generic N (WrappingSub)
)]
pub fn wrapping_sub(a: N, b: N) -> N {
a.wrapping_sub(&b)
}
/// Substract values from two streams, wrapping on bounds.
///
/// Values passed through `b` are substracted to `a` and send in `diff`.
/// This treatment is infaillible and wrap if `a - b` reach boundary of the data type.
#[mel_treatment(
generic N (WrappingSub)
input a Stream<N>
input b Stream<N>
output diff Stream<N>
)]
pub async fn wrapping_sub() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(diff.send_one(a.wrapping_sub(&b)).await)
}
}
/// Multiply `a` and `b`
///
/// This function is infaillible but may overflow if `a × b` is out of bounds for the data type.
#[mel_function(
generic N (Mul)
)]
pub fn mul(a: N, b: N) -> N {
a.mul(&b)
}
/// Multiply values from two streams.
///
/// Values passed through `a` & `b` are multiplied and send in `prod`.
/// This treatment is infaillible but output may overflow if `a × b` is out of bounds for the data type.
#[mel_treatment(
generic N (Mul)
input a Stream<N>
input b Stream<N>
output prod Stream<N>
)]
pub async fn mul() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(prod.send_one(a.mul(&b)).await)
}
}
/// Multiply `a` and `b`, checking if overflow occurs
///
/// This function returns an option containing `a × b`, or none if result cause overflow in data type.
#[mel_function(
generic N (CheckedMul)
)]
pub fn checked_mul(a: N, b: N) -> Option<N> {
a.checked_mul(&b)
}
/// Multiply values from two streams, checking if overflow occurs.
///
/// Values passed through `a` & `b` are multiplied and send in `prod`.
/// This treatment outputs an option containing `a × b`, or none if result cause overflow in data type.
#[mel_treatment(
generic N (CheckedMul)
input a Stream<N>
input b Stream<N>
output prod Stream<Option<N>>
)]
pub async fn checked_mul() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(prod.send_one(a.checked_mul(&b).into()).await)
}
}
/// Multiply `a` and `b`, saturating to bounds.
///
/// This function is infaillible and saturate to the closest bound, minimal or maximal, if `a × b` is out of bounds for the data type.
#[mel_function(
generic N (SaturatingMul)
)]
pub fn saturating_mul(a: N, b: N) -> N {
a.saturating_mul(&b)
}
/// Multiply values from two streams, saturating to bounds.
///
/// Values passed through `a` & `b` are multiplied and send in `prod`.
/// This treatment is infaillible and saturate to the closest bound, minimal or maximal, if `a × b` is out of bounds for the data type.
#[mel_treatment(
generic N (SaturatingMul)
input a Stream<N>
input b Stream<N>
output prod Stream<N>
)]
pub async fn saturating_mul() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(prod.send_one(a.saturating_mul(&b)).await)
}
}
/// Multiply `a` and `b`, wrapping on bounds.
///
/// This function is infaillible and wrap if `a × b` reach boundary of the data type.
#[mel_function(
generic N (WrappingMul)
)]
pub fn wrapping_mul(a: N, b: N) -> N {
a.wrapping_mul(&b)
}
/// Multiply values from two streams, wrapping on bounds.
///
/// Values passed through `a` & `b` are multiplied and send in `prod`.
/// This treatment is infaillible and wrap if `a × b` reach boundary of the data type.
#[mel_treatment(
generic N (WrappingMul)
input a Stream<N>
input b Stream<N>
output prod Stream<N>
)]
pub async fn wrapping_mul() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(prod.send_one(a.wrapping_mul(&b)).await)
}
}
/// Divide `a` by `b`
///
/// This function is infaillible but may overflow if `a ÷ b` is out of bounds for the data type.
/// ⚠️ For integers, this function returns `0` for divisions by `0`.
/// ℹ️ For floating types, this function return infinity for divisions by `0`.
#[mel_function(
generic N (Div)
)]
pub fn div(a: N, b: N) -> N {
a.div(&b)
}
/// Divide values from two streams.
///
/// Values passed through `a` are divided by `b` and send in `quot`.
/// This treatment is infaillible but may overflow if `a ÷ b` is out of bounds for the data type.
/// ⚠️ For integers, this treatment outputs `0` for divisions by `0`.
/// ℹ️ For floating types, this treatment return infinity for divisions by `0`.
#[mel_treatment(
generic N (Div)
input a Stream<N>
input b Stream<N>
output quot Stream<N>
)]
pub async fn div() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(quot.send_one(a.div(&b)).await)
}
}
/// Divide `a` by `b`, checking if division is possible or if overflow occurs
///
/// This function returns an option containing `a ÷ b`, or none if division is not possible (as division by 0), or result cause overflow in data type.
#[mel_function(
generic N (CheckedDiv)
)]
pub fn checked_div(a: N, b: N) -> Option<N> {
a.checked_div(&b)
}
/// Divide values from two streams, checking if division is possible or if overflow occurs.
///
/// Values passed through `a` are divided by `b` and send in `quot`.
/// This treatment outputs an option containing `a ÷ b`, or none if division is not possible (as division by 0), or result cause overflow in data type.
#[mel_treatment(
generic N (CheckedDiv)
input a Stream<N>
input b Stream<N>
output quot Stream<Option<N>>
)]
pub async fn checked_div() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(quot.send_one(a.checked_div(&b).into()).await)
}
}
/// Gives remainder of `a` divided by `b`
///
/// This function is infaillible but may overflow if `a _mod_ b` is out of bounds for the data type.
/// ⚠️ For integers, this function returns `0` for divisions by `0`.
/// ℹ️ For floating types, this function return not-a-number (`NaN`) for divisions by `0`.
#[mel_function(
generic N (Rem)
)]
pub fn rem(a: N, b: N) -> N {
a.rem(&b)
}
/// Gives remainder of division from two streams.
///
/// Remainder is computed for values passed through `a` divided by `b` and send in `rem`.
/// This treatment is infaillible but may overflow if `a _mod_ b` is out of bounds for the data type.
/// ⚠️ For integers, this treatment outputs `0` for divisions by `0`.
/// ℹ️ For floating types, this treatment return not-a-number (`NaN`) for divisions by `0`.
#[mel_treatment(
generic N (Rem)
input a Stream<N>
input b Stream<N>
output rem Stream<N>
)]
pub async fn rem() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(rem.send_one(a.rem(&b)).await)
}
}
/// Gives remainder of `a` divided by `b`, checking if division is possible or if overflow occurs
///
/// This function returns an option containing `a _mod_ b`, or none if division is not possible (as division by 0), or result cause overflow in data type.
#[mel_function(
generic N (CheckedRem)
)]
pub fn checked_rem(a: N, b: N) -> Option<N> {
a.checked_rem(&b)
}
/// Gives remainder of division from two streams, checking if division is possible or if overflow occurs.
///
/// Remainder is computed for values passed through `a` divided by `b` and send in `rem`.
/// This treatment outputs an option containing `a _mod_ b`, or none if division is not possible (as division by 0), or result cause overflow in data type.
#[mel_treatment(
generic N (CheckedRem)
input a Stream<N>
input b Stream<N>
output rem Stream<Option<N>>
)]
pub async fn checked_rem() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(rem.send_one(a.checked_rem(&b).into()).await)
}
}
/// Return negation of given value
///
/// This function is infaillible but may overflow if `-val` is out of bounds for the data type.
#[mel_function(
generic N (Neg)
)]
pub fn neg(val: N) -> N {
val.neg()
}
/// Give negation of streamed values
///
/// This treatment is infaillible but output may overflow if `-value` is out of bounds for the data type.
#[mel_treatment(
generic N (Neg)
input value Stream<N>
output neg Stream<N>
)]
pub async fn neg() {
while let Ok(values) = value
.recv_many()
.await
.map(|values| Into::<VecDeque<Value>>::into(values))
{
check!(
neg.send_many(TransmissionValue::Other(
values.into_iter().map(|val| val.neg()).collect()
))
.await
)
}
}
/// Return negation of given value, checking if overflow occurs
///
/// This function returns an option containing `-val`, or none if result cause overflow in data type.
#[mel_function(
generic N (CheckedNeg)
)]
pub fn checked_neg(val: N) -> Option<N> {
val.checked_neg()
}
/// Give negation of streamed values, checking if overflow occurs.
///
/// This treatment outputs an option containing `-value`, or none if result cause overflow in data type.
#[mel_treatment(
generic N (CheckedNeg)
input value Stream<N>
output neg Stream<Option<N>>
)]
pub async fn checked_neg() {
while let Ok(values) = value
.recv_many()
.await
.map(|values| Into::<VecDeque<Value>>::into(values))
{
check!(
neg.send_many(TransmissionValue::Other(
values
.into_iter()
.map(|val| val.checked_neg().into())
.collect()
))
.await
)
}
}
/// Return negation of given value, wrapping on bounds.
///
/// This function is infaillible and wrap if `-val` reach boundary of the data type.
#[mel_function(
generic N (WrappingNeg)
)]
pub fn wrapping_neg(val: N) -> N {
val.wrapping_neg()
}
/// Give negation of streamed values, wrapping on bounds.
///
/// This treatment is infaillible and wrap if `-value` reach boundary of the data type.
#[mel_treatment(
generic N (WrappingNeg)
input value Stream<N>
output neg Stream<N>
)]
pub async fn wrapping_neg() {
while let Ok(values) = value
.recv_many()
.await
.map(|values| Into::<VecDeque<Value>>::into(values))
{
check!(
neg.send_many(TransmissionValue::Other(
values
.into_iter()
.map(|val| val.wrapping_neg().into())
.collect()
))
.await
)
}
}
/// Compute exponent of given value
///
/// This function is infaillible but may overflow if `valᵉˣᵖ` is out of bounds for the data type.
#[mel_function(
generic N (Pow)
)]
pub fn pow(val: N, exp: u32) -> N {
val.pow(&exp)
}
/// Compute exponent of streamed values
///
/// This treatment is infaillible but output may overflow if `valueᵉˣᵖ` is out of bounds for the data type.
#[mel_treatment(
generic N (Pow)
input value Stream<N>
output pow Stream<N>
)]
pub async fn pow(exp: u32) {
while let Ok(values) = value
.recv_many()
.await
.map(|values| Into::<VecDeque<Value>>::into(values))
{
check!(
pow.send_many(TransmissionValue::Other(
values.into_iter().map(|val| val.pow(&exp)).collect()
))
.await
)
}
}
/// Compute exponent of given value, checking if overflow occurs
///
/// This function returns an option containing `valᵉˣᵖ`, or none if result cause overflow in data type.
#[mel_function(
generic N (CheckedPow)
)]
pub fn checked_pow(val: N, exp: u32) -> Option<N> {
val.checked_pow(&exp)
}
/// Compute exponent of streamed values, checking if overflow occurs.
///
/// This treatment outputs an option containing `valᵉˣᵖ`, or none if result cause overflow in data type.
#[mel_treatment(
generic N (CheckedPow)
input value Stream<N>
output pow Stream<Option<N>>
)]
pub async fn checked_pow(exp: u32) {
while let Ok(values) = value
.recv_many()
.await
.map(|values| Into::<VecDeque<Value>>::into(values))
{
check!(
pow.send_many(TransmissionValue::Other(
values
.into_iter()
.map(|val| val.checked_pow(&exp).into())
.collect()
))
.await
)
}
}
/// Proceed to euclidian division of `a` by `b`
///
/// This function is infaillible but may overflow if `a ÷ b` is out of bounds for the data type.
/// ⚠️ For integers, this function returns `0` for divisions by `0`.
/// ℹ️ For floating types, this function return infinity for divisions by `0`.
#[mel_function(
generic N (Euclid)
)]
pub fn euclid_div(a: N, b: N) -> N {
a.euclid_div(&b)
}
/// Proceed to euclidian division from two streams.
///
/// Values passed through `a` are divided by `b` and send in `quot`.
/// This treatment is infaillible but may overflow if `a ÷ b` is out of bounds for the data type.
/// ⚠️ For integers, this treatment outputs `0` for divisions by `0`.
/// ℹ️ For floating types, this treatment return infinity for divisions by `0`.
#[mel_treatment(
generic N (Euclid)
input a Stream<N>
input b Stream<N>
output quot Stream<N>
)]
pub async fn euclid_div() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(quot.send_one(a.euclid_div(&b)).await)
}
}
/// Gives euclidian remainder of `a` divided by `b`
///
/// This function is infaillible but may overflow if `a _mod_ b` is out of bounds for the data type.
/// ⚠️ For integers, this function returns `0` for divisions by `0`.
/// ℹ️ For floating types, this function return not-a-number (`NaN`) for divisions by `0`.
#[mel_function(
generic N (Euclid)
)]
pub fn euclid_rem(a: N, b: N) -> N {
a.euclid_rem(&b)
}
/// Gives euclidian remainder of division from two streams.
///
/// Remainder is computed for values passed through `a` divided by `b` and send in `rem`.
/// This treatment is infaillible but may overflow if `a _mod_ b` is out of bounds for the data type.
/// ⚠️ For integers, this treatment outputs `0` for divisions by `0`.
/// ℹ️ For floating types, this treatment return not-a-number (`NaN`) for divisions by `0`.
#[mel_treatment(
generic N (Euclid)
input a Stream<N>
input b Stream<N>
output rem Stream<N>
)]
pub async fn euclid_rem() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(rem.send_one(a.euclid_rem(&b)).await)
}
}
/// Proceed to euclidian division of `a` by `b`, checking if division is possible or if overflow occurs
///
/// This function returns an option containing `a ÷ b`, or none if division is not possible (as division by 0), or result cause overflow in data type.
/// ℹ️ For floating types, this function return infinity for divisions by `0`.
#[mel_function(
generic N (CheckedEuclid)
)]
pub fn checked_euclid_div(a: N, b: N) -> Option<N> {
a.checked_euclid_div(&b)
}
/// Proceed to euclidian division from two streams, checking if division is possible or if overflow occurs.
///
/// Values passed through `a` are divided by `b` and send in `quot`.
/// This treatment outputs an option containing `a ÷ b`, or none if division is not possible (as division by 0), or result cause overflow in data type.
/// ℹ️ For floating types, this function return infinity for divisions by `0`.
#[mel_treatment(
generic N (CheckedEuclid)
input a Stream<N>
input b Stream<N>
output quot Stream<Option<N>>
)]
pub async fn checked_euclid_div() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(quot.send_one(a.checked_euclid_div(&b).into()).await)
}
}
/// Gives euclidian remainder of `a` divided by `b`, checking if division is possible or if overflow occurs
///
/// This function returns an option containing `a _mod_ b`, or none if division is not possible (as division by 0), or result cause overflow in data type.
#[mel_function(
generic N (CheckedEuclid)
)]
pub fn checked_euclid_rem(a: N, b: N) -> Option<N> {
a.checked_euclid_rem(&b)
}
/// Gives euclidian remainder of division from two streams, checking if division is possible or if overflow occurs.
///
/// Remainder is computed for values passed through `a` divided by `b` and send in `rem`.
/// This treatment outputs an option containing `a _mod_ b`, or none if division is not possible (as division by 0), or result cause overflow in data type.
#[mel_treatment(
generic N (CheckedEuclid)
input a Stream<N>
input b Stream<N>
output rem Stream<Option<N>>
)]
pub async fn checked_euclid_rem() {
while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
check!(rem.send_one(a.checked_euclid_rem(&b).into()).await)
}
}