Steve Byan's Lists wrote on 05/31/2017 10:05 PM:
I'd appreciate a short example of what you mean by using `apply` and
`lambda` to destructure the list.
I'll babble more than you want here, in case anyone on the list is
curious in general...
#lang racket/base
(define (do-something-with-pmem-drain-record thread-id start-time
elapsed-time result)
(log-debug "DRAIN RESULT ~S" result))
(define (do-something-with-pmem-flush-record thread-id start-time
elapsed-time result addr length)
(log-debug "FLUSH RESULT ~S" result))
(define (do-something-with-record record)
(case (car record)
((pmem_drain) (apply do-something-with-pmem-drain-record
(cdr record)))
((pmem_flush) (apply do-something-with-pmem-flush-record
(cdr record)))
(else (error 'do-something-with-record
"invalid record ~S"
record))))
(define example-input
(open-input-string "(pmem_drain 140339047277632 923983542378153 83 0)"))
(do-something-with-record (read example-input))
That's just one way to do it that's rapid to write initially,
maintainable, and (possibly) very efficient at runtime.
For the efficiency, if this still looks like a target for improving
performance, you could compare to other approaches, including using
vectors like I mentioned earlier. (Ideally, profile the alternatives
with your real-world application and typical deployment data, with your
deployment version of Racket, with your deployment hardware/system
config under representative deployment loads/contentions. Because, of
course, this is more muddled than "O(n log n) is better than O(n^3)". :)
After `do-something-with-record` is implemented how you want, for all
your records, you might throw in a syntax extension, if that makes
things more maintainable. For a simple, probably-not-worthwhile sugar
example, if your final code just looks like the above, only with
additional clauses of the same form, you could decide it's worthwhile to
use special syntax:
(dispatch-on-record-type
record
(pmem_drain do-something-with-pmem-drain-record)
(pmem_flush do-something-with-pmem-flush-record))
Special syntax would make more sense if your message formats varied
more, or you had optimizations to make more maintainable. Note that the
original code example is instantly maintainable by any first-day
Racket/Scheme programmer of the last few decades, :) and you could
always tidy it up with a `(let ((args (cdr record))) ...)`.
If I have to give up too much in programming convenience, I may as well just
recode it in C++. Racket would still have been valuable in enabling me to
quickly evolve the design.
I agree fully. I just want to point out that, as a general comment, for
the list... With substantial systems, one will often see how tools like
Racket can be big wins, but still encounter occasions when you're glad
you also know how to play rough, since that capability makes the other
wins viable.
--
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.