Re: update a ref struct

2008-11-24 Thread Kevin Downey

I know you are asking about refs, but you might want to think about
using reduce to walk the line-seq. the nature of reduce lets you have
access to the line-seq, two lines at a time no need for a ref.

On Mon, Nov 24, 2008 at 2:17 PM, Brian Doyle <[EMAIL PROTECTED]> wrote:
> I am parsing a file and to compare the current line
> with the previous line of the file.  I am using line-seq
> to go thru the file and I thought I would create a
> ref to store the previous line.   When I want to update
> the previous line value I can't seem to do it.  I've
> never used refs before so I'm sure I'm doing something
> very stupid.
>
> (defstruct line :lat :lon :name)
>
> (defn convert [file]
>   (let [prev-line (ref (struct line))]
> (with-open [r (reader file)]
>(doseq [l (line-seq r)]
>  (let [ps (split #"," l)
> c-line (struct line (ps 0) (ps 1) (ps 2))]
>(if (not= c-line @pre-line)
>  (do ; do stuff here then update pre-line
> (dosync ref-set pre-line (apply struct line (vals c-line)))
> (println @pre-line  ; this prints out all nils
> doesn't seem to work
>
>
>
> Sorry if this email is not formatted correctly.  Something is wrong with my
> browser
> currently typing in a textarea.  Thanks.
>
>
>
> >
>



-- 
The Mafia way is that we pursue larger goals under the guise of
personal relationships.
Fisheye

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: update a ref struct

2008-11-24 Thread Brian Doyle
Thanks Kevin, I will try using reduce instead.  I would like to know what
I'm doing wrong with updating the ref for future reference.  Thanks.

On Mon, Nov 24, 2008 at 3:23 PM, Kevin Downey <[EMAIL PROTECTED]> wrote:

>
> I know you are asking about refs, but you might want to think about
> using reduce to walk the line-seq. the nature of reduce lets you have
> access to the line-seq, two lines at a time no need for a ref.
>
> On Mon, Nov 24, 2008 at 2:17 PM, Brian Doyle <[EMAIL PROTECTED]>
> wrote:
> > I am parsing a file and to compare the current line
> > with the previous line of the file.  I am using line-seq
> > to go thru the file and I thought I would create a
> > ref to store the previous line.   When I want to update
> > the previous line value I can't seem to do it.  I've
> > never used refs before so I'm sure I'm doing something
> > very stupid.
> >
> > (defstruct line :lat :lon :name)
> >
> > (defn convert [file]
> >   (let [prev-line (ref (struct line))]
> > (with-open [r (reader file)]
> >(doseq [l (line-seq r)]
> >  (let [ps (split #"," l)
> > c-line (struct line (ps 0) (ps 1) (ps 2))]
> >(if (not= c-line @pre-line)
> >  (do ; do stuff here then update pre-line
> > (dosync ref-set pre-line (apply struct line (vals
> c-line)))
> > (println @pre-line  ; this prints out all nils
> > doesn't seem to work
> >
> >
> >
> > Sorry if this email is not formatted correctly.  Something is wrong with
> my
> > browser
> > currently typing in a textarea.  Thanks.
> >
> >
> >
> > >
> >
>
>
>
> --
> The Mafia way is that we pursue larger goals under the guise of
> personal relationships.
>Fisheye
>
> >
>

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: update a ref struct

2008-11-24 Thread Shawn Hoover
On Mon, Nov 24, 2008 at 5:17 PM, Brian Doyle <[EMAIL PROTECTED]> wrote:

> I am parsing a file and to compare the current line
> with the previous line of the file.  I am using line-seq
> to go thru the file and I thought I would create a
> ref to store the previous line.   When I want to update
> the previous line value I can't seem to do it.  I've
> never used refs before so I'm sure I'm doing something
> very stupid.
>
> (defstruct line :lat :lon :name)
>
> (defn convert [file]
>   (let [prev-line (ref (struct line))]
> (with-open [r (reader file)]
>(doseq [l (line-seq r)]
>  (let [ps (split #"," l)
> c-line (struct line (ps 0) (ps 1) (ps 2))]
>(if (not= c-line @pre-line)
>  (do ; do stuff here then update pre-line
> (dosync ref-set pre-line (apply struct line (vals c-line)))
> (println @pre-line  ; this prints out all nils
> doesn't seem to work
>
>
Your let binds prev-line, while the code inside uses pre-line. Is that just
a typo in your email, or is that the code you're running, too?

Shawn

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: update a ref struct

2008-11-24 Thread Kevin Downey

ref-set needs its one set of parens, and the last thing in the ref-set
call needs to be a function either (fn [x] ...) or a symbol for a var
that holds a function

On Mon, Nov 24, 2008 at 2:30 PM, Brian Doyle <[EMAIL PROTECTED]> wrote:
> Thanks Kevin, I will try using reduce instead.  I would like to know what
> I'm doing wrong with updating the ref for future reference.  Thanks.
>
> On Mon, Nov 24, 2008 at 3:23 PM, Kevin Downey <[EMAIL PROTECTED]> wrote:
>>
>> I know you are asking about refs, but you might want to think about
>> using reduce to walk the line-seq. the nature of reduce lets you have
>> access to the line-seq, two lines at a time no need for a ref.
>>
>> On Mon, Nov 24, 2008 at 2:17 PM, Brian Doyle <[EMAIL PROTECTED]>
>> wrote:
>> > I am parsing a file and to compare the current line
>> > with the previous line of the file.  I am using line-seq
>> > to go thru the file and I thought I would create a
>> > ref to store the previous line.   When I want to update
>> > the previous line value I can't seem to do it.  I've
>> > never used refs before so I'm sure I'm doing something
>> > very stupid.
>> >
>> > (defstruct line :lat :lon :name)
>> >
>> > (defn convert [file]
>> >   (let [prev-line (ref (struct line))]
>> > (with-open [r (reader file)]
>> >(doseq [l (line-seq r)]
>> >  (let [ps (split #"," l)
>> > c-line (struct line (ps 0) (ps 1) (ps 2))]
>> >(if (not= c-line @pre-line)
>> >  (do ; do stuff here then update pre-line
>> > (dosync ref-set pre-line (apply struct line (vals
>> > c-line)))
>> > (println @pre-line  ; this prints out all nils
>> > doesn't seem to work
>> >
>> >
>> >
>> > Sorry if this email is not formatted correctly.  Something is wrong with
>> > my
>> > browser
>> > currently typing in a textarea.  Thanks.
>> >
>> >
>> >
>> > >
>> >
>>
>>
>>
>> --
>> The Mafia way is that we pursue larger goals under the guise of
>> personal relationships.
>>Fisheye
>>
>>
>
>
> >
>



-- 
The Mafia way is that we pursue larger goals under the guise of
personal relationships.
Fisheye

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: update a ref struct

2008-11-24 Thread Jeff Rose

For using references in general, here is a little example:

[EMAIL PROTECTED]:~$ clj
Clojure
user=> (def foo (ref 0))
#'user/foo
user=> foo
#
user=> @foo
0
user=> (ref-set foo 1)
java.lang.IllegalStateException: No transaction running (NO_SOURCE_FILE:0)
user=> (dosync (ref-set foo 1))
1
user=> @foo
1
user=>


Notice that you have to wrap ref-set in a transaction using dosync, and 
reading the reference requires the @ macro.

-Jeff

Brian Doyle wrote:
> I am parsing a file and to compare the current line
> with the previous line of the file.  I am using line-seq
> to go thru the file and I thought I would create a
> ref to store the previous line.   When I want to update
> the previous line value I can't seem to do it.  I've
> never used refs before so I'm sure I'm doing something
> very stupid.  
> 
> (defstruct line :lat :lon :name)
> 
> (defn convert [file]
>   (let [prev-line (ref (struct line))]
> (with-open [r (reader file)]
>(doseq [l (line-seq r)]
>  (let [ps (split #"," l)
> c-line (struct line (ps 0) (ps 1) (ps 2))]
>(if (not= c-line @pre-line)
>  (do ; do stuff here then update pre-line
> (dosync ref-set pre-line (apply struct line (vals c-line)))
> (println @pre-line  ; this prints out all nils 
> doesn't seem to work
> 
> 
> 
> Sorry if this email is not formatted correctly.  Something is wrong with 
> my browser
> currently typing in a textarea.  Thanks.
> 
> 
> 
> > 


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: update a ref struct

2008-11-24 Thread Kevin Downey

On Mon, Nov 24, 2008 at 2:34 PM, Kevin Downey <[EMAIL PROTECTED]> wrote:
> ref-set needs its one set of parens, and the last thing in the ref-set
> call needs to be a function either (fn [x] ...) or a symbol for a var
> that holds a function

I made a mistake here. I was thinking of alter, not ref-set.


> On Mon, Nov 24, 2008 at 2:30 PM, Brian Doyle <[EMAIL PROTECTED]> wrote:
>> Thanks Kevin, I will try using reduce instead.  I would like to know what
>> I'm doing wrong with updating the ref for future reference.  Thanks.
>>
>> On Mon, Nov 24, 2008 at 3:23 PM, Kevin Downey <[EMAIL PROTECTED]> wrote:
>>>
>>> I know you are asking about refs, but you might want to think about
>>> using reduce to walk the line-seq. the nature of reduce lets you have
>>> access to the line-seq, two lines at a time no need for a ref.
>>>
>>> On Mon, Nov 24, 2008 at 2:17 PM, Brian Doyle <[EMAIL PROTECTED]>
>>> wrote:
>>> > I am parsing a file and to compare the current line
>>> > with the previous line of the file.  I am using line-seq
>>> > to go thru the file and I thought I would create a
>>> > ref to store the previous line.   When I want to update
>>> > the previous line value I can't seem to do it.  I've
>>> > never used refs before so I'm sure I'm doing something
>>> > very stupid.
>>> >
>>> > (defstruct line :lat :lon :name)
>>> >
>>> > (defn convert [file]
>>> >   (let [prev-line (ref (struct line))]
>>> > (with-open [r (reader file)]
>>> >(doseq [l (line-seq r)]
>>> >  (let [ps (split #"," l)
>>> > c-line (struct line (ps 0) (ps 1) (ps 2))]
>>> >(if (not= c-line @pre-line)
>>> >  (do ; do stuff here then update pre-line
>>> > (dosync ref-set pre-line (apply struct line (vals
>>> > c-line)))
>>> > (println @pre-line  ; this prints out all nils
>>> > doesn't seem to work
>>> >
>>> >
>>> >
>>> > Sorry if this email is not formatted correctly.  Something is wrong with
>>> > my
>>> > browser
>>> > currently typing in a textarea.  Thanks.
>>> >
>>> >
>>> >
>>> > >
>>> >
>>>
>>>
>>>
>>> --
>>> The Mafia way is that we pursue larger goals under the guise of
>>> personal relationships.
>>>Fisheye
>>>
>>>
>>
>>
>> >>
>>
>
>
>
> --
> The Mafia way is that we pursue larger goals under the guise of
> personal relationships.
>Fisheye
>



-- 
The Mafia way is that we pursue larger goals under the guise of
personal relationships.
Fisheye

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: update a ref struct

2008-11-24 Thread Stuart Halloway

Another possible approach. Key idea here is to use partition to create  
a sliding window over the lines, plus a sentinel value (I picked "")  
before the first line. Pretty sure I like partition over reduce for  
this particular example.

(ns examples.convert
 (:use [clojure.contrib.duck-streams :only (reader)])
 (:use [clojure.contrib.str-utils :only (re-split)]))

(defstruct line :lat :lon :name)

(defn convert [file]
   (with-open [r (reader file)]
 (doseq [[l1 l2] (partition 2 1 (cons "" (line-seq r)))]
   (if (not= l1 l2)
(println (apply struct line (re-split #"," l2)))

> I am parsing a file and to compare the current line
> with the previous line of the file.  I am using line-seq
> to go thru the file and I thought I would create a
> ref to store the previous line.   When I want to update
> the previous line value I can't seem to do it.  I've
> never used refs before so I'm sure I'm doing something
> very stupid.
>
> (defstruct line :lat :lon :name)
>
> (defn convert [file]
>   (let [prev-line (ref (struct line))]
> (with-open [r (reader file)]
>(doseq [l (line-seq r)]
>  (let [ps (split #"," l)
> c-line (struct line (ps 0) (ps 1) (ps 2))]
>(if (not= c-line @pre-line)
>  (do ; do stuff here then update pre-line
> (dosync ref-set pre-line (apply struct line (vals c- 
> line)))
> (println @pre-line  ; this prints out all  
> nils doesn't seem to work
>
>
>
> Sorry if this email is not formatted correctly.  Something is wrong  
> with my browser
> currently typing in a textarea.  Thanks.
>
>
>
> >


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: update a ref struct

2008-11-24 Thread Brian Doyle
Yep, that's just a typo in the email.  Something was wrong with my browser
and I
couldn't just paste the code in :(

On Mon, Nov 24, 2008 at 3:33 PM, Shawn Hoover <[EMAIL PROTECTED]>wrote:

> On Mon, Nov 24, 2008 at 5:17 PM, Brian Doyle <[EMAIL PROTECTED]>wrote:
>
>> I am parsing a file and to compare the current line
>> with the previous line of the file.  I am using line-seq
>> to go thru the file and I thought I would create a
>> ref to store the previous line.   When I want to update
>> the previous line value I can't seem to do it.  I've
>> never used refs before so I'm sure I'm doing something
>> very stupid.
>>
>> (defstruct line :lat :lon :name)
>>
>> (defn convert [file]
>>   (let [prev-line (ref (struct line))]
>> (with-open [r (reader file)]
>>(doseq [l (line-seq r)]
>>  (let [ps (split #"," l)
>> c-line (struct line (ps 0) (ps 1) (ps 2))]
>>(if (not= c-line @pre-line)
>>  (do ; do stuff here then update pre-line
>> (dosync ref-set pre-line (apply struct line (vals
>> c-line)))
>> (println @pre-line  ; this prints out all nils
>> doesn't seem to work
>>
>>
> Your let binds prev-line, while the code inside uses pre-line. Is that just
> a typo in your email, or is that the code you're running, too?
>
> Shawn
>
>
> >
>

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---