Turab Jafri <syajafri@iu.edu>
and Paulette Koronkevich <pkoronke@iu.edu>
source code: https://github.com/trajafri/r-linq
This package provides a simple implementation of .NET’s LINQ in Racket.
LINQ’s grammar (as described here) is represented in the Racket language as follows:
A var is a sequence of characters that can be used as a variable in the Racket language.
A src is an expression that evaluates to a sequence.
A expr is a Racket expression.
A comp-func is a Racket procedure that can compare two values.
Following operators can be used in a LINQ query body (a query body must terminate with final query operator). The documentation below is based on the official documentation:
final query operator
( select expr)
'(1 2 3)
'(4 5 6)
'(4 5 6)
Since a select operator finalizes a query, it might be helpful to post-process the results of a select clause. To do this, the into form of select can be used:
'(1 2 3)
'(1 2 3)
More usage of select can be seen under other query operator’s examples.
'(("h" ("hello" "hi"))
("t" ("there" "test"))
("c" ("cow" "car" "cat"))
("d" ("dog")))
'((1 (1 4 7)) (2 (2 5 8)) (0 (3 6 9)))
'((#f (1 3 5 7 9)) (#t (2 4 6 8)))
'((#f (2 4 6 8 10)) (#t (3 5 7 9)))
(DanaJoe)(JoshuaLalo)(JacobNick)(NickLalo)(SamJoshua)(WillJacob)(BrieDana)(JoeJoshua)(JoeWill))'((Turab (Paulette Joshua))
(Paulette (Turab))
(Lalo (Turab))
(Dana (Joe))
(Joshua (Lalo))
(Jacob (Nick))
(Nick (Lalo))
(Sam (Joshua))
(Will (Jacob))
(Brie (Dana))
(Joe (Joshua Will)))
Similar to select , we can post-process the results of a group clause by using an into form:
'(("hello" "hi") ("there" "test") ("cow" "car" "cat") ("dog"))
'((1 3 5 7 9) (2 4 6 8))
'(("h" ("hello" "hi"))
("t" ("there" "test"))
("c" ("cow" "car" "cat"))
("d" ("dog")))
'(2 3 4)
'(2 3 4)
'(#f #t #t)
'((#t (1 3 5 7 9)) (#f (2 4 6 8)))
query operator
( where expr)
The data source on which the query or sub-query will be run.
A local range variable that represents each element in the source sequence.
After initializing a query, the mentioned form of from can be used anywhere in a query body clause to introduce a new data source. The data source referenced in the from clause must be sequence.
'((1 4) (1 5) (1 6) (2 4) (2 5) (2 6) (3 4) (3 5) (3 6))
'(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
'((1 4) (2 3))
'((2 6) (3 6))
1(letfact(!i))'((1 5) (1 6) (2 5) (2 6) (6 5) (6 6))
A join clause takes two source sequences as input. The elements in each sequence must contain a property that can be compared using the function passed in the join clause. The expr on the right can not contain any range variable other than the one being introduced. The expr on the left can contain any range variable other than the one being introduced.
'((1 4) (1 5) (1 6) (2 4) (2 5) (2 6) (3 4) (3 5) (3 6))
'((1 4) (1 4) (1 4) (1 5) (1 5) (1 5) (1 6) (1 6) (1 6))
(LaloTurab)(DanaJoe)(JoshuaLalo)(JacobNick)(NickLalo)(SamJoshua)(WillJacob)(BrieDana)(JoeJoshua)(JoeWill))'((Turab Paulette) (Turab Joshua) (Paulette Turab) (Lalo Turab) (Joshua Lalo))
'(("Peter" #t "Toshiba")
("Jack" #f "Lenovo")
("Tom" #t "Dell")
("Tom" #t "Mac"))
To perform a group-join, you can use the into form of join:
'(("Peter" #t ("Toshiba"))
("Jack" #f ("Lenovo"))
("Tom" #t ("Dell" "Mac"))
("Pat" #t ()))
query operator
( orderby [key-exprcomp-func]...+)
'(3 2 1)
'((3 2) (3 2) (3 1) (3 1) (2 2) (2 2) (2 1) (2 1) (1 2) (1 2) (1 1) (1 1))
'((3 1) (3 1) (3 2) (3 2) (2 1) (2 1) (2 2) (2 2) (1 1) (1 1) (1 2) (1 2))
The operators above are implemented using the following functions that are also provided by r-linq. The documentation below is based on the official documentation:
procedure
( select sel-funcsrc)→(listofY)
sel-func:(X->Y)src:(sequenceofX)
'(3 4 5)
'(1 2 3 4)
'(#\h #\e #\l #\l #\o)
procedure
( select-many sel-funcres-funcsrc)→(listofZ)
sel-func:(X->Y)res-func:(XY->Z)src:(sequenceofX)
'((1 4) (1 5) (1 6) (2 4) (2 5) (2 6) (3 4) (3 5) (3 6))
'((0 0) (1 0) (1 1) (2 0) (2 1) (2 2))
'((2 4 6) (8 10 12) (14 16 18))
procedure
( where predsrc)→(listofX)
pred:(X->boolean)src:(sequenceofX)
'(2 4)
'(#\h #\l #\l)
'((3 2 4 3 1) (1 3 2 4))
procedure
( groupby sel-funcpart-by-funcsrc)
sel-func:(X->Y)part-by-func:(X->Z)src:(sequenceofX)
'((1 (1 4)) (2 (2 5)) (0 (3 6)))
'((#f (2 4 6)) (#t (3 5 7)))
procedure
inner-sel-funcsel-whensel-funcouter-srcinner-src) → (listofZ)outer-sel-func:(A->X)inner-sel-func:(B->Y)sel-when:(XY->boolean)sel-func:(AB->Z)outer-src:(sequenceofA)inner-src:(sequenceofB)
'((1 1) (2 1) (2 2) (3 1) (3 3) (4 1) (4 2) (4 4) (5 1) (5 5))
'((#\h . #\h) (#\e . #\e) (#\l . #\l) (#\o . #\o))
procedure
inner-sel-funcsel-whensel-funcouter-srcinner-src) → (listofZ)outer-sel-func:(A->X)inner-sel-func:(B->Y)sel-when:(XY->boolean)sel-func:(A(listofB)->Z)outer-src:(sequenceofA)inner-src:(sequenceofB)
'((1 (1)) (2 (1 2)) (3 (1 3)) (4 (1 2 4)) (5 (1 5)))
procedure
( orderby srcsort-seq)→(listofX)
src:(sequenceofX)sort-seq:(nelistof[XX->boolean])
These are the literals used in linq query operators.
syntax
syntax
syntax
syntax