Re: Conceptual difference between map and class

2020-03-31 Thread James Gatannah
It might be worth mentioning that, ultimately, python class instances are 
syntactical sugar built around an internal __dict__. It gets twistier, 
since classes are also instances of the base class object.

It would be tricky (though I've seen an example...maybe in Joy of Clojure? 
I think the author's conclusion was that it was an interesting experiment, 
but not worth doing in practice) to implement inheritance using clojure 
maps.

For me, the conceptual difference has been that it's better to just write 
functions that work on simple data structures, rather than tying a few 
together into a unit that only work on a single data structure (plus its 
derived classes). Clojure's emphasis on functional purity is another major 
piece of this puzzle.

Unit testing is one thing that becomes much easier with this approach.

I deal with a ton of python code that has tests built around dozens of mock 
objects mimicking things like database records from an ORM. And then we 
mock out a few low-level HTTP calls in base classes so the class instances 
we're trying to test in isolation don't break when we run the tests with no 
web server to call. Then someone refactors the code, and all the tests 
break because they're tied to a specific package/module structure.

By contrast, if you have your business logic in one set of pure functions, 
and you keep your side-effecting parts well isolated on the fringes of your 
system, you don't need any of that. Just call the business logic function 
with appropriate values and double-check the results.

You absolutely can write python code that way. But your pythonic colleagues 
will hate you for it.

Hope that helps,
James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/c570d4aa-df51-47a1-bbdf-1efd61c5c7fa%40googlegroups.com.


Re: Conceptual difference between map and class

2020-03-31 Thread Matching Socks
Witty and instructive:
http://wiki.c2.com/?ClosuresAndObjectsAreEquivalent

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/37035a1d-0237-47ad-a2f0-16b56bcb58ab%40googlegroups.com.


Re: Conceptual difference between map and class

2020-03-31 Thread Justin Smith
I think it's also important here that Clojure methods are actual Java
methods - Clojure likes to stay close to the host functionality. A map
with a function isn't a class with a method because the JVM bytecode
doesn't let you invoke it that way directly. A Clojure function is not
a method because methods are not first class, it's an Object with a
static .invoke method (and some other details that are less important
here).

This object as hash with functions model is useful for learning (and
there's a lot of scheme and common lisp code that actually works this
way). With some effort, you can do everything with a function in a map
that you can with an object with a method if you are careful to
provide the object of the first arg.

But it's normal in Clojure to use the built in affordances. For real
code, defrecord gives you a new class that operates in a way similar
to this but with lower friction to the underlying bytecode (it
implements actual methods but also acts like a hash map for its
fields). Protocols let you use a hash map for extension (they act like
interfaces for classes, but you can add new implementations
dynamically, letting you do what you would do by adding a function
under a key on a hash, but tracking it on the protocol side).

On Tue, Mar 31, 2020 at 7:39 AM Jason Felice  wrote:
>
> A subtle difference between a map of functions and a Python class is that the 
> class has implicit "self" or "this".  Otherwise, these are semantically the 
> same.  Well, ignoring that Clojure maps are immutable.
>
> In fact, C++ compilers compile methods by inserting a first "this" argument 
> and mangling the name.  Virtual methods (ones which can be overridden) are 
> collected into what is effectively a map attached to the instance.
>
> On Tue, Mar 31, 2020 at 4:41 AM Dieter Van Eessen 
>  wrote:
>>
>> Hello,
>>
>> I've got a clojure and a python piece of code. Both seem to create what can 
>> be considered an instance of a class. Wherein lies the conceptual difference?
>>
>> Python:
>> class MYCLASS():
>> def __init__(self, x):
>>   self.x = x
>> def MYMETHOD(self):
>>  ...
>>
>> def MYFUNCTION():
>> lol = MYCLASS()
>>
>> Clojure:
>> (defn MYCLASS [x]
>>  {:x [x]
>>   :MYMETHOD (fn [] (MYCLASS ...))})
>>
>> (let [lol (MYCLASS ...)])
>>
>> I know its not valid code, but I hope you see what I'm aiming at: isn't 
>> using a map with functions in it just the same as a class?
>> Or is only the user interface of the language conceptually equal, while the 
>> underlying plumbing is completely different?
>> If this is the case, wherein lies the major differences?
>>
>> If one could simply point me in the right direction, I'd already be very 
>> pleased. Most literature I've read so far only explains clojure can be used 
>> this way, but never focuses deeper on the subject.
>>
>> kind regards,
>> Dieter
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clojure@googlegroups.com
>> Note that posts from new members are moderated - please be patient with your 
>> first post.
>> To unsubscribe from this group, send email to
>> clojure+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups 
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to clojure+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/clojure/49cd26ca-48ae-48b4-a5ab-b0f7c711b77e%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/clojure/CAB6_SoYCTeKi2teO%3DbgyEB-Vu3C-fCGpX6T%3DXZB8ApmDP-c8vA%40mail.gmail.com.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are 

