Re: OO in Python? ^^



In article <1134557152.748134.255180@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
bonono@xxxxxxxxx wrote:
> Magnus Lycka wrote:
> > Assume that you didn't use Python, but rather something with
> > static typing. How could you make a module such as my_module.py,
> > which is capable of working with any type that supports some
> > standard copy functionality and the +-operator?
>
> The following is a very short Haskell function I defined which I hope
> can give you some idea.
....

> *MyList> :type breakKeyword
> breakKeyword :: (Eq a) => [a] -> [a] -> ([a], [a])
>
> What it means is that breakKeyword can take any list of object type "a"
> so long it belongs to the class Eq. It can be char, number or whatever
> so long it is an instance of Eq.
>
> All that is needed for my custom data type(whatever it is) is that it
> must implment the compare function that "elem" would use to compare if
> a given object is in the list of token.
>
> *MyList> :type elem
> elem :: (Eq a) => a -> [a] -> Bool

Moreover, 1) this compare implementation is (as I understand it)
made available via runtime information, so a typeclass instance
may be implemented after the function that encounters it was compiled,
and 2) implementation often requires no more than "deriving Eq"
after the data type declaration -- assuming the data type is
composed of other types of data, you can infer the functional
composition of a typeclass instance.

The sum function in my_module.py was, in a very approximate sense,
like the standard "foldr" function. Of course foldr doesn't need to
copy, nor does it use any += operator. foldr is generic to any
function of type (a -> a -> b) (i.e., takes two parameters of one
type and returns a value of another type.) Lists don't support
a (+) operation (numeric only), but they support (++) concatenation
and (:) composition. foldr also takes an initial parameter of type b.

so,
foldr (:) "" ['a', 'b', 'c']
foldr (++) [] [[1, 2, 3], [4, 4, 4]]

Really, this kind of abstraction of data types is not only well
supported in Haskell, it can be almost a curse, at least for
someone like myself who has fairly superficial experience with
this kind of programming. After all the functions have been
zealously scrubbed clean of any trace of concrete data types and
rendered maximally abstract, they can be a little hard to understand.

Donn Cave, donn@xxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: Get recent time/date
    ... Does anyone know if there's ever a problem converting a ... > VB stores datetime values as doubles so I expect it's a simple copy ... > operation but why would you convert them when you can compare them ... I'm just thinking that VB probably compares the Double data type more ...
    (microsoft.public.vb.general.discussion)
  • Re: subform question
    ... In the table field for percent of composition I have double as field ... the data type is listed as a number. ... Dim TotalPercent As Long ... to undo all the edits to the current record. ...
    (microsoft.public.access.formscoding)
  • Re: long if function
    ... If it is a date/time data type, even though you are displaying as ... you can't compare Hour (ArrivalHour) to more than ... It might be a little cumbersome trying to rewrite the If...Then...ElseIf ...
    (microsoft.public.access.queries)
  • Mismatched Data Type in an Expression
    ... Below is a sub I am using to compare two request counters ... message on the SQL statement that says the data type is ... Dim M As Recordset, D As Database, C As Recordset ...
    (microsoft.public.access.modulesdaovba)
  • Re: Comparing string input to enum data type
    ... for something I'm attempting to do with enum data types. ... What I am doing now is reading from a text file a string. ... color it is from the enum data type. ... How can I compare a string like ...
    (comp.lang.c)