chain_linq

Crates.iochain_linq
lib.rschain_linq
version0.1.3
sourcesrc
created_at2021-11-14 23:31:20.110765
updated_at2021-11-20 22:14:10.31949
descriptionAn implementation of .NET's LINQ in rust as a declarative macro.
homepage
repositoryhttps://github.com/PROMETHIA-27/chain_linq
max_upload_size
id481906
size14,831
(PROMETHIA-27)

documentation

README

Chain-LINQ

This crate is an implementation of .NET's LINQ query syntax in rust as a declarative macro. The declarative macro is generated by another declarative macro, specifically the branching_parser from my other crate, big_mac.

Available statements closely mirror standard LINQ operations; notably join is missing, as I was unable to find equivalent functionality from iterators.

Generally, each statement maps to an iterator method; here is a list of them, with a description and equivalent method if present:

  • from # in #: selects and names variables from a collection. Maps to into_iter().
  • select #: ends a linq query and returns an iterator. Maps to map().
  • select # into #: partially ends a linq query and puts an iterator into a range variable. Creates a continuation.
  • where #: selects only elements that match a particular criteria. Maps to filter().
  • let # = #: creates a range element. Does not map to a method.
  • collect #: calls .collect().
  • collect # as #: calls .collect with a generic type. Follows select syntax but the as parameter is the destination type.

REQUIRES ITERTOOLS:

  • orderby #: sorts iterator ascending by a criteria. Maps to unstable_sort_by_key().
  • orderby # ascending: sorts iterator ascending by a criteria. Maps to unstable_sort_by_key().
  • orderby # descending: sorts iterator descending by a criteria. Maps to unstable_sort_by_key().rev().
  • group # by #: groups elements into groups based on some criteria and returns the result. Maps to group_by().
  • group # by # into #: groups elements into groups based on some criteria, and then creates a continuation.. Maps to group_by().

For more explanation of how LINQ works, check Microsoft's docs here

Also useful: Keyword breakdowns

Examples

use chain_linq::linq;

let xs = [(3, 1), (2, 2), (1, 3)];

let x = linq!(
    from (x, y) in xs
    let z = x + y
    select z into zs
    from z in zs
    select z * 2
);
use chain_linq::linq;

let xss = [vec!(27, 13, 12), vec!(69), vec!(76, 7, 420)];

let x = linq!(
    from xs in xss
    group xs by xs.len() into gs
    from iter in gs
    from x in iter
    collect x + 1 as Vec<i32>
);
use chain_linq::linq;

let xss = [vec!(27, 13, 12), vec!(69), vec!(76, 7, 420)];

let x = linq!(
    from xs in xss
    from x in xs.into_iter().rev()
    collect x as Vec<i32>
);
Commit count: 15

cargo fmt