3 ms·
TXR Lisp: $ txr -i version.tl 1> (equal (new (app-ver "1.2.003")) (new (app-ver "1.2.3"))) t 2> (equal (new (app-ver "1.2.003")) (new (app-ver "1.2.4")
by kazinator 1y ago
TXR Lisp:
$ txr -i version.tl
1> (equal (new (app-ver "1.2.003")) (new (app-ver "1.2.3")))
t
2> (equal (new (app-ver "1.2.003")) (new (app-ver "1.2.4")))
nil
3> (less (new (app-ver "1.2")) (new (app-ver "1.2.3")))
t
4> (greater (new (app-ver "1.2")) (new (app-ver "1.2.3")))
nil
5> (tostringp (new (app-ver "1.2.3.4")))
"1.2.3.4"
6> (tostring (new (app-ver "1.2.3.4")))
"#S(app-ver str \"1.2.3.4\" vec (1 2 3 4))"
Code:
(defstruct (app-ver str) ()
str
vec
(:postinit (me)
(set me.vec (flow me.str (spl ".") (mapcar int-str))))
(:method equal (me) me.vec)
(:method print (me stream pretty-p)
(if pretty-p (put-string `@{me.vec "."}` stream) :)))
- kazinator 1y agoThese objects can be inserted into a hash table, and are keyed by their semantic value, not the string: 1> (hash) #H(()) 2> *1 #H(()) 3> (set [*1 (new (app-ver "1.2.3.0004"))] 'mapped) mapped 4> *1 #H(() (#S(app-ver str "1.2.3.0004" vec (1 2 3 4)) mapped)) 5> [*1 (new (app-ver "1.2.3.4"))] mapped 6> [*1 (new (app-ver "1.2.03.4"))] mapped 7> [*1 (new (app-ver "1.2.02.4"))] nil