gmauer
2021-10-17 15:36:06

what’s the most straightforward way of creating a function decorator to do logging of function inputs/outputs? I’m thinking there has to be something like advice in emacs-lisp or decorators in python/js I could leverage, but google is failing me


capfredf
2021-10-17 15:38:30

you could try trace


gmauer
2021-10-17 15:44:00

ah, I guess that’s nice. Not quite what I’ looking for here but good to know about definitely


gmauer
2021-10-17 15:45:56

Alright, one more question - So in my program I find I am using a bunch of requires regularly, threading, racket/matrix, racket/array, data/collections

Is it idiomatic for me to define my own language that extends racket or racket/base or something but already has those requires in it?


laurent.orseau
2021-10-17 16:00:02

I’m not sure there’s anything idiomatic like that, but that’s not something frowned upon at least, afaict


laurent.orseau
2021-10-17 16:01:20

I think there’s even a package/utility that helps with the repetition of provide/require, but I don’t recall exactly what


laurent.orseau
2021-10-17 16:04:14

For the sake of DRY, it should even be encourage I guess


soegaard2
2021-10-17 17:04:56

You don’t need a new language. You can make a module in, say, “foo.rkt”.

(require threading racket/matrix racket/array data/collections) (provide (all-from-out threading racket/matrix racket/array data/collections)) And simple use (require "foo.rkt") in your files.


greg
2021-10-17 17:18:43

Not quite, how? (I ask because racket/trace is like advice or decorators. IIRC trace can mutate an already existing definition (which is like advice IIUC) and define-trace does it as part of defining the definition (which is like decorators IIUC).)


greg
2021-10-17 17:22:41

p.s. Some months ago I was exploring some ideas around tracing/logging with https://github.com/greghendershott/vestige. It’s not a package, it’s just a repo; you’ll have to read the docs as .scrbl not html; and I don’t know if/when I’ll make it “more official”. But you’re welcome to take a look on the chance it’s of any interest or use.


gmauer
2021-10-17 20:26:18

on my read of the docs it will output on every function call, and it also requires starting racket a certain way (-l trace?) So maybe I’m missing something but for scripts which call lots of functions (usually via recursion or in loops) or where you can’t easily control how racket is run (literate programming) it’s not as easy as just (around-advice ...) or somtehing.

But again, I might be misreading hte docs


gmauer
2021-10-17 20:57:06

hmm fair enough


alexharsanyi
2021-10-17 21:47:01

Some time ago I wrote a tracing capability for my own use, which allows me to trace only the functions I want. Basically, you can replace (define (some-function ...) ...) with (define/trace (some-function ...) ...) to trace a function. I suppose it is somewhat like decorators in other languages… If you are interested, you can find it here: https://gist.github.com/alex-hhh/331eb722ca7375ea6b53169db847d921


greg
2021-10-17 23:01:49

@gmauer Maybe you’re thinking of errortrace (one reason I named my thing “vestige” is because “trace” is so overloaded :smile:). Anyway you just (require racket/trace). Then you can either replace define with trace-define (which I suppose is ~= a decorated definition) or use the mutating trace (which I supposed is ~= advising an already-defined function).


greg
2021-10-17 23:02:41

The mutating trace and untrace are handy if you’re in a REPL, you can toggle the tracing on and off.


gmauer
2021-10-18 02:07:28

ah, yeah ok, I see that - I was looking at this page and didn’t see that https://docs.racket-lang.org/trace/index.html#%28mod-path._trace%29