Re: Conceptual difference between map and class

2020-03-31 Thread Jason Felice
A subtle difference between a map of functions and a Python class is that
the class has implicit "self" or "this".  Otherwise, these are semantically
the same.  Well, ignoring that Clojure maps are immutable.

In fact, C++ compilers compile methods by inserting a first "this" argument
and mangling the name.  Virtual methods (ones which can be overridden) are
collected into what is effectively a map attached to the instance.

On Tue, Mar 31, 2020 at 4:41 AM Dieter Van Eessen <
dieter.van.ees...@gmail.com> wrote:

> Hello,
>
> I've got a clojure and a python piece of code. Both seem to create what
> can be considered an instance of a class. Wherein lies the conceptual
> difference?
>
> Python:
> class MYCLASS():
> def __init__(self, x):
>   self.x = x
> def MYMETHOD(self):
>  ...
>
> def MYFUNCTION():
> lol = MYCLASS()
>
> Clojure:
> (defn MYCLASS [x]
>  {:x [x]
>   :MYMETHOD (fn [] (MYCLASS ...))})
>
> (let [lol (MYCLASS ...)])
>
> I know its not valid code, but I hope you see what I'm aiming at: isn't
> using a map with functions in it just the same as a class?
> Or is only the user interface of the language conceptually equal, while
> the underlying plumbing is completely different?
> If this is the case, wherein lies the major differences?
>
> If one could simply point me in the right direction, I'd already be very
> pleased. Most literature I've read so far only explains clojure can be used
> this way, but never focuses deeper on the subject.
>
> kind regards,
> Dieter
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/clojure/49cd26ca-48ae-48b4-a5ab-b0f7c711b77e%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/CAB6_SoYCTeKi2teO%3DbgyEB-Vu3C-fCGpX6T%3DXZB8ApmDP-c8vA%40mail.gmail.com.


Conceptual difference between map and class

2020-03-31 Thread Dieter Van Eessen
Hello,

I've got a clojure and a python piece of code. Both seem to create what can 
be considered an instance of a class. Wherein lies the conceptual 
difference?

Python:
class MYCLASS():
def __init__(self, x):
  self.x = x
def MYMETHOD(self):
 ...

def MYFUNCTION():
lol = MYCLASS()

Clojure:
(defn MYCLASS [x]
 {:x [x]
  :MYMETHOD (fn [] (MYCLASS ...))})

(let [lol (MYCLASS ...)])

I know its not valid code, but I hope you see what I'm aiming at: isn't 
using a map with functions in it just the same as a class?
Or is only the user interface of the language conceptually equal, while the 
underlying plumbing is completely different?
If this is the case, wherein lies the major differences?

If one could simply point me in the right direction, I'd already be very 
pleased. Most literature I've read so far only explains clojure can be used 
this way, but never focuses deeper on the subject.

kind regards,
Dieter

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/49cd26ca-48ae-48b4-a5ab-b0f7c711b77e%40googlegroups.com.