-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch05.re
213 lines (156 loc) · 5.19 KB
/
ch05.re
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
open Js;
type undefined = exn;
type result('success, 'failure) =
| Ok('success)
| Error('failure);
// === Modeling Simple Values ===
type customerId =
| CustomerId(int);
type customerId2 = CustomerId(int);
type widgetCode = WidgetCode(string);
type unitQuantity = UnitQuantity(int);
type kilogramQuantity = KilogramQuantity(float);
// === Working with Single Case Unions ===
let customerId = CustomerId(42);
let CustomerId(innerValue) = customerId;
log(innerValue);
let processCustomerId = (CustomerId(innerValue)) => {
log({j|innerValue is $innerValue|j})
};
processCustomerId(customerId);
// === Avoiding Performance Issues with Simple Types
type unitQuantity3 = int;
// No translation yet for [<Struct>]
// No translation yet for UnitQuantities of int[]
// === Modeling Complex Data / Modeling Unknown Types ===
type customerInfo = undefined;
type shippingAddress = undefined;
type billingAddress = undefined;
type orderLine = {id: int, price: int};
type billingAmount = undefined;
type order = {
customerInfo: customerInfo,
shippingAddress: shippingAddress,
billingAddress: billingAddress,
orderLines: list(orderLine),
amountToBill: billingAmount
};
// === Modeling with Choice Types ===
type gizmoCode = undefined;
type productCode =
| Widget(widgetCode)
| Gizmo(gizmoCode);
type orderQuantity =
| Unit(unitQuantity)
| Kilogram(kilogramQuantity);
// === Modeling Workflows with Functions ===
type unvalidatedOrder = undefined;
type validatedOrder = undefined;
type validateOrder = (unvalidatedOrder) => validatedOrder;
// === Working with Complex Inputs and Outputs ===
type acknowledgementSent = undefined;
type orderPlaced = undefined;
type billableOrderPlaced = undefined;
type placeOrderEvents = {
acknowledgementSent: acknowledgementSent,
orderPlaced: orderPlaced,
billableOrderPlaced: billableOrderPlaced
};
type placeOrder = (unvalidatedOrder) => placeOrderEvents;
type quoteForm = undefined;
type orderForm = undefined;
type envelopeContents = EnvelopeContents(string);
type categorizedMail =
| Quote(quoteForm)
| Order(orderForm);
type categorizeInboundMail = (envelopeContents) => categorizedMail;
type productCatalog = undefined;
type pricedOrder = undefined;
type calculatePrices = (orderForm, productCatalog) => pricedOrder;
type calculatePricesInput = {
orderForm: orderForm,
productCatalog: productCatalog
};
type calculatePrices2 = (calculatePricesInput) => pricedOrder;
// === Documenting Effects in the Function Signature ===
type validateOrder2 = (unvalidatedOrder) => result(validatedOrder, list(validationError))
and validationError = {
fieldName: string,
errorDescription: string
};
// No solution yet for Async
type validationResponse('a) = result('a, list(validationError));
type validateOrder3 = (unvalidatedOrder) => validationResponse(validatedOrder);
// === A Question of Identity: Value Objects ===
let widgetCode1 = WidgetCode("W1234");
let widgetCode2 = WidgetCode("W1234");
log(widgetCode1 == widgetCode2); // prints "true"
type name = {firstName: string, lastName: string};
let name1 = {firstName: "Alex", lastName: "Adams"};
let name2 = {firstName: "Alex", lastName: "Adams"};
log(name1 == name2) // prints "true"
// etc.
// === A Question of Identity: Entities ===
type contactId = ContactId(int);
type contact = {
contactId: contactId,
phoneNumber: undefined,
emailAddress: undefined
};
// === Adding Identifiers to Data Definitions ===
// The outside approach (less common):
type unpaidInvoiceInfo = undefined;
type paidInvoiceInfo = undefined;
type invoiceInfo =
| Unpaid(unpaidInvoiceInfo)
| Paid(paidInvoiceInfo);
type invoiceId = int;
type invoice = {
invoiceId: invoiceId, // "outside" the two child cases
invoiceInfo: invoiceInfo
};
// The inside approach (more common):
type unpaidInvoice = {
invoiceId: invoiceId // id stored "inside"
};
type paidInvoice = {
invoiceId: invoiceId // id stored "inside"
};
type invoice2 =
| Unpaid(unpaidInvoice)
| Paid(paidInvoice);
let invoice = Paid({invoiceId: 42});
switch(invoice) {
| Unpaid(id) => log({j|The unpaid invoiceId is $id|j})
| Paid(id) => log({j|The paid invoiceId is $id|j})
};
// === Implementing Equality for Entities ===
type contactId2 = int;
type phoneNumber = int;
type emailAddress = string;
type contact2 = {
contactId: contactId2,
phoneNumber: phoneNumber,
emailAddress: emailAddress
};
let c1 = {contactId: 42, phoneNumber: 949121, emailAddress: "[email protected]"};
let c2 = {contactId: 42, phoneNumber: 949121, emailAddress: "[email protected]"};
log(c1 == c2); // Wrong! Need to find a solution yet
// === Immutability and Identity ===
type personId = PersonId(int);
type person = {personId: personId, name: string};
let initialPerson = {personId: PersonId(42), name: "Joseph"};
let updatedPerson = {...initialPerson, name: "Joe"};
log(updatedPerson);
type updateName = (person, name) => person;
// === Aggregates ===
/*
let changeOrderLinePrice = (order, orderLineId, newPrice) => {
let orderLine = order.orderLines |> findOrderLine(orderLineId);
let newOrderLine = {...orderLine, price: newPrice};
let newOrderLines =
order.orderLines |> replaceOrderLine(orderLineId, newOrderLine);
let newOrder = {...order, orderLines: newOrderLines};
newOrder;
};
*/