On Tue, Jan 8, 2019 at 12:47 PM Eric Griffis <ded...@gmail.com> wrote:
> Apologies for the backtracking, but I'm still having trouble understanding > before/after/between and ordinary function composition. For non-method > functions, what's the difference? > Easy extension of existing code. For example: (define/contract (process hsh) (-> (hash/c symbol? any/c) list?) (for/list ([key (sort (hash-keys hsh) symbol<?)]) (some-func key)) 'process' is used a lot in your module and it gets its data from something that generates hash keys as symbols. Now you'd like to connect this module to a source that generates hash keys as strings -- maybe a database call. You can either go in and modify 'process' throughout the module, or you can modify the new source, or you can write a wrapper that converts the argument to have symbols as keys. One easy way to do that is: (require (only-in handy/hash hash-keys->symbols)) (before process hash-keys->symbols) Or, perhaps you want to tighten the contract of 'process' from "returns a list" to "returns a list of integers", but only in one module. You can use with-contract, or you can write a wrapper function, or you can say (after process (or/c (and/c (listof integer?) identity) (curry raise-arguments-error 'oops "bad result")) Or, maybe you want to ensure that, iff you're running in debug mode, all calls to 'process' will log their inputs and outputs, but you don't want to modify all the places where 'process' is called in the entire file. How about this?: ; at top of file (when is-debug-mode? (around process (lambda (hsh) (log-debug hsh) (define result (inner hsh)) (log-debug result) result)) ...and now you don't need to change it anywhere else. > Also, I'd never heard of these functions as a Perl dev. A quick search > turns up Moose. Is that what you're talking about? > Yep. On Tue, Jan 8, 2019 at 2:28 PM Neil Van Dyke <n...@neilvandyke.org> wrote: > > [1] However, if `before` in the quoted example is actually only > syntactic sugar for `define` with `apply`, then it's not mutating the > original procedure, so I can put down the fire extinguisher. In that > syntactic sugar case, whether to do it is a very local decision, of each > module that internally opts to use that sugar. Maybe that sugar makes > sense, for those particular modules that use it, within whatever > project/organization maintains that source code. This. In an ideal world, before/after/around would be parameterized so that you can make the change only for a defined scope. Still, the intent was never that it would extend its effects outside the current module. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.