• Stars
    star
    112
  • Rank 310,547 (Top 7 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Language Integrated Query in Rust.

Linq in Rust

CI Average time to resolve an issue Percentage of issues still open

Language Integrated Query in Rust (created by declarative macros).

This project is under development! API might be changed.

Quick Start

This is an example:

use linq::linq;
use linq::Queryable;

fn try_linq_methods() {
    let x = 1..100;
    let mut 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);
}

fn try_linq_expr() {
    let x = 1..100;
    let mut 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.

Usage

The two imports is necessary:

use linq::linq;         // for `linq!` macro
use linq::iter::Enumerable;    // for LINQ methods and `linq!` macro

Methods

The trait linq::Queryable supports LINQ methods on Iterator. 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

Expressions

The query expression begins with from clause and ends with select 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)
  • in
  • select
  • where
  • orderby
  • descending
  • group_by
  • more...

From

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 first from clause in select 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 in 0..p, select t).collect();

assert_eq!(e, y);

If you want to zip or enumerate value-pairs of two sets, use zfrom for the second from:

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 in 0..p, select (p,t)).collect();

assert_eq!(e, y);

The expression in zfrom recieve the cloned value in the first from, and the elements in two sets will be cloned for select clause.

Where

while <expr>,

You can use where clause in single-from query, and the expression will recieve a variable named the id in from clause. The expression need to return a boolean value.

Orderby

orderby <expr>,
orderby <expr>, descending,

You can use orderby clause in single-from query. This query will collect the iterator, and sort them by the expression, then return the new iterator.

Development

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.

$ cargo test

More Repositories

1

acblog

An open source extensible static & dynamic blog system. (an alternative tool with same features at StardustDL/paperead)
C#
64
star
2

RazorComponents.Markdown

Razor component for Markdown rendering.
C#
29
star
3

NJU-OS-Lab

This is the source codes of my programming assignment of OS2019 (Operation System) courses at NJU.
C
19
star
4

movie2comic

A tool to transfer movie into comics by keyframe extracting, voice recognition and style transfer techniques.
Python
19
star
5

ImagingS

A toolset for computer graphics and imaging processing.
Python
18
star
6

generator-oj-problem

A command-line tool to generate Online-Judge problem.
Python
11
star
7

raft-impl

A demo 1-to-1 implementation with high availability in Golang for Raft, based on 6.824's raft labs. (NJU dissys course's lab code)
Go
10
star
8

reCLI

Launcher for Windows, an alternative to Alfred and Launchy.
C#
10
star
9

codesim

A similarity measurer on two programming assignments on Online Judge.
Python
9
star
10

NJU-ICS-PA

My source code of programming-assignment experiment of ICS2018 (Introduction to Computer System) courses at NJU.
C
9
star
11

DeepCard

A bank card number recognition system based on deep-learning.
Python
7
star
12

aexpy

AexPy /eɪkspaɪ/ is Api EXplorer in PYthon for detecting API breaking changes in Python packages. (ISSRE'22)
Python
7
star
13

paperead

A tiny tool to present and manage your reading and notes.
Vue
6
star
14

lamcal

An online calculator for lambda calculus (λx. y).
Vue
6
star
15

delights

A collection of useful tiny tools based on Modulight, a modular framework aimed to be low intrusive based on dependency injection for .NET 5, ASP.NET Core and Blazor.
C#
6
star
16

NJU-PTC-Lab

This is the source code of my programming assignment of Principles and Techniques of Compiler courses (2020 Spring) at NJU.
C
5
star
17

modulight

Modulight is a light modular framework aimed to be low intrusive based on dependency injection for .NET, ASP.NET, Blazor, and command-line.
C#
5
star
18

judge0-dotnet

Client SDK for Judge0 RESTful API.
C#
4
star
19

PiCar

An embedded application for toy-car controlling based on Raspberry Pi 3 Model B and AlphaBot2-Pi.
Python
4
star
20

schemdule

A tiny tool using script as schema to schedule one day and remind you to do something during a day.
Python
4
star
21

nju-lib-index

NJU 图书馆中文阅览区简要索引
3
star
22

razorcomponents

Razor component libraries built on Modulight.
C#
3
star
23

turing-machine-emulator

An emulator for multi-tape deterministic turing machine.
Python
3
star
24

jmtrace

A java agent that trace all shared memory accesses of the classes in a given Java jar package.
Java
3
star
25

MIPS-Instruction-Tools

An experimental tool for MIPS architecture (MIPS-32).
Go
2
star
26

Game.GoldenNumber

A test framework for golden number game.
Python
2
star
27

StarOJ

An online-judge platform.
Java
2
star
28

micro-pos

南京大学软件体系结构课程项目,实现了一个微服务架构的响应式 POS 机。
Java
2
star
29

listat

Listat is a light statistic service.
Go
1
star
30

scheme-from-python

An experimental scheme interpreter in Python.
Python
1
star
31

Blog

Liang's Blog
HTML
1
star
32

coxbuild

Coxbuild is a tiny python-script-based build automation tool, an alternative to make, psake and so on.
Python
1
star
33

ACM-ICPC

The codes of ACM-ICPC contests and some problems on online judge.
C++
1
star
34

stardustdl.github.io

HTML
1
star
35

cheal

Connection-HEALing is a modeler and solver for connection healing strategy problem based on quadratic programming.
Python
1
star
36

loment

Loment is a light comment service.
Go
1
star