Changeset 39470 in project
 Timestamp:
 12/30/20 05:26:53 (3 weeks ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

wiki/eggref/5/srfi189
r39422 r39470 69 69 <procedure>(just obj ...)</procedure> 70 70 71 Monadic pure. Returns the {{objs}}wrapped in a Just.71 Monadic pure. Returns the ''objs'' wrapped in a Just. 72 72 73 73 <procedure>(nothing)</procedure> … … 77 77 <procedure>(right obj ...)</procedure> 78 78 79 Monadic pure. Returns the {{objs}}wrapped in a Right.79 Monadic pure. Returns the ''objs'' wrapped in a Right. 80 80 81 81 <procedure>(left obj ...)</procedure> 82 82 83 Returns the {{objs}}wrapped in a Left.83 Returns the ''objs'' wrapped in a Left. 84 84 85 85 <procedure>(list>just list)</procedure> … … 87 87 <procedure>(list>right list)</procedure> 88 88 89 Returns a Just / Left / Right whose values are the elements of {{list}}. Note89 Returns a Just / Left / Right whose values are the elements of ''list''. Note 90 90 that these procedures are not monadic; they are equivalent to 91 91 just/left/right but accept a list rather than multiple arguments. There is … … 94 94 <procedure>(maybe>either maybe obj ...)</procedure> 95 95 96 If {{maybe}}is a Just, returns a Right with the same payload objects in the97 sense of {{eqv?}}; otherwise returns a Left of _objs_.96 If ''maybe'' is a Just, returns a Right with the same payload objects in the 97 sense of {{eqv?}}; otherwise returns a Left of ''objs''. 98 98 99 99 <procedure>(either>maybe either)</procedure> 100 100 101 If {{either}}is a Right, returns a Just with the same payload objects in the101 If ''either'' is a Right, returns a Just with the same payload objects in the 102 102 sense of {{eqv?}}; otherwise returns Nothing. 103 103 104 104 <procedure>(eitherswap either)</procedure> 105 105 106 If {{either}}is a Left, return a Right with the same payload objects (in the106 If ''either'' is a Left, return a Right with the same payload objects (in the 107 107 sense of {{eqv?}}), and vice versa. 108 108 … … 114 114 <procedure>(left? obj)</procedure> 115 115 116 Returns {{#t}} if {{obj}}is a Just, Nothing, Left, or Right respectively, and116 Returns {{#t}} if ''obj'' is a Just, Nothing, Left, or Right respectively, and 117 117 {{#f}} otherwise. 118 118 119 119 <procedure>(maybe? obj)</procedure> 120 120 121 Returns {{#t}} if {{obj}}is a Maybe (that is, either a Just or Nothing) and {{#f}}121 Returns {{#t}} if ''obj'' is a Maybe (that is, either a Just or Nothing) and {{#f}} 122 122 otherwise. 123 123 124 124 <procedure>(either? obj)</procedure> 125 125 126 Returns {{#t}} if {{obj}}is an Either (that is, either a Right or a Left) and126 Returns {{#t}} if ''obj'' is an Either (that is, either a Right or a Left) and 127 127 {{#f}} otherwise. 128 128 129 129 <procedure>(maybe= equal maybe1 maybe2 ...)</procedure> 130 130 131 Returns {{#t}} if all {{maybes}}are Nothing, or if they all are Justs and their132 respective payload objects are the same in the sense of {{equal}}, and {{#f}}131 Returns {{#t}} if all ''maybes'' are Nothing, or if they all are Justs and their 132 respective payload objects are the same in the sense of ''equal'', and {{#f}} 133 133 otherwise. 134 134 135 135 <procedure>(either= equal either1 either2 ...)</procedure> 136 136 137 Returns {{#t}} if all {{eithers}}are all Lefts or all Rights and their respective138 payload objects are the same in the sense of {{equal}}, and {{#f}} otherwise.137 Returns {{#t}} if all ''eithers'' are all Lefts or all Rights and their respective 138 payload objects are the same in the sense of ''equal'', and {{#f}} otherwise. 139 139 140 140 === Accessors … … 142 142 <procedure>(mayberef maybe failure [success])</procedure> 143 143 144 If {{maybe}} is a Just, tailcalls the procedure {{success}}on the values of its144 If ''maybe'' is a Just, tailcalls the procedure ''success'' on the values of its 145 145 payload and returns the result. Otherwise, it tailcalls the procedure 146 {{failure}}on no arguments and returns the result. The default value of147 {{success}}is {{values}}.146 ''failure'' on no arguments and returns the result. The default value of 147 ''success'' is {{values}}. 148 148 149 149 <procedure>(eitherref either failure [success])</procedure> 150 150 151 If {{either}} is a Right, tailcalls the procedure {{success}}on the values of151 If ''either'' is a Right, tailcalls the procedure ''success'' on the values of 152 152 its payload and returns the result. Otherwise, it tailcalls the procedure 153 {{failure}}on the values of its payload and returns the result. The default154 value of {{success}}is {{values}}.153 ''failure'' on the values of its payload and returns the result. The default 154 value of ''success'' is {{values}}. 155 155 156 156 <procedure>(mayberef/default maybe default ...)</procedure> 157 157 <procedure>(eitherref/default either default ...)</procedure> 158 158 159 If {{maybe/either}}is a Just/Right, returns the values of its payload;160 otherwise returns the {{defaults}}as multiple values.159 If ''maybe''/''either'' is a Just/Right, returns the values of its payload; 160 otherwise returns the ''defaults'' as multiple values. 161 161 162 162 === Join and bind … … 165 165 <procedure>(eitherjoin either)</procedure> 166 166 167 Monadic join. If {{maybe/either}}is a Just/Right whose single payload object is167 Monadic join. If ''maybe''/''either'' is a Just/Right whose single payload object is 168 168 a Maybe/Either, returns that payload; if it is a Nothing/Left, returns 169 {{maybe}}/{{either}}. In all other cases (including a Just/Right with multiple169 ''maybe''/''either''. In all other cases (including a Just/Right with multiple 170 170 values) it is an error. Thus {{(maybejoin (just (just x))}} returns Just 171 171 {{x}} and {{(maybejoin (just (nothing))}} returns Nothing, and similarly for … … 176 176 177 177 Returns an mproc that accepts zero or more arguments and applies the following 178 iterative algorithm to each {{mproc}}:179 180 The {{mproc}}is applied to the arguments and returns a Maybe/Either interim178 iterative algorithm to each ''mproc'': 179 180 The ''mproc'' is applied to the arguments and returns a Maybe/Either interim 181 181 result. If the result is Nothing / a Left, it is returned as the final result. 182 If it is a Just/Right, the next {{mproc}}is applied to the values of its183 payload, returning the next interim result. When no more {{mprocs}}are182 If it is a Just/Right, the next ''mproc'' is applied to the values of its 183 payload, returning the next interim result. When no more ''mprocs'' are 184 184 available, the interim result is returned as the final result. 185 185 186 It is an error if one of the {{mprocs}}does not accept as arguments the number186 It is an error if one of the ''mprocs'' does not accept as arguments the number 187 187 of values wrapped in the Just/Right produced by its predecessor. 188 188 … … 190 190 <procedure>(eitherbind either mproc1 mproc2 ...)</procedure> 191 191 192 Monadic bind. If {{maybe/either}}is a Just/Right, it behaves as if193 {{maybecompose}}/{{eithercompose}} were applied to {{mprocs}}, and the194 resulting mproc applied to the payload of {{maybe}}/{{either}}. But if195 {{maybe}}/{{either}}is a Nothing/Left, it is immediately returned.192 Monadic bind. If ''maybe/either'' is a Just/Right, it behaves as if 193 {{maybecompose}}/{{eithercompose}} were applied to ''mprocs'', and the 194 resulting mproc applied to the payload of ''maybe''/''either''. But if 195 ''maybe''/''either'' is a Nothing/Left, it is immediately returned. 196 196 197 197 === Sequence operations … … 204 204 <procedure>(eitherlength either)</procedure> 205 205 206 Return 1 if {{maybe}}/{{either}}is a Just/Right, and 0 otherwise.206 Return 1 if ''maybe''/''either'' is a Just/Right, and 0 otherwise. 207 207 208 208 <procedure>(maybefilter pred maybe)</procedure> … … 211 211 <procedure>(eitherremove pred either obj ...)</procedure> 212 212 213 If {{maybe}}/{{either}}is a Just/Right and the values of its payload satisfy / do214 not satisfy {{pred}}, then return {{maybe}}; otherwise, returns Nothing / a Left215 of {{objs}}.213 If ''maybe''/''either'' is a Just/Right and the values of its payload satisfy / do 214 not satisfy ''pred'', then return ''maybe''; otherwise, returns Nothing / a Left 215 of ''objs''. 216 216 217 217 <procedure>(maybesequence mappable map [aggregator])</procedure> … … 222 222 type. 223 223 224 The {{mappable}} argument is the collection to be transformed, and the {{map}}224 The ''mappable'' argument is the collection to be transformed, and the ''map'' 225 225 argument is the appropriate procedure that can transform it, one element at a 226 time. The signature of {{map}} is {{(map proc mappable)}}, where {{proc}}is226 time. The signature of ''map'' is {{(map proc mappable)}}, where ''proc'' is 227 227 supplied by the implementation. Typical mappables are lists and vectors, and 228 228 {{map}} and {{vectormap}} are useful map functions, though not the only possible … … 230 230 231 231 Each element is processed as follows: If it is a Just/Right, its values are 232 unwrapped and the {{aggregator}}function (which defaults to {{list}}) is applied232 unwrapped and the ''aggregator'' function (which defaults to {{list}}) is applied 233 233 to them. But if it is a Left/Nothing, that object is immediately returned as 234 234 the value of {{maybesequence}}/{{eithersequence}}. 235 235 236 Assuming there are no Lefts/Nothings, the {{map}}function is then responsible236 Assuming there are no Lefts/Nothings, the ''map'' function is then responsible 237 237 for constructing the new mappable out of the results of the calls on 238 {{aggregator}}.238 ''aggregator''. 239 239 240 240 For example, a list of Justs becomes a Just list of lists if the map procedure … … 267 267 <procedure>(either>list either)</procedure> 268 268 269 Returns a list whose elements are the payload of {{maybe}}/{{either}}; if270 {{maybe}}/{{either}}is Nothing/Left, returns the empty list. It is an error to269 Returns a list whose elements are the payload of ''maybe''/''either''; if 270 ''maybe''/''either'' is Nothing/Left, returns the empty list. It is an error to 271 271 mutate the result of this procedure. 272 272 … … 274 274 <procedure>(list>either list obj ...)</procedure> 275 275 276 If {{list}} is empty, returns Nothing / wraps {{objs}}in a Left and returns it;276 If ''list'' is empty, returns Nothing / wraps ''objs'' in a Left and returns it; 277 277 otherwise, wraps the list elements in a Just/Right and returns that. 278 278 … … 284 284 <procedure>(either>truth either)</procedure> 285 285 286 If {{maybe}}/{{either}}is a Just/Right, returns its payload; otherwise returns286 If ''maybe''/''either'' is a Just/Right, returns its payload; otherwise returns 287 287 {{#f}}. It is an error if the Just/Right does not have exactly one value. 288 288 … … 290 290 <procedure>(truth>either obj failobj ...)</procedure> 291 291 292 If {{obj}} is {{#f}}, return Nothing / a Left of {{failobjs}}; otherwise, return a293 Just/Right whose payload is {{obj}}.292 If ''obj'' is {{#f}}, return Nothing / a Left of ''failobjs''; otherwise, return a 293 Just/Right whose payload is ''obj''. 294 294 295 295 â The following procedures interface between the Maybe protocol and the … … 300 300 <procedure>(either>listtruth either)</procedure> 301 301 302 If {{maybe}}/{{either}}is a Just/Right, returns a list whose elements are the302 If ''maybe''/''either'' is a Just/Right, returns a list whose elements are the 303 303 payload; otherwise, returns {{#f}}. It is an error to mutate the result of this 304 304 procedure. … … 307 307 <procedure>(listtruth>either listorfalse obj ...)</procedure> 308 308 309 If {{listorfalse}} is {{#f}}, returns Nothing / a Left of {{objs}}; otherwise,309 If ''listorfalse'' is {{#f}}, returns Nothing / a Left of ''objs''; otherwise, 310 310 wraps the list elements in a Just/Right and returns it. 311 311 … … 317 317 <procedure>(either>generation either)</procedure> 318 318 319 If {{maybe}}/{{either}}is a Just/Right, then its payload is returned. It is an320 error if the Just/Right does not have exactly one value. If {{maybe}}/{{either}}319 If ''maybe''/''either'' is a Just/Right, then its payload is returned. It is an 320 error if the Just/Right does not have exactly one value. If ''maybe''/''either'' 321 321 is Nothing / a Left, then an endoffile object is returned. 322 322 … … 324 324 <procedure>(generation>either obj failobjs ...)</procedure> 325 325 326 If {{obj}} is an endoffile object, return Nothing / a Left of {{failobjs}}.327 Otherwise, return {{obj}}wrapped in a Just/Right.326 If ''obj'' is an endoffile object, return Nothing / a Left of ''failobjs''. 327 Otherwise, return ''obj'' wrapped in a Just/Right. 328 328 329 329 â The following procedures interface between the Maybe and Either protocols … … 334 334 <procedure>(either>values either)</procedure> 335 335 336 If {{maybe}}/{{either}}is a Just/Right, returns the values of its payload;336 If ''maybe''/''either'' is a Just/Right, returns the values of its payload; 337 337 otherwise returns no values. 338 338 … … 340 340 <procedure>(values>either producer failobj ...)</procedure> 341 341 342 These procedures invoke {{producer}}with no arguments. If no values are343 returned, Nothing / Left of {{failobjs}}is returned. Otherwise the values are342 These procedures invoke ''producer'' with no arguments. If no values are 343 returned, Nothing / Left of ''failobjs'' is returned. Otherwise the values are 344 344 wrapped in a Just/Right and returned. 345 345 … … 352 352 <procedure>(maybe>twovalues maybe)</procedure> 353 353 354 If {{maybe}}is a Just, returns the value of its payload and the additional355 value {{#t}}; otherwise returns two values, both {{#f}}. It is an error if {{maybe}}354 If ''maybe'' is a Just, returns the value of its payload and the additional 355 value {{#t}}; otherwise returns two values, both {{#f}}. It is an error if ''maybe'' 356 356 does not have exactly one value. 357 357 358 358 <procedure>(twovalues>maybe producer)</procedure> 359 359 360 This procedure is the inverse of {{maybe>twovalues}}. It invokes {{producer}}360 This procedure is the inverse of {{maybe>twovalues}}. It invokes ''producer'' 361 361 with no arguments. If the second value is true, the first value is wrapped in 362 362 a Just and returned; otherwise, Nothing is returned. … … 364 364 <procedure>(exception>either pred thunk)</procedure> 365 365 366 This procedure is in a sense the inverse of {{mayberef}}. A call to {{thunk}}is366 This procedure is in a sense the inverse of {{mayberef}}. A call to ''thunk'' is 367 367 made, wrapped in an exception handler that examines any exception signaled 368 during {{thunk}}'s execution. If the condition object satisfies {{pred}}, it is368 during ''thunk'''s execution. If the condition object satisfies ''pred'', it is 369 369 wrapped in a Left which is then returned; if it does not, the exception is 370 reraised. But if no exception is raised, the values returned by {{thunk}}are370 reraised. But if no exception is raised, the values returned by ''thunk'' are 371 371 wrapped in a Right and the Right is returned. 372 372 … … 376 376 <procedure>(eithermap proc either)</procedure> 377 377 378 Monadic map. If {{maybe}}/{{either}} is a Just/Right, applies {{proc}}to the payload378 Monadic map. If ''maybe''/''either'' is a Just/Right, applies ''proc'' to the payload 379 379 values and wraps the returned values as a Just/Right; otherwise returns 380 {{maybe}}/{{either}}.380 ''maybe''/''either''. 381 381 382 382 <procedure>(maybeforeach proc maybe)</procedure> 383 383 <procedure>(eitherforeach proc either)</procedure> 384 384 385 If {{maybe}}/{{either}} is a Just/Right, applies {{proc}}to the payload values and385 If ''maybe''/''either'' is a Just/Right, applies ''proc'' to the payload values and 386 386 discards the result; otherwise does nothing. Returns an unspecified result. 387 387 … … 389 389 <procedure>(eitherfold kons nil either)</procedure> 390 390 391 If {{maybe}}/{{either}} is a Just/Right, {{kons}}is invoked on the values of its392 payload and the additional value {{nil}}and the result is returned; otherwise,393 {{nil}}is returned.391 If ''maybe''/''either'' is a Just/Right, ''kons'' is invoked on the values of its 392 payload and the additional value ''nil'' and the result is returned; otherwise, 393 ''nil'' is returned. 394 394 395 395 <procedure>(maybeunfold stop? mapper successor seed ...)</procedure> 396 396 <procedure>(eitherunfold stop? mapper successor seed ...)</procedure> 397 397 398 If {{stop?}} returns true on {{seeds}}, a Nothing / a Left of {{seeds}}is returned.399 Otherwise, {{successor}} is applied to {{seeds}}. If {{stop?}}returns false on the400 results of {{successor}}, it is an error. But if the second call to {{stop?}}401 returns true, {{mapper}} is applied to {{seeds}}and the results are wrapped in a398 If ''stop?'' returns true on ''seeds'', a Nothing / a Left of ''seeds'' is returned. 399 Otherwise, ''successor'' is applied to ''seeds''. If ''stop?'' returns false on the 400 results of ''successor'', it is an error. But if the second call to ''stop?'' 401 returns true, ''mapper'' is applied to ''seeds'' and the results are wrapped in a 402 402 Just/Right and returned. 403 403 … … 407 407 408 408 Equivalent to {{(if (just? maybeexpr) justexpr nothingexpr)}}, except 409 that an error satisfying {{errorobject?}} is signaled if {{maybeexpr}}does not409 that an error satisfying {{errorobject?}} is signaled if ''maybeexpr'' does not 410 410 evaluate to a Maybe. 411 411 … … 413 413 <syntax>(eitherand expr ...)</syntax> 414 414 415 The {{expr}}s are evaluated in order. If an {{expr}}evaluates to a Nothing /415 The ''expr''s are evaluated in order. If an ''expr'' evaluates to a Nothing / 416 416 Left, it is returned without evaluating any more expressions. Otherwise, the 417 last Just / Right is returned. If there are no {{exprs}}, then a Just/Right of418 an unspecified value is returned. If an {{expr}}does not evaluate to a417 last Just / Right is returned. If there are no ''exprs'', then a Just/Right of 418 an unspecified value is returned. If an ''expr'' does not evaluate to a 419 419 Maybe/Either, an error satisfying {{errorobject}} is signaled. 420 420 … … 422 422 <syntax>(eitheror expr ...)</syntax> 423 423 424 The {{exprs}} are evaluated in order. If an {{expr}}evaluates to a Just/Right, it424 The ''exprs'' are evaluated in order. If an ''expr'' evaluates to a Just/Right, it 425 425 is returned without evaluating any more expressions. Otherwise, the last 426 Nothing/Left is returned. If there are no {{exprs}}, then Nothing / a Left of an427 unspecified value is returned. If an {{expr}}does not evaluate to a426 Nothing/Left is returned. If there are no ''exprs'', then Nothing / a Left of an 427 unspecified value is returned. If an ''expr'' does not evaluate to a 428 428 Maybe/Either, an error satisfying {{errorobject}} is signaled. 429 429 … … 433 433 The Maybe/Either equivalent of {{andlet*}} from [[http://srfi.schemers.org/srfi2/srfi2.htmlSRFI 2]]. 434 434 However, instead of testing 435 whether a {{claw}}is true or false, it tests whether it is Just/Right or436 Nothing/Left. If a {{claw}}does not evaluate to a Maybe/Either, an error437 satisfying {{errorobject}} is signaled. The same is true if a {{claw}}evaluates435 whether a ''claw'' is true or false, it tests whether it is Just/Right or 436 Nothing/Left. If a ''claw'' does not evaluate to a Maybe/Either, an error 437 satisfying {{errorobject}} is signaled. The same is true if a ''claw'' evaluates 438 438 to a Just or Right that wraps zero values or more than one value. 439 439 440 A {{claw}}is either a bound identifier, or a list of length 1, in which case it440 A ''claw'' is either a bound identifier, or a list of length 1, in which case it 441 441 is an expression, or a list of length 2, in which case it is a variable and an 442 expression. In either case, the value of the {{claw}}is the value of the bound443 variable or expression. If the value of a {{claw}}is Nothing / a Left, the444 expression immediately returns that value. But if the value of a {{claw}}is a442 expression. In either case, the value of the ''claw'' is the value of the bound 443 variable or expression. If the value of a ''claw'' is Nothing / a Left, the 444 expression immediately returns that value. But if the value of a ''claw'' is a 445 445 Just/Right, then the variable (if any) is bound to its unwrapped value; the 446 scope of this binding is any remaining {{claws}} and the {{body}}.447 448 If the values of all the {{claws}} are Just/Right, then the {{body}}is evaluated446 scope of this binding is any remaining ''claws'' and the ''body''. 447 448 If the values of all the ''claws'' are Just/Right, then the ''body'' is evaluated 449 449 as if it were in a {{(let () ...)}}, and its values are wrapped in a Just/Right 450 450 and returned. … … 464 464 <syntax>(eitherguard predexpr . body)</syntax> 465 465 466 The syntactic analogue of {{exception>either}}. The {{body}}is evaluated, and466 The syntactic analogue of {{exception>either}}. The ''body'' is evaluated, and 467 467 the values it produces are wrapped in a Right and returned, unless an 468 468 exception occurs. If the condition object that is raised satisfies the 469 predicate that is the value of {{predexpr}}, the condition object is wrapped in469 predicate that is the value of ''predexpr'', the condition object is wrapped in 470 470 a Left and returned; otherwise the condition is reraised as if by {{raisecontinuable}}. 471 471 … … 485 485 <procedure>(trinot maybe)</procedure> 486 486 487 Returns Just {{#t}} if {{maybe}} is false, Just {{#f}} if {{maybe}}is true, and488 Nothing if {{maybe}}is Nothing.487 Returns Just {{#t}} if ''maybe'' is false, Just {{#f}} if ''maybe'' is true, and 488 Nothing if ''maybe'' is Nothing. 489 489 490 490 <procedure>(tri=? maybe1 maybe2 ...)</procedure> 491 491 492 Similar to {{boolean=?}}, returning Just {{#t}} if all the {{maybes}}are true or if493 all are false. Otherwise, if any {{maybe}} is Nothing or any two {{maybes}}have492 Similar to {{boolean=?}}, returning Just {{#t}} if all the ''maybes'' are true or if 493 all are false. Otherwise, if any ''maybe'' is Nothing or any two ''maybes'' have 494 494 different (trivalent) truth values, returns Just {{#f}}. 495 495 … … 499 499 <procedure>(triand maybe ...)</procedure> 500 500 501 If all {{maybes}} are true, Just {{#t}} is returned. If any {{maybe}}is false or502 Nothing, then the first such {{maybe}}is returned. If there are no arguments,501 If all ''maybes'' are true, Just {{#t}} is returned. If any ''maybe'' is false or 502 Nothing, then the first such ''maybe'' is returned. If there are no arguments, 503 503 Just {{#t}} is returned. 504 504 505 505 <procedure>(trior maybe ...)</procedure> 506 506 507 If all {{maybes}} are false, Just {{#f}} is returned. If any {{maybe}}is true or508 Nothing, then the first such {{maybe}}is returned. If there are no arguments,507 If all ''maybes'' are false, Just {{#f}} is returned. If any ''maybe'' is true or 508 Nothing, then the first such ''maybe'' is returned. If there are no arguments, 509 509 Just {{#f}} is returned. 510 510 511 511 <procedure>(trimerge maybe ...)</procedure> 512 512 513 If any {{maybes}} are true or false, then the first such {{maybe}}is returned. If514 all {{maybes}}are Nothing, then Nothing is returned. If there are no arguments,513 If any ''maybes'' are true or false, then the first such ''maybe'' is returned. If 514 all ''maybes'' are Nothing, then Nothing is returned. If there are no arguments, 515 515 Nothing is returned. 516 516
Note: See TracChangeset
for help on using the changeset viewer.