<mathrick>
what's the most elegant and/or compact way of only passing in an argument if the value is non-NIL, and otherwise leaving the default value untouched?
<mathrick>
ie. not this: (defun my-intern (name &optional package) (intern name package)), because this will just pass in NIL as package, rather than letting INTERN use the non-NIL default
<yitzi>
mathrick: specify a default value for package or use or.
<mathrick>
yitzi: but I don't want to know what INTERN's default is. It's implementation-specific and can change. I just want to let it have it if I have nothing useful to add
<mathrick>
the best I can think of is (apply #'intern name (when package `(,package))), but that's really ugly
<mathrick>
and also doesn't scale very well to keyword arguments or multiple defaults
<yitzi>
mathrick: Checkout the docs on lambda lists. They show you how to specify default values for arguments.
<mathrick>
yitzi: I know how to do that :) I just don't want to repeat the same default the function I'm calling has already specified. It's really not an uncommon problem, and not specific to INTERN
<yitzi>
You could use the supplied-p-parameter if you want to avoid knowing the default.
<mathrick>
the same problem in python is solved by kwargs = {'arg': arg} if arg else {}; other_func(**kwargs)
<mathrick>
yitzi: OK, tell me more please. How do I apply this knowledge to the actual call to INTERN?
<Bike>
mathrick: i understand you're not talking specifically about intern, but for the record, the default is definitely *package*. (sb-int:sane-package) is a wrapper function sbcl uses so that you can recover from mistakes you've made with *package*, but it will still eventually return *package*.
<mathrick>
Bike: right. It was actually prompted by INTERN in this particular instance, but you're right, it's not at all specific to INTERN. In this case, I wanted to have the same protections (SB-INT:SANE-PACKAGE) affords me
<Bike>
mathrick: the lisp equivalent to the python you wrote is pretty directly (let ((kwargs (if arg (list arg) nil))) (apply other-func kwargs)), or just (apply other-func (if arg (list arg) nil)).
<mathrick>
but it is a much more painful issue when calling a function in a third-party library with many keyword arguments with defaults
<mathrick>
Bike: aye, it's what I settled on. I just don't like how indirect and hard to read it is for what simple function it serves
<Bike>
if the python solution is ok i don't understand why the lisp wouldn't be
<mathrick>
mostly because python's solution still looks like a regular function call, but in CL, it changes to (APPLY #'FOO ...)
<mathrick>
although TBH, I've never found python's solution to be all that great either, it's just functional enough that I can mostly live with it
<mathrick>
and also I don't think I could do better
rgherdt_ has joined #commonlisp
<Bike>
if you really want, you could define a reader macro to make {foo ...} read as (apply #'foo ...). but i don't think that's a good idea
rgherdt has quit [Ping timeout: 252 seconds]
aartaka has quit [Ping timeout: 240 seconds]
aartaka has joined #commonlisp
<mathrick>
I don't either. I guess APPLY is just the best I can do, and will have to live with that
<yitzi>
Python's ternary operator is an abomination, so there's that.
<mathrick>
it is
<mathrick>
it's not the worst part of its syntax though, that title almost certainly goes to undecorated tuple literals. Ie. return foo, # <-- this is now a single-element tuple of (foo,)
tyson2 has quit [Remote host closed the connection]
comatory has quit [Ping timeout: 244 seconds]
tyson2 has joined #commonlisp
comatory has joined #commonlisp
tyson2 has quit [Remote host closed the connection]
sjl has joined #commonlisp
<aeth>
{} is for hash tables, though :-p
<aeth>
the best way to reset-to-default if the value is NIL is to provide the default twice, once in the lambda-list and once again in the body
<aeth>
I guess that would be (unless foo (setf foo default)) or something if you want a messy hack without adding an extra level of parens
<aeth>
Common Lisp has an amazing ternary
<aeth>
Because you just pull the setf/setq out, as if factoring. Very intuitive. (setf x (if c a b)) vs (if c (setf x a) (setf x b))
karlosz has joined #commonlisp
McParen has joined #commonlisp
susam_ is now known as susam
<mathrick>
aeth: I know it's for hash tables (or dicts, technically :). But you can do the same with *list, and also python allows you to refer to positional args with keywords, which I personally find immensely handy
<mathrick>
it took them a while to get keyword-only arguments (which are just keyword arguments in CL), but having the ability to treat positional arguments as keywords can be very helpful
<pve>
_death: Thanks for showing me that wikidata exists (with your Turing example yesterday), I had no idea. That thing is a gold mine. My code name generator will benefit greatly :)
Lord_of_Life_ has joined #commonlisp
Lord_of_Life has quit [Ping timeout: 244 seconds]
Lord_of_Life_ is now known as Lord_of_Life
morganw has joined #commonlisp
ttree has joined #commonlisp
karlosz has quit [Ping timeout: 244 seconds]
Oladon has joined #commonlisp
<aeth>
mathrick: it would actually be a very useful feature in Common Lisp when combined with allow-other-keys
<aeth>
everyone probably knows about &allow-other-keys but you can actually force it on at the caller, too
<aeth>
(defun foo (&key bar baz) (values bar baz)) (foo :bar 42 :barfoo 24 :allow-other-keys t)
<mathrick>
oh yeah, I just learn about it like two days ago
<aeth>
this means the APIs don't need to exactly match, as long as they're keyword
<mathrick>
*learnt
<aeth>
not really useful with the example I gave, but extremely useful with higher order functions (which you then funcall, so the example would be the same just replace "foo" with "funcall foo")
<aeth>
such a higher order function should work on anything that has the right keywords, but not every keyword is critically important to actually implement
azimut has quit [Ping timeout: 258 seconds]
cage has quit [Quit: rcirc on GNU Emacs 27.1]
tyson2 has joined #commonlisp
karlosz has joined #commonlisp
tyson2` has joined #commonlisp
tyson2` has quit [Remote host closed the connection]
tyson2 has quit [Ping timeout: 244 seconds]
tyson2 has joined #commonlisp
<_death>
pve: cool.. likely want to create a list of names in a single query and then filter them off, instead of a query per word
tyson2 has quit [Remote host closed the connection]