Re: Record base implementation in ClojureScript

2015-06-15 Thread Damien Lepage
My apologies, I just realized that there is a clojurescript group. I will
ask my question there.

2015-06-15 23:02 GMT-04:00 Damien Lepage damienlep...@gmail.com:

 Hello,

 I found the following technique in order to have a record inheriting a
 base behavior, using extend: https://gist.github.com/david-mcneil/661983

 Unfortunately this can't work in ClojureScript at this point, due to the
 lack of extend. Is there any alternative or should I fall back to bare
 functions instead of defrecord?

 Maybe some context on my code would help you answer this question.
 I transform some data through a pipeline of wrappers implementing this
 protocol:

 (defprotocol Wrapper
   (wrap-data [this data]))

 Some of my wrappers can be toggled on/off so I added this protocol:

 (defprotocol Togglable
   (toggle [this])
   (on? [this]))

 Such a togglable wrapper would be implemented like this:

 (defrecord MyWrapper [on]
   Togglable
   (toggle [this] (update-in this [:on] not))
   (on? [this] on)
   Wrapper
   (wrap-data [this data]
 (if on
   (do-something data)
   data)))

 Now I realized that my implementation for the Togglable protocol would
 always be the same, so I would have liked a way to write the code only
 once. The only solution I found to do that while keeping my Togglable
 protocol, is the following macro:

 (defmacro deftogglable [name fields  specs]
   `(defrecord ~name [~@fields ~'on]
  ~'Togglable
  (~'toggle [this#] (update-in this# [:on] not))
  (~'on? [~'this] ~'on)
  ~@specs))

 Which I can use like this:

 (macro/deftogglable MyWrapper []
 Wrapper
 (wrap-data [this data]
 (if on
   (do-something data)
   chart)))

 I don't like this approach too much because it's very specific and
 wouldn't compose well, or allow overriding part of the functions.

 How would you go to solve this problem?

 Thanks

 Damien



-- 
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.
For more options, visit https://groups.google.com/d/optout.


Record base implementation in ClojureScript

2015-06-15 Thread Damien Lepage
Hello,

I found the following technique in order to have a record inheriting a base
behavior, using extend: https://gist.github.com/david-mcneil/661983

Unfortunately this can't work in ClojureScript at this point, due to the
lack of extend. Is there any alternative or should I fall back to bare
functions instead of defrecord?

Maybe some context on my code would help you answer this question.
I transform some data through a pipeline of wrappers implementing this
protocol:

(defprotocol Wrapper
  (wrap-data [this data]))

Some of my wrappers can be toggled on/off so I added this protocol:

(defprotocol Togglable
  (toggle [this])
  (on? [this]))

Such a togglable wrapper would be implemented like this:

(defrecord MyWrapper [on]
  Togglable
  (toggle [this] (update-in this [:on] not))
  (on? [this] on)
  Wrapper
  (wrap-data [this data]
(if on
  (do-something data)
  data)))

Now I realized that my implementation for the Togglable protocol would
always be the same, so I would have liked a way to write the code only
once. The only solution I found to do that while keeping my Togglable
protocol, is the following macro:

(defmacro deftogglable [name fields  specs]
  `(defrecord ~name [~@fields ~'on]
 ~'Togglable
 (~'toggle [this#] (update-in this# [:on] not))
 (~'on? [~'this] ~'on)
 ~@specs))

Which I can use like this:

(macro/deftogglable MyWrapper []
Wrapper
(wrap-data [this data]
(if on
  (do-something data)
  chart)))

I don't like this approach too much because it's very specific and wouldn't
compose well, or allow overriding part of the functions.

How would you go to solve this problem?

Thanks

Damien

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: Bret Victor's live editable game in ClojureScript

2012-02-27 Thread Damien Lepage
Awesome! Thanks for putting this together.
I don't know if the most impressive is the end result or the little time
you needed to code it.
Both are mind blowing.


2012/2/27 Base basselh...@gmail.com

 Agreed.  Pretty damn sweet!

 On Feb 27, 6:01 pm, John Szakmeister j...@szakmeister.net wrote:
  On Mon, Feb 27, 2012 at 3:14 PM, Chris Granger ibdk...@gmail.com
 wrote:
   Hey folks,
 
   In reference to the previous thread on Inventing On Principle, I
   built a ClojureScript example of his live editable game :)
 
  http://www.chris-granger.com/2012/02/26/connecting-to-your-creation/
 
   Enjoy!
 
  Nice!  You rock!
 
  -John

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




-- 
Damien Lepage
http://damienlepage.com
@damienlepage https://twitter.com/#!/damienlepage
linkedin.com/in/damienlepage http://www.linkedin.com/in/damienlepage

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

Bret Victor - Inventing on Principle

2012-02-24 Thread Damien Lepage
Hi Everyone,

You may have seen this already, if not I believe it's worth investing 1h of
your life:
http://vimeo.com/36579366

That's already a good candidate for the technical talk of the year, if not
the decade IMO.
Ok, I'm getting a bit too enthusiastic here but this is so inspiring.

After watching it, you can't help thinking that we have a whole new world
to invent.
As a side note, you may start thinking that a REPL is not good enough.
- Personal message to Laurent Petit: please watch and start thinking about
CCW 1.0 ;o) -
It also feels like ClojureScript is on the right path.

But, most importantly, beyond any technical consideration, the last part is
a great life lesson.

-- 
Damien Lepage
http://damienlepage.com
@damienlepage https://twitter.com/#!/damienlepage
linkedin.com/in/damienlepage http://www.linkedin.com/in/damienlepage

-- 
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: Bret Victor - Inventing on Principle

2012-02-24 Thread Damien Lepage
Sorry, I certainly didn't intend to start such a heated debate ;o)
Hopefully some of you appreciate the link but you're all free to ignore.
The truth is, no matter the media, there are too many interesting things
and you need to choose.
I had this video in my todo list for a week before I took the time to watch
it.
After that, I thought it was kind of special and worth sharing, any textual
representation would betray its essence in my opinion.


2012/2/24 Andy Fingerhut andy.finger...@gmail.com

 Perhaps someone will volunteer to transcribe it and post that.  You know,
 maybe someone who can type quickly and prefers text. :-)

 I've done that for one of Rich's earlier talks posted as video.  It takes
 time, and I'm not volunteering for this one.

 Andy

 On Feb 24, 2012, at 11:57 AM, Cedric Greevey wrote:

  On Fri, Feb 24, 2012 at 2:51 PM, Daniel E. Renfer duck112...@gmail.com
 wrote:
  On 02/24/2012 02:42 PM, Cedric Greevey wrote:
  OK. I googled the group archives. Seems there was a Ken Wesson active
  on the list for a while, but he disappeared a couple of months before
  I joined. I'm not sure why people think I might be him.
 
  Ken Wesson was noted for having strong opinions as was a noted hater of
  videos where text will do.
 
  Most of us on this list probably have strong opinions -- it tends to
  go with the territory of being smarter than the average bear. As for
  hating videos where text will do, need I repeat my list of bullet
  points again? It's quite *obvious*. It's no more surprising for
  multiple techies to find videos-where-text-will-do to be a troubling
  trend than for techies to find that Lisps and even Algols are nicer
  languages to program large systems in than Visual Basic. :) Especially
  since we techies are very used to using search tools and other things
  specifically geared around finding *text*, and often have multiple
  devices including mobile where bandwidth costs can quickly become
  exorbitant.
 
  --
  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




-- 
Damien Lepage
http://damienlepage.com
@damienlepage https://twitter.com/#!/damienlepage
linkedin.com/in/damienlepage http://www.linkedin.com/in/damienlepage

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

ANN: vhector 0.2

2012-02-05 Thread Damien Lepage
Hello,

Vhector is a clojure client for the NoSQL database Apache Cassandra,
wrapping the Java client Hector.
It is now upgraded to work on Clojure 1.3 with Cassandra 1.0
https://github.com/damienlepage/vhector

-- 
Damien Lepage
http://damienlepage.com
@damienlepage https://twitter.com/#!/damienlepage
linkedin.com/in/damienlepage http://www.linkedin.com/in/damienlepage

-- 
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: Flattening a tree

2011-10-22 Thread Damien Lepage
I was looking for the exact same thing a few months ago. Take a look at this
thread:
http://groups.google.com/group/clojure/browse_thread/thread/a86d8d8c78bcab6b



2011/10/21 Timothy Baldridge tbaldri...@gmail.com

 I'm a bit unsure as to the best way to solve this. Assuming I have the
 following tree:

 {:parent1
{:relationship1
   {:child1 1}
   {:child2 2}}
   {:relationship2
   {child3 3}}
   {:_meta}}

 I want to get:

 [:parent1 :relationship1 :child1]
 [:parent1 :relationship1 :child2]
 [:parent1 :relationship2 :child3]

 How do I got about getting this? Notice how I also want to filter out
 all :_meta nodeswhat I want is to get a sequence of vectors where
 each vector consists of all the lowest child keys along with the path
 required to get to that key.

 Thanks for the help,

 Timothy


 --
 “One of the main causes of the fall of the Roman Empire was
 that–lacking zero–they had no way to indicate successful termination
 of their C programs.”
 (Robert Firth)

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




-- 
Damien Lepage
http://damienlepage.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

Re: is there a 4Clojure forum anywhere?

2011-08-31 Thread Damien Lepage
What about Google Group or Discussion Group?


2011/8/31 Alan Malloy a...@malloys.org

 Hey, thanks for that. I'll add a link in the next build, though I
 haven't decided what to call it: Forums sounds like it's part of
 4clojure, Unofficial forum sounds kinda untrue if the official site
 is linking to it, ...

 Anyway, I'd appreciate having admin rights but I don't really plan to
 use them, and I certainly don't want exclusive rights; feel free to
 keep yours.

 On Aug 31, 11:38 am, chepprey chepp...@gmail.com wrote:
  ...nd now with our 2nd I need a hint! posting, I think it's time
  I take Alan Malloy's advice and start a forum for 4Clojure problems.
 
  May I present to you:  http://groups.google.com/group/4clojure
 
  @Alan M - feel free to link to this from 4clojure.com.  Also if you
  would like to have any sort of administrative control over the group,
  I'm delighted to hand any / all control over to you.  I've never set
  up a GG before, I have no idea how much responsibility there is in
  owning one.
 
  @finbeu - be sure to post your Compress a sequence question!
 
  Enjoy!
 
  -chepprey
 
  On Aug 30, 11:12 am, finbeu info_pe...@t-online.de wrote:
 
 
 
 
 
 
 
   Hmm, I got stuck on the Compress a sequence. I had a look at the
   clojure/core.clj distinct function and I guess I have to use something
   similar to define my function. Or is it easier?
 
   - finbeu

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




-- 
Damien Lepage
http://damienlepage.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

ANN: Vhector, client for Apache Cassandra

2011-05-01 Thread Damien Lepage
Hi,

I just published a Clojure wrapper for the java library Hector, client for
Apache Cassandra.
https://github.com/damienlepage/vhector

This is my first project with Clojure so the internals are certainly far
from optimal.
However I'm very happy with the API I've been able to put together.

-- 
Damien Lepage
http://damienlepage.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

Who's using Clojure?

2011-04-19 Thread Damien Lepage
Hi Everyone,

I'm on a mission: introducing Clojure in my company, which is a big
consulting company like many others.

I started talking about Clojure to my manager yesterday.
I was prepared to talk about all the technical benefits and he was
interested.
I still have a long way to go but I think that was a good start.

However I need to figure out how to answer to one of his questions: who is
using Clojure?

Obviously I know each of you is using Clojure, that makes almost 5,000
people.
I know there is Relevance and Clojure/core.
I read about BackType or FlightCaster using Clojure.

But, let's face it, that doesn't give me a killer answer.

What could help is a list of success stories, a bit like MongoDB published
here:
http://www.mongodb.org/display/DOCS/Production+Deployments

Is there a place where I could find this kind of information for Clojure?

Thanks

-- 
Damien Lepage
http://damienlepage.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

Re: Who's using Clojure?

2011-04-19 Thread Damien Lepage
Thanks Everyone for your input and especially to Christopher for creating
the community page.
I'm looking forward to read about your success stories there.


2011/4/19 Sean Allen s...@monkeysnatchbanana.com

 Akamai was at the conj looking to hire clojure programmers so I would
 assume they are as well.

 On Tue, Apr 19, 2011 at 10:38 AM, Damien Lepage damienlep...@gmail.com
 wrote:
  Hi Everyone,
 
  I'm on a mission: introducing Clojure in my company, which is a big
  consulting company like many others.
 
  I started talking about Clojure to my manager yesterday.
  I was prepared to talk about all the technical benefits and he was
  interested.
  I still have a long way to go but I think that was a good start.
 
  However I need to figure out how to answer to one of his questions: who
 is
  using Clojure?
 
  Obviously I know each of you is using Clojure, that makes almost 5,000
  people.
  I know there is Relevance and Clojure/core.
  I read about BackType or FlightCaster using Clojure.
 
  But, let's face it, that doesn't give me a killer answer.
 
  What could help is a list of success stories, a bit like MongoDB
 published
  here:
  http://www.mongodb.org/display/DOCS/Production+Deployments
 
  Is there a place where I could find this kind of information for Clojure?
 
  Thanks
 
  --
  Damien Lepage
  http://damienlepage.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 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




-- 
Damien Lepage
http://damienlepage.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

Can this function be simpler?

2011-03-10 Thread Damien Lepage
Hi

I wrote a function to transform a variable number of arguments into embedded
maps.
Here is what it does:

 (enmap 1 2)
{1 2}
 (enmap 1 2 3)
{1 {2 3}}
 (enmap 1 2 3 4)
{1 {2 {3 4}}}
 (enmap 1 2 3 4 {5 6 7 8})
{1 {2 {3 {4 {5 6, 7 8}

Here is my implementation:

(defn enmap [arg  args]
  (if-let [more (butlast args)]
  (let [k (last more), v (last args)]
(if-let [even-more (butlast more)]
  (apply enmap arg (concat even-more (list (hash-map k v
  (enmap arg (hash-map k v
  (apply hash-map arg args)))

Two things bother me:

   - Is there a way to make this function less complicated? without
   recursion maybe?
   - Is there something simpler than (concat even-more (list (hash-map k
   v)) to append an element at the end of a sequence?

Thanks

-- 
Damien

-- 
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: Can this function be simpler?

2011-03-10 Thread Damien Lepage
Thanks a lot for your help, there's an awesome community here.

Sorry for the dumb questions, I'll try no to be too noisy on this list.

BTW, shouldn't it be better to create a separate mailing list for beginners?
Well, we would still need some experienced clojurers to answer the questions
though ...

Or maybe beginners like me should rather be encouraged to post their
questions on StackOverflow?
That would also give more visibility to the language (there are just about 2
or 3 questions a day about Clojure there).

What do you think? Where do you want to see my next stupid trivial question?

Damien



2011/3/10 Takahiro fat...@googlemail.com

 more concise:

 (defn enmap [args]
  (reduce #(hash-map %2 %1) (reverse args)))

 2011/3/11 Takahiro fat...@googlemail.com:
  Interesting. Here is my attempt.
 
  (defn enmap [args]
   (let [[fs  res] (reverse args)]
 (reduce (fn [v k] (hash-map k v)) fs res)))
 
  (enmap [1 2 3 4 {5 6 7 8}])
  = {1 {2 {3 {4 {5 6, 7 8}
 
 
 (let [[tail more] ((juxt last (comp reverse butlast)) [1 2 3 4 {5 6 7
 8}])]
  (reduce #(hash-map %2 %1) tail more))
  I still cannot understand what is going on in this code. juxt always
  make my head crashed.
 
  Thanks.
 
  2011/3/11 Damien Lepage damienlep...@gmail.com:
  Hi
 
  I wrote a function to transform a variable number of arguments into
 embedded
  maps.
  Here is what it does:
  (enmap 1 2)
  {1 2}
  (enmap 1 2 3)
  {1 {2 3}}
  (enmap 1 2 3 4)
  {1 {2 {3 4}}}
  (enmap 1 2 3 4 {5 6 7 8})
  {1 {2 {3 {4 {5 6, 7 8}
  Here is my implementation:
  (defn enmap [arg  args]
(if-let [more (butlast args)]
(let [k (last more), v (last args)]
  (if-let [even-more (butlast more)]
(apply enmap arg (concat even-more (list (hash-map k v
(enmap arg (hash-map k v
(apply hash-map arg args)))
  Two things bother me:
 
  Is there a way to make this function less complicated? without recursion
  maybe?
  Is there something simpler than (concat even-more (list (hash-map k
 v)) to
  append an element at the end of a sequence?
 
  Thanks
  --
  Damien
 
  --
  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




-- 
Damien Lepage
http://damienlepage.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

Re: Transforming map entries

2011-02-22 Thread Damien Lepage
Hi,

I thought update-in could help but it looks as complicated as your solution:

(use 'clojure.contrib.string)

(loop [m mymap, ks (keys m)]
  (let [new-m (update-in m [(first ks)] #(if (string? %) (upper-case %) %))
more (rest ks)]
(if-not (empty? more)
  (recur new-m more)
  new-m)))

I'm a total newbie so I may also miss an obvious and more simple solution.


2011/2/21 yair yair@gmail.com

 I'm hoping this is a dumb question and I've missed something obvious.
 I have a map with various key-value pairs and I want to transform some
 of the values, e.g.

 (def mymap {:first john :last smith :age 25}) and say I want to
 change the strings to be upper case.
 Right now all I can think of doing is using reduce and passing in an
 empty map and the re-associating each key with the (possibly)
 transformed value.  Is there something like the map function that
 takes two parameters, one a function that receives a pair and returns
 a new pair, and the other a map, and returns a map that's
 reconstituted from those pairs?

 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




-- 
Damien Lepage
http://damienlepage.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

Re: flattening a tree

2011-02-21 Thread Damien Lepage
Thanks, I expected something more simple but this protocol works well.
Before I stick to it, I'm just going to give a little bit more context.

I want to write a wrapper for the Java library
Hectorhttps://github.com/rantav/hector,
client for Cassandra DB.
And this tree structure would be useful in the API.

Hector allows inserting many fields at once using the following syntax:

mutator.addInsertion(”jsmith,”
Identification,HFactory.createStringColumn(“first”, “John”))
   .addInsertion(“jsmith”, “Identification”,
HFactory.createStringColumn(“last”, “Smith”))
   .addInsertion(“jsmith”, “Identification”,
HFactory.createStringColumn(“middle”, “Q”))
   .execute();

To avoid the repetition of key (jsmith) and column family
(“Identification”), I thought a tree structure would be nice.
Something like:

(insert! {“jsmith”
 {“Identification”
  {“first” “John”, “last” “Smith”,  “middle” “Q”}}
 {“Professional”
  {“occupation” “programmer”}}})

Does it make sense to use this kind of Map here, along with the protocol
proposed by Meikel to flatten it and call the Hector API?
Would I be better with vectors or something else?

Thanks a lot


2011/2/21 Meikel Brandmeyer m...@kotka.de

 Hi,

 On 21 Feb., 08:17, Damien damienlep...@gmail.com wrote:

  Not sure if I should talk about flattening but basically I'm trying to
  achieve the following transformation:
 
  user=(flatten-tree {1 {2 {3 4 5 6} 7 {8 9}}})
  ((1 2 3 4) (1 2 5 6) (1 7 8 9))

 Using protocols seems overkill:

 (defprotocol TreeFlattener (flatten-tree [this]))

 (extend-protocol TreeFlattener
  clojure.lang.IPersistentMap
  (flatten-tree
[this]
(mapcat (fn [[k v]] (map #(cons k %) (flatten-tree v))) this))
  Object
  (flatten-tree
[this]
(list (list this)))
  nil
  (flatten-tree [_] (list (list nil

 user= (flatten-tree {1 {2 {3 [4 nil] 5 6} 7 {8 9}}})
 ((1 2 3 [4 nil]) (1 2 5 6) (1 7 8 9))

 ... but it also allows to add other data structures later on:

 (extend-type clojure.lang.IPersistentVector
  TreeFlattener
  (flatten-tree
[this]
(mapcat (fn [idx x] (map #(cons idx %) (flatten-tree x))) (range)
 this)))

 user= (flatten-tree {1 {2 {3 [4 nil] 5 6} 7 {8 9}}})
 ((1 2 3 0 4) (1 2 3 1 nil) (1 2 5 6) (1 7 8 9))

 Beware structure depths and stack overflows, though.

 Hope that helps.

 Sincerely
 Meikel

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



-- 
Damien Lepage
http://damienlepage.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

Re: flattening a tree

2011-02-21 Thread Damien Lepage
Thanks Meikel, James and Rob for your inputs.
I'm gonna use the simple function provided by James and maybe try to come up
with a better word than flatten ;o)


2011/2/21 rob levy r.p.l...@gmail.com

 That is, had flattening actually been your goal.  It seem like you didn't
 really want to throw out that structure, just transform it, so flattening is
 irrelevant I guess other than the subject line. :)

 On Mon, Feb 21, 2011 at 5:34 PM, rob levy r.p.l...@gmail.com wrote:

 One way to approve the problem is to write a function to convert the
 nested maps into nested seqs. Once it is in that form you can use flatten on
 it and partition the flat list as you like:

 (defn flatten-maptree [m]
   (letfn [(maptree-seqtree
[m]
(lazy-seq
 (cond
  (map? m) (map #(cons
  (key %)
  (maptree-seqtree (val %)))
m)
  :else [m])))]
 (flatten (maptree-seqtree m


 user= (partition 4 (flatten-maptree {1 {2 {3 4 5 6} 7 {8 9}}}))
 ((1 2 3 4) (5 6 7 8))


 On Mon, Feb 21, 2011 at 3:25 PM, James Reeves jree...@weavejester.comwrote:

 On 21 February 2011 07:17, Damien damienlep...@gmail.com wrote:
  Not sure if I should talk about flattening but basically I'm trying to
  achieve the following transformation:
 
  user=(flatten-tree {1 {2 {3 4 5 6} 7 {8 9}}})
  ((1 2 3 4) (1 2 5 6) (1 7 8 9))
 
  Any suggestion?

 (defn flatten-tree [t]
  (if (map? t)
(for [[k v] t, w (flatten-tree v)]
  (cons k w))
(list (list t

 In this case, I think using protocols would be over-engineering. We
 can always add protocols in later if we happen to need them. That's
 one of the benefits of protocols as compared to Java's interfaces.

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




-- 
Damien Lepage
http://damienlepage.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