IRC channel logs

2024-09-10.log

back to list of logs

<xelxebar>What's an idiomatic way of building an explicit list where some items are conditionally included?
<xelxebar>The noise in `(blah ... ,@(if cond (item)) ...) kind of grates against me.
<xelxebar>Wondering if something like (let ((lst '())) (if cond (cons item list)) ...) is reasonable.
<xelxebar>Uh. Obviously we'd need to wrap in set!. Blah... Probably even worse.
<xelxebar>Wait. Maybe putting #f or whatever in the if alternate and post-processing with a filter is better.
<xelxebar>(filter identity (list ... (if cond item) ...))
<xelxebar>Whoops. (if cond item #f)
<flatwhatson>xelxebar: filter-map or append-map from srfi-1 can be useful for this
<sneek>wb dthompson :D
<xelxebar>flatwhatson: Ah, okay. Convenient wrappers. Chers.
<mwette>what about fold
<mwette>filter prob better
<morenonatural>can/should/would I do `match` on a association list?
<morenonatural>on that same matter, I'm having trouble doing assq-ref on a association list that's constructed using (STRING . FLOAT) key-values... cannot write a MWE, but can consistently reproduce
<morenonatural>not sure on where to look, I'm doing asserts on key and value... key is string, value is float, (display)s expected key-values
<morenonatural>... but assq-ref returns #f
<morenonatural>also tried doing 'STRING instead of "STRING" as key
<dthompson>morenonatural: you need to use assoc-ref for equal? based lookup
<dthompson>and 'match' will not work on association lists as they are unordered
<morenonatural>dthompson, that worked... thanks
<morenonatural>any examples on ($ my-record) matching? I'm getting (unbound)s on the slots
<dthompson>consider: (define-record-type <foo> make-foo foo? (bar foo-bar))
<dthompson>or rather: (define-record-type <foo> (make-foo bar) foo? (bar foo-bar))
<dthompson>(match (make-foo 42) (($ <foo> bar) bar)) ;; => 42
<morenonatural>ooh, my bad ... I was using define-class
<morenonatural>dthompson, your suggestion did the trick
<dthompson>👍