1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
//! Traversal step methods for anonymous traversals.
//!
//! This module provides the chainable step methods on `Traversal<In, Value>`.
//! These methods allow building anonymous traversal pipelines that can be
//! composed with bound traversals.
use crate::traversal::aggregate;
use crate::traversal::branch;
use crate::traversal::context;
use crate::traversal::filter;
use crate::traversal::mutation;
use crate::traversal::navigation;
use crate::traversal::pipeline::Traversal;
use crate::traversal::predicate;
use crate::traversal::sideeffect;
use crate::traversal::transform;
use crate::value::Value;
// -----------------------------------------------------------------------------
// Traversal Step Methods for Anonymous Traversals
// -----------------------------------------------------------------------------
impl<In> Traversal<In, Value> {
/// Filter elements by label (for anonymous traversals).
///
/// Keeps only vertices/edges whose label matches the given label.
/// Non-element values (integers, strings, etc.) are filtered out.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to person vertices
/// let anon = __.has_label("person");
/// let people = g.v().append(anon).to_list();
/// ```
pub fn has_label(self, label: impl Into<String>) -> Traversal<In, Value> {
self.add_step(filter::HasLabelStep::single(label))
}
/// Filter elements by any of the given labels (for anonymous traversals).
///
/// Keeps only vertices/edges whose label matches any of the given labels.
/// Non-element values (integers, strings, etc.) are filtered out.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to person or company vertices
/// let anon = __.has_label_any(&["person", "company"]);
/// let entities = g.v().append(anon).to_list();
/// ```
pub fn has_label_any<I, S>(self, labels: I) -> Traversal<In, Value>
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.add_step(filter::HasLabelStep::any(labels))
}
/// Filter elements by property existence (for anonymous traversals).
///
/// Keeps only vertices/edges that have the specified property.
/// Non-element values (integers, strings, etc.) are filtered out.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to vertices with "age" property
/// let anon = Traversal::<Value, Value>::new().has("age");
/// let with_age = g.v().append(anon).to_list();
/// ```
pub fn has(self, key: impl Into<String>) -> Traversal<In, Value> {
self.add_step(filter::HasStep::new(key))
}
/// Filter elements by property absence (for anonymous traversals).
///
/// Keeps only vertices/edges that do NOT have the specified property.
/// Non-element values (integers, strings, etc.) pass through since they
/// don't have properties.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to vertices without "email" property
/// let anon = Traversal::<Value, Value>::new().has_not("email");
/// let without_email = g.v().append(anon).to_list();
/// ```
pub fn has_not(self, key: impl Into<String>) -> Traversal<In, Value> {
self.add_step(filter::HasNotStep::new(key))
}
/// Filter elements by property value equality (for anonymous traversals).
///
/// Keeps only vertices/edges where the specified property equals the given value.
/// Non-element values (integers, strings, etc.) are filtered out.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to vertices where name == "Alice"
/// let anon = Traversal::<Value, Value>::new().has_value("name", "Alice");
/// let alice = g.v().append(anon).to_list();
/// ```
pub fn has_value(
self,
key: impl Into<String>,
value: impl Into<Value>,
) -> Traversal<In, Value> {
self.add_step(filter::HasValueStep::new(key, value))
}
/// Filter elements by property value using a predicate (for anonymous traversals).
///
/// Keeps only vertices/edges where the specified property satisfies the predicate.
/// Non-element values (integers, strings, etc.) are filtered out.
/// Elements without the specified property are also filtered out.
///
/// # Example
///
/// ```ignore
/// use interstellar::traversal::p;
///
/// // Create an anonymous traversal that filters to adults
/// let anon = Traversal::<Value, Value>::new().has_where("age", p::gte(18));
/// let adults = g.v().append(anon).to_list();
///
/// // With string predicates
/// let anon = Traversal::<Value, Value>::new().has_where("name", p::starting_with("A"));
/// let a_names = g.v().append(anon).to_list();
/// ```
pub fn has_where(
self,
key: impl Into<String>,
predicate: impl predicate::Predicate + 'static,
) -> Traversal<In, Value> {
self.add_step(filter::HasWhereStep::new(key, predicate))
}
/// Filter by testing the current value against a predicate (for anonymous traversals).
///
/// Unlike `has_where()` which tests a property of vertices/edges, `is_()` tests
/// the traverser's current value directly. This is useful after extracting
/// property values with `values()`.
///
/// # Example
///
/// ```ignore
/// use interstellar::traversal::p;
///
/// // Filter ages greater than 25
/// let anon = Traversal::<Value, Value>::new().is_(p::gt(25));
/// let adults = g.v().values("age").append(anon).to_list();
///
/// // Filter ages in a range
/// let anon = Traversal::<Value, Value>::new().is_(p::between(20, 40));
/// let in_range = g.v().values("age").append(anon).to_list();
/// ```
pub fn is_(self, predicate: impl predicate::Predicate + 'static) -> Traversal<In, Value> {
self.add_step(filter::IsStep::new(predicate))
}
/// Filter by testing the current value for equality (for anonymous traversals).
///
/// This is a convenience method equivalent to `is_(p::eq(value))`.
/// Useful after extracting property values with `values()`.
///
/// # Example
///
/// ```ignore
/// // Filter to ages equal to 29
/// let anon = Traversal::<Value, Value>::new().is_eq(29);
/// let age_29 = g.v().values("age").append(anon).to_list();
///
/// // Filter to a specific name
/// let anon = Traversal::<Value, Value>::new().is_eq("Alice");
/// let alice = g.v().values("name").append(anon).to_list();
/// ```
pub fn is_eq(self, value: impl Into<Value>) -> Traversal<In, Value> {
self.add_step(filter::IsStep::eq(value))
}
/// Filter elements using a custom predicate (for anonymous traversals).
///
/// The predicate receives the execution context and the value, returning
/// `true` to keep the traverser or `false` to filter it out.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to positive integers
/// let anon = Traversal::<Value, Value>::new()
/// .filter(|_ctx, v| matches!(v, Value::Int(n) if *n > 0));
/// let positives = g.inject([1i64, -2i64, 3i64]).append(anon).to_list();
/// ```
pub fn filter<F>(self, predicate: F) -> Traversal<In, Value>
where
F: Fn(&context::ExecutionContext, &Value) -> bool + Clone + Send + Sync + 'static,
{
self.add_step(filter::FilterStep::new(predicate))
}
/// Deduplicate traversers by value (for anonymous traversals).
///
/// Removes duplicate values from the traversal, keeping only the first
/// occurrence of each value. Uses `Value`'s `Hash` implementation.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that deduplicates values
/// let anon = Traversal::<Value, Value>::new().dedup();
/// let unique = g.v().out().append(anon).to_list();
/// ```
pub fn dedup(self) -> Traversal<In, Value> {
self.add_step(filter::DedupStep::new())
}
/// Deduplicate traversers by property value (for anonymous traversals).
///
/// Removes duplicates based on a property value extracted from elements.
/// Only the first occurrence of each unique property value passes through.
///
/// # Arguments
///
/// * `key` - The property key to use for deduplication
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that deduplicates by age
/// let anon = Traversal::<Value, Value>::new().dedup_by_key("age");
/// let unique_ages = g.v().has_label("person").append(anon).to_list();
/// ```
pub fn dedup_by_key(self, key: impl Into<String>) -> Traversal<In, Value> {
self.add_step(filter::DedupByKeyStep::new(key))
}
/// Deduplicate traversers by element label (for anonymous traversals).
///
/// Removes duplicates based on element label. Only the first occurrence
/// of each unique label passes through.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that keeps one per label
/// let anon = Traversal::<Value, Value>::new().dedup_by_label();
/// let one_per_label = g.v().append(anon).to_list();
/// ```
pub fn dedup_by_label(self) -> Traversal<In, Value> {
self.add_step(filter::DedupByLabelStep::new())
}
/// Deduplicate traversers by sub-traversal result (for anonymous traversals).
///
/// Executes the given sub-traversal for each element and uses the first
/// result as the deduplication key.
///
/// # Arguments
///
/// * `sub` - The sub-traversal to execute for each element
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that deduplicates by out-degree
/// let anon = Traversal::<Value, Value>::new()
/// .dedup_by(__.out().count());
/// let unique_outdegree = g.v().append(anon).to_list();
/// ```
pub fn dedup_by(self, sub: Traversal<Value, Value>) -> Traversal<In, Value> {
self.add_step(filter::DedupByTraversalStep::new(sub))
}
/// Limit the number of traversers passing through (for anonymous traversals).
///
/// Returns at most the specified number of traversers, stopping iteration
/// after the limit is reached.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that limits to 5 elements
/// let anon = Traversal::<Value, Value>::new().limit(5);
/// let first_five = g.v().append(anon).to_list();
/// ```
pub fn limit(self, count: usize) -> Traversal<In, Value> {
self.add_step(filter::LimitStep::new(count))
}
/// Skip the first n traversers (for anonymous traversals).
///
/// Discards the first n traversers and passes through all remaining ones.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that skips 10 elements
/// let anon = Traversal::<Value, Value>::new().skip(10);
/// let after_skip = g.v().append(anon).to_list();
/// ```
pub fn skip(self, count: usize) -> Traversal<In, Value> {
self.add_step(filter::SkipStep::new(count))
}
/// Select traversers within a given range (for anonymous traversals).
///
/// Equivalent to `skip(start).limit(end - start)`. Returns traversers
/// from index `start` (inclusive) to index `end` (exclusive).
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that selects elements 10-19
/// let anon = Traversal::<Value, Value>::new().range(10, 20);
/// let page = g.v().append(anon).to_list();
/// ```
pub fn range(self, start: usize, end: usize) -> Traversal<In, Value> {
self.add_step(filter::RangeStep::new(start, end))
}
/// Filter to only paths with no repeated elements (simple paths).
///
/// A simple path visits each element at most once. This is useful
/// for preventing cycles during traversal and finding unique paths.
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().repeat(out()).until(hasLabel("target")).simplePath()
/// ```
///
/// # Example
///
/// ```ignore
/// // Find all simple paths of length 3
/// let simple = g.v()
/// .repeat(__.out())
/// .times(3)
/// .simple_path()
/// .to_list();
/// ```
pub fn simple_path(self) -> Traversal<In, Value> {
self.add_step(filter::SimplePathStep::new())
}
/// Filter to only paths with at least one repeated element (cyclic paths).
///
/// A cyclic path contains at least one element that appears more than once.
/// This is the inverse of `simple_path()`.
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().repeat(out()).until(hasLabel("target")).cyclicPath()
/// ```
///
/// # Example
///
/// ```ignore
/// // Find all cyclic paths
/// let cyclic = g.v()
/// .repeat(__.out())
/// .times(4)
/// .cyclic_path()
/// .to_list();
/// ```
pub fn cyclic_path(self) -> Traversal<In, Value> {
self.add_step(filter::CyclicPathStep::new())
}
/// Return only the last element (for anonymous traversals).
///
/// This is a **barrier step** - it collects ALL input before returning
/// only the last element. Equivalent to `tail_n(1)`.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that returns only the last element
/// let anon = Traversal::<Value, Value>::new().tail();
/// let last = g.v().append(anon).to_list();
/// ```
pub fn tail(self) -> Traversal<In, Value> {
self.add_step(filter::TailStep::last())
}
/// Return only the last n elements (for anonymous traversals).
///
/// This is a **barrier step** - it collects ALL input before returning
/// the last n elements. Elements are returned in their original order.
///
/// # Behavior
///
/// - If fewer than n elements exist, all elements are returned
/// - Empty traversal returns empty result
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that returns the last 5 elements
/// let anon = Traversal::<Value, Value>::new().tail_n(5);
/// let last_five = g.v().append(anon).to_list();
/// ```
pub fn tail_n(self, count: usize) -> Traversal<In, Value> {
self.add_step(filter::TailStep::new(count))
}
/// Probabilistic filter using random coin flip (for anonymous traversals).
///
/// Each traverser has a probability `p` of passing through. Useful for
/// random sampling or probabilistic traversals.
///
/// # Arguments
///
/// * `probability` - Probability of passing (0.0 to 1.0, clamped)
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that randomly samples ~50%
/// let anon = Traversal::<Value, Value>::new().coin(0.5);
/// let sample = g.v().append(anon).to_list();
/// ```
pub fn coin(self, probability: f64) -> Traversal<In, Value> {
self.add_step(filter::CoinStep::new(probability))
}
/// Randomly sample n elements using reservoir sampling (for anonymous traversals).
///
/// This is a **barrier step** that collects all input elements and returns
/// a random sample of exactly n elements. If the input has fewer than n
/// elements, all elements are returned.
///
/// # Arguments
///
/// * `count` - The number of elements to sample
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that samples 5 random elements
/// let anon = Traversal::<Value, Value>::new().sample(5);
/// let sampled = g.v().append(anon).to_list();
/// ```
///
/// # Note
///
/// Results are non-deterministic. For reproducible results in tests,
/// use statistical tolerances.
pub fn sample(self, count: usize) -> Traversal<In, Value> {
self.add_step(filter::SampleStep::new(count))
}
/// Filter property objects by key name (for anonymous traversals).
///
/// This step filters property maps (from `properties()`) to keep only those
/// with a matching "key" field.
///
/// # Arguments
///
/// * `key` - The property key to filter for
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to "name" properties
/// let anon = Traversal::<Value, Value>::new().has_key("name");
/// let names = g.v().properties().append(anon).to_list();
/// ```
pub fn has_key(self, key: impl Into<String>) -> Traversal<In, Value> {
self.add_step(filter::HasKeyStep::new(key))
}
/// Filter property objects by any of the specified key names (for anonymous traversals).
///
/// This step filters property maps (from `properties()`) to keep only those
/// with a "key" field matching any of the specified keys.
///
/// # Arguments
///
/// * `keys` - The property keys to filter for
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to "name" or "age" properties
/// let anon = Traversal::<Value, Value>::new().has_key_any(["name", "age"]);
/// let props = g.v().properties().append(anon).to_list();
/// ```
pub fn has_key_any<I, S>(self, keys: I) -> Traversal<In, Value>
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.add_step(filter::HasKeyStep::any(keys))
}
/// Filter property objects by value (for anonymous traversals).
///
/// This step filters property maps (from `properties()`) to keep only those
/// with a matching "value" field.
///
/// # Arguments
///
/// * `value` - The property value to filter for
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to properties with value "Alice"
/// let anon = Traversal::<Value, Value>::new().has_prop_value("Alice");
/// let alice_props = g.v().properties().append(anon).to_list();
/// ```
pub fn has_prop_value(self, value: impl Into<Value>) -> Traversal<In, Value> {
self.add_step(filter::HasPropValueStep::new(value))
}
/// Filter property objects by any of the specified values (for anonymous traversals).
///
/// This step filters property maps (from `properties()`) to keep only those
/// with a "value" field matching any of the specified values.
///
/// # Arguments
///
/// * `values` - The property values to filter for
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to properties with value "Alice" or "Bob"
/// let anon = Traversal::<Value, Value>::new().has_prop_value_any(["Alice", "Bob"]);
/// let props = g.v().properties().append(anon).to_list();
/// ```
pub fn has_prop_value_any<I, V>(self, values: I) -> Traversal<In, Value>
where
I: IntoIterator<Item = V>,
V: Into<Value>,
{
self.add_step(filter::HasPropValueStep::any(values))
}
/// Filter traversers by testing their current value against a predicate (for anonymous traversals).
///
/// This step is the predicate-based variant of `where()`, complementing the
/// traversal-based `where_(traversal)` step. It tests the current traverser
/// value directly against the predicate.
///
/// # Arguments
///
/// * `predicate` - The predicate to test values against
///
/// # Example
///
/// ```ignore
/// use interstellar::traversal::p;
///
/// // Create an anonymous traversal that filters values > 25
/// let anon = Traversal::<Value, Value>::new().where_p(p::gt(25));
/// let adults = g.v().values("age").append(anon).to_list();
///
/// // Filter to values within a set
/// let anon = Traversal::<Value, Value>::new().where_p(p::within(["Alice", "Bob"]));
/// ```
pub fn where_p(
self,
predicate: impl crate::traversal::predicate::Predicate + 'static,
) -> Traversal<In, Value> {
self.add_step(filter::WherePStep::new(predicate))
}
/// Filter elements by a single ID (for anonymous traversals).
///
/// Keeps only vertices/edges whose ID matches the given ID.
/// Non-element values (integers, strings, etc.) are filtered out.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to a specific vertex
/// let anon = Traversal::<Value, Value>::new().has_id(VertexId(1));
/// let vertex = g.v().append(anon).to_list();
/// ```
pub fn has_id(self, id: impl Into<Value>) -> Traversal<In, Value> {
self.add_step(filter::HasIdStep::from_value(id))
}
/// Filter elements by multiple IDs (for anonymous traversals).
///
/// Keeps only vertices/edges whose ID matches any of the given IDs.
/// Non-element values (integers, strings, etc.) are filtered out.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters to multiple vertices
/// let anon = Traversal::<Value, Value>::new().has_ids([VertexId(1), VertexId(2)]);
/// let vertices = g.v().append(anon).to_list();
/// ```
pub fn has_ids<I, T>(self, ids: I) -> Traversal<In, Value>
where
I: IntoIterator<Item = T>,
T: Into<Value>,
{
self.add_step(filter::HasIdStep::from_values(
ids.into_iter().map(Into::into).collect(),
))
}
// -------------------------------------------------------------------------
// Navigation steps (for anonymous traversals)
// -------------------------------------------------------------------------
/// Traverse to outgoing adjacent vertices (for anonymous traversals).
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().out();
/// let neighbors = g.v().append(anon).to_list();
/// ```
pub fn out(self) -> Traversal<In, Value> {
self.add_step(navigation::OutStep::new())
}
/// Traverse to outgoing adjacent vertices via edges with given labels.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().out_labels(&["knows"]);
/// let friends = g.v().append(anon).to_list();
/// ```
pub fn out_labels(self, labels: &[&str]) -> Traversal<In, Value> {
let labels: Vec<String> = labels.iter().map(|s| s.to_string()).collect();
self.add_step(navigation::OutStep::with_labels(labels))
}
/// Traverse to incoming adjacent vertices (for anonymous traversals).
///
/// Note: Named `in_` to avoid conflict with Rust's `in` keyword.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().in_();
/// let known_by = g.v().append(anon).to_list();
/// ```
pub fn in_(self) -> Traversal<In, Value> {
self.add_step(navigation::InStep::new())
}
/// Traverse to incoming adjacent vertices via edges with given labels.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().in_labels(&["knows"]);
/// let known_by = g.v().append(anon).to_list();
/// ```
pub fn in_labels(self, labels: &[&str]) -> Traversal<In, Value> {
let labels: Vec<String> = labels.iter().map(|s| s.to_string()).collect();
self.add_step(navigation::InStep::with_labels(labels))
}
/// Traverse to adjacent vertices in both directions (for anonymous traversals).
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().both();
/// let neighbors = g.v().append(anon).to_list();
/// ```
pub fn both(self) -> Traversal<In, Value> {
self.add_step(navigation::BothStep::new())
}
/// Traverse to adjacent vertices in both directions via edges with given labels.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().both_labels(&["knows"]);
/// let connected = g.v().append(anon).to_list();
/// ```
pub fn both_labels(self, labels: &[&str]) -> Traversal<In, Value> {
let labels: Vec<String> = labels.iter().map(|s| s.to_string()).collect();
self.add_step(navigation::BothStep::with_labels(labels))
}
/// Traverse to outgoing edges (for anonymous traversals).
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().out_e();
/// let edges = g.v().append(anon).to_list();
/// ```
pub fn out_e(self) -> Traversal<In, Value> {
self.add_step(navigation::OutEStep::new())
}
/// Traverse to outgoing edges with given labels.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().out_e_labels(&["knows"]);
/// let edges = g.v().append(anon).to_list();
/// ```
pub fn out_e_labels(self, labels: &[&str]) -> Traversal<In, Value> {
let labels: Vec<String> = labels.iter().map(|s| s.to_string()).collect();
self.add_step(navigation::OutEStep::with_labels(labels))
}
/// Traverse to incoming edges (for anonymous traversals).
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().in_e();
/// let edges = g.v().append(anon).to_list();
/// ```
pub fn in_e(self) -> Traversal<In, Value> {
self.add_step(navigation::InEStep::new())
}
/// Traverse to incoming edges with given labels.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().in_e_labels(&["knows"]);
/// let edges = g.v().append(anon).to_list();
/// ```
pub fn in_e_labels(self, labels: &[&str]) -> Traversal<In, Value> {
let labels: Vec<String> = labels.iter().map(|s| s.to_string()).collect();
self.add_step(navigation::InEStep::with_labels(labels))
}
/// Traverse to all incident edges (for anonymous traversals).
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().both_e();
/// let edges = g.v().append(anon).to_list();
/// ```
pub fn both_e(self) -> Traversal<In, Value> {
self.add_step(navigation::BothEStep::new())
}
/// Traverse to all incident edges with given labels.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().both_e_labels(&["knows"]);
/// let edges = g.v().append(anon).to_list();
/// ```
pub fn both_e_labels(self, labels: &[&str]) -> Traversal<In, Value> {
let labels: Vec<String> = labels.iter().map(|s| s.to_string()).collect();
self.add_step(navigation::BothEStep::with_labels(labels))
}
/// Get the source vertex of an edge (for anonymous traversals).
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().out_v();
/// let sources = g.e().append(anon).to_list();
/// ```
pub fn out_v(self) -> Traversal<In, Value> {
self.add_step(navigation::OutVStep::new())
}
/// Get the target vertex of an edge (for anonymous traversals).
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().in_v();
/// let targets = g.e().append(anon).to_list();
/// ```
pub fn in_v(self) -> Traversal<In, Value> {
self.add_step(navigation::InVStep::new())
}
/// Get both vertices of an edge (for anonymous traversals).
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().both_v();
/// let vertices = g.e().append(anon).to_list();
/// ```
pub fn both_v(self) -> Traversal<In, Value> {
self.add_step(navigation::BothVStep::new())
}
/// Get the "other" vertex of an edge (for anonymous traversals).
///
/// When traversing from a vertex to an edge, `other_v()` returns the
/// vertex at the opposite end from where the traverser came from.
/// Requires path tracking to be enabled.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().other_v();
/// let others = g.v().out_e().append(anon).to_list();
/// ```
pub fn other_v(self) -> Traversal<In, Value> {
self.add_step(navigation::OtherVStep::new())
}
// -------------------------------------------------------------------------
// Transform steps (for anonymous traversals)
// -------------------------------------------------------------------------
/// Extract property values from vertices/edges (for anonymous traversals).
///
/// For each input element, extracts the value of the specified property.
/// Missing properties are silently skipped.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().values("name");
/// let names = g.v().has_label("person").append(anon).to_list();
/// ```
pub fn values(self, key: impl Into<String>) -> Traversal<In, Value> {
self.add_step(transform::ValuesStep::new(key))
}
/// Extract multiple property values from vertices/edges (for anonymous traversals).
///
/// For each input element, extracts the values of the specified properties.
/// Missing properties are silently skipped.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().values_multi(["name", "age"]);
/// let data = g.v().append(anon).to_list();
/// ```
pub fn values_multi<I, S>(self, keys: I) -> Traversal<In, Value>
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.add_step(transform::ValuesStep::from_keys(keys))
}
/// Extract all property objects from vertices/edges (for anonymous traversals).
///
/// Unlike `values()` which returns just property values, `properties()` returns
/// the full property including its key as a Map with "key" and "value" entries.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().properties();
/// let props = g.v().has_label("person").append(anon).to_list();
/// // Each result is Value::Map { "key": "name", "value": "Alice" } etc.
/// ```
pub fn properties(self) -> Traversal<In, Value> {
self.add_step(transform::PropertiesStep::new())
}
/// Extract specific property objects from vertices/edges (for anonymous traversals).
///
/// Unlike `values()` which returns just property values, `properties_keys()` returns
/// the full property including its key as a Map with "key" and "value" entries.
/// Only the specified property keys are extracted.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().properties_keys(&["name", "age"]);
/// let props = g.v().append(anon).to_list();
/// ```
pub fn properties_keys(self, keys: &[&str]) -> Traversal<In, Value> {
let keys: Vec<String> = keys.iter().map(|s| s.to_string()).collect();
self.add_step(transform::PropertiesStep::with_keys(keys))
}
/// Get all properties as a map (for anonymous traversals).
///
/// Transforms each element into a `Value::Map` containing all its properties.
/// Property values are wrapped in `Value::List` for multi-property compatibility.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().value_map();
/// let maps = g.v().append(anon).to_list();
/// // Returns: [{"name": ["Alice"], "age": [30]}, ...]
/// ```
pub fn value_map(self) -> Traversal<In, Value> {
self.add_step(transform::ValueMapStep::new())
}
/// Get specific properties as a map (for anonymous traversals).
///
/// Transforms each element into a `Value::Map` containing only the
/// specified properties. Property values are wrapped in `Value::List`.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().value_map_keys(&["name"]);
/// let maps = g.v().append(anon).to_list();
/// // Returns: [{"name": ["Alice"]}, {"name": ["Bob"]}]
/// ```
pub fn value_map_keys<I, S>(self, keys: I) -> Traversal<In, Value>
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.add_step(transform::ValueMapStep::from_keys(keys))
}
/// Get all properties as a map including id and label tokens (for anonymous traversals).
///
/// Like `value_map()`, but also includes "id" and "label" entries.
/// The id and label are NOT wrapped in lists, but property values are.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().value_map_with_tokens();
/// let maps = g.v().append(anon).to_list();
/// // Returns: [{"id": 0, "label": "person", "name": ["Alice"], "age": [30]}]
/// ```
pub fn value_map_with_tokens(self) -> Traversal<In, Value> {
self.add_step(transform::ValueMapStep::new().with_tokens())
}
/// Get complete element representation as a map (for anonymous traversals).
///
/// Transforms each element into a `Value::Map` with id, label, and all
/// properties. Unlike `value_map()`, property values are NOT wrapped in lists.
/// For edges, also includes "IN" and "OUT" vertex references.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().element_map();
/// let maps = g.v().append(anon).to_list();
/// // Returns: [{"id": 0, "label": "person", "name": "Alice", "age": 30}]
/// ```
pub fn element_map(self) -> Traversal<In, Value> {
self.add_step(transform::ElementMapStep::new())
}
/// Get element representation with specific properties (for anonymous traversals).
///
/// Like `element_map()`, but includes only the specified properties
/// along with the id, label, and (for edges) IN/OUT references.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().element_map_keys(&["name"]);
/// let maps = g.v().append(anon).to_list();
/// // Returns: [{"id": 0, "label": "person", "name": "Alice"}]
/// ```
pub fn element_map_keys<I, S>(self, keys: I) -> Traversal<In, Value>
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.add_step(transform::ElementMapStep::from_keys(keys))
}
/// Get all properties as a map of property objects (for anonymous traversals).
///
/// Transforms each element into a `Value::Map` where keys are property names
/// and values are lists of property objects (maps with "key" and "value" entries).
///
/// # Difference from valueMap
///
/// - `value_map()`: Returns `{name: ["Alice"], age: [30]}` (just values in lists)
/// - `property_map()`: Returns `{name: [{key: "name", value: "Alice"}], age: [{key: "age", value: 30}]}` (property objects in lists)
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().property_map();
/// let maps = g.v().append(anon).to_list();
/// // Returns: [{name: [{key: "name", value: "Alice"}], age: [{key: "age", value: 30}]}]
/// ```
pub fn property_map(self) -> Traversal<In, Value> {
self.add_step(transform::PropertyMapStep::new())
}
/// Get specific properties as a map of property objects (for anonymous traversals).
///
/// Like `property_map()`, but includes only the specified properties.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().property_map_keys(&["name"]);
/// let maps = g.v().append(anon).to_list();
/// // Returns: [{name: [{key: "name", value: "Alice"}]}]
/// ```
pub fn property_map_keys<I, S>(self, keys: I) -> Traversal<In, Value>
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.add_step(transform::PropertyMapStep::from_keys(keys))
}
/// Unroll collections into individual elements (for anonymous traversals).
///
/// This step expands `Value::List` and `Value::Map` into separate traversers:
/// - `Value::List`: Each element becomes a separate traverser
/// - `Value::Map`: Each key-value pair becomes a single-entry map traverser
/// - Non-collection values pass through unchanged
///
/// # Example
///
/// ```ignore
/// // Unfold a list
/// let anon = Traversal::<Value, Value>::new().unfold();
/// let items = g.inject([Value::List(vec![Value::Int(1), Value::Int(2)])])
/// .append(anon)
/// .to_list();
/// // Results: [Value::Int(1), Value::Int(2)]
///
/// // Round-trip: fold then unfold
/// let original = g.v().fold().unfold().to_list();
/// // Returns original vertices
/// ```
pub fn unfold(self) -> Traversal<In, Value> {
self.add_step(transform::UnfoldStep::new())
}
/// Calculate the arithmetic mean (average) of numeric values.
///
/// This is a **barrier step** - it collects ALL input values before producing
/// a single output. Only numeric values (`Value::Int` and `Value::Float`) are
/// included in the calculation; non-numeric values are silently ignored.
///
/// # Behavior
///
/// - Collects all numeric values from input traversers
/// - `Value::Int` values are converted to `f64` for calculation
/// - `Value::Float` values are used directly
/// - Non-numeric values (strings, booleans, vertices, etc.) are ignored
/// - Returns `Value::Float` with the mean if any numeric values exist
/// - Returns empty (no output) if no numeric values are found
///
/// # Example
///
/// ```ignore
/// // Calculate average age of all people
/// let avg_age = g.v().has_label("person").values("age").mean().next();
///
/// // Mixed values - non-numeric ignored
/// let avg = g.inject(vec![Value::Int(1), Value::Int(2), Value::String("three".into())])
/// .mean().next(); // Returns Some(Value::Float(1.5))
/// ```
pub fn mean(self) -> Traversal<In, Value> {
self.add_step(transform::MeanStep::new())
}
/// Count the number of traversers.
///
/// This is a **barrier step** - it collects ALL input before producing a count.
/// Produces a single `Value::Int` containing the count.
///
/// # Example
///
/// ```ignore
/// // Count how many people the current vertex knows
/// let t = __.out("knows").count();
/// // Used in group().by_value_traversal(t) to count per group
/// ```
pub fn count(self) -> Traversal<In, Value> {
self.add_step(aggregate::CountStep::new())
}
/// Collect all input traversers into a single list.
///
/// This is a **barrier step** - it collects ALL input before producing
/// a single `Value::List` containing all values.
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().fold() // Collect all vertices into a list
/// g.V().values("age").fold() // Collect all ages into a list
/// ```
///
/// # Example
///
/// ```ignore
/// // Collect all vertex IDs into a list
/// let all_ids = __.id().fold();
///
/// // Fold is often paired with unfold for per-element processing
/// let t = __.fold().project(&["count", "items"])
/// .by(__.count_local())
/// .by(__.identity())
/// .build();
/// ```
pub fn fold(self) -> Traversal<In, Value> {
self.add_step(transform::FoldStep::new())
}
/// Sum all numeric input values.
///
/// This is a **barrier step** - it collects ALL input before producing
/// the sum as a single `Value::Int` or `Value::Float`.
///
/// # Behavior
///
/// - Sums all numeric values (`Value::Int` and `Value::Float`)
/// - Non-numeric values are silently ignored
/// - If all inputs are integers, returns `Value::Int`
/// - If any input is a float, returns `Value::Float`
/// - Empty input returns `Value::Int(0)`
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().values("age").sum() // Sum all ages
/// ```
///
/// # Example
///
/// ```ignore
/// // Sum all transaction amounts
/// let total = __.values("amount").sum();
/// ```
pub fn sum(self) -> Traversal<In, Value> {
self.add_step(transform::SumStep::new())
}
/// Find the minimum value across all traversers.
///
/// This is a **barrier step** - it collects ALL input before producing
/// the minimum value.
///
/// # Behavior
///
/// - Compares numeric values (`Value::Int` and `Value::Float`)
/// - Also compares strings lexicographically
/// - Non-comparable values are skipped
/// - Empty input returns `Value::Null`
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().values("age").min() // Find minimum age
/// ```
///
/// # Example
///
/// ```ignore
/// // Find minimum transaction amount
/// let min_amount = __.values("amount").min();
/// ```
pub fn min(self) -> Traversal<In, Value> {
self.add_step(aggregate::MinStep::new())
}
/// Find the maximum value across all traversers.
///
/// This is a **barrier step** - it collects ALL input before producing
/// the maximum value.
///
/// # Behavior
///
/// - Compares numeric values (`Value::Int` and `Value::Float`)
/// - Also compares strings lexicographically
/// - Non-comparable values are skipped
/// - Empty input returns `Value::Null`
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().values("age").max() // Find maximum age
/// ```
///
/// # Example
///
/// ```ignore
/// // Find maximum transaction amount
/// let max_amount = __.values("amount").max();
/// ```
pub fn max(self) -> Traversal<In, Value> {
self.add_step(aggregate::MaxStep::new())
}
/// Count elements within each collection value (local scope).
///
/// Unlike the global `count()` which counts traversers in the stream,
/// `count_local()` counts elements *within* each traverser's collection value.
/// This implements Gremlin's `count(local)` semantics.
///
/// # Behavior
///
/// - `Value::List`: Returns the number of elements in the list
/// - `Value::Map`: Returns the number of entries in the map
/// - `Value::String`: Returns the length of the string
/// - Other values: Returns 1
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().out().fold().count(local) // Count items in each folded list
/// ```
///
/// # Example
///
/// ```ignore
/// // Count friends per person after folding
/// let friend_counts = __.fold().count_local();
/// ```
pub fn count_local(self) -> Traversal<In, Value> {
self.add_step(transform::CountLocalStep::new())
}
/// Sum elements within each collection value (local scope).
///
/// Unlike the global `sum()` which sums across all traversers,
/// `sum_local()` sums elements *within* each traverser's collection value.
/// This implements Gremlin's `sum(local)` semantics.
///
/// # Behavior
///
/// - `Value::List`: Sums all numeric elements in the list
/// - `Value::Int`/`Value::Float`: Returns the value unchanged
/// - Other values: Returns 0
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().values("scores").fold().sum(local) // Sum scores per vertex
/// ```
///
/// # Example
///
/// ```ignore
/// // Sum transaction amounts per user after folding
/// let totals = __.values("amount").fold().sum_local();
/// ```
pub fn sum_local(self) -> Traversal<In, Value> {
self.add_step(transform::SumLocalStep::new())
}
/// Extract keys from Map values.
///
/// For each traverser with a Map value, extracts the keys.
/// Single-entry maps return the key directly; multi-entry maps
/// return a List of keys. Non-Map values are filtered out.
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().group().by(label).unfold().select(keys)
/// ```
///
/// # Example
///
/// ```ignore
/// // Get group keys after grouping
/// let labels = __.unfold().select_keys();
/// ```
pub fn select_keys(self) -> Traversal<In, Value> {
self.add_step(transform::SelectKeysStep::new())
}
/// Extract values from Map values.
///
/// For each traverser with a Map value, extracts the values.
/// Single-entry maps return the value directly; multi-entry maps
/// return a List of values. Non-Map values are filtered out.
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().group().by(label).unfold().select(values)
/// ```
///
/// # Example
///
/// ```ignore
/// // Get group values after grouping
/// let groups = __.unfold().select_values();
/// ```
pub fn select_values(self) -> Traversal<In, Value> {
self.add_step(transform::SelectValuesStep::new())
}
/// Sort traversers using a fluent builder.
///
/// This is a **barrier step** - it collects ALL input before producing sorted output.
/// Returns an `OrderBuilder` that allows chaining multiple sort keys using `by` methods.
///
/// # Behavior
///
/// - Collects all input traversers (barrier)
/// - Sorts according to configured keys
/// - Multiple `by` clauses create multi-level sorts
/// - Supports sorting by:
/// - Natural order of current value
/// - Property values from vertices/edges
/// - Results of sub-traversals
///
/// # Example
///
/// ```ignore
/// // Sort by natural order ascending (default)
/// let sorted = g.v().values("name").order().build().to_list();
///
/// // Sort by property descending
/// let sorted = g.v().has_label("person")
/// .order().by_key_desc("age").build()
/// .to_list();
///
/// // Multi-level sort: by age desc, then name asc
/// let sorted = g.v().has_label("person")
/// .order()
/// .by_key_desc("age")
/// .by_key_asc("name")
/// .build()
/// .to_list();
/// ```
pub fn order(self) -> transform::OrderBuilder<In> {
let (_, steps) = self.into_steps();
transform::OrderBuilder::new(steps)
}
/// Evaluate a mathematical expression (for anonymous traversals).
///
/// The expression can reference the current value using `_` and labeled
/// path values using their label names. Use `by()` to specify which
/// property to extract from labeled elements.
///
/// Uses the `mathexpr` crate for full expression parsing and evaluation,
/// supporting:
/// - Operators: `+`, `-`, `*`, `/`, `%`, `^`
/// - Functions: `sqrt`, `abs`, `sin`, `cos`, `tan`, `log`, `exp`, `pow`, `min`, `max`, etc.
/// - Constants: `pi`, `e`
/// - Parentheses for grouping
///
/// # Examples
///
/// ```ignore
/// // Double current values
/// g.v().values("age").math("_ * 2").build()
///
/// // Calculate difference between labeled values
/// g.v().as_("a").out("knows").as_("b")
/// .math("a - b")
/// .by("a", "age")
/// .by("b", "age")
/// .build()
///
/// // Complex expression with functions
/// g.v().values("x").math("sqrt(_ ^ 2 + 1)").build()
/// ```
#[cfg(feature = "gql")]
pub fn math(self, expression: &str) -> transform::MathBuilder<In> {
let (_, steps) = self.into_steps();
transform::MathBuilder::new(steps, expression)
}
/// Create a projection with named keys (for anonymous traversals).
///
/// The `project()` step creates a map with specific named keys. Each key's value
/// is defined by a `by()` modulator, which can extract a property or execute
/// a sub-traversal.
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().hasLabel('person')
/// .project('name', 'age', 'friends')
/// .by('name')
/// .by('age')
/// .by(out('knows').count())
/// ```
///
/// # Example
///
/// ```ignore
/// use __; // Anonymous traversal module
///
/// let results = g.v().has_label("person")
/// .project(&["name", "friend_count"])
/// .by_key("name")
/// .by(__.out("knows").count())
/// .build()
/// .to_list();
/// // Results: [{name: "Alice", friend_count: 2}, ...]
/// ```
///
/// # Arguments
///
/// * `keys` - The keys for the projection map
///
/// # Returns
///
/// A `ProjectBuilder` that requires `by()` clauses to be added for each key.
pub fn project(self, keys: &[&str]) -> transform::ProjectBuilder<In> {
let (_, steps) = self.into_steps();
let key_strings: Vec<String> = keys.iter().map(|k| k.to_string()).collect();
transform::ProjectBuilder::new(steps, key_strings)
}
/// Group traversers by a key and collect values (for anonymous traversals).
///
/// The `group()` step is a **barrier step** that collects all input traversers,
/// groups them by a key, and produces a single `Value::Map` output where:
/// - Keys are the grouping keys (converted to strings)
/// - Values are lists of collected values for each group
///
/// # Gremlin Equivalent
///
/// ```groovy
/// g.V().group().by(label) // Group by label
/// g.V().group().by("age").by("name") // Group by age, collect names
/// g.V().group().by(label).by(out().count()) // Group by label, count outgoing
/// ```
///
/// # Example
///
/// ```ignore
/// use __; // Anonymous traversal module
///
/// // Group vertices by label
/// let groups = g.v()
/// .group().by_label().by_value().build()
/// .next();
/// // Returns: Map { "person" -> [v1, v2], "software" -> [v3] }
///
/// // Group by property, collect other property
/// let groups = g.v().has_label("person")
/// .group().by_key("age").by_value_key("name").build()
/// .next();
/// // Returns: Map { "29" -> ["Alice", "Bob"], "30" -> ["Charlie"] }
/// ```
///
/// # Returns
///
/// A `GroupBuilder` that allows configuring the grouping key and value collector.
pub fn group(self) -> aggregate::GroupBuilder<In> {
let (_, steps) = self.into_steps();
aggregate::GroupBuilder::new(steps)
}
/// Count traversers grouped by a key (for anonymous traversals).
///
/// Creates a `GroupCountBuilder` that allows specifying how to group and count
/// the traversers. The result is a single `Value::Map` where keys are the
/// grouping keys and values are integer counts.
///
/// # Example
///
/// ```ignore
/// use interstellar::*;
/// use interstellar::traversal::__;
///
/// // Count vertices by label
/// let t = __.v().group_count().by_label().build();
///
/// // Count vertices by a property
/// let t2 = __.v().group_count().by_key("age").build();
/// ```
///
/// # Returns
///
/// A `GroupCountBuilder` that allows configuring the grouping key.
pub fn group_count(self) -> aggregate::GroupCountBuilder<In> {
let (_, steps) = self.into_steps();
aggregate::GroupCountBuilder::new(steps)
}
/// Extract the ID from vertices/edges (for anonymous traversals).
///
/// For each input element, extracts its ID as a `Value::Int`.
/// Non-element values are filtered out.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().id();
/// let ids = g.v().has_label("person").append(anon).to_list();
/// ```
pub fn id(self) -> Traversal<In, Value> {
self.add_step(transform::IdStep::new())
}
/// Extract the label from vertices/edges (for anonymous traversals).
///
/// For each input element, extracts its label as a `Value::String`.
/// Non-element values are filtered out.
///
/// # Example
///
/// ```ignore
/// let anon = Traversal::<Value, Value>::new().label();
/// let labels = g.v().append(anon).to_list();
/// ```
pub fn label(self) -> Traversal<In, Value> {
self.add_step(transform::LabelStep::new())
}
/// Transform each value using a closure (for anonymous traversals).
///
/// The closure receives the execution context and the current value,
/// returning a new value. This is a 1:1 mapping.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that doubles integer values
/// let anon = Traversal::<Value, Value>::new()
/// .map(|_ctx, v| {
/// if let Value::Int(n) = v {
/// Value::Int(n * 2)
/// } else {
/// v.clone()
/// }
/// });
/// let doubled = g.inject([1i64, 2i64]).append(anon).to_list();
/// ```
pub fn map<F>(self, f: F) -> Traversal<In, Value>
where
F: Fn(&context::ExecutionContext, &Value) -> Value + Clone + Send + Sync + 'static,
{
self.add_step(transform::MapStep::new(f))
}
/// Transform each value to multiple values using a closure (for anonymous traversals).
///
/// The closure receives the execution context and the current value,
/// returning a `Vec<Value>`. This is a 1:N mapping - each input can
/// produce zero or more outputs.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that generates ranges
/// let anon = Traversal::<Value, Value>::new()
/// .flat_map(|_ctx, v| {
/// if let Value::Int(n) = v {
/// (0..*n).map(|i| Value::Int(i)).collect()
/// } else {
/// vec![]
/// }
/// });
/// let expanded = g.inject([3i64]).append(anon).to_list();
/// // Results: [0, 1, 2]
/// ```
pub fn flat_map<F>(self, f: F) -> Traversal<In, Value>
where
F: Fn(&context::ExecutionContext, &Value) -> Vec<Value> + Clone + Send + Sync + 'static,
{
self.add_step(transform::FlatMapStep::new(f))
}
/// Replace each traverser's value with a constant (for anonymous traversals).
///
/// For each input traverser, replaces the value with the specified constant.
/// All traverser metadata (path, loops, bulk, sack) is preserved.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that replaces values with "found"
/// let anon = Traversal::<Value, Value>::new().constant("found");
/// let results = g.v().append(anon).to_list();
/// // All results: Value::String("found")
///
/// // With numeric constant
/// let anon = Traversal::<Value, Value>::new().constant(42i64);
/// ```
pub fn constant(self, value: impl Into<Value>) -> Traversal<In, Value> {
self.add_step(transform::ConstantStep::new(value))
}
/// Convert the traverser's path to a Value::List (for anonymous traversals).
///
/// Replaces the traverser's value with a list containing all elements
/// from its path history. Each path element is converted to its
/// corresponding Value representation.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that returns the path
/// let anon = Traversal::<Value, Value>::new().out().path();
/// let paths = g.v().append(anon).to_list();
/// // Each result is a Value::List of path elements
/// ```
pub fn path(self) -> Traversal<In, Value> {
self.add_step(transform::PathStep::new())
}
/// Label the current position in the traversal path (for anonymous traversals).
///
/// Records the current traverser's value in the path with the specified label.
/// This enables later retrieval via `select()` or `select_one()`.
///
/// Unlike automatic path tracking, `as_()` labels are always recorded
/// regardless of whether `with_path()` was called.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal with labeled positions
/// let anon = Traversal::<Value, Value>::new()
/// .as_("start").out().as_("end").select(&["start", "end"]);
/// let results = g.v().append(anon).to_list();
/// ```
pub fn as_(self, label: &str) -> Traversal<In, Value> {
self.add_step(transform::AsStep::new(label))
}
/// Select multiple labeled values from the path (for anonymous traversals).
///
/// Retrieves values that were labeled with `as_()` and returns them as a Map.
/// Traversers without any of the requested labels are filtered out.
///
/// # Arguments
///
/// * `labels` - The labels to select from the path
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that selects labeled values
/// let anon = Traversal::<Value, Value>::new()
/// .as_("a").out().as_("b").select(&["a", "b"]);
/// let results = g.v().append(anon).to_list();
/// // Returns Map { "a" -> vertex1, "b" -> vertex2 }
/// ```
pub fn select(self, labels: &[&str]) -> Traversal<In, Value> {
let labels: Vec<String> = labels.iter().map(|s| s.to_string()).collect();
self.add_step(transform::SelectStep::new(labels))
}
/// Select a single labeled value from the path (for anonymous traversals).
///
/// Retrieves the value that was labeled with `as_()` and returns it directly
/// (not wrapped in a Map). Traversers without the requested label are filtered out.
///
/// # Arguments
///
/// * `label` - The label to select from the path
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that selects a single labeled value
/// let anon = Traversal::<Value, Value>::new()
/// .as_("x").out().select_one("x");
/// let results = g.v().append(anon).to_list();
/// // Returns the labeled vertex directly (not a Map)
/// ```
pub fn select_one(self, label: &str) -> Traversal<In, Value> {
self.add_step(transform::SelectStep::single(label))
}
// -------------------------------------------------------------------------
// Filter steps using anonymous traversals
// -------------------------------------------------------------------------
/// Filter by sub-traversal existence (for anonymous traversals).
///
/// Emits input traverser only if the sub-traversal produces at least one result.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters by sub-traversal
/// let anon = Traversal::<Value, Value>::new()
/// .where_(__.out());
/// let with_out = g.v().append(anon).to_list();
/// ```
pub fn where_(self, sub: Traversal<Value, Value>) -> Traversal<In, Value> {
self.add_step(branch::WhereStep::new(sub))
}
/// Filter by sub-traversal non-existence (for anonymous traversals).
///
/// Emits input traverser only if the sub-traversal produces NO results.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that filters out vertices with outgoing edges
/// let anon = Traversal::<Value, Value>::new()
/// .not(__.out());
/// let leaves = g.v().append(anon).to_list();
/// ```
pub fn not(self, sub: Traversal<Value, Value>) -> Traversal<In, Value> {
self.add_step(branch::NotStep::new(sub))
}
/// Filter by comparing current value to a labeled path value (not equal).
///
/// Emits input traverser only if the current value is NOT equal to the value
/// stored at the specified path label.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that excludes a labeled vertex
/// let anon = Traversal::<Value, Value>::new()
/// .in_labels(&["knows"]).where_neq("alice");
/// let not_alice = g.v().as_("alice").append(anon).to_list();
/// ```
pub fn where_neq(self, label: &str) -> Traversal<In, Value> {
self.add_step(branch::WhereNeqStep::new(label))
}
/// Filter by comparing current value to a labeled path value (equal).
///
/// Emits input traverser only if the current value IS equal to the value
/// stored at the specified path label.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that matches a labeled vertex
/// let anon = Traversal::<Value, Value>::new()
/// .in_labels(&["knows"]).where_eq("alice");
/// let is_alice = g.v().as_("alice").append(anon).to_list();
/// ```
pub fn where_eq(self, label: &str) -> Traversal<In, Value> {
self.add_step(branch::WhereEqStep::new(label))
}
/// Filter by multiple sub-traversals (AND logic) (for anonymous traversals).
///
/// Emits input traverser only if ALL sub-traversals produce at least one result.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that requires both conditions
/// let anon = Traversal::<Value, Value>::new()
/// .and_(vec![__.out(), __.in_()]);
/// let connected = g.v().append(anon).to_list();
/// ```
pub fn and_(self, subs: Vec<Traversal<Value, Value>>) -> Traversal<In, Value> {
self.add_step(branch::AndStep::new(subs))
}
/// Filter by multiple sub-traversals (OR logic) (for anonymous traversals).
///
/// Emits input traverser if ANY sub-traversal produces at least one result.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that accepts either condition
/// let anon = Traversal::<Value, Value>::new()
/// .or_(vec![__.has_label("person"), __.has_label("software")]);
/// let entities = g.v().append(anon).to_list();
/// ```
pub fn or_(self, subs: Vec<Traversal<Value, Value>>) -> Traversal<In, Value> {
self.add_step(branch::OrStep::new(subs))
}
// -------------------------------------------------------------------------
// Branch steps using anonymous traversals
// -------------------------------------------------------------------------
/// Execute multiple branches and merge results (for anonymous traversals).
///
/// All branches receive each input traverser; results are merged.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that executes multiple branches
/// let anon = Traversal::<Value, Value>::new()
/// .union(vec![__.out(), __.in_()]);
/// let neighbors = g.v().append(anon).to_list();
/// ```
pub fn union(self, branches: Vec<Traversal<Value, Value>>) -> Traversal<In, Value> {
self.add_step(branch::UnionStep::new(branches))
}
/// Try branches in order, return first non-empty result (for anonymous traversals).
///
/// Short-circuits on first successful branch.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that tries branches in order
/// let anon = Traversal::<Value, Value>::new()
/// .coalesce(vec![__.values("nickname"), __.values("name")]);
/// let names = g.v().append(anon).to_list();
/// ```
pub fn coalesce(self, branches: Vec<Traversal<Value, Value>>) -> Traversal<In, Value> {
self.add_step(branch::CoalesceStep::new(branches))
}
/// Conditional branching (for anonymous traversals).
///
/// Evaluates condition; if it produces results, executes if_true, otherwise if_false.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal with conditional branching
/// let anon = Traversal::<Value, Value>::new()
/// .choose(__.has_label("person"), __.out_labels(&["knows"]), __.out());
/// let results = g.v().append(anon).to_list();
/// ```
pub fn choose(
self,
condition: Traversal<Value, Value>,
if_true: Traversal<Value, Value>,
if_false: Traversal<Value, Value>,
) -> Traversal<In, Value> {
self.add_step(branch::ChooseStep::new(condition, if_true, if_false))
}
/// Optional traversal with fallback to input (for anonymous traversals).
///
/// If sub-traversal produces results, emit those; otherwise emit input.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal with optional step
/// let anon = Traversal::<Value, Value>::new()
/// .optional(__.out_labels(&["knows"]));
/// let results = g.v().append(anon).to_list();
/// ```
pub fn optional(self, sub: Traversal<Value, Value>) -> Traversal<In, Value> {
self.add_step(branch::OptionalStep::new(sub))
}
/// Execute sub-traversal in isolated scope (for anonymous traversals).
///
/// Aggregations operate independently for each input traverser.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal with local scope
/// let anon = Traversal::<Value, Value>::new()
/// .local(__.out().limit(1));
/// let results = g.v().append(anon).to_list();
/// ```
pub fn local(self, sub: Traversal<Value, Value>) -> Traversal<In, Value> {
self.add_step(branch::LocalStep::new(sub))
}
// -------------------------------------------------------------------------
// Mutation Steps
// -------------------------------------------------------------------------
/// Add or update a property on the current element.
///
/// This step modifies the current traverser's element (vertex or edge)
/// by setting a property value. For pending vertex/edge creations,
/// the property is accumulated. For existing elements, a pending
/// mutation is created.
///
/// The actual property update happens when the traversal is executed
/// via `MutationExecutor`.
///
/// # Example
///
/// ```ignore
/// // Chain properties after add_v
/// let vertex = g.add_v("person")
/// .property("name", "Alice")
/// .property("age", 30);
///
/// // Update properties on existing vertices
/// let updated = g.v_id(id).property("status", "active");
/// ```
pub fn property(self, key: impl Into<String>, value: impl Into<Value>) -> Traversal<In, Value> {
self.add_step(mutation::PropertyStep::new(key, value))
}
/// Delete the current element (vertex or edge).
///
/// When a vertex is dropped, all its incident edges are also dropped.
/// The actual deletion happens when the traversal is executed via
/// `MutationExecutor`.
///
/// # Example
///
/// ```ignore
/// // Drop specific vertices
/// let deleted = g.v_id(id).drop();
///
/// // Drop vertices matching criteria
/// let deleted = g.v().has_label("temp").drop();
/// ```
pub fn drop(self) -> Traversal<In, Value> {
self.add_step(mutation::DropStep::new())
}
// -------------------------------------------------------------------------
// Side Effect Steps (for anonymous traversals)
// -------------------------------------------------------------------------
/// Store traverser values in a side-effect collection (for anonymous traversals).
///
/// This is a **lazy step** - values are stored as they pass through the iterator,
/// not all at once. The traverser values pass through unchanged.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that stores values
/// let anon = Traversal::<Value, Value>::new()
/// .out()
/// .store("neighbors");
/// ```
pub fn store(self, key: impl Into<String>) -> Traversal<In, Value> {
self.add_step(sideeffect::StoreStep::new(key))
}
/// Aggregate all traverser values into a side-effect collection (for anonymous traversals).
///
/// This is a **barrier step** - it collects ALL values before continuing.
/// All input traversers are collected, stored, then re-emitted.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that aggregates values
/// let anon = Traversal::<Value, Value>::new()
/// .out()
/// .aggregate("all_neighbors");
/// ```
pub fn aggregate(self, key: impl Into<String>) -> Traversal<In, Value> {
self.add_step(sideeffect::AggregateStep::new(key))
}
/// Retrieve side-effect data by key (for anonymous traversals).
///
/// For a single key, returns the collection as a `Value::List`.
/// Consumes all input traversers before producing the result.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that retrieves stored data
/// let anon = Traversal::<Value, Value>::new()
/// .store("x")
/// .cap("x");
/// ```
pub fn cap(self, key: impl Into<String>) -> Traversal<In, Value> {
self.add_step(sideeffect::CapStep::new(key))
}
/// Retrieve multiple side-effect collections as a map (for anonymous traversals).
///
/// Returns a `Value::Map` with keys being the collection names
/// and values being `Value::List` of the stored items.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that retrieves multiple collections
/// let anon = Traversal::<Value, Value>::new()
/// .store("x")
/// .store("y")
/// .cap_multi(["x", "y"]);
/// ```
pub fn cap_multi<I, S>(self, keys: I) -> Traversal<In, Value>
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.add_step(sideeffect::CapStep::multi(keys))
}
/// Execute a sub-traversal for its side effects (for anonymous traversals).
///
/// The sub-traversal is executed for each input traverser, but its output
/// is discarded. The original traverser passes through unchanged.
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal that executes a side effect
/// let anon = Traversal::<Value, Value>::new()
/// .side_effect(__.out().store("neighbors"));
/// ```
pub fn side_effect(self, traversal: Traversal<Value, Value>) -> Traversal<In, Value> {
self.add_step(sideeffect::SideEffectStep::new(traversal))
}
/// Profile the traversal step timing and counts (for anonymous traversals).
///
/// Records the number of traversers and elapsed time in milliseconds
/// to the side-effects under the default key "~profile".
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal with profiling
/// let anon = Traversal::<Value, Value>::new()
/// .out()
/// .profile();
/// ```
pub fn profile(self) -> Traversal<In, Value> {
self.add_step(sideeffect::ProfileStep::new())
}
/// Profile the traversal with a custom key (for anonymous traversals).
///
/// Like `profile()`, but stores data under the specified key instead
/// of the default "~profile".
///
/// # Example
///
/// ```ignore
/// // Create an anonymous traversal with custom profile key
/// let anon = Traversal::<Value, Value>::new()
/// .out()
/// .profile_as("out_step_profile");
/// ```
pub fn profile_as(self, key: impl Into<String>) -> Traversal<In, Value> {
self.add_step(sideeffect::ProfileStep::with_key(key))
}
}