Skip to content

Commit 1e14414

Browse files
committed
Add a builder for replacements
- The builder can perform replacements or create an iterator - Rename replace_all to replace - Rename replace to replace_iter in Python API
1 parent a91c620 commit 1e14414

17 files changed

+564
-535
lines changed

examples/fibonacci.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use symbolica::{
22
atom::{Atom, AtomCore},
3-
id::{Match, WildcardRestriction},
3+
id::Match,
44
parse, symbol,
55
};
66

@@ -13,8 +13,7 @@ fn main() {
1313
let rhs_one = Atom::new_num(1).to_pattern();
1414

1515
// prepare the pattern restriction `x_ > 1`
16-
let restrictions =
17-
symbol!("x_").restrict(WildcardRestriction::filter(|v: &Match| v.to_atom() > 1));
16+
let restrictions = symbol!("x_").filter(|v: &Match| v.to_atom() > 1);
1817

1918
let mut target = parse!("f(10)").unwrap();
2019

@@ -25,10 +24,14 @@ fn main() {
2524

2625
for _ in 0..9 {
2726
let out = target
28-
.replace_all(&pattern, &rhs, Some(&restrictions), None)
27+
.replace(&pattern)
28+
.when(&restrictions)
29+
.with(&rhs)
2930
.expand()
30-
.replace_all(&lhs_zero_pat, &rhs_one, None, None)
31-
.replace_all(&lhs_one_pat, &rhs_one, None, None);
31+
.replace(&lhs_zero_pat)
32+
.with(&rhs_one)
33+
.replace(&lhs_one_pat)
34+
.with(&rhs_one);
3235

3336
println!("\t{}", out);
3437

examples/partition.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ fn main() {
44
let input = parse!("f(1,3,2,3,1)").unwrap();
55
let (f, g) = symbol!("f", "g");
66

7-
let o = input.replace_all(
8-
&parse!("f(x__)").unwrap().to_pattern(),
9-
&Pattern::Transformer(Box::new((
7+
let o = input
8+
.replace(parse!("f(x__)").unwrap())
9+
.with(Pattern::Transformer(Box::new((
1010
Some(parse!("x__").unwrap().to_pattern()),
1111
vec![Transformer::Partition(
1212
vec![(f, 2), (g, 2), (f, 1)],
1313
false,
1414
false,
1515
)],
16-
))),
17-
None,
18-
None,
19-
);
16+
))));
2017

2118
println!("> {}", o);
2219
}

examples/replace_all.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use symbolica::{atom::AtomCore, parse};
22

33
fn main() {
44
let expr = parse!(" f(1,2,x) + f(1,2,3)").unwrap();
5-
let pat = parse!("f(1,2,y_)").unwrap().to_pattern();
6-
let rhs = parse!("f(1,2,y_+1)").unwrap().to_pattern();
5+
let pat = parse!("f(1,2,y_)").unwrap();
6+
let rhs = parse!("f(1,2,y_+1)").unwrap();
77

8-
let out = expr.replace_all(pat, rhs, None, None);
8+
let out = expr.replace(pat).with(rhs);
99
println!("{}", out);
1010
}

examples/replace_once.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ fn main() {
1616
expr.as_view()
1717
);
1818

19-
for x in expr.replace_iter(&pattern, &rhs, None, None) {
19+
for x in expr.replace(pattern).iter(rhs) {
20+
// _iter(&pattern, &rhs, None, None) {
2021
println!("\t{}", x);
2122
}
2223
}

examples/streaming.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use brotli::CompressorWriter;
22
use symbolica::{
33
atom::AtomCore,
4-
id::Pattern,
54
parse,
65
streaming::{TermStreamer, TermStreamerConfig},
76
};
@@ -19,7 +18,7 @@ fn main() {
1918
stream.push(input);
2019

2120
// map every term in the expression
22-
stream = stream.map(|x| x.replace_all(&pattern, &rhs, None, None).expand());
21+
stream = stream.map(|x| x.replace(&pattern).with(&rhs).expand());
2322

2423
let res = stream.to_expression();
2524
println!("\t+ {}", res);

0 commit comments

Comments
 (0)