freenode/lisp - IRC Chatlog
Search
20:53:02
aeth
The correct solution, though, is always the one that's done through the type system. (handler-case (progn (check-type x null) x) (type-error () t))
20:58:24
grewal
I was trying to find a sillier way than aeth's example to get normalized truth values
21:12:03
aeth
Well you can have fun with heavyweight libraries to do a simple task, e.g. using specialization-store to type-dispatch: (defstore coerce-to-boolean (object)) (defspecialization coerce-to-boolean ((object (not null))) boolean (declare (ignore object)) t) (defspecialization coerce-to-boolean ((object null)) boolean object)
21:14:04
aeth
If you want to do something using just built-ins, I think this similar approach works: (defgeneric coerce-to-boolean (object)) (defmethod coerce-to-boolean ((object null)) object) (defmethod coerce-to-boolean ((object t)) (declare (ignore object)) t)
21:15:34
grewal
Yeah, I guess medium length joke things aren't as funny. It needs to be short or completely over the top
21:18:04
pjb
(not x) is one function call and one if. (not (not x)) is 2 function calls and two IFs. (if x t nil) is just one IF.
21:19:10
pjb
(not x) may be understood by random people, but they'll have a hard time, if they don't know lisp to infer that it'll return either the symbol T or the symbol NIL. (if x T NIL) on the other hand should be obvious to everybody, even most non-programmers.
21:20:22
aeth
pjb: trying to microoptimize this doesn't make sense because even the simplest optimizing compilers will probably optimize this away. e.g. I think SBCL has all of the simple examples as equivalent
21:21:00
pjb
aeth: I'm not optimizing the compiler, or compiled code, but the understanding by human readers.
21:23:08
Inline
truthy xes are also input to it but it just skips them, doin nothing on them or returning anything back....
21:26:48
aeth
This is the proper way to do things, btw. (let ((*print-case* :upcase)) (numberp (string/= "NIL" (format nil "~A" x))))
21:29:44
pjb
(mapcar (lambda (x) (let ((*print-case* :upcase)) (numberp (string/= "NIL" (format nil "~A" x))))) '(42 XLII NIL)) #| --> (t t nil) |#
21:38:16
sjl_
(defgeneric ensure-boolean (object)) (defmethod ensure-boolean ((object (eql nil))) object) (defmethod ensure-boolean (object) t)
21:47:01
selwyn
a few weeks ago, someone posted a good article that discussed how a hypothetical 'when-let*' macro would be fundamentally broken
21:48:41
mfiano
IIRC sjl_ posted about why, more than a few weeks ago: http://stevelosh.com/blog/2018/07/fun-with-macros-if-let/
21:49:14
sjl_
I don't think I claimed it would be fundamentally broken, only that's it's not as trivial as it appears at first glance.
21:49:45
sjl_
If you take care to do something reasonable with the declarations and such, I think you can have a fairly useful if-let*
21:51:18
sjl_
clojure's if-let doesn't support multiple bindings. is that maybe what you're thinking of? https://stackoverflow.com/questions/11676120/why-dont-when-let-and-if-let-support-multiple-bindings-by-default
21:52:55
sjl_
(and if there's only a single binding, there's no different between the -let and -let* variants, so it would make no sense to have it)
1:10:52
aeth
On the earlier topic of COERCE-TO-BOOLEAN, here's my latest attempt. It's too long to fit on an IRC line. https://gitlab.com/mbabich/cl-brainfuck/blob/5db780f08e6c0e686c1d8fe2f5ea78acf0d94163/brainfuck-examples.lisp#L64-105
1:11:37
aeth
pjb: and slight correction with earlier. It has to be ~S not ~A or else "NIL" also passes
3:25:05
pjb
hectorhonn: don't use :before :after :around Instead, write a primary method, and use (when (next-method-p) (call-next-method))
5:02:40
aeth
oh, instead of using ~s in format you can use format's conditionals to tell if it's true or false
5:17:31
aeth
So I think this is the best FORMAT-based version: (= 1 (length (format nil "~:[~; ~]" x)))
5:24:11
clothespin
i'm trying to find out how people think exceptions and signals should work in a lisp implementation before i go "fix" something
5:27:17
clothespin
i discovered c++ exceptions in foreign code work fine in sbcl on mac but are masked out by an overzealous handler on windows