Re: Creating map from string

2012-03-19 Thread Jimmy
Ok, those sugggestions work great. Thanks  for the help.

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

Re: Creating map from string

2012-03-19 Thread Ben Smith-Mannschott
On Sun, Mar 18, 2012 at 21:04, Jimmy jimmy.co...@gmail.com wrote:
 Hi,

 I would like to generate a hashmap from a string. The key portions of
 the string  will have some a prefix such as @ to define that they are
 a key. So the following string

 @key1  this is a value  @another-key  and another value @test1 and
 other value

 would get converted to.

 { :@key1  this is a value,  :@another-key  and another
 value ,  :@test1 and other value}

 What's the best way to do this?
 Thanks,
 Jimmy

Things to think about when designing an ad-hoc format:

- How are you handling white space?

Your example seems to indicate that any amount of white space is
allowed and ignored between key and value or value and key, but white
space internal to the value is preserved. I suppose keys will never
contain white space since that's not expressible as the first bit of
white space following the key marks the transition to value.

- Values can't be empty, consist of, start with or end with white space.

- Values can't contain @ unless you provide some kind of escaping convention.

// Ben

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


Creating map from string

2012-03-18 Thread Jimmy
Hi,

I would like to generate a hashmap from a string. The key portions of
the string  will have some a prefix such as @ to define that they are
a key. So the following string

@key1  this is a value  @another-key  and another value @test1 and
other value

would get converted to.

{ :@key1  this is a value,  :@another-key  and another
value ,  :@test1 and other value}

What's the best way to do this?
Thanks,
Jimmy

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


Re: Creating map from string

2012-03-18 Thread David Powell
You could use a regexp to pick out the key and the value - something like
this:

(into {}
  (map (fn [[_ x y]] [(keyword x) (clojure.string/trim y)])
(re-seq #(@[^ ]*) *([^@]*) s)))

-- 
Dave

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

Re: Creating map from string

2012-03-18 Thread Moritz Ulrich
On Sun, Mar 18, 2012 at 21:14, David Powell djpow...@djpowell.net wrote:
 (into {}
   (map (fn [[_ x y]] [(keyword x) (clojure.string/trim y)])
     (re-seq #(@[^ ]*) *([^@]*) s)))

I think `for' is cleaner than map + anonymous function:

(into {}
  (for [[_ k v] (re-seq #(@[^ ]*) *([^@]*) s)]
[(keyword k) (clojure.string/trim v)]))

-- 
Moritz Ulrich

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


Re: Creating map from string

2012-03-18 Thread gaz jones
argh i come back to paste in my answer and you beat me to it :(

i was gonna say:

(let [s @key1  this is a value  @another-key  and another value
@test1 and other value]
  (reduce (fn [m [_ k v]] (assoc m k (string/trim v))) {} (re-seq
#(@[\w-]+)([^@]*) s)))

much the same...

On Sun, Mar 18, 2012 at 3:14 PM, David Powell djpow...@djpowell.net wrote:

 You could use a regexp to pick out the key and the value - something like
 this:

 (into {}
   (map (fn [[_ x y]] [(keyword x) (clojure.string/trim y)])
     (re-seq #(@[^ ]*) *([^@]*) s)))

 --
 Dave

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


Re: into map with vectors versus lists (Re: Creating map from string)

2010-12-24 Thread Emeka
Did you try this

(apply hash-map (partition 2 (split (slurp data) #,)))



On Sat, Dec 4, 2010 at 11:12 AM, Remco van 't Veer rwvtv...@gmail.comwrote:

 I expected this to work:

  (into {} (partition 2 (split (slurp data) #,)))

 But unfortunately, `into' doesn't seem to allow pushing lists of pairs
 into a map.  But vectors are allowed:

  (into {} (map vec (partition 2 (split (slurp data) #,

 Can somebody explain why vectors are allowed and lists not?


 On 2010/12/03 15:40, Laurent PETIT wrote:

  Hi,
 
  2010/12/3 Anclj anb...@gmail.com
 
  Hi,
 
  I have a string of data and I would like to get a map {:key
  value, :key value, …}
 
  How could I do that?
 
  I've got:
 
  user (split (slurp data) #,)
  [0 2 1 5 2 8 3 15 4 9]
 
  And I would like:
  {:0 2, :1 5, :2 8, :3 15, :4 9}
 
  Any idea?
 
  (let [s (split (slurp data) #,)]
(zipmap (take-nth 2 s) (take-nth 2 (rest s
 
  HTH,
 
  --
  Laurent
 
  --
  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.comclojure%2bunsubscr...@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 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.comclojure%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en




-- 
*Satajanus  Nig. Ltd


*

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

Re: Creating map from string

2010-12-07 Thread Anclj
Thanks a lot for all the answers :)

I've been busy trying to understand all the scripts that you posted.
The code works but I also wanted to know why.
I'm new to Clojure and it's hard for me to understand advanced code,
but looking through the docs and the api I'm learning a lot.

Cheers!

On 4 Des, 23:16, Tyler Perkins thinks.outs...@gmail.com wrote:
 Or, better yet,

        (into {}
              (- (str { (slurp data) })
                   read-string
                   (map (fn [[k v]] [(keyword (str k)) v]

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


into map with vectors versus lists (Re: Creating map from string)

2010-12-04 Thread Remco van 't Veer
I expected this to work:

  (into {} (partition 2 (split (slurp data) #,)))

But unfortunately, `into' doesn't seem to allow pushing lists of pairs
into a map.  But vectors are allowed:

  (into {} (map vec (partition 2 (split (slurp data) #,

Can somebody explain why vectors are allowed and lists not?


On 2010/12/03 15:40, Laurent PETIT wrote:

 Hi,

 2010/12/3 Anclj anb...@gmail.com

 Hi,

 I have a string of data and I would like to get a map {:key
 value, :key value, …}

 How could I do that?

 I've got:

 user (split (slurp data) #,)
 [0 2 1 5 2 8 3 15 4 9]

 And I would like:
 {:0 2, :1 5, :2 8, :3 15, :4 9}

 Any idea?

 (let [s (split (slurp data) #,)]
   (zipmap (take-nth 2 s) (take-nth 2 (rest s

 HTH,

 --
 Laurent

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


Re: Creating map from string

2010-12-04 Thread Tyler Perkins
How about just

(apply hash-map (split (slurp data) #,))

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


Re: Creating map from string

2010-12-04 Thread Tim Robinson
Using 'apply hash-map' doesn't handle the transformations.
Note the original post requested keywords for keys and integers for
vals in the output.


On Dec 4, 11:49 am, Tyler Perkins thinks.outs...@gmail.com wrote:
 How about just

 (apply hash-map (split (slurp data) #,))

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


Re: Creating map from string

2010-12-04 Thread Tyler Perkins
On Dec 4, 12:21 pm, Tim Robinson tim.blacks...@gmail.com wrote:
 Using 'apply hash-map' doesn't handle the transformations.
 Note the original post requested keywords for keys and integers for
 vals in the output.

I'm not the only one who overlooked that! :o) Might just as well use
strings as keys. But OK, try this:

  (apply hash-map
 (- (str { (slurp data) })
  read-string
  (map (fn [[k v]] [(keyword (str k)) v]))
  flatten))

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


Re: Creating map from string

2010-12-04 Thread Tyler Perkins
Or, better yet,

   (into {}
 (- (str { (slurp data) })
  read-string
  (map (fn [[k v]] [(keyword (str k)) v]

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


Creating map from string

2010-12-03 Thread Anclj
Hi,

I have a string of data and I would like to get a map {:key
value, :key value, …}

How could I do that?

I've got:

user (split (slurp data) #,)
[0 2 1 5 2 8 3 15 4 9]

And I would like:
{:0 2, :1 5, :2 8, :3 15, :4 9}

Any idea?
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
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


Re: Creating map from string

2010-12-03 Thread Laurent PETIT
Hi,

2010/12/3 Anclj anb...@gmail.com

 Hi,

 I have a string of data and I would like to get a map {:key
 value, :key value, …}

 How could I do that?

 I've got:

 user (split (slurp data) #,)
 [0 2 1 5 2 8 3 15 4 9]

 And I would like:
 {:0 2, :1 5, :2 8, :3 15, :4 9}

 Any idea?



(let [s (split (slurp data) #,)]
  (zipmap (take-nth 2 s) (take-nth 2 (rest s


HTH,

-- 
Laurent

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

Re: Creating map from string

2010-12-03 Thread Sunil S Nandihalli
Here is what I came up with ...

(let [d (split (slurp data) #,)]
(- d
   (apply hash-map)
   (clojure.walk/keywordize-keys)
   (clojure.contrib.generic.functor/fmap read-string)))


Sunil.
On Fri, Dec 3, 2010 at 7:52 PM, Anclj anb...@gmail.com wrote:

 Hi,

 I have a string of data and I would like to get a map {:key
 value, :key value, …}

 How could I do that?

 I've got:

 user (split (slurp data) #,)
 [0 2 1 5 2 8 3 15 4 9]

 And I would like:
 {:0 2, :1 5, :2 8, :3 15, :4 9}

 Any idea?
 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
 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.comclojure%2bunsubscr...@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 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

Re: Creating map from string

2010-12-03 Thread Laurent PETIT
2010/12/3 Laurent PETIT laurent.pe...@gmail.com

 Hi,

 2010/12/3 Anclj anb...@gmail.com

 Hi,

 I have a string of data and I would like to get a map {:key
 value, :key value, …}

 How could I do that?

 I've got:

 user (split (slurp data) #,)
 [0 2 1 5 2 8 3 15 4 9]

 And I would like:
 {:0 2, :1 5, :2 8, :3 15, :4 9}

 Any idea?



 (let [s (split (slurp data) #,)]
   (zipmap (take-nth 2 s) (take-nth 2 (rest s

 Sorry, this respects the contract better:

(let [s [1 2 3 4 5 6]]
  (zipmap
(map keyword (take-nth 2 s))
(map #(Integer/valueOf %) (take-nth 2 (rest s)

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

Re: Creating map from string

2010-12-03 Thread Ken Wesson
On Fri, Dec 3, 2010 at 10:08 AM, Laurent PETIT laurent.pe...@gmail.com wrote:
 2010/12/3 Laurent PETIT laurent.pe...@gmail.com
 2010/12/3 Anclj anb...@gmail.com
 I've got:

 user (split (slurp data) #,)
 [0 2 1 5 2 8 3 15 4 9]

 And I would like:
 {:0 2, :1 5, :2 8, :3 15, :4 9}

 Any idea?


 (let [s (split (slurp data) #,)]
   (zipmap (take-nth 2 s) (take-nth 2 (rest s

 Sorry, this respects the contract better:

 (let [s [1 2 3 4 5 6]]
   (zipmap
     (map keyword (take-nth 2 s))
     (map #(Integer/valueOf %) (take-nth 2 (rest s)

Both of these traverse s twice. How about:

(let [s (split (slurp data) #,)]
  (into {}
(map (fn [[k v]] [(keyword (str k)) v])
  (partition 2 s

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


Re: Creating map from string

2010-12-03 Thread Laurent PETIT
2010/12/3 Ken Wesson kwess...@gmail.com

 On Fri, Dec 3, 2010 at 10:08 AM, Laurent PETIT laurent.pe...@gmail.com
 wrote:
  2010/12/3 Laurent PETIT laurent.pe...@gmail.com
  2010/12/3 Anclj anb...@gmail.com
  I've got:
 
  user (split (slurp data) #,)
  [0 2 1 5 2 8 3 15 4 9]
 
  And I would like:
  {:0 2, :1 5, :2 8, :3 15, :4 9}
 
  Any idea?
 
 
  (let [s (split (slurp data) #,)]
(zipmap (take-nth 2 s) (take-nth 2 (rest s
 
  Sorry, this respects the contract better:
 
  (let [s [1 2 3 4 5 6]]
(zipmap
  (map keyword (take-nth 2 s))
  (map #(Integer/valueOf %) (take-nth 2 (rest s)

 Both of these traverse s twice. How about:

 (let [s (split (slurp data) #,)]
   (into {}
(map (fn [[k v]] [(keyword (str k)) v])
  (partition 2 s


Indeed, if I analyze my solution: the 2 take-nth traverse s 2 times
(creating 2 x 1/2 x n ISeq instances), and the two maps over the 2 resulting
seq traverse these resulting seqs also, creating again 2 x 1/2 x n ISeq
instances).
In the end, 2 x 1/2 x n + 2 x 1/2 x n = 2 x n ISeq instances are created and
visited.

Now yours :-) : (partition 2 s) traverses s once, and creates  1/2 n ISeq
instances each made of a seq of 2 ISeq instances = 1/2 n x 2 = n ISeq
instances.
Then map is applied and creates 1/2 x n vectors in 1/2 x n ISeq instances =
also n instances created.
In the end, your solution also creates 2 x n instances (ISeq instances and
vector instances), before being consumed by into.

In fact, when one knows the algorithm needs to consume the entire seq *by
definition* (since in clojure datastructures themselves are not lazy, only
seqs are), it sometimes feels odd to create all these intermediate objects.

But I generally resist the temptation to too quickly resort to using loop,
which is the real solution to not create too many intermediate objects:

(let [s [1 2 3 4 5 6 7 8]]
  (loop [s (seq s) m {}]
(if s
  (recur (nnext s) (assoc m (keyword (first s)) (Integer/valueOf (fnext
s
  m)))

or else by leveraging transients:


(let [s [1 2 3 4 5 6 7 8]]
  (loop [s (seq s) m (transient {})]
(if s
  (recur (nnext s) (assoc! m (keyword (first s)) (Integer/valueOf (fnext
s
  (persistent! m

But really I generally not start (anymore) writing loops unless I see a
performance problem!

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

Re: Creating map from string

2010-12-03 Thread Ken Wesson
Object creation wasn't my concern -- modern JVMs are very efficient in
that regard -- just traversals.

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


Re: Creating map from string

2010-12-03 Thread Laurent PETIT
2010/12/3 Ken Wesson kwess...@gmail.com

 Object creation wasn't my concern -- modern JVMs are very efficient in
 that regard -- just traversals.


Ken,

I've done my homework, and of course you were right, and me wrong. At some
point in my reasoning I've conflated number of object allocations with
number of traversals, and I was off track.

Cheers,

-- 
Laurent

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