#higher-order #traits #init #map #array

nightly no-std higher_order_functions

A small collection of traits for implementing higher order functions

4 releases

Uses new Rust 2021

0.2.0 Jun 27, 2022
0.1.2 Apr 4, 2021
0.1.1 Sep 8, 2020
0.1.0 Sep 2, 2020

#6 in #init

Download history 25/week @ 2022-06-03 5/week @ 2022-06-10 8/week @ 2022-06-17 32/week @ 2022-06-24 18/week @ 2022-07-01 14/week @ 2022-07-08 21/week @ 2022-07-15 14/week @ 2022-07-22 14/week @ 2022-07-29 11/week @ 2022-08-05 9/week @ 2022-08-12 14/week @ 2022-08-19 9/week @ 2022-08-26 10/week @ 2022-09-02 22/week @ 2022-09-09 7/week @ 2022-09-16

54 downloads per month
Used in 2 crates

MIT license

15KB
174 lines

higher_order_functions

Crate

Documentation

Repository

Changelog

A small collection of traits for implementing higher order functions.

Init:

use higher_order_functions::Init;

struct House { number: usize }

// [T; N]: Init<T, usize>
let road = <[House; 3]>::init(|i| House { number: i + 1 });

assert_eq!(road[0].number, 1);
assert_eq!(road[1].number, 2);
assert_eq!(road[2].number, 3);

Map:

use higher_order_functions::Map;

let arr = [1, 4, 6, -3, 6].map(|x| x * 2);

assert_eq!(arr, [2, 8, 12, -6, 12]);
use higher_order_functions::Map;

let arr = [1, 4, 6, -3, 6].map(f64::from);

assert_eq!(arr, [1.0, 4.0, 6.0, -3.0, 6.0]);

Zip:

use higher_order_functions::Zip;

let a = [1, 2, 3];
let b = ["a", "b", "c"];

let arr = a.zip_with(b, |ax, bx| (ax, bx));

assert_eq!(arr, [(1, "a"), (2, "b"), (3, "c")]);
use higher_order_functions::Zip;

let a = [1, 2, 3];
let b = [4, 5, 6];

let arr = a.zip_with(b, |ax, bx| ax * bx);

assert_eq!(arr, [4, 10, 18]);

Section:

use higher_order_functions::Section;

let a: [u32; 8] = [1, 2, 3, 4, 5, 6, 7, 8];

let arr: [u32; 4] = a.section(3); // Extracts 4 elements starting at a[3]

assert_eq!(arr, [4, 5, 6, 7]);

To use this, add it as a dependency to your Cargo.toml:

[dependencies]
higher_order_functions = "^0.2.0"

No runtime deps