On Sun, Jan 11, 2009 at 10:50 AM, e <evier...@gmail.com> wrote:

>
> refs seem silly in this context!  Now I REALLY have to get my head
> wrapped around clojure.  sooooooo I only have one thread, but I have
> to do a dosync?  Why can this whole function just work in a thread
> agnostic way?  It's a local variable!  If a thread calls it, then a
> thread should own the memory that's created.  Each thread should have
> its own asynchronous stack to push local variables onto that no one
> else is allowed to see.  Seems like all that transaction, dosync stuff
> ought to be optional.
>

> (let [x (ref 3)]
>   (dosync
>     (while (> @x 0)
>       (ref-set x (- @x 1)))
>     @x))

Refs synchronize within a transaction. Atoms only synchronize within a
single operation. swap! takes an atom and a function that should be
atomically applied to that atom.

(let [x (atom 3)]
  (while (> @x 0)
    (swap! x dec))
  @x)

http://clojure.org/atoms

But you want to use the (set x (- x 1)) form that is common in many other
languages? We can create an unsafe-set that does that and completely ignores
synchronization, since you know that synchronization won't be needed.
compare-and-set! takes an atom, the atom's expected value, and a new value.
Since you want to just assume that there will be no conflicts, we can make
an unsafe-set that skips the comparison and just performs the operation.

(defn unsafe-set [a v]
  (compare-and-set! a @a v)
  v)

(let [x (atom 3)]
  (while (> @x 0)
    (unsafe-set x (- @x 1)))
  @x)


> Oh, and my other choice is to use recursion.  I'll look into that
> recur thing.


The Clojure standard library is designed to support functional programming
(including recursion) and concurrency. I recommend going with the flow, but
the language is flexible enough to work the way you want. Just define the
operations that you like to use. Also, you have access to Java libraries,
which have mutable collections.



>
>
> On Jan 11, 1:27 am, "Eric Lavigne" <lavigne.e...@gmail.com> wrote:
> > > I have no idea how to iteratively mess with it since everything is
> > > persistent.  Ok, like, say it's a list of lists and I am going to be
> > > merging the lists, like Tarjan's mergesort from some book from
> > > college.
> >
> > Sorting is done much more easily with recursion than with iteration.
> > However, it looks like you are focused on learning language features
> rather
> > than programming strategy, so I will just answer your questions.
> >
> > I have not actually done any iterative programming in Clojure, as I
> prefer
> > the functional approach, so my answers are based on my limited
> understanding
> > of the Clojure documentation.
> >
> > so I have myList with contents [[11] [2] [4] [1] [99]]
> >
> >
> >
> >
> >
> > > here's how I would do it in python:
> >
> > > def msort(myList):
> > >  myList = [[x] for x in someList]
> > >  while len(myList) > 1:
> > >    l1 = myList.pop(0)
> > >    l2 = myList.pop(0)
> > >    listmerge = some_merge_function(l1, l2)
> > >    myList.append(listmerge)          # important that newly merged go
> > > to back of queue to get proper runtime
> > >  return myList[0]
> >
> > > here's what I'm trying to do for clojure, and it's a mess:
> >
> > > (defn msort [toSort]
> > >  (def sorted (let
> >
> > Be careful with def. I think that it creates a global, and it looks like
> you
> > want something with a scope limited to this function.
> >
> > http://clojure.org/Vars
> >
> > >   [myList (for [x toSort] [x])]      <----- so far so good (not a
> > > real comment.  I don't know how, yet)
> >
> > Use a semicolon to create a comment:
> >
> >      some code ; a comment
> >
> > >   [
> > >    (while (> (count myList) 1)      <------- infinite loop the way
> > > written?  I don't know how to overwrite myList
> > >      (let [l1 (nth myList 0)][])
> >
> > The line above does nothing. When you write "(let [x 1] expr1) expr2" the
> > new value of x has a narrow scope so that it only affects expr1.
> >
> > You are wanting to create a local variable that you can change. Refs can
> do
> > that:
> >
> > (let [x (ref 3)]
> >   (dosync
> >     (while (> @x 0)
> >       (ref-set x (- @x 1)))
> >     @x))
> >
> > http://clojure.org/Refs
> >
> > >      (let [l2 (nth myList 1)][])
> > >      (let [listmerge (some_merge_func l1 l2)][])
> > >      (let [myList (concat (drop 2 myList) listmerge)][myList])  <---
> > > probably a different local variable
> > >      )
> > >   ]))
> > >  sorted)
> >
> > After all that work you just return the original list? I must have missed
> > the part where you tried to change sorted. Any such attempt would fail,
> > though, because Clojure collections are immutable. Maybe sorted should be
> a
> > ref to a list instead of just a list.
> >
> > Here is a discussion of sorting implementations in Clojure. I hope that
> you
> > find it useful.
> >
> > http://www.fatvat.co.uk/2008/12/bubbling-clojure.html
> >
> > --
> > Education is what survives when what has been learned has been forgotten.
> >
> >                    - B. F. Skinner
> >
>


-- 
Education is what survives when what has been learned has been forgotten.

                   - B. F. Skinner

--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to