4 ms·
This is the TXR Lisp interactive listener of TXR 215. Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet. 1> [chain tostring trim-str] #<
by kazinator 8y ago
This is the TXR Lisp interactive listener of TXR 215.
Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for
cheatsheet.
1> [chain tostring trim-str]
#<intrinsic fun: 0 param + variadic>
2> [*1 3.4]
"3.4"
3> [[chain tostring trim-str] 3.4]
"3.4"
No ready example of a datatype that requires trimming after a string conversion! We'd have to write one:
4> (defstruct foo nil
(:method print (me stream pretty-p)
(put-string "foo " stream)))
#<struct-type foo>
5> (tostring (new foo))
"foo "
Okay we are in business:
6> [[chain tostring trim-str] (new foo)]
"foo"
Method chaining syntax is possible, but with actual methods only. Most library functions aren't methods; you have to roll your own:
1> (defstruct accum nil
(val 0)
(:method inc (me : (delta 1))
(inc me.val delta)
me)
(:method mul (me : (factor 2))
(upd me.val (* factor))
me))
#<struct-type accum>
2> (new accum).(inc).(inc).(mul 3)
#S(accum val 6)
3> (new accum).(inc).(inc).(mul 3).val
6
Support for idiom isn't the result of a conscious design decision; it just arises naturally when you have this sort of postfix syntax.
TXR's dot syntax is a very straightforward sugar for Lisp structure. Basically x.y.z == (qref x y z). Whitespace is not allowed around the dot. Numbers are also not allowed: 3.4 is a floating-point constant and a.3 or 3.a are invalid. If the leading element is missing, then it's the uref (unbound ref) operator: .y.z == (uref y z). This compiles to a function resembling (lambda (obj) obj.y.z).