- Notifications
You must be signed in to change notification settings - Fork7
Language Integrated Query in Rust.
License
StardustDL/Linq-in-Rust
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Language Integrated Query in Rust (created by declarative macros).
- Inspired byLINQ in .NET.
- What's LINQ
This project is under development! API might bechanged.
This is an example:
use linq::linq;use linq::Queryable;fntry_linq_methods(){let x =1..100;letmut y:Vec<i32> = x.clone().filter(|p| p <=&5).collect(); y.sort_by_key(|t| -t);let y:Vec<i32> = y.into_iter().map(|t| t*2).collect();let e:Vec<i32> = x.clone().where_by(|p| p <=&5).order_by(|p| -p).select(|p| p*2).collect();assert_eq!(e, y);}fntry_linq_expr(){let x =1..100;letmut y:Vec<i32> = x.clone().filter(|p| p <=&5).collect(); y.sort_by_key(|t| -t);let y:Vec<i32> = y.into_iter().map(|t| t*2).collect();let e:Vec<i32> =linq!(from p in x.clone(),where p <=&5, orderby -p, select p*2).collect();assert_eq!(e, y);}
If you are familier with LINQ in C#, you will find this is easy to use.
The two imports is necessary:
use linq::linq;// for `linq!` macrouse linq::iter::Enumerable;// for LINQ methods and `linq!` macro
The traitlinq::Queryable
supports LINQ methods onIterator
. You can find the correspondences below.
- Normal items mean they are builtin methods of
Iterator
in std. - Bold items mean they are implemented in this project. You can find them in module
linq::iter
(but they are private so that you can't import them). - Italic items mean they are not in roadmap. Happy for your suggestions.
- where =>where_by => filter
- select => map
- select_many =>select_many_single, select_many
- skip
- skip_while
- take
- take_while
- join
- group_join
- concate => chain
- order_by
- order_by_descending
- then_by
- then_by_descending
- reverse => rev
- group_by
- distinct
- union
- intersect
- except
- first => next
- single
- element_at => nth
- all
- any
- contains
- count
- sum
- product
- min
- max
- average
- aggregate => fold
The query expression begins withfrom
clause and ends withselect
clause. Use,
to seperate every clause.
linq!(from x in coll, select x)
Now we supports these keywords:
- from
- from (
select_many_single
) - zfrom (
select_many
)
- from (
- in
- select
- where
- orderby
- descending
- group_by
- more...
from <id> in <iter expr>,
Also you can enumerate elements of each set in the collection (Attention: for this type, you can't access the value that is in the firstfrom
clause inselect
clause):
let x =1..5;let y =vec![0,0,1,0,1,2,0,1,2,3];let e:Vec<i32> =linq!(from p in x.clone(), from t in0..p, select t).collect();assert_eq!(e, y);
If you want to zip or enumerate value-pairs of two sets, usezfrom
for the secondfrom
:
let x =1..5;let y =vec![(1,0),(2,0),(2,1),(3,0),(3,1),(3,2),(4,0),(4,1),(4,2),(4,3),];let e:Vec<_> =linq!(from p in x.clone(), zfrom t in0..p, select(p,t)).collect();assert_eq!(e, y);
The expression inzfrom
recieve the cloned value in the firstfrom
,and the elements in two sets will be cloned forselect
clause.
while <expr>,
You can usewhere
clause in single-from query, and the expression will recieve a variable named theid
infrom
clause. The expression need to return a boolean value.
orderby <expr>,orderby <expr>, descending,
You can useorderby
clause in single-from query. This query will collect the iterator, and sort them by the expression, then return the new iterator.
We need more unit-test samples. If you have any ideas, open issues to tell us.
Since the expression procedural macros is not stable, I only create macros by declarative macros.
$ cargotest
About
Language Integrated Query in Rust.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.