Thinking in Clojure

92 views
Skip to first unread message

HB

unread,
Sep 2, 2010, 9:29:29 PM9/2/10
to Clojure
Hey,
I finished reading "Programming Clojure" and "Practical Clojure" and
I'm hooked :)
Please count me in the Clojure club.
But I failed how to think in Clojure.
My main career is around Java web applications (Hibernate, Spring,
Lucene) and Web services.
Lets not talk about Java web frameworks neither Clojure ones, I want
to talk in general.
Usually we create some domain entities, map them with Hibernate/
iBatis.
I don't know how a Clojure application would be build without objects.
I think Scala really shines here, this OOP/FP is really powerful
approach (please note I'm not saying Clojure isn't good, I don't seel
flame war)
How to think in Clojure? how to achieve this shift?

David Nolen

unread,
Sep 2, 2010, 9:55:02 PM9/2/10
to clo...@googlegroups.com
It does require a significant shift in thinking. I think you'll be surprised how far maps and functions will take you if you're used to thinking in OOP.

And contrary to popular belief Clojure is also a hybrid OOP/FP approach: multimethods, protocols, deftype, defrecord, definterface, etc. will let you utilize the better aspects of OOP design. However you should be cautious to reach for these. They are easily misapplied. Stick with the core datastructures (maps, vectors, sets, lists) and fns and you'll do just fine.

David 

HB

unread,
Sep 2, 2010, 10:02:03 PM9/2/10
to Clojure
So in idiomatic Clojure applications, maps are considered like
objects?
And to operate on them we pass them to functions?

On Sep 3, 4:55 am, David Nolen <dnolen.li...@gmail.com> wrote:

Wilson MacGyver

unread,
Sep 2, 2010, 10:02:25 PM9/2/10
to clo...@googlegroups.com
I highly recommend "Joy of Clojure". It's a good "2nd book on clojure".
It shows you the "why things are the way they are", and how to
do things the clojure way as much as possible.

--
Omnem crede diem tibi diluxisse supremum.

Miki

unread,
Sep 3, 2010, 12:05:20 AM9/3/10
to Clojure
I'd go over SICP, though it not in Clojure but in Scheme - it will
show you how to "think" functional.

Sean Corfield

unread,
Sep 3, 2010, 2:49:16 AM9/3/10
to clo...@googlegroups.com
On Thu, Sep 2, 2010 at 6:29 PM, HB <hubag...@gmail.com> wrote:
> Usually we create some domain entities, map them with Hibernate/
> iBatis.
> I don't know how a Clojure application would be build without objects.

I wonder if watching this talk by Rich Hickey will help?

http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey

It focuses on issues with mutability in the context of OO.
--
Sean A Corfield -- (904) 302-SEAN
Railo Technologies, Inc. -- http://getrailo.com/
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

Bob Hutchison

unread,
Sep 3, 2010, 8:00:13 AM9/3/10
to clo...@googlegroups.com

On 2010-09-02, at 10:02 PM, HB wrote:

> So in idiomatic Clojure applications, maps are considered like
> objects?
> And to operate on them we pass them to functions?

I think that considering maps as a state representation is reasonable. There are alternatives, but that's fine-tuning I think.

There's a lot you're going to have to take in, but I suspect the biggest sticking point will be around values/immutable-state, references to that state, and when/how the reference changes. When you get this, you'll be able to see how state/reference/time relate to the OO concepts you already know. At which point you'll be laughing.

I don't remember anybody I've worked with that has had any difficulty with picking up functional programming (aside from the immutability thing). It's one of those things that changes how you work forever -- you are going to feel pain when working with Java :-)

I strongly second the recommendation of the book "Joy Of Clojure" as a next thing to read (it's from Manning and you can get from their MEAP in pdf/epub formats)

Cheers,
Bob

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

----
Bob Hutchison
Recursive Design Inc.
http://www.recursive.ca/
weblog: http://xampl.com/so


Sean Allen

unread,
Sep 2, 2010, 11:24:22 PM9/2/10
to clo...@googlegroups.com

Reading a bit about OOP in either Common Lisp or Dylan might help ease you into
'thinking clojure'. It can provide a bridge between then Java OOP way
and other ways of doing OOP.

I had the opposite problem from you. When I started programming,
Dylan and Common Lisp style OOP made far more sense to me then the C++/Java way
( and the Smalltalk way but I didn't want to group it with C++/Java ).
There is a fundamental
difference in OOP in Lisp style languages from the Algol ones and
knowing more about those
other forms of OOP might make the path easier to see.

There is an older Guy Steele essay that talks in part about Scheme and
OOP ( something
that most people consider anti-ethical )
http://dreamsongs.com/ObjectsHaveNotFailedNarr.html
Basic message, some form of OOP is possible in any language, it is
just how easy it is made.

As David said, I would be careful about reaching for the methods in
Clojure that makes OOP
easier and work more with maps, vectors, fns etc and then look at the others.

-

for more info on OOP in dylan see:

http://www.opendylan.org/books/drm/

for more info about OOP in Common Lisp, I would recommened:

http://www.amazon.com/Object-Oriented-Programming-Common-Lisp-Programmers/dp/0201175894/ref=sr_1_9?ie=UTF8&s=books&qid=1283484149&sr=8-9

Peter Buckley

unread,
Sep 2, 2010, 11:36:32 PM9/2/10
to clo...@googlegroups.com
I'm only a little ways through Joy of Clojure (my first Clojure book)
but bear with me as I'm thinking aloud on what it means for me to
"think in Clojure." I hope list members will forgive me if I get
things wrong - and please correct my working concept(s) as well.

One of the things that stuck out for me that I heard somewhere (can't
remember exactly) was that OOP is about framing questions in terms of
"nouns" and FP is about framing questions in terms of "verbs."

Partly for my own benefit (and the list's critique and refinement) I
want to talk about this as it relates to an example. The first task
I'm working on in Clojure is taking a text file which is sortof a
template, and then removing/adding/changing certain lines/strings in
it based on an xml kindof config file, in the end spitting out an
improved text file.

If I was in the OOP frame of mind, I would have a class to represent
my text file, and a class to represent my xml file. There would
possibly be a shared base class to let me share the common slurping
(reading of lines) from the files. As verb-y as that is (I hope
Clojure is polluting my thought process already!) the base class would
probably be called "File" or something super noun-y. In any case, the
classes, the files, these are all "nouns."

But in the FP frame of mind, I just have data or information, and the
focus is on the transformations that I do with the data. The
transformations are things like zip-my-xml-into-a-struct-map or
read-my-lines-into-a-seq and replace-text-values-with-xml-values. The
focus is on the actions (verbs being action words), and the functions
are essentially the "verbs."

I feel like I'm starting to get the concept of thinking in a
function-al way, although I have a lot of work to do yet on learning
Clojure the language.

HTH,
Peter

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

--
The king’s heart is like a stream of water directed by the Lord; He
guides it wherever He pleases.

MarkH

unread,
Sep 3, 2010, 11:22:58 AM9/3/10
to Clojure
Sean and Peter made comments that ring very true for me. I've always
had a problem with Java/Smalltalk/C# type OO. I also thought the
Common Lisp/Dylan way of generic functions and data structures made
more sense. Like Peter mentioned, I tend to think in terms of verbs
and transformations of data. It just makes a lot more sense to me.

Chouser

unread,
Sep 3, 2010, 11:41:53 AM9/3/10
to clo...@googlegroups.com
On Thu, Sep 2, 2010 at 11:36 PM, Peter Buckley <buckme...@gmail.com> wrote:
>
> One of the things that stuck out for me that I heard somewhere (can't
> remember exactly) was that OOP is about framing questions in terms of
> "nouns" and FP is about framing questions in terms of "verbs."

Perhaps you heard it in Yegge's enjoyable essay:

http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

--Chouser

Michael Ossareh

unread,
Sep 3, 2010, 11:23:59 AM9/3/10
to clo...@googlegroups.com
On Thu, Sep 2, 2010 at 21:05, Miki <miki....@gmail.com> wrote:
I'd go over SICP, though it not in Clojure but in Scheme - it will
show you how to "think" functional.



--
!new number!
415-400-6772

HB

unread,
Sep 3, 2010, 12:12:50 PM9/3/10
to Clojure
Really nice example Peter,
Thanks, I appreciate it.

CuppoJava

unread,
Sep 3, 2010, 1:25:14 PM9/3/10
to Clojure
I had the exact same problem transitioning from OOP to Lisp, and I can
only offer my own experiences. I finally "understood" lisp, by
programming a new pet project FROM SCRATCH, in the most
STRAIGHTFORWARD way possible. I originally started by porting over a
program I had written in Java, and found that that did not help my
understanding at all.
-Patrick

Raoul Duke

unread,
Sep 3, 2010, 1:42:36 PM9/3/10
to clo...@googlegroups.com
On Fri, Sep 3, 2010 at 8:23 AM, Michael Ossareh <oss...@gmail.com> wrote:
>> I'd go over SICP, though it not in Clojure but in Scheme - it will
>> show you how to "think" functional.
> +1 on this.
> http://mitpress.mit.edu/sicp/

i think some folks argue that
http://www.htdp.org/
is even better :-)
or at least a good 'nother one to skim.

sincerely.

jt

unread,
Sep 3, 2010, 4:57:55 PM9/3/10
to Clojure
This may help!

http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/

"These twenty video lectures by Hal Abelson and Gerald Jay Sussman are
a complete presentation of the course, given in July 1986 for Hewlett-
Packard employees, and professionally produced by Hewlett-Packard
Television. The videos have been used extensively in corporate
training at Hewlett-Packard and other companies, as well as at several
universities and in MIT short courses for industry. "



On Sep 3, 10:23 am, Michael Ossareh <ossa...@gmail.com> wrote:

HB

unread,
Sep 3, 2010, 7:04:29 PM9/3/10
to Clojure
I'm interested in Conjure webframework and I'm considering trying to
read its source code and participate later.
What do you think?
Do you other projects in mind?
This discussion is amazing guys :)
Reply all
Reply to author
Forward
0 new messages