Data Pipeline Patterns
Transform Prices
A transform stage computes a new value for each input item.
Program
Play the program to subtract the selected discount from each price.
transform_prices.dart
Replay: real traced execution (multi-file project)
void main() {
var prices = [10, 25, 40];
var discount = 5;
var net = <int>[];
for (var price in prices) {
net.add(price - discount);
}
print('net=${net.join(",")}');
}
void main() {
var prices = [10, 25, 40];
var discount = 10;
var net = <int>[];
for (var price in prices) {
net.add(price - discount);
}
print('net=${net.join(",")}');
}
prices ← [10, 25, 40]
1void main() {2 var prices = [10, 25, 40];3 var discount = 5;values this step[10, 25, 40]pricesdiscount ← 5
2var prices = [10, 25, 40];3var discount = 5;4var net = <int>[];values this step5discountnet ← []
3var discount = 5;4var net = <int>[];5for (var price in prices) {values this step[]netprice ← 10
4var net = <int>[];5for (var price in prices) {6 net.add(price - discount);values this step10pricenet ← [5]
5for (var price in prices) {6 net.add(price - discount);7}values this step[5]net10price5discountprice ← 25
4var net = <int>[];5for (var price in prices) {6 net.add(price - discount);values this step25pricenet ← [5, 20]
5for (var price in prices) {6 net.add(price - discount);7}values this step[5, 20]net25price5discountprice ← 40
4var net = <int>[];5for (var price in prices) {6 net.add(price - discount);values this step40pricenet ← [5, 20, 35]
5for (var price in prices) {6 net.add(price - discount);7}values this step[5, 20, 35]net40price5discountprint('net=${net.join(",")}');
7 }8 print('net=${net.join(",")}');9}outputnet=5,20,35values this step[5, 20, 35]net
prices ← [10, 25, 40]
1void main() {2 var prices = [10, 25, 40];3 var discount = 10;values this step[10, 25, 40]pricesdiscount ← 10
2var prices = [10, 25, 40];3var discount = 10;4var net = <int>[];values this step10discountnet ← []
3var discount = 10;4var net = <int>[];5for (var price in prices) {values this step[]netprice ← 10
4var net = <int>[];5for (var price in prices) {6 net.add(price - discount);values this step10pricenet ← [0]
5for (var price in prices) {6 net.add(price - discount);7}values this step[0]net10price10discountprice ← 25
4var net = <int>[];5for (var price in prices) {6 net.add(price - discount);values this step25pricenet ← [0, 15]
5for (var price in prices) {6 net.add(price - discount);7}values this step[0, 15]net25price10discountprice ← 40
4var net = <int>[];5for (var price in prices) {6 net.add(price - discount);values this step40pricenet ← [0, 15, 30]
5for (var price in prices) {6 net.add(price - discount);7}values this step[0, 15, 30]net40price10discountprint('net=${net.join(",")}');
7 }8 print('net=${net.join(",")}');9}outputnet=0,15,30values this step[0, 15, 30]net
list
`[10, 25, 40]` creates an ordered list.
transform
`price - discount` computes one output per input.
typed list
`<int>[]` creates an empty integer list.