Re: comparison of objects



On Sweetmorn, The Aftermath 64, 3172 YOLD, Rainer Joswig wrote:

Now anyone can use this package and shadowing-import your specialized
< operator. Any comparisons of numbers will be delegated to the
standard < operator and comparisons of time will go to your date-time
functions.

Personally, though, I think a better and more idiomatic solution is to
define a set of specific time comparison operators, like time<, time>,
time=, etc.

I agree. Using a function that looks like the built-in function is
likely to be confusing, since it becomes difficult to tell visually when
the standard function is being used versus your redefinition. Also,
your function won't be used by any other functions that do comparisons,
unless you shadow them as well. For instance, you won't be able to do:

OTOH, that's one of two things I like in Python (second one are
generators). Duck typing. Many libraries' data types are objects
that quack like an iterable, dict or overload attribute access and
with just a handful of language construction (for ... in ...,
attribute access, list comprehensions / generator expressions...). I
don't have to think about which of umpteen mapping functions should I
use or how do I compare one DateTime to another. And if my objects
quack as if they were less or greater than each other, I can safely
use all build-in and library functions that operate on objects by
comparing them for free. Same for quacking like a dict or iterable,
for attribute access, etc, etc, etc.

You get the same in CLOS-based libraries. There is not much CLOS-using
stuff in ANSI CL, but most Lisp systems already come with some
CLOS-based libraries or CLOS-based implementations.

Many CL implementations are using CLOS for example to implement streams,
windows, error handling and other things.

Yup, I do know CLOS and I use it. My problem is that CL doesn't ;)
comparison, iteration, element access and such don't have standard
CLOS-based API. I know I can invent some, since most standard types
are also classes and can be specialized on, but I was talking about
plain ANSI.

But your comment led mi to thinking about generic protocol for basic
operations. It would be divided into user's and implementor's
interface. User's interface would give means to iteration, element
access etc. facilities and implementor's interface would be generic
functions that should be specialized on in order to quack like
something. For starters, it could look like this:

* Element access

** User and implementor interface:

- Generic (ELEMENT object key) => OBJECT
- Generic ((SETF ELEMENT) new-value object key)
- General element access (with methods for e.g. nth, aref, hashref,
assoc, getprop).

* Iteration and sequences

Basic object would be an iterator function which yields consecutive
elements of an iterable, signalling END-OF-ITERATION condition when
elements end.

An iterable is an object for which an iterator can be defined.

** Implementor interface

- Condition END-OF-ITERATION
- Generic (ITERATOR object) => function
- Returns an iterator function for an object.

For objects that can be iterated multiple ways (e.g. files --
iterators can return characters, bytes, lines of text or Lisp forms)
there can be multiple functions returning different iterators -- an
iterator itself is an iterable.

** User interface

*** Basic interface

- Generic (IMAP iterable function) => iterator
- Returns iterator that FUNCALLs function on consecutive elements of iterable.

- Generic (IMAP* iterable function) => iterator
- Ditto, but APPLY'es elements to function

*** Utility interface

- Function (IDISCARD iterable) => NIL
- Function (ILIST iterable) => LIST
- Function (IARRAY iterable) => ARRAY
- Function (ISTRING iterable) => STRING
- Possibly more
- Runs over the iterator, returns respective type of sequence (or
just discards return values and returns NIL).

- Function (IZIP &rest iterables) => iterator
- Returns an iterator that yields a list of values of given
iterables (e.g. (IZIP '(A B C) '(D E F) '(G H I))
=> (A D G);(B E H);(C F I))

- Macro (IDO (variable iterable) &body body)
- Iterates BODY with VARIABLE bound to consecutive ITERABLE values.

- Macro (IDO* (variable iterable) &body body)
- Ditto, but variable can be a list and it's given to DESCRUCTURING-BIND.

* Operator overloading

** User and implementor interface

- Generic function (OVERLOADED op &rest args) => RESULT
- Specializes OP with (EQL) on standard function to apply
OVERLOADED-OP generic to ARGS, e.g.:

(defmethod overloaded ((op (eql #'<)) &rest args)
(apply #'overloaded-< args))

(defmethod overloaded-< (a b)
(funcall #'< a b))

(defmethod overloaded-< ((a string) (b string))
(funcall #'string< a b))


Does it make sense? Performance-wise it surely doesn't, but
readability- and elegance-wise? What more patterns can be abstracted
out this way?

See the attached png file for the subclasses of the mixin
HCI:HARDCOPY-DEVICE-MIXIN in Genera.

Well, I see just a greeting screen...

--
__ Maciek Pasternacki <maciekp@xxxxxxxxxxxxxxx> [ http://japhy.fnord.org/ ]
`| _ |_\ / { 2.718281828459045235360287471352662497757247093699959574966967
,|{-}|}| }\/ 62772407663035354759457138217852516642742746639193200305992181741
\/ |____/ 359662904357290033429526059563073813232862794349076... ( e ) -><-
.



Relevant Pages

  • Re: Virtual Iterators
    ... > Classes A and B both conform to the same abstract Interface class. ... > virtual VirtualIterator begin{return ... > copy of the real iterator. ...
    (comp.lang.cpp)
  • Re: Forevery() writable iterator mechanism in C# 3
    ... The '_itr_mth' LLP tag means 'Iterator Method'. ... Why would the interface need ref/out itself? ... A destructive operation will by definition be refelected in all references ... collection at all - that's really a major point of "yield", ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: javax.swing.text.HTMLDocument getIterator returns null for some Tags
    ... > I have encountered a problem which is reported multiple times in this ... > types, while working, equally mysteriously, for other tag types. ... It certainly is a peculiar interface, since a normal iterator ...
    (comp.lang.java.gui)
  • Re: Forevery() writable iterator mechanism in C# 3
    ... Currently you can not pass ref and out arguments to an iterator ... Why would the interface need ref/out itself? ... A destructive operation will by definition be refelected in all references ... collection at all - that's really a major point of "yield", ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] Add seq_file howto to Documentation/
    ... +position within the virtual file - that position is, likely as not, in the ... * An iterator interface which lets a virtual file implementation ... but which requires the seq_file pointer as an argument. ...
    (Linux-Kernel)