Re: core.async top use cases

2016-09-19 Thread Mond Ray
Pushing asynchrony further into the stack is useful for reliability and 
fault tolerance. We can also use it as a basis for Complex Event Processing 
using time series windows. 

I wrote up a few examples in my blog 

 
if you have the time to check out a longer explanation with code.

I recently wrote a small set of functions to enable HTML5 Server Sent 
Events from any Kafka topic which also uses core.async (with an example 
using Aleph and Compojure). You might like to check that repo 
 out too.

Ray

On Sunday, 18 September 2016 08:37:38 UTC+2, Matan Safriel wrote:
>
> Hi,
>
> It's very easy to see how core.async solves callback hell for front-end 
> development with clojurescript.
> In what use cases would you use it for server-side? we already have 
> non-blocking IO from Java, and we have clojure agents. So what's a bunch of 
> salient use cases?
> Are there prominent clojure http server implementations which rely on it 
> for transcending the threaded web service paradigm?
>
> Thanks,
> Matan
>
>

-- 
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: Why is this not considered to be in a go block?

2016-08-27 Thread mond
Good point Moe, given the pasted code. Actually I made an error when trying 
to simplify the code for this list. The comp fn should have been:

kafka-ch (chan 1 (comp (map sse-data)
   (filter #(matching-event 
client-filter (.key %)

But my mistake was even more embarrassing. It should have been

kafka-ch (chan 1 (comp (filter #(matching-event client-filter (.key %)))
   (map sse-data)))]

Doh!


On Saturday, 27 August 2016 01:25:57 UTC+2, Moe Aboulkheir wrote:
>
>
>
> On Sat, Aug 27, 2016 at 12:08 AM, mond <r...@mcdermott.be > 
> wrote:
>
>> Is that the same thing or have I made a(nother) / different mistake?
>>
>>
> At a glance, it looks like the functions you're passing into map and 
> filter are shaped wrong - (comp (map sse-data) (filter 
> matching-event-client-filter)) may have been the intention.  #(fn ..) is 
> going to create a no-arg fn which returns the explicit (fn ...) when 
> called.  Both # and fn turn out to be unnecessary as you're not doing 
> anything on top of passing through the single argument.
>
> I have no idea if this is the cause of your error, though.
>
> Take care,
> Moe
>

-- 
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: Why is this not considered to be in a go block?

2016-08-26 Thread mond
The flood gates are open Timothy! and on the back of that, I'm gonna dump 
some code in here so pls excuse me - it's on GitHub 

 
too so maybe look there if it's too heavy for email.

I have this code in the go-loop but if I put in the transducer, that code 
throws an exception stating that the object (which I can print) is a String

(defn sse-data 
  [kafka-record] (...))

(defn matching-event
  [client-event-filter kafka-record] (...))

;-- INLINE version - works: record on ConsumerRecord
(defn kafka-proxy-handler
  [request]
  (let [client-filter (or (get (:params request) "filter") ".*")
consumer (topic-consumer "topic")
kafka-ch (chan)]
(go-loop []
  (if-let [records (.poll consumer 100)]
(doseq [record records]
  (if (matching-event client-filter record)
(>! kafka-ch (sse-data record)
  (recur))
{:status  200
 :body(s/->source kafka-ch)}))

;-- TRANSDUCER version - fails: says method .offset does 
not exist on String 
(defn kafka-proxy-handler
  [request]
  (let [client-filter (or (get (:params request) "filter") ".*")
consumer (topic-consumer "topic")
kafka-ch (chan 1 (comp (map #(fn [kr] (sse-data kr)))
   (filter #(fn [kr] (matching-event 
client-filter kr)]
(go-loop []
  (if-let [records (.poll consumer 100)]
(doseq [record records]
  (>! kafka-ch record)))
  (recur))
{:status  200
 :body(s/->source kafka-ch)}))


Is that the same thing or have I made a(nother) / different mistake?

Cheers

Ray


On Friday, 26 August 2016 02:02:24 UTC+2, tbc++ wrote:
>
> I'm not sure I've ever addressed this publicly, so I assume now's as good 
> a time as ever. 
>
> The reason the go block stops at function boundaries, is that changing a 
> function to be "async" changes it return type. With code transforms like 
> these a function that performs a parking take on a channel no longer 
> returns a object, it returns a "async object" that eventually returns an 
> object. Notice the difference here: 
>
> (fn [c]
>   (
> (fn [c]
>   (go (
> Adding the "go" to the function (which is required to kick off the 
> transformation) changes the return type to being a channel of objects 
> instead of a single object. This is surfaced in other languages as well 
> that support parking behavior. Performing an async/await operation in C# 
> for example changes the type from "Object" to "Task". In essence, 
> parking is infectious. Any function that calls an async function must 
> itself either become blocking or parking.
>
> For example, for this code to work:
>
> (go (vec (map 
> You would have to transform all the code in map, persistent vectors, seqs 
> and a few other bits of code. 
>
> Now there are some languages/libraries that support this behavior on the 
> JVM, two are Erjang and Pulsar. Both of these provide this functionality 
> via whole program code transformation. That is to say they perform 
> async/go-like transforms to all the code in the JVM, or at least all the 
> code that interfaces with async code. I consider this a valid approach, but 
> it is rather invasive. As such, we made a design decision early on in the 
> development of core.async to only perform local transformation. 
>
> Hopefully that provides some context. 
>
> Timothy
>
> On Thu, Aug 25, 2016 at 5:29 PM, Kevin Downey  > wrote:
>
>> The analysis for the go macro to determine that the fn never escapes the
>> go block is not something core.async does. Because of that functions are
>> sort of a black box to the transforms the go macro does.
>>
>> http://dev.clojure.org/jira/browse/ASYNC-93 is a declined issue
>> regarding this. http://dev.clojure.org/jira/browse/ASYNC-57 is another
>> similar declined issue.
>>
>> On 08/25/2016 04:21 PM, hiskennyness wrote:
>> > I am getting an error about >! not being in a go block with this code:
>> >
>> > |
>> >   (go-loop [state :nl
>> > column 0
>> > last-ws nil
>> > buff ""]
>> > (let [line-out(fn [c b]
>> >  (>!out(apply str b (repeat (-col-width (count
>> > b))\space]
>> >   (cond
>> > (>=column col-width)
>> > (condp =state
>> >   :ws (do
>> > (line-out\|buff)
>> > (recur :nl 0nil""))
>> >  ..etc etc
>> > |
>> >
>> > I just changed the line-out call to just do...
>> >
>> > |
>> > (>!out-chan buff)
>> > |
>> >
>> > ...and it worked fine.
>> >
>> > So the failing code is both dynamically and lexically within the scope
>> > of the go-loop --- is that supposed to be that way? Or am I completely
>> > missing something?
>> >
>> > -kt
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "Clojure" 

Re: Two suggestions re: core.spec, `ns`, and clojure 1.9alpha11

2016-08-24 Thread Mond Ray
I agree Colin, this feels more like the beatings shall continue until 
morale improves ;-)

More seriously, I understand the point of the musical instruments analogy 
to be a reminder to programmers that learning a language and understanding 
it in depth will increase your power and expressivity with that language. 
That should not be used as a reason to increase the difficulties caused by 
obscure error reporting. My initial understanding of the sales pitch for 
specs was that it would serve to improve error messages as the macro 
expansions / transformations would be more tractable in the compiler. I get 
that it is a work in progress but let's retain that original goal.

Unlike you however, I would prefer correctness and the consequent ripples 
over the continuing acceptance of incorrect expressions. My reasoning is 
that code which has fewer compatibility style branches will be easier to 
equip with the necessary instrumentation for generating more human friendly 
error messages.

Ray

PS I think this require vs :require thing comes from the way that novices 
confuse the ns macro with the function that pulls dependencies in at the 
REPL. Cutting / pasting between the REPL and the file can allow that to 
bleed in. I know it confused me.

On Wednesday, 24 August 2016 01:09:48 UTC+2, Colin Fleming wrote:
>
> But creating error messages that are optimal for a user with no knowledge 
>> or Clojure or spec is just not the goal.
>
>
> This is a totally false dichotomy. No-one in this thread is asking for 
> that. This thread has several examples of expert Clojure users for whom the 
> error messages are incomprehensible. 
>
> I am equally unapologetic about thinking that the musical instrument 
> analogy is mostly bogus here. There are things that will always be 
> difficult about learning Clojure because they're conceptual, such as 
> functional programming. I think the analogy is fair there, they are just 
> things that will require effort and practice to learn. But the error 
> messages are about giving people the information they need *so that they 
> can actually learn from their mistakes*. Clojure has historically been 
> appallingly bad at that, and no-one should expect their users to flail 
> around randomly trying things to see what works. I've spoken to various 
> smart people who have described their experience of using Clojure as 
> exactly that, even after a non-trivial amount of time using it. I hope spec 
> can improve on that experience.
>
>
> On 24 August 2016 at 02:45, Alex Miller  > wrote:
>
>> I do not have an idea of what the final end point will look like exactly. 
>> I don't get the feeling that there is any answer that you will find 
>> satisfying, so I'm not sure what else I can do for you. We expect Clojure 
>> users to become familiar with spec and its output as it is (now) an 
>> essential part of the language. You will see specs in error messages. 
>>
>> The focus in Clojure has always been biased towards building a powerful 
>> and expressive tool that is rewarding for experts vs optimizing for 
>> novices. Rich has talked at length about why that is (see 
>> https://www.infoq.com/presentations/design-composition-performance-keynote 
>> / 
>> https://github.com/matthiasn/talk-transcripts/blob/master/Hickey_Rich/DesignCompositionPerformance.md
>>  
>> in the section around languages as instruments). Pertinent bit (but you 
>> should watch the whole thing for context):
>>
>> So we need players. I would rant here, but I won't. But look at this 
>> guitar player with blisters. A harpist has blisters, a bass player with 
>> blisters. There's this barrier to overcome for every musician. Imagine if 
>> you downloaded something from GitHub and it gave you blisters.
>>
>> [Audience laughter]
>>
>> Right? The horrors! And yet how many people here play an instrument or 
>> have at one point in their lives? Yeah, a lot of programmers do. And for 
>> how many people did you just pick it up and it was awesome? How many 
>> wished, like, something could have made it more straightforward to get 
>> started with and, like, just made it easy? And how many would have believed 
>> after that that they could play it later? No, not at all. This is - it's 
>> actually quite important. The level of engagement that's required is quite 
>> important.
>>
>> So we shouldn't sell humanity short. Humans are incredible. In 
>> particular, they're incredible learners.
>>
>> One of the things that's really cool is you give a five-year-old or, I 
>> don't know, eight, maybe, a cello and some decent instruction, and they 
>> will learn how to play cello if they spend enough time doing it. In fact, 
>> humans will pretty much learn how to do anything that they spend enough 
>> time doing. We're incredibly good at it.
>>
>> And we're also really good teachers, in general. So I don't think we need 
>> to go to our tools and our instruments and make them oriented towards the 
>> first five 

Re: Frustrations so far

2016-07-23 Thread mond
You are not alone - it's the number one frustration in the community

https://www.reddit.com/r/Clojure/comments/433y02/state_of_clojure_2015_survey_results/

But also see the responses - maybe we will have some major improvements 
with 1.9. Spec looks like good infra to support those efforts for improving 
macro errors.

On a practical note - and it's not a 'solution'  - iterative development 
can helps since you can go usually go back to a known good position and 
work backwards. 

Like I say, not a great solution but might help to reduce the frustration.

Ray


On Friday, 22 July 2016 18:10:21 UTC+2, Peter Romfeld wrote:
>
> NPE is just so painful! most exceptions are not that easy to debug, would 
> be cool if it could say from where the problem was initiated.. (well 
> because most of the time i forget to print the stacktrace with 
> `print-stack-trace `)
>

-- 
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: [ANN] defn podcast ep.3 : A tour of REPLs feat. Mike Fikes :)

2016-06-16 Thread Mond Ray
Thanks Ashish. We will certainly add to the list of interesting subjects. 

To be transparent about our approach, we are starting with the (many) core 
concepts and technologies and will gradually fan out to specialist items.

So items such as ML and other 'big data' related topics will be in our 
sights, but eventually.

In any case, we are going to need some help on that one ;-)

Ray

On Thursday, 16 June 2016 09:52:13 UTC+2, Ashish Negi wrote:
>
> Thanks for the podcast..
> I would highly appreciate one on Machine Learning in Clojure.
>
> On Tuesday, 14 June 2016 13:42:25 UTC+5:30, Vijay Kiran wrote:
>>
>> Hello Everyone!
>>
>> We published the third episode of defn yesterday in which we take a tour 
>> of REPLs in Clojure Land.
>>
>> We are very grateful to Mike Fikes  
>> (Planck/Replete/Ambly fame) - who joined us  on the podcast to share his 
>> experience. We would love to hear your feedback if you got a chance to 
>> listen.
>>
>> defn on soundcloud: https://soundcloud.com/defn-771544745/defn3-repls
>>
>> Cheers!
>> Vijay & Ray 
>>
>>
>>

-- 
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: [ANN] defn podcast ep.3 : A tour of REPLs feat. Mike Fikes :)

2016-06-16 Thread Mond Ray
Thanks for listening Jason and for taking the time to give us feedback. 

To echo Vijay's comments, we will be happy to have a 'follow-up' section in 
the podcast to correct mistakes and omissions.

I really need to work on a defn related pun to end such emails but for now, 
I hope you keep on listening!

Cheers

Ray


On Thursday, 16 June 2016 09:22:00 UTC+2, Vijay Kiran wrote:
>
>
> Thanks for listening and your feedback, Jason.
>
> Sorry for the mistake regarding the Atom REPL, we'll update the blog post, 
> and explain in the next episode. 
>
> I think having more discussion about Developer Experience (IDEs/Build 
> tools etc.) is certainly on my mind for future episode :)
>
> Regards,
> Vijay
>
> On Wednesday, June 15, 2016 at 1:59:13 AM UTC+2, Jason Gilman wrote:
>>
>> Thanks for starting a new Clojure podcast! It's nice to see that the 
>> active count of Clojure podcasts has grown to 2. I had one small note about 
>> the Atom REPL you mentioned around 43 minutes in. You called it the "Atom 
>> Ink" REPL on the podcast. The REPL for Atom is called Proto REPL 
>> . There's a plugin called 
>> Atom ink that plays a small role in how it displays a nested tree of 
>> values. I'd love to hear more discussion of different development 
>> environments and using visualizations to help understand the code we're 
>> writing. Another one in that vein is Gorilla REPL which inspired some of 
>> the visualization aspects of Proto REPL.
>>
>> Thanks, Jason Gilman
>>
>> On Tuesday, June 14, 2016 at 4:12:25 AM UTC-4, Vijay Kiran wrote:
>>>
>>> Hello Everyone!
>>>
>>> We published the third episode of defn yesterday in which we take a tour 
>>> of REPLs in Clojure Land.
>>>
>>> We are very grateful to Mike Fikes  
>>> (Planck/Replete/Ambly fame) - who joined us  on the podcast to share his 
>>> experience. We would love to hear your feedback if you got a chance to 
>>> listen.
>>>
>>> defn on soundcloud: https://soundcloud.com/defn-771544745/defn3-repls
>>>
>>> Cheers!
>>> Vijay & Ray 
>>>
>>>
>>>

-- 
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: [ANN] New Clojure Podcast: defn

2016-05-20 Thread Mond Ray
Thanks Ashish - that's really good feedback. We will improve the notes 
along the lines that you request.

On Friday, 20 May 2016 12:02:05 UTC+2, Ashish Negi wrote:
>
> The notes has :
> ```
> Credits: 
>
> *Music:* Thanks to the very talented *ptzery* for the permitting us to 
> use his music on the opening and closing of the podcast. This track is 
> Melon Hamburger. You can hear more on his SoundCloud 
> .
>
> *Audio:* Thanks to the audio wizard and general tech hero, Mr Wouter 
> Dullaert  for mixing the show and 
> helping us to overcome some serious technical issues.
> ```
>
> I also expect a little about what the podcast is about. What is the 
> speaker discussing in this podcast ?
>
>

-- 
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: [ANN] New Clojure Podcast: defn

2016-05-20 Thread Mond Ray
That's great feedback.

We can (and will) definitely improve the notes along the lines you suggest.

On Friday, 20 May 2016 12:19:17 UTC+2, Alan Forrester wrote:
>
> On 20 May 2016, at 10:32, Vijay Kiran  
> wrote: 
>
> > On Friday, May 20, 2016 at 9:29:29 AM UTC+2, Ashish Negi wrote: 
> >> One suggestion : 
> >> 
> >> Please provide a short description for every podcast episode. This 
> helps immensely. 
> > 
> > Hi Ashish, 
> > 
> > Thanks for tuning in, we post the show notes on https://defn.audio 
> > 
> > I'm not sure where you want to see the description (SoundCloud?) 
>
> If you look at the pages for some podcasts on iTunes, such as the Wired UK 
> podcast, there is a description for the podcast in general, and for each 
> episode: 
>
>
> https://itunes.apple.com/gb/podcast/wired.co.uk-wired-uk-podcast/id404893471?mt=2
>  
>
> For your podcast, there is not much description of the content of the 
> episode: 
>
> https://itunes.apple.com/podcast/defn/id1114899563 
>
> There is also no description of the individual episode when you have 
> downloaded it, just a title #01. 
>
> I haven’t podcasted but my understanding is that Apple gets the 
> information for the description of individual episodes from an RSS feed. 
> The structure of that feed is explained here 
>
> https://blog.idrsolutions.com/2014/08/create-rss-feed/ 
>
> Alan

-- 
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: core.async | compojure ... odd error ... help!!

2014-09-27 Thread mond
Hi James,

Er, nice tip on the routes - that was another thing that I didn't expect to 
work but was happy when it did ;-) I will of course adapt it to your 
recommendation.

Speaking to the main point, no I don't want to put a channel on to the 
response so that's a mistake that I see and would like to avoid. I would 
like to understand where I have gone wrong.

Maybe I need to use HTTPkit instead?

Thanks

Ray



On Saturday, 27 September 2014 02:08:50 UTC+2, James Reeves wrote:

 Hi Ray,

 I don't entirely understand why you expected this to work. Channels aren't 
 a valid Ring response body. The error message is essentially telling you 
 that Compojure has no way of turning the channel object you've returned 
 into a valid response.

 The other problem you have is that the Ring Jetty adapter doesn't have any 
 support for asynchronous operations.

 Another small point. You're using * as an argument name, but this isn't 
 really recommended. This only works by coincidence, and it may be removed 
 in future versions. Instead use something like:

 (GET [/:brand/:country/:resource :resource #.*] [brand country 
 resource] ...)

 - James

 On 27 September 2014 00:14, mond r...@mcdermott.be javascript: wrote:

 My first core.async program ... all works outside of the web app but 
 barfs once I put the functions inside a web container. I hope somebody in 
 the group can point to my obvious mistake...

 The idea is that the function 'respond-within-sla' will give back a 
 result or a come back later message after N milliseconds. It is passed a 
 number of ms, three functions and the arguments for the final function ... 
 which is the one that should operate within the SLA.

 (defn respond-within-sla [expected-result-milliseconds respond-ok 
 respond-later data-fetcher  args]
   (let [data-channel (timeout expected-result-milliseconds)]
 (go (if-let [data (!! data-channel)]
   ((async/close! data-channel)
(respond-ok data))
   (respond-later)))
 (go
   (! data-channel (apply data-fetcher args)

 To keep the volume of code to parse to a minimum I have made a few toy 
 functions that demonstrate the failure...

 ; test funcs

 (defn ok [data]
   (prn (str send HTTP 200 ... got data  data)))

 (defn later []
   (prn (str send HTTP 202 ... request received, come back later)))

 (defn fetcher [arg1 arg2 arg3]
   (prn (str fetching data with args  arg1   arg2   arg3))
   response-data)

 (defn failer [ args]
   (Thread/sleep 1000)
   (str never gets here  args))

 ; test funcs

 (defn ok [data]
   (prn (str send HTTP 200 ... got data  data)))

 (defn later []
   (prn (str send HTTP 202 ... request received, come back later)))

 (defn fetcher [arg1 arg2 arg3]
   (prn (str fetching data with args  arg1   arg2   arg3))
   response-data)

 (defn failer [ args]
   (Thread/sleep 1000)
   (str never gets here  args))

 (defn generate-response [brand country resource]
   (let [sla (or (env :sla-milliseconds) 100)]
 (respond-within-sla sla ok later fetcher brand country resource)))

 (defn generate-fail [brand country resource]
   (let [sla (or (env :sla-milliseconds) 100)]
 (respond-within-sla sla ok later failer brand country resource)))

 From within the REPL it all works fine...

 (generate-response A B C)
 = #ManyToManyChannel 
 clojure.core.async.impl.channels.ManyToManyChannel@4b7ae3f7
 fetching data with args A B C
 send HTTP 200 ... got data response-data
 (generate-fail A B C)
 = #ManyToManyChannel 
 clojure.core.async.impl.channels.ManyToManyChannel@4eb8b5a9
 send HTTP 202 ... request received, come back later

 Here is the compojure route..

 (defroutes app
(GET /:brand/:country/* [brand country *]
 (generate-response brand country *))

(ANY * []
 (route/not-found You must use a REST style to specify 
 brand and country keys in the URL)))

 If I now start it up 'lein run' and try to exercise the functions from 
 the web server...

 $ curl -I http://localhost:5000/A/B/D.jpg
 HTTP/1.1 500 Server Error
 Date: Fri, 26 Sep 2014 23:02:03 GMT
 Content-Length: 0
 Connection: close
 Server: Jetty(7.6.8.v20121106)

 And on the server I see this:

 $ lein run
 Compiling redirector.web
 2014-09-27 01:01:48.426:INFO:oejs.Server:jetty-7.6.8.v20121106
 2014-09-27 01:01:48.458:INFO:oejs.AbstractConnector:Started 
 SelectChannelConnector@0.0.0.0:5000
 2014-09-27 01:02:03.535:WARN:oejs.AbstractHttpConnection:/A/B/D.jpg
 java.lang.IllegalArgumentException: No implementation of method: :render 
 of protocol: #'compojure.response/Renderable found for class: 
 clojure.core.async.impl.channels.ManyToManyChannel
 at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:544)
 at compojure.response$fn__213$G__208__220.invoke(response.clj:9)
 at compojure.core$make_route$fn__332.invoke(core.clj:100)
 at compojure.core$if_route$fn__320.invoke(core.clj:46)
 at compojure.core$if_method$fn__313.invoke(core.clj:33)
 at compojure.core

core.async | compojure ... odd error ... help!!

2014-09-26 Thread mond
My first core.async program ... all works outside of the web app but barfs 
once I put the functions inside a web container. I hope somebody in the 
group can point to my obvious mistake...

The idea is that the function 'respond-within-sla' will give back a result 
or a come back later message after N milliseconds. It is passed a number of 
ms, three functions and the arguments for the final function ... which is 
the one that should operate within the SLA.

(defn respond-within-sla [expected-result-milliseconds respond-ok 
respond-later data-fetcher  args]
  (let [data-channel (timeout expected-result-milliseconds)]
(go (if-let [data (!! data-channel)]
  ((async/close! data-channel)
   (respond-ok data))
  (respond-later)))
(go
  (! data-channel (apply data-fetcher args)

To keep the volume of code to parse to a minimum I have made a few toy 
functions that demonstrate the failure...

; test funcs

(defn ok [data]
  (prn (str send HTTP 200 ... got data  data)))

(defn later []
  (prn (str send HTTP 202 ... request received, come back later)))

(defn fetcher [arg1 arg2 arg3]
  (prn (str fetching data with args  arg1   arg2   arg3))
  response-data)

(defn failer [ args]
  (Thread/sleep 1000)
  (str never gets here  args))

; test funcs

(defn ok [data]
  (prn (str send HTTP 200 ... got data  data)))

(defn later []
  (prn (str send HTTP 202 ... request received, come back later)))

(defn fetcher [arg1 arg2 arg3]
  (prn (str fetching data with args  arg1   arg2   arg3))
  response-data)

(defn failer [ args]
  (Thread/sleep 1000)
  (str never gets here  args))

(defn generate-response [brand country resource]
  (let [sla (or (env :sla-milliseconds) 100)]
(respond-within-sla sla ok later fetcher brand country resource)))

(defn generate-fail [brand country resource]
  (let [sla (or (env :sla-milliseconds) 100)]
(respond-within-sla sla ok later failer brand country resource)))

From within the REPL it all works fine...

(generate-response A B C)
= #ManyToManyChannel 
clojure.core.async.impl.channels.ManyToManyChannel@4b7ae3f7
fetching data with args A B C
send HTTP 200 ... got data response-data
(generate-fail A B C)
= #ManyToManyChannel 
clojure.core.async.impl.channels.ManyToManyChannel@4eb8b5a9
send HTTP 202 ... request received, come back later

Here is the compojure route..

(defroutes app
   (GET /:brand/:country/* [brand country *]
(generate-response brand country *))

   (ANY * []
(route/not-found You must use a REST style to specify 
brand and country keys in the URL)))

If I now start it up 'lein run' and try to exercise the functions from the 
web server...

$ curl -I http://localhost:5000/A/B/D.jpg
HTTP/1.1 500 Server Error
Date: Fri, 26 Sep 2014 23:02:03 GMT
Content-Length: 0
Connection: close
Server: Jetty(7.6.8.v20121106)

And on the server I see this:

$ lein run
Compiling redirector.web
2014-09-27 01:01:48.426:INFO:oejs.Server:jetty-7.6.8.v20121106
2014-09-27 01:01:48.458:INFO:oejs.AbstractConnector:Started 
SelectChannelConnector@0.0.0.0:5000
2014-09-27 01:02:03.535:WARN:oejs.AbstractHttpConnection:/A/B/D.jpg
java.lang.IllegalArgumentException: No implementation of method: :render of 
protocol: #'compojure.response/Renderable found for class: 
clojure.core.async.impl.channels.ManyToManyChannel
at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:544)
at compojure.response$fn__213$G__208__220.invoke(response.clj:9)
at compojure.core$make_route$fn__332.invoke(core.clj:100)
at compojure.core$if_route$fn__320.invoke(core.clj:46)
at compojure.core$if_method$fn__313.invoke(core.clj:33)
at compojure.core$routing$fn__338.invoke(core.clj:113)
at clojure.core$some.invoke(core.clj:2515)
at compojure.core$routing.doInvoke(core.clj:113)
at clojure.lang.RestFn.applyTo(RestFn.java:139)
at clojure.core$apply.invoke(core.clj:626)
at compojure.core$routes$fn__342.invoke(core.clj:118)
at clojure.lang.Var.invoke(Var.java:379)
at 
ring.middleware.keyword_params$wrap_keyword_params$fn__534.invoke(keyword_params.clj:35)
at 
ring.middleware.nested_params$wrap_nested_params$fn__576.invoke(nested_params.clj:84)
at ring.middleware.params$wrap_params$fn__507.invoke(params.clj:64)
at 
ring.middleware.multipart_params$wrap_multipart_params$fn__612.invoke(multipart_params.clj:118)
at ring.middleware.flash$wrap_flash$fn__1286.invoke(flash.clj:35)
at ring.middleware.session$wrap_session$fn__1273.invoke(session.clj:98)
at ring.adapter.jetty$proxy_handler$fn__1426.invoke(jetty.clj:18)
at 
ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$ff19274a.handle(Unknown
 
Source)
at 
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:363)
at 
org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:483)
at 

is it possible to join on lazy seqs?

2013-07-16 Thread mond
Sorry if this is a RTFM style question ... this is what I have in terms of 
types and was wondering if there is another form of join or another library 
/ collection combination that can achieve the same relational join?

Thanks in advance,

Ray

check-delta-feeds.core= (def changed-records (map find-changed-records 
(set/select #(= (:entity %) (entity-names :project))  query-parts)))   

#'check-delta-feeds.core/changed-records
check-delta-feeds.core=check-delta-feeds.core= 
check-delta-feeds.core= (def feed-entries (obtain-feed-entries 
(fetch-atom-feed-until tgb-feed-url until-datetime)))
#'check-delta-feeds.core/feed-entries
check-delta-feeds.core= (type changed-records )
clojure.lang.LazySeq
check-delta-feeds.core= (type feed-entries)
clojure.lang.LazySeq
check-delta-feeds.core= (set/join changed-records feed-entries {:ID 
:dh-uuid})
ClassCastException clojure.lang.LazySeq cannot be cast to java.util.Map 
 clojure.lang.RT.find (RT.java:733)



-- 
-- 
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/groups/opt_out.




Re: keys maps ... gak

2013-06-01 Thread mond
Dang it, I could have sworn that I had tried that combination but actually 
was missing the set wrapping the map!

So thanks TJ - great work and I really appreciate the support.

Ray

On Saturday, June 1, 2013 3:26:32 AM UTC+2, Tj Gabbour wrote:

 Hi Ray,

 Perhaps this?


 user (let [desired-entity-names (set (map entity-names [:model :project]))]
 (select #(desired-entity-names (:entity %))
 query-parts))
 #{{:entity Model,   :main-query select * from model where ...} 
   {:entity Project, :main-query select * from project where ...}}


 All the best,
   Tj

 On Friday, May 31, 2013 10:18:09 PM UTC+2, mond wrote:

 Sometimes its just so frustrating when you know you are close but just 
 cannot get that last little bit ... so I am appealing again for more newbie 
 support 

 I have a map for some names:

 user= (def entity-names {:project Project :model Model})

 Which I then reference in a set of maps:

 user= def query-parts #{{:entity (entity-names :project )
 :main-query select * from project where ... }
{:entity (entity-names :model )
 :main-query select * from model where ... }})

 This query-parts is actually a large set. It will form the basis of 
 execution and sometimes I want to execute a smaller set of the queries.

 So I want to be able to filter it based on a subset of the entity names, 
 for example just :model.

 user= (select #(= (:model entity-names) (:entity %)) query-parts)
 #{{:entity Model, :main-query Model Query}}

 So that works great.

 But I would like a list of entities (:model :project :foo :bar) that 
 represents a subset of the complete set of query parts.

 So I just (sic!) need some advice on how to run this select over a list 
 of entity names rather than just the one shown above.

 I'm ashamed to say that I have spent an hour on this already so this is 
 my white flag. I hope you can help.

 Thanks

 Ray



-- 
-- 
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/groups/opt_out.




keys maps ... gak

2013-05-31 Thread mond
Sometimes its just so frustrating when you know you are close but just 
cannot get that last little bit ... so I am appealing again for more newbie 
support 

I have a map for some names:

user= (def entity-names {:project Project :model Model})

Which I then reference in a set of maps:

user= def query-parts #{{:entity (entity-names :project )
:main-query select * from project where ... }
   {:entity (entity-names :model )
:main-query select * from model where ... }})

This query-parts is actually a large set. It will form the basis of 
execution and sometimes I want to execute a smaller set of the queries.

So I want to be able to filter it based on a subset of the entity names, 
for example just :model.

user= (select #(= (:model entity-names) (:entity %)) query-parts)
#{{:entity Model, :main-query Model Query}}

So that works great.

But I would like a list of entities (:model :project :foo :bar) that 
represents a subset of the complete set of query parts.

So I just (sic!) need some advice on how to run this select over a list of 
entity names rather than just the one shown above.

I'm ashamed to say that I have spent an hour on this already so this is my 
white flag. I hope you can help.

Thanks

Ray

-- 
-- 
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/groups/opt_out.




Re: lazy sequence termination

2013-05-28 Thread Mond Ray
Quite a few views but no bites ... what have I done wrong in asking this 
question? If you can't help with the problem would you please take a moment 
to help me understand what has put you off? 

Too much code not enough data? 

Problem too complex to grasp with the supplied info? 

Too dull?

Am I asking too much ... in other words I have got this far and I should be 
expected to know the answer (I really feel like I should too by the way!)

Use of the term Clojurians?

Something else?

Thanks

Ray


On Tuesday, 28 May 2013 13:44:57 UTC+2, Mond Ray wrote:

 Hi Clojurians,

 I have a program which generates a lazy sequence to read ATOM feed chunks. 
 That part works OK but now I want to only take a limited number of the 
 chunks based on some predicate. My predicate is currently returning an 
 empty list so I think (hope!) I am missing something simple.

 Here is an excerpt from the feed (yes the feed is in JSON and I have used 
 cheshire to map to Clojure data):

 {:FEED_ID 5650f3ad-b793-42c6-9fef-80ba105f847d, :TITLE Delta Feed, 
 :UPDATED 2013-05-28T11:15:34.860+02:00, :GENERATOR Products, :LINK 
 {:REL Self, :HREF http://some-url}, :PREVIOUS_LINK {:REL 
 prev-archive, :HREF http://another-url}, .

 Here is the sequence generator:

 (defn fetch-atom-chunk
   Returns an ATOM chunk from the feed-url
   [feed-url]
   (:ATOM_FEED (:body (client/get feed-url {:as :json}

 (defn fetch-atom-feed
   Returns a list of ATOM chunks from the feed-url going back to the 
 from-date
   ;  [feed-url from-date] (fetch-atom-feed feed-url from-date ())
   [feed-url]
   (let [chunk (fetch-atom-chunk feed-url)]
 (lazy-seq
   (cons chunk (fetch-atom-feed (:HREF (:PREVIOUS_LINK chunk)))

 Here is the predicate:

 (defn feed-date-to-clj-date
   Patch for ATOM feed date formats like '2013-05-27T14:35:00.982+02:00'
we have to remove the final ':' from the string to enable its 
 conversion
   [feed-date]
   (.parse (java.text.SimpleDateFormat. -MM-dd'T'HH:mm:ss.SSSZ)
 (str/replace feed-date #(.*)(\d\d):(\d\d) $1$2$3)))

 (defn after?
   Returns true if first-date is after second-date
   [first-date second-date]
   ( (.getTime first-date) (.getTime second-date)))

 (defn more-feed-chunks-needed?
   Look at the atom chunk and decide if we have enough
   [from-date atom-chunk]
   (after? from-date (feed-date-to-clj-date (:UPDATED atom-chunk

 And here is how I run it forever:

 (fetch-atom-feed https://producta.blah.com/feed/current;)

 And here is how I run it with a predicate:

 (def date-from (.parse (java.text.SimpleDateFormat. -MM-dd) 
 2013-05-27))
 (take-while #(more-feed-chunks-needed? date-from %) (fetch-atom-feed 
 https://producta.blah.com/feed/current;))

 Maybe this is too detailed for the group (do let me know) but otherwise I 
 would appreciate your advice / tips on what I could try.

 Thanks

 Ray




-- 
-- 
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/groups/opt_out.




Re: lazy sequence termination

2013-05-28 Thread Mond Ray
Ah ... client/get is part of the standard httpclient library so it's not my 
code - it fetches from a url. Sorry I should have included the libraries 
that I used.

On Tuesday, May 28, 2013 10:33:52 PM UTC+2, nchurch wrote:

 Could you include the code in client/get?  Not sure where that comes 
 from and it would be helpful to run the code to see what happensno 
 guarantees though =) 

 On May 28, 1:05 pm, Mond Ray mondraym...@gmail.com wrote: 
  Quite a few views but no bites ... what have I done wrong in asking this 
  question? If you can't help with the problem would you please take a 
 moment 
  to help me understand what has put you off? 
  
  Too much code not enough data? 
  
  Problem too complex to grasp with the supplied info? 
  
  Too dull? 
  
  Am I asking too much ... in other words I have got this far and I should 
 be 
  expected to know the answer (I really feel like I should too by the 
 way!) 
  
  Use of the term Clojurians? 
  
  Something else? 
  
  Thanks 
  
  Ray 
  
  
  
  
  
  
  
  On Tuesday, 28 May 2013 13:44:57 UTC+2, Mond Ray wrote: 
  
   Hi Clojurians, 
  
   I have a program which generates a lazy sequence to read ATOM feed 
 chunks. 
   That part works OK but now I want to only take a limited number of the 
   chunks based on some predicate. My predicate is currently returning an 
   empty list so I think (hope!) I am missing something simple. 
  
   Here is an excerpt from the feed (yes the feed is in JSON and I have 
 used 
   cheshire to map to Clojure data): 
  
   {:FEED_ID 5650f3ad-b793-42c6-9fef-80ba105f847d, :TITLE Delta Feed, 
   :UPDATED 2013-05-28T11:15:34.860+02:00, :GENERATOR Products, :LINK 
   {:REL Self, :HREF http://some-url}, :PREVIOUS_LINK {:REL 
   prev-archive, :HREF http://another-url}, . 
  
   Here is the sequence generator: 
  
   (defn fetch-atom-chunk 
 Returns an ATOM chunk from the feed-url 
 [feed-url] 
 (:ATOM_FEED (:body (client/get feed-url {:as :json} 
  
   (defn fetch-atom-feed 
 Returns a list of ATOM chunks from the feed-url going back to the 
   from-date 
 ;  [feed-url from-date] (fetch-atom-feed feed-url from-date ()) 
 [feed-url] 
 (let [chunk (fetch-atom-chunk feed-url)] 
   (lazy-seq 
 (cons chunk (fetch-atom-feed (:HREF (:PREVIOUS_LINK chunk))) 
  
   Here is the predicate: 
  
   (defn feed-date-to-clj-date 
 Patch for ATOM feed date formats like 
 '2013-05-27T14:35:00.982+02:00' 
  we have to remove the final ':' from the string to enable its 
   conversion 
 [feed-date] 
 (.parse (java.text.SimpleDateFormat. -MM-dd'T'HH:mm:ss.SSSZ) 
   (str/replace feed-date #(.*)(\d\d):(\d\d) $1$2$3))) 
  
   (defn after? 
 Returns true if first-date is after second-date 
 [first-date second-date] 
 ( (.getTime first-date) (.getTime second-date))) 
  
   (defn more-feed-chunks-needed? 
 Look at the atom chunk and decide if we have enough 
 [from-date atom-chunk] 
 (after? from-date (feed-date-to-clj-date (:UPDATED atom-chunk 
  
   And here is how I run it forever: 
  
   (fetch-atom-feed https://producta.blah.com/feed/current;) 
  
   And here is how I run it with a predicate: 
  
   (def date-from (.parse (java.text.SimpleDateFormat. -MM-dd) 
   2013-05-27)) 
   (take-while #(more-feed-chunks-needed? date-from %) (fetch-atom-feed  
  https://producta.blah.com/feed/current;)) 
  
   Maybe this is too detailed for the group (do let me know) but 
 otherwise I 
   would appreciate your advice / tips on what I could try. 
  
   Thanks 
  
   Ray 


-- 
-- 
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/groups/opt_out.




Re: lazy sequence termination

2013-05-28 Thread Mond Ray
Thanks - I wasn't sure how much to assume so that's appreciated. I will 
definitely check for the correctness of the predicate vs the date - that 
would really be a face palm!

OTOH I feel good that I'm actually writing sane Clojure code even if it is 
broken with my data ;-)

Ray

On Tuesday, 28 May 2013 23:46:34 UTC+2, Michael Gardner wrote:

 On May 28, 2013, at 15:05 , Mond Ray mondr...@gmail.com javascript: 
 wrote: 

  Quite a few views but no bites ... what have I done wrong in asking this 
 question? If you can't help with the problem would you please take a moment 
 to help me understand what has put you off? 

 It would help to post less code, to make it easier for people to zero in 
 on the critical parts. For example, it's not necessary to include 
 'fetch-atom-chunk and 'fetch-atom-feed since you can just post some sample 
 parsed data (which you did). 

 When I try running your predicate against your sample data, the predicate 
 returns false because 2013-05-27 isn't after the feed's :UPDATED date. 
 Might you have the arguments to 'after? reversed?

-- 
-- 
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/groups/opt_out.




lazy seq termination

2013-05-28 Thread mond
I have managed to create a function that will read an ATOM feed 
indefinitely and now need a way to stop it ;-)

(defn fetch-atom-chunk
  Returns an ATOM chunk from the feed-url
  [feed-url]
  (:ATOM_FEED (:body (client/get feed-url {:as :json}

(defn fetch-atom-feed
  Returns a list of ATOM chunks from the feed-url going back to the 
from-date
  [feed-url]
  (let [chunk (fetch-atom-chunk feed-url)]
(lazy-seq
  (cons chunk (fetch-atom-feed (:HREF (:PREVIOUS_LINK chunk)))

(defn after?
  Returns true if first-date is after second-date
  [first-date second-date]
  ( (.getTime first-date) (.getTime second-date)))

(defn feed-date-to-clj-date
  Patch for ATOM feed date formats like '2013-05-27T14:35:00.982+02:00'
   we have to remove the final ':' from the string to enable its conversion
  [feed-date]
  (.parse (java.text.SimpleDateFormat. -MM-dd'T'HH:mm:ss.SSSZ)
(str/replace feed-date #(.*)(\d\d):(\d\d) $1$2$3)))

(defn more-feed-chunks-needed?
  Look at the atom chunk and decide if we have enough
  [from-date atom-chunk]
  (after? from-date (feed-date-to-clj-date (:UPDATED atom-chunk

(def date-from (.parse (java.text.SimpleDateFormat. -MM-dd) 
2013-05-27))

(take-while #(more-feed-chunks-needed? date-from %) (fetch-atom-feed 
some-url))

; example of the feed
; {:FEED_ID 5650f3ad-b793-42c6-9fef-80ba105f847d, :TITLE Delta Feed, 
:UPDATED 2013-05-28T11:15:34.860+02:00, :GENERATOR Products, :LINK 
{:REL Self, :HREF http://some-url}, :PREVIOUS_LINK {:REL 
prev-archive, :HREF http://another-url}, .

If I run without the take-while it runs forever (so I am happy that its 
generating data) but when I add the take-while I get an empty list.

I guess I'm missing something simple and would welcome suggestions.

Thanks

Ray

-- 
-- 
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/groups/opt_out.




Re: creating maps out of sql

2013-05-25 Thread Mond Ray
My updated approach is now like this:

(defn find-uuids [query a2p-id]
  (sql/with-connection sqlserver
(sql/with-query-results rs
  [query a2p-id]
  (:uuid (first rs)

(defn find-records [{query :main-query from-date :from-date to-date 
:to-date sub-query-fn :sub-query-fn}]
  (sql/with-connection db2
(sql/with-query-results rs
  [query from-date to-date]
(doall (map #(hash-map :a2p-id (:id %) :dh-uuid (sub-query-fn (:id 
%))) rs)

; from / to will be supplied by user so I will factor it out here for now
(def user-from 2013-05-24)
(def user-to 2013-05-25)

(def query-parts #{{:entity Project
   :main-query select project_id as id from project where 
last_updated  ? and last_updated  ?
   :from-date user-from
  :to-date user-to
  :sub-query-fn (partial find-uuids select project_uuid as 
uuid from project where a2p-id = ?)
}
   {:entity Model
 :main-query select model_id as id from model where 
last_updated  ? and last_updated  ?
 :from-date user-from
 :to-date user-to
 :sub-query-fn (partial find-uuids select model_uuid 
as uuid from model where a2p-id = ?)
 }
   })

(map find-records query-parts)

Looking at the set of maps it's clear that there may be room for further 
compression but I want to wait until I get all of the queries and odd cases 
out of the way first.

Thanks again for your help.

Ray

On Friday, 24 May 2013 23:20:11 UTC+2, Mond Ray wrote:

 Fantastic answer Marc - I had been fiddling about with map and hash maps 
 and could quite get the parens right but now its sorted. Brilliant - thanks 
 again for the help.

 Ray

 On Friday, 24 May 2013 18:56:54 UTC+2, mlimotte wrote:

 Hi Ray,

 First, I'd remove the print(ln)s from the functions.  No need for 
 side-effects there.  Just have the functions return the result value and 
 then when you call a2p-records-by-date you can wrap it in a println.  Then 
 you want to create a Map as the return value of the inner loop 
 in a2p-records-by-date

 (defn a2p-records-by-date [query from-date to-date dh-sub-query]
   (sql/with-connection db2
   (sql/with-query-results rs [query from-date to-date]
 (doall (map #(hashmap :a2pid (:project_id %) :uuid (dh-sub-query 
 (:project_id %))) rs)
 ; the doall is needed so the rs is completely consumed before 
 (sql/with-query-results 
 …) closes the connection

 (defn dh-records-by-a2p-id [query a2p-id]
   (sql/with-connection mssql
   (sql/with-query-results rs [query a2p-id]
  (:project_uuid (first rs)
 ; I'm assuming there's only zero or one :project_uuid for a given a2p-id. 
 ; In the zero case, rs is an empty Seq, so (first rs) returns nil and 
 (:project_uuid nil) is nil

 (defn dh-sub-query [a2p-id] (dh-records-by-a2p-id select PROJECT_UUID 
 from PROJECT where A2P_PROJECT_ID = ? a2p-id))

 (println (a2p-records-by-date select project_id from PROJECT where 
 timestamp  ? and timestamp  ? 2012-03-02 2012-03-07 dh-sub-query))


 Of course, I haven't tested any of this, so plenty of opportunity for 
 mistakes.

 Marc


 On Fri, May 24, 2013 at 10:55 AM, Mond Ray mondr...@gmail.com wrote:

 I am starting out to use Clojure to combine and verify data between DB2 
 on a Mainframe, SQL Server and an Atom Feed. Yes, it's such fun working in 
 a start-up ;-)

 Database wise, all is connecting OK after some Leiningen shenanigans and 
 I am now stuck on the mapping part ;-)

 The code is below.  I want to generate a set of maps that show the 
 following:

 #({:a2pid 269, :uuid nil}
 {:a2pid 270, :uuid nil}
 {:a2pid 258, :uuid nil}
 {:a2pid 261, :uuid nil}
 {:a2pid 251, :uuid E7D4262C-62B3-4129-9CE4-B342DC1C39FC})

 The idea is to have a list of maps that can show where there are gaps 
 between the two DBs and, coming next, the Atom feed.

 It is essentially a join operation - and maybe that's what I need but I 
 cannot figure out where to plonk the magic words.

 (defn a2p-records-by-date [query from-date to-date dh-sub-query]
   (sql/with-connection db2
   (sql/with-query-results rs [query from-date to-date]
 (doseq [row rs] (println (str   (:project_id row) 
 (dh-sub-query (:project_id row

  (defn dh-records-by-a2p-id [query a2p-id]
   (sql/with-connection mssql
   (sql/with-query-results rs [query a2p-id]
 (dorun (map #(print (:project_uuid %)) rs)

 (defn dh-sub-query [a2p-id] (dh-records-by-a2p-id select PROJECT_UUID 
 from PROJECT where A2P_PROJECT_ID = ? a2p-id))

 (a2p-records-by-date select project_id from PROJECT where timestamp  ? 
 and timestamp  ? 2012-03-02 2012-03-07 dh-sub-query)

 The output looks like this, so I am close!

  269
  270
  258
  261
 E7D4262C-62B3-4129-9CE4-B342DC1C39FC 251

  Can anyone help me out about how to generate the maps?

 This is just the start as I have many

filter on sets ... [reward: me face palming]

2013-05-25 Thread Mond Ray
I am missing something obvious... I get a list of maps back from a function 
and I want to find the elements with nil

(({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid 
def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, :dh-uuid 
abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} {:a2p-id 3, 
:dh-uuid nil}))

I try the select function but it has no effect ... same list

(set/select #(not (:dh-uuid %)) (map find-records query-parts))

also tried the previously working example... same list

(filter #(not (:dh-uuid %)) (map find-records query-parts))

I am assuming that I am not indexing into each of the maps but I cannot 
remember or find out how to do this ... all examples only show one map

Thanks

Ray

-- 
-- 
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/groups/opt_out.




Re: filter on sets ... [reward: me face palming]

2013-05-25 Thread Mond Ray
Bingo:

user= (filter (complement :dh-uuid) (apply concat (map find-records 
query-parts)))
({:a2p-id 3, :dh-uuid nil} {:a2p-id 3, :dh-uuid nil})

Thanks everyone!


On Saturday, 25 May 2013 19:29:55 UTC+2, Andy Fingerhut wrote:

 Woops, and here I wasn't being careful enough...  You have a list (or 
 sequence) of two elements, both of which are lists (or sequences) of maps.  
 You can use (apply concat (map find-records query-parts)) to return a 
 single list containing nothing but maps.

 Andy


 On Sat, May 25, 2013 at 10:27 AM, Andy Fingerhut 
 andy.fi...@gmail.comjavascript:
  wrote:

 If (map find-records query-parts) is returning this expression:


 (({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid 
 def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, :dh-uuid 
 abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} {:a2p-id 3, 
 :dh-uuid nil}))

 then especially note the double parentheses.  That is a list (or 
 sequence), whose first element is a list (or sequence) of maps.  You can 
 use (first (map find-records query-parts)) to get the inner list.

 Andy


 On Sat, May 25, 2013 at 4:21 AM, Mond Ray mondr...@gmail.comjavascript:
  wrote:

 I am missing something obvious... I get a list of maps back from a 
 function and I want to find the elements with nil

 (({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid 
 def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, :dh-uuid 
 abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} {:a2p-id 3, 
 :dh-uuid nil}))

 I try the select function but it has no effect ... same list

 (set/select #(not (:dh-uuid %)) (map find-records query-parts))

 also tried the previously working example... same list

 (filter #(not (:dh-uuid %)) (map find-records query-parts))

 I am assuming that I am not indexing into each of the maps but I cannot 
 remember or find out how to do this ... all examples only show one map

 Thanks

 Ray

 -- 
 -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.comjavascript:
 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 javascript:
 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+u...@googlegroups.com javascript:.
 For more options, visit https://groups.google.com/groups/opt_out.
  
  





-- 
-- 
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/groups/opt_out.




Re: filter on sets ... [reward: me face palming]

2013-05-25 Thread Mond Ray
On the other hand ... does that mean that the sequence being filtered is 
not lazy?

On Saturday, 25 May 2013 20:09:05 UTC+2, Mond Ray wrote:

 Bingo:

 user= (filter (complement :dh-uuid) (apply concat (map find-records 
 query-parts)))
 ({:a2p-id 3, :dh-uuid nil} {:a2p-id 3, :dh-uuid nil})

 Thanks everyone!


 On Saturday, 25 May 2013 19:29:55 UTC+2, Andy Fingerhut wrote:

 Woops, and here I wasn't being careful enough...  You have a list (or 
 sequence) of two elements, both of which are lists (or sequences) of maps.  
 You can use (apply concat (map find-records query-parts)) to return a 
 single list containing nothing but maps.

 Andy


 On Sat, May 25, 2013 at 10:27 AM, Andy Fingerhut andy.fi...@gmail.comwrote:

 If (map find-records query-parts) is returning this expression:


 (({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid 
 def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, :dh-uuid 
 abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} {:a2p-id 3, 
 :dh-uuid nil}))

 then especially note the double parentheses.  That is a list (or 
 sequence), whose first element is a list (or sequence) of maps.  You can 
 use (first (map find-records query-parts)) to get the inner list.

 Andy


 On Sat, May 25, 2013 at 4:21 AM, Mond Ray mondr...@gmail.com wrote:

 I am missing something obvious... I get a list of maps back from a 
 function and I want to find the elements with nil

 (({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid 
 def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, :dh-uuid 
 abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} {:a2p-id 3, 
 :dh-uuid nil}))

 I try the select function but it has no effect ... same list

 (set/select #(not (:dh-uuid %)) (map find-records query-parts))

 also tried the previously working example... same list

 (filter #(not (:dh-uuid %)) (map find-records query-parts))

 I am assuming that I am not indexing into each of the maps but I cannot 
 remember or find out how to do this ... all examples only show one map

 Thanks

 Ray

 -- 
 -- 
 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
 --- 
 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+u...@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.
  
  





-- 
-- 
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/groups/opt_out.




Re: filter on sets ... [reward: me face palming]

2013-05-25 Thread Mond Ray
It's as I presented it - a list of a list of maps

On Saturday, 25 May 2013 13:27:45 UTC+2, atkaaz wrote:

 can you tell what this returns? 
 (map find-records query-parts)


 On Sat, May 25, 2013 at 2:21 PM, Mond Ray mondr...@gmail.comjavascript:
  wrote:

 I am missing something obvious... I get a list of maps back from a 
 function and I want to find the elements with nil

 (({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid 
 def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, :dh-uuid 
 abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} {:a2p-id 3, 
 :dh-uuid nil}))

 I try the select function but it has no effect ... same list

 (set/select #(not (:dh-uuid %)) (map find-records query-parts))

 also tried the previously working example... same list

 (filter #(not (:dh-uuid %)) (map find-records query-parts))

 I am assuming that I am not indexing into each of the maps but I cannot 
 remember or find out how to do this ... all examples only show one map

 Thanks

 Ray

 -- 
 -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.comjavascript:
 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 javascript:
 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+u...@googlegroups.com javascript:.
 For more options, visit https://groups.google.com/groups/opt_out.
  
  




-- 
-- 
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/groups/opt_out.




Re: filter on sets ... [reward: me face palming]

2013-05-25 Thread Mond Ray
This is my latest working invocation:

(doall (map #(remove :dh-uuid %) (map find-records query-parts)))
(({:a2p-id 3, :dh-uuid nil}) ({:a2p-id 3, :dh-uuid nil}))

The list of maps is retained which might be useful later.

Thanks

On Saturday, 25 May 2013 20:15:19 UTC+2, sdegutis wrote:

 Wouldn't (remove :dh-uuid (apply concat (map find-records query-parts))) 
 be better?


 On Sat, May 25, 2013 at 1:09 PM, Mond Ray mondr...@gmail.comjavascript:
  wrote:

 Bingo:

 user= (filter (complement :dh-uuid) (apply concat (map find-records 
 query-parts)))
 ({:a2p-id 3, :dh-uuid nil} {:a2p-id 3, :dh-uuid nil})

 Thanks everyone!


 On Saturday, 25 May 2013 19:29:55 UTC+2, Andy Fingerhut wrote:

 Woops, and here I wasn't being careful enough...  You have a list (or 
 sequence) of two elements, both of which are lists (or sequences) of maps.  
 You can use (apply concat (map find-records query-parts)) to return a 
 single list containing nothing but maps.

 Andy


 On Sat, May 25, 2013 at 10:27 AM, Andy Fingerhut 
 andy.fi...@gmail.comwrote:

 If (map find-records query-parts) is returning this expression:


 (({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid 
 def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, :dh-uuid 
 abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} {:a2p-id 3, 
 :dh-uuid nil}))

 then especially note the double parentheses.  That is a list (or 
 sequence), whose first element is a list (or sequence) of maps.  You can 
 use (first (map find-records query-parts)) to get the inner list.

 Andy


 On Sat, May 25, 2013 at 4:21 AM, Mond Ray mondr...@gmail.com wrote:

 I am missing something obvious... I get a list of maps back from a 
 function and I want to find the elements with nil

 (({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid 
 def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, :dh-uuid 
 abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} {:a2p-id 
 3, 
 :dh-uuid nil}))

 I try the select function but it has no effect ... same list

 (set/select #(not (:dh-uuid %)) (map find-records query-parts))

 also tried the previously working example... same list

 (filter #(not (:dh-uuid %)) (map find-records query-parts))

 I am assuming that I am not indexing into each of the maps but I 
 cannot remember or find out how to do this ... all examples only show one 
 map

 Thanks

 Ray

 -- 
 -- 
 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=enhttp://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+u...@**googlegroups.com.

 For more options, visit 
 https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
 .
  
  



  -- 
 -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.comjavascript:
 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 javascript:
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en
 --- 
 You received this message because you are subscribed to a topic in the 
 Google Groups Clojure group.
 To unsubscribe from this topic, visit 
 https://groups.google.com/d/topic/clojure/0TfUVMsfcD8/unsubscribe?hl=en.
 To unsubscribe from this group and all its topics, send an email to 
 clojure+u...@googlegroups.com javascript:.
 For more options, visit https://groups.google.com/groups/opt_out.
  
  




-- 
-- 
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/groups/opt_out.




Re: filter on sets ... [reward: me face palming]

2013-05-25 Thread Mond Ray
Thanks for the demo ... the next part of my code will decide if the key is 
in an atom feed so I will be testing for false in the results too - FTW 
indeed :D

On Saturday, 25 May 2013 21:01:31 UTC+2, atkaaz wrote:

 = (doall (map #(remove :dh-uuid %) 
 '(({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, 
 :dh-uuid def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, 
 :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} 
 {:a2p-id 3, :dh-uuid *false*}))
 ))
 (({:a2p-id 3, :dh-uuid nil}) ({:a2p-id 3, :dh-uuid *false*}))

 idiomatic clojure ftw :) 


 On Sat, May 25, 2013 at 9:38 PM, Mond Ray mondr...@gmail.comjavascript:
  wrote:

 This is my latest working invocation:

 (doall (map #(remove :dh-uuid %) (map find-records query-parts)))
 (({:a2p-id 3, :dh-uuid nil}) ({:a2p-id 3, :dh-uuid nil}))

 The list of maps is retained which might be useful later.

 Thanks

 On Saturday, 25 May 2013 20:15:19 UTC+2, sdegutis wrote:

 Wouldn't (remove :dh-uuid (apply concat (map find-records query-parts))) 
 be better?


 On Sat, May 25, 2013 at 1:09 PM, Mond Ray mondr...@gmail.com wrote:

  Bingo:

 user= (filter (complement :dh-uuid) (apply concat (map find-records 
 query-parts)))
 ({:a2p-id 3, :dh-uuid nil} {:a2p-id 3, :dh-uuid nil})

 Thanks everyone!


 On Saturday, 25 May 2013 19:29:55 UTC+2, Andy Fingerhut wrote:

 Woops, and here I wasn't being careful enough...  You have a list (or 
 sequence) of two elements, both of which are lists (or sequences) of 
 maps.  
 You can use (apply concat (map find-records query-parts)) to return a 
 single list containing nothing but maps.

 Andy


 On Sat, May 25, 2013 at 10:27 AM, Andy Fingerhut andy.fi...@gmail.com
  wrote:

 If (map find-records query-parts) is returning this expression:


 (({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid 
 def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, :dh-uuid 
 abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} {:a2p-id 
 3, 
 :dh-uuid nil}))

 then especially note the double parentheses.  That is a list (or 
 sequence), whose first element is a list (or sequence) of maps.  You can 
 use (first (map find-records query-parts)) to get the inner list.

 Andy


 On Sat, May 25, 2013 at 4:21 AM, Mond Ray mondr...@gmail.com wrote:

 I am missing something obvious... I get a list of maps back from a 
 function and I want to find the elements with nil

 (({:a2p-id 1, :dh-uuid abc-def-ghi-klm} {:a2p-id 2, :dh-uuid 
 def-ghi-klm-opq} {:a2p-id 3, :dh-uuid nil}) ({:a2p-id 1, :dh-uuid 
 abc-def-ghi-klm} {:a2p-id 2, :dh-uuid def-ghi-klm-opq} {:a2p-id 
 3, 
 :dh-uuid nil}))

 I try the select function but it has no effect ... same list

 (set/select #(not (:dh-uuid %)) (map find-records query-parts))

 also tried the previously working example... same list

 (filter #(not (:dh-uuid %)) (map find-records query-parts))

 I am assuming that I am not indexing into each of the maps but I 
 cannot remember or find out how to do this ... all examples only show 
 one 
 map

 Thanks

 Ray

 -- 
 -- 
 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=enhttp://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+u...@**googlegroups.com.

 For more options, visit 
 https://groups.google.com/**grou**ps/opt_outhttps://groups.google.com/groups/opt_out
 .
  
  



  -- 
 -- 
 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=enhttp://groups.google.com/group/clojure?hl=en
 --- 
 You received this message because you are subscribed to a topic in the 
 Google Groups Clojure group.
 To unsubscribe from this topic, visit https://groups.google.com/d/**
 topic/clojure/0TfUVMsfcD8/**unsubscribe?hl=enhttps://groups.google.com/d/topic/clojure/0TfUVMsfcD8/unsubscribe?hl=en
 .
  To unsubscribe from this group and all its topics, send an email to 
 clojure+u...@**googlegroups.com.

 For more options, visit 
 https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
 .
  
  


  -- 
 -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email

creating maps out of sql

2013-05-24 Thread Mond Ray
I am starting out to use Clojure to combine and verify data between DB2 on 
a Mainframe, SQL Server and an Atom Feed. Yes, it's such fun working in a 
start-up ;-)

Database wise, all is connecting OK after some Leiningen shenanigans and I 
am now stuck on the mapping part ;-)

The code is below.  I want to generate a set of maps that show the 
following:

#({:a2pid 269, :uuid nil}
{:a2pid 270, :uuid nil}
{:a2pid 258, :uuid nil}
{:a2pid 261, :uuid nil}
{:a2pid 251, :uuid E7D4262C-62B3-4129-9CE4-B342DC1C39FC})

The idea is to have a list of maps that can show where there are gaps 
between the two DBs and, coming next, the Atom feed.

It is essentially a join operation - and maybe that's what I need but I 
cannot figure out where to plonk the magic words.

(defn a2p-records-by-date [query from-date to-date dh-sub-query]
  (sql/with-connection db2
  (sql/with-query-results rs [query from-date to-date]
(doseq [row rs] (println (str   (:project_id row) (dh-sub-query 
(:project_id row

(defn dh-records-by-a2p-id [query a2p-id]
  (sql/with-connection mssql
  (sql/with-query-results rs [query a2p-id]
(dorun (map #(print (:project_uuid %)) rs)

(defn dh-sub-query [a2p-id] (dh-records-by-a2p-id select PROJECT_UUID from 
PROJECT where A2P_PROJECT_ID = ? a2p-id))

(a2p-records-by-date select project_id from PROJECT where timestamp  ? 
and timestamp  ? 2012-03-02 2012-03-07 dh-sub-query)

The output looks like this, so I am close!

 269
 270
 258
 261
E7D4262C-62B3-4129-9CE4-B342DC1C39FC 251

Can anyone help me out about how to generate the maps?

This is just the start as I have many queries to run for many entities, so 
doubtless I will be back again ;-)

Thanks in advance

Ray

-- 
-- 
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/groups/opt_out.




Re: creating maps out of sql

2013-05-24 Thread Mond Ray
Fantastic answer Marc - I had been fiddling about with map and hash maps 
and could quite get the parens right but now its sorted. Brilliant - thanks 
again for the help.

Ray

On Friday, 24 May 2013 18:56:54 UTC+2, mlimotte wrote:

 Hi Ray,

 First, I'd remove the print(ln)s from the functions.  No need for 
 side-effects there.  Just have the functions return the result value and 
 then when you call a2p-records-by-date you can wrap it in a println.  Then 
 you want to create a Map as the return value of the inner loop 
 in a2p-records-by-date

 (defn a2p-records-by-date [query from-date to-date dh-sub-query]
   (sql/with-connection db2
   (sql/with-query-results rs [query from-date to-date]
 (doall (map #(hashmap :a2pid (:project_id %) :uuid (dh-sub-query 
 (:project_id %))) rs)
 ; the doall is needed so the rs is completely consumed before 
 (sql/with-query-results 
 …) closes the connection

 (defn dh-records-by-a2p-id [query a2p-id]
   (sql/with-connection mssql
   (sql/with-query-results rs [query a2p-id]
  (:project_uuid (first rs)
 ; I'm assuming there's only zero or one :project_uuid for a given a2p-id. 
 ; In the zero case, rs is an empty Seq, so (first rs) returns nil and 
 (:project_uuid nil) is nil

 (defn dh-sub-query [a2p-id] (dh-records-by-a2p-id select PROJECT_UUID 
 from PROJECT where A2P_PROJECT_ID = ? a2p-id))

 (println (a2p-records-by-date select project_id from PROJECT where 
 timestamp  ? and timestamp  ? 2012-03-02 2012-03-07 dh-sub-query))


 Of course, I haven't tested any of this, so plenty of opportunity for 
 mistakes.

 Marc


 On Fri, May 24, 2013 at 10:55 AM, Mond Ray mondr...@gmail.comjavascript:
  wrote:

 I am starting out to use Clojure to combine and verify data between DB2 
 on a Mainframe, SQL Server and an Atom Feed. Yes, it's such fun working in 
 a start-up ;-)

 Database wise, all is connecting OK after some Leiningen shenanigans and 
 I am now stuck on the mapping part ;-)

 The code is below.  I want to generate a set of maps that show the 
 following:

 #({:a2pid 269, :uuid nil}
 {:a2pid 270, :uuid nil}
 {:a2pid 258, :uuid nil}
 {:a2pid 261, :uuid nil}
 {:a2pid 251, :uuid E7D4262C-62B3-4129-9CE4-B342DC1C39FC})

 The idea is to have a list of maps that can show where there are gaps 
 between the two DBs and, coming next, the Atom feed.

 It is essentially a join operation - and maybe that's what I need but I 
 cannot figure out where to plonk the magic words.

 (defn a2p-records-by-date [query from-date to-date dh-sub-query]
   (sql/with-connection db2
   (sql/with-query-results rs [query from-date to-date]
 (doseq [row rs] (println (str   (:project_id row) (dh-sub-query 
 (:project_id row

  (defn dh-records-by-a2p-id [query a2p-id]
   (sql/with-connection mssql
   (sql/with-query-results rs [query a2p-id]
 (dorun (map #(print (:project_uuid %)) rs)

 (defn dh-sub-query [a2p-id] (dh-records-by-a2p-id select PROJECT_UUID 
 from PROJECT where A2P_PROJECT_ID = ? a2p-id))

 (a2p-records-by-date select project_id from PROJECT where timestamp  ? 
 and timestamp  ? 2012-03-02 2012-03-07 dh-sub-query)

 The output looks like this, so I am close!

  269
  270
  258
  261
 E7D4262C-62B3-4129-9CE4-B342DC1C39FC 251

  Can anyone help me out about how to generate the maps?

 This is just the start as I have many queries to run for many entities, 
 so doubtless I will be back again ;-)

 Thanks in advance

 Ray

 -- 
 -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.comjavascript:
 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 javascript:
 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+u...@googlegroups.com javascript:.
 For more options, visit https://groups.google.com/groups/opt_out.
  
  




-- 
-- 
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/groups/opt_out.




Re: creating maps out of sql

2013-05-24 Thread Mond Ray
Thanks. I had ruled that style of option out as this is small one-off, 
specific task.

On Friday, 24 May 2013 21:41:45 UTC+2, Mark wrote:

 You might want to consider a federated database tool like http://teiid.org
 It would be pretty easy to link DB2 and SQL Server.  Hooking up the Atom 
 feed would require a bit more work but probably not too bad.

 On Friday, May 24, 2013 7:55:17 AM UTC-7, Mond Ray wrote:

 I am starting out to use Clojure to combine and verify data between DB2 
 on a Mainframe, SQL Server and an Atom Feed. Yes, it's such fun working in 
 a start-up ;-)

 Database wise, all is connecting OK after some Leiningen shenanigans and 
 I am now stuck on the mapping part ;-)

 The code is below.  I want to generate a set of maps that show the 
 following:

 #({:a2pid 269, :uuid nil}
 {:a2pid 270, :uuid nil}
 {:a2pid 258, :uuid nil}
 {:a2pid 261, :uuid nil}
 {:a2pid 251, :uuid E7D4262C-62B3-4129-9CE4-B342DC1C39FC})

 The idea is to have a list of maps that can show where there are gaps 
 between the two DBs and, coming next, the Atom feed.

 It is essentially a join operation - and maybe that's what I need but I 
 cannot figure out where to plonk the magic words.

 (defn a2p-records-by-date [query from-date to-date dh-sub-query]
   (sql/with-connection db2
   (sql/with-query-results rs [query from-date to-date]
 (doseq [row rs] (println (str   (:project_id row) (dh-sub-query 
 (:project_id row

 (defn dh-records-by-a2p-id [query a2p-id]
   (sql/with-connection mssql
   (sql/with-query-results rs [query a2p-id]
 (dorun (map #(print (:project_uuid %)) rs)

 (defn dh-sub-query [a2p-id] (dh-records-by-a2p-id select PROJECT_UUID 
 from PROJECT where A2P_PROJECT_ID = ? a2p-id))

 (a2p-records-by-date select project_id from PROJECT where timestamp  ? 
 and timestamp  ? 2012-03-02 2012-03-07 dh-sub-query)

 The output looks like this, so I am close!

  269
  270
  258
  261
 E7D4262C-62B3-4129-9CE4-B342DC1C39FC 251

 Can anyone help me out about how to generate the maps?

 This is just the start as I have many queries to run for many entities, 
 so doubtless I will be back again ;-)

 Thanks in advance

 Ray



-- 
-- 
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/groups/opt_out.




Re: creating maps out of sql

2013-05-24 Thread Mond Ray
Thanks - I will check it out.

On Friday, 24 May 2013 17:35:46 UTC+2, Niels van Klaveren wrote:

 If you need to do join like operations on database tables from different 
 sources that you can't join through SQL, there's a nice library called 
 table-utilshttp://stackoverflow.com/questions/13009939/outer-join-in-clojurethat
  you can use. Since it's not released on clojars there's some steps 
 you'd need to take to use it, but it's all explained in the link.

 After you've put it in your project, you can use it by putting the result 
 sets in a map by just putting a doall over the result set, and join them 
 like you would in SQL. It has support for inner, outer and full joins.

 (let [db2 (sql/with-connection db
(sql/with-query-results rs [select fields from table]
   (doall rs)))
sql (sql/with-connection db
   (sql/with-query-results rs [select fields from table]
  (doall rs)))]
(left-outer-join db2 sql :db2field :sqlfield))

  
 On Friday, May 24, 2013 4:55:17 PM UTC+2, Mond Ray wrote:

 I am starting out to use Clojure to combine and verify data between DB2 
 on a Mainframe, SQL Server and an Atom Feed. Yes, it's such fun working in 
 a start-up ;-)

 Database wise, all is connecting OK after some Leiningen shenanigans and 
 I am now stuck on the mapping part ;-)

 The code is below.  I want to generate a set of maps that show the 
 following:

 #({:a2pid 269, :uuid nil}
 {:a2pid 270, :uuid nil}
 {:a2pid 258, :uuid nil}
 {:a2pid 261, :uuid nil}
 {:a2pid 251, :uuid E7D4262C-62B3-4129-9CE4-B342DC1C39FC})

 The idea is to have a list of maps that can show where there are gaps 
 between the two DBs and, coming next, the Atom feed.

 It is essentially a join operation - and maybe that's what I need but I 
 cannot figure out where to plonk the magic words.

 (defn a2p-records-by-date [query from-date to-date dh-sub-query]
   (sql/with-connection db2
   (sql/with-query-results rs [query from-date to-date]
 (doseq [row rs] (println (str   (:project_id row) (dh-sub-query 
 (:project_id row

 (defn dh-records-by-a2p-id [query a2p-id]
   (sql/with-connection mssql
   (sql/with-query-results rs [query a2p-id]
 (dorun (map #(print (:project_uuid %)) rs)

 (defn dh-sub-query [a2p-id] (dh-records-by-a2p-id select PROJECT_UUID 
 from PROJECT where A2P_PROJECT_ID = ? a2p-id))

 (a2p-records-by-date select project_id from PROJECT where timestamp  ? 
 and timestamp  ? 2012-03-02 2012-03-07 dh-sub-query)

 The output looks like this, so I am close!

  269
  270
  258
  261
 E7D4262C-62B3-4129-9CE4-B342DC1C39FC 251

 Can anyone help me out about how to generate the maps?

 This is just the start as I have many queries to run for many entities, 
 so doubtless I will be back again ;-)

 Thanks in advance

 Ray



-- 
-- 
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/groups/opt_out.




Re: Multiple replacements in string using a map

2013-04-23 Thread mond
Chapeau - nicely, ahem, reduced

On Tuesday, April 23, 2013 8:23:27 AM UTC+2, rod naph wrote:

 I scratched my itch with...


 (defn map-replace [m text]
   (reduce
 (fn [acc [k v]] (s/replace acc (str k) (str v)))
 text m))


 Hope it helps.

 On Tuesday, April 23, 2013 5:55:51 AM UTC+1, Mond Ray wrote:

 Man - you guys are good and *fast*.

 I was pootling around with a version that would directly replace 
 /a/:key/b {:key value} with /a/value/b and a failed version sneaked 
 into my code.

 Incidentally that is my only slight complaint about the code as it 
 stands: I have to use something like

 (def url (replace-map https://google.$tld;  {:$tld com}))

 rather than the more elegant and seemingly idiomatic

 (def url (replace-map https://google.:tld;  {:tld com}))

 Since I am struggling to win this small battle, do you guys have any 
 ideas (including that its not a good idea!)?

 Cheers

 Ray

 On Monday, 22 April 2013 23:19:16 UTC+2, Sean Corfield wrote:

 On Mon, Apr 22, 2013 at 1:45 PM, Mond Ray mondr...@gmail.com wrote: 
  Something very odd going on here - one day it works the next day it 
 fails :( 

 This code is different to what you posted the other day... 

#_= (map #(java.util.regex.Pattern/quote (keyword %))) 

 That won't work - Pattern/quote will not accept a keyword (which is 
 what the exception is telling you). In the code you posted before, 
 which did work, you had (name %) instead which would yield a String 
 from either a keyword or a string - which is clearly what you want 
 here. 
 -- 
 Sean A Corfield -- (904) 302-SEAN 
 An Architect's View -- http://corfield.org/ 
 World Singles, LLC. -- http://worldsingles.com/ 

 Perfection is the enemy of the good. 
 -- Gustave Flaubert, French realist novelist (1821-1880) 



-- 
-- 
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/groups/opt_out.




Re: Multiple replacements in string using a map

2013-04-22 Thread Mond Ray
Something very odd going on here - one day it works the next day it fails :(

$ lein repl
nREPL server started on port 51502
REPL-y 0.1.10
Clojure 1.5.1
Exit: Control+D or (exit) or (quit)
Commands: (user/help)
Docs: (doc function-name-here)
  (find-doc part-of-name-here)
  Source: (source function-name-here)
  (user/sourcery function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
Examples from clojuredocs.org: [clojuredocs or cdoc]
  (user/clojuredocs name-here)
  (user/clojuredocs ns-here name-here)
user= (require '[clojure.string :as s])
nil
user= ; code from the clojure google group to help map properties into 
paths

user= ; first a way to take the keys and produce a regex

user= (defn key-pattern
  #_=   Create a regex Pattern of the form 'key1|key2', the key names
  #_=   will be quoted in case they contain special regex characters
  #_=   [m]
  #_=   (- (keys m)
  #_= (map #(java.util.regex.Pattern/quote (keyword %)))
  #_= (s/join |)
  #_= java.util.regex.Pattern/compile))
#'user/key-pattern
user= 

user= ; second a way to use the above function to achieve the replacement

user= (defn replace-map [text m]
  #_=   Replace keys in a String from matching values in a map
  #_=   (s/replace text
  #_= (key-pattern m)
  #_= (fn [field-name]
  #_=   (java.util.regex.Matcher/quoteReplacement (str (get m (keyword 
field-name)))
#'user/replace-map
user= 

user= (def param-map {:$tld com})
#'user/param-map
user= 

user= (def url (replace-map https://google.$tld; param-map))
ClassCastException clojure.lang.Keyword cannot be cast to java.lang.String 
 user/key-pattern/fn--352 (NO_SOURCE_FILE:6)

user= 

You see my full REPL session.

I reckon I must be doing something dumb wrong - any ideas on what's going 
on?

Thanks

Ray

On Saturday, 20 April 2013 09:51:21 UTC+2, Mond Ray wrote:

 Just tried again - using lein repl (clojure 1.4.0) and it worked fine.

 It was late - who knows what I did ;-)

 Thanks for checking guys.

 On Saturday, 20 April 2013 02:37:14 UTC+2, Andy Fingerhut wrote:

 I fired up a Clojure 1.5.1 REPL, did (require '[clojure.string :as s]) 
 first, then copied and pasted those two function definitions, and did not 
 get the errors you are seeing.  I don't have a good guess why you are 
 getting those errors.  Did you do the require first?  What version of 
 Clojure are you using?

 Andy


 On Fri, Apr 19, 2013 at 5:17 PM, Mond Ray mondr...@gmail.com wrote:

 Old thread but what the heck... it doesn't work in my REPL

 user= (defn key-pattern
   #_= Create a regex Pattern of the form 'key1|key2', the key 
 names
   #_= will be quoted in case they contain special regex characters
   #_= [m]
   #_= (- (keys m)
   #_= (map #(java.util.regex.Pattern/quote (name %)))
   #_= (s/join |)
   #_= java.util.regex.Pattern/compile))
 #'user/key-pattern
 user= 

 user= (defn replace-map [text m]
   #_= (s/replace text
   #_=(key-pattern m)
   #_=(fn [field-name]
   #_=   (java.util.regex.Matcher/quoteReplacement (str (get m
   #_= (keyword field-name)))
 #'user/replace-map
 user= (replace-map /path/:p0/b/:p1 {:p0 1 :p1 2})
 ClassCastException java.lang.String cannot be cast to clojure.lang.IFn 
  user/key-pattern/fn--408 (NO_SOURCE_FILE:6)

 user= (key-pattern {:a 1})
 ClassCastException java.lang.String cannot be cast to clojure.lang.IFn 
  user/key-pattern/fn--408 (NO_SOURCE_FILE:6)

 Am I doing something wrong or is there a typo in your code?



 On Tuesday, 15 March 2011 16:35:04 UTC+1, Aaron Cohen wrote:

 On Mon, Mar 14, 2011 at 2:17 PM, Daniel Solano Gomez
 clo...@sattvik.com wrote:
  On Mon Mar 14 13:02 2011, shuaybi2 shuaybi2 wrote:
  I have a string such as:
 
  select * from account where acctId = _ACCT-ID_ and acctTyp = 
 _ACCT-TYP_

 There are several clojure libraries that exist to improve the ease and
 safety of doing something like this. Amongst them are
 clojure.contrib.sql and ClojureQL, which take different approaches.
 They all should be sufficient to guard against SQL injection and
 should probably be the first place you look.

 For the more general question you were asking about how to generically
 replace a map of matches-to-replacements though, Daniel did a good job
 showing how to use a reduce over the map. That method will call
 replaceAll once per entry in the map, which is probably fine if you
 don't have many substitutions.

 Another way to do it is using clojure.string.replace, which has an
 often-overlooked third overload which matches with a regex and
 replaces with a mapping function.

 Starting with a simple example:
 user=(require '[clojure.string :as s])
 nil
 user=(s/replace a b a #a|b {a 1 b 2})
 1 2 1

 In the example, the map was being used as a replacement function.

 ---
 If you're willing to change your map to use strings as keys and
 values, then the previous example is good enough.

 Otherwise, because you're wanting

Re: Multiple replacements in string using a map

2013-04-22 Thread Mond Ray
Man - you guys are good and *fast*.

I was pootling around with a version that would directly replace 
/a/:key/b {:key value} with /a/value/b and a failed version sneaked 
into my code.

Incidentally that is my only slight complaint about the code as it stands: 
I have to use something like

(def url (replace-map https://google.$tld;  {:$tld com}))

rather than the more elegant and seemingly idiomatic

(def url (replace-map https://google.:tld;  {:tld com}))

Since I am struggling to win this small battle, do you guys have any ideas 
(including that its not a good idea!)?

Cheers

Ray

On Monday, 22 April 2013 23:19:16 UTC+2, Sean Corfield wrote:

 On Mon, Apr 22, 2013 at 1:45 PM, Mond Ray mondr...@gmail.comjavascript: 
 wrote: 
  Something very odd going on here - one day it works the next day it 
 fails :( 

 This code is different to what you posted the other day... 

#_= (map #(java.util.regex.Pattern/quote (keyword %))) 

 That won't work - Pattern/quote will not accept a keyword (which is 
 what the exception is telling you). In the code you posted before, 
 which did work, you had (name %) instead which would yield a String 
 from either a keyword or a string - which is clearly what you want 
 here. 
 -- 
 Sean A Corfield -- (904) 302-SEAN 
 An Architect's View -- http://corfield.org/ 
 World Singles, LLC. -- http://worldsingles.com/ 

 Perfection is the enemy of the good. 
 -- Gustave Flaubert, French realist novelist (1821-1880) 


-- 
-- 
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/groups/opt_out.




Re: Multiple replacements in string using a map

2013-04-20 Thread Mond Ray
Just tried again - using lein repl (clojure 1.4.0) and it worked fine.

It was late - who knows what I did ;-)

Thanks for checking guys.

On Saturday, 20 April 2013 02:37:14 UTC+2, Andy Fingerhut wrote:

 I fired up a Clojure 1.5.1 REPL, did (require '[clojure.string :as s]) 
 first, then copied and pasted those two function definitions, and did not 
 get the errors you are seeing.  I don't have a good guess why you are 
 getting those errors.  Did you do the require first?  What version of 
 Clojure are you using?

 Andy


 On Fri, Apr 19, 2013 at 5:17 PM, Mond Ray mondr...@gmail.comjavascript:
  wrote:

 Old thread but what the heck... it doesn't work in my REPL

 user= (defn key-pattern
   #_= Create a regex Pattern of the form 'key1|key2', the key 
 names
   #_= will be quoted in case they contain special regex characters
   #_= [m]
   #_= (- (keys m)
   #_= (map #(java.util.regex.Pattern/quote (name %)))
   #_= (s/join |)
   #_= java.util.regex.Pattern/compile))
 #'user/key-pattern
 user= 

 user= (defn replace-map [text m]
   #_= (s/replace text
   #_=(key-pattern m)
   #_=(fn [field-name]
   #_=   (java.util.regex.Matcher/quoteReplacement (str (get m
   #_= (keyword field-name)))
 #'user/replace-map
 user= (replace-map /path/:p0/b/:p1 {:p0 1 :p1 2})
 ClassCastException java.lang.String cannot be cast to clojure.lang.IFn 
  user/key-pattern/fn--408 (NO_SOURCE_FILE:6)

 user= (key-pattern {:a 1})
 ClassCastException java.lang.String cannot be cast to clojure.lang.IFn 
  user/key-pattern/fn--408 (NO_SOURCE_FILE:6)

 Am I doing something wrong or is there a typo in your code?



 On Tuesday, 15 March 2011 16:35:04 UTC+1, Aaron Cohen wrote:

 On Mon, Mar 14, 2011 at 2:17 PM, Daniel Solano Gomez
 clo...@sattvik.com wrote:
  On Mon Mar 14 13:02 2011, shuaybi2 shuaybi2 wrote:
  I have a string such as:
 
  select * from account where acctId = _ACCT-ID_ and acctTyp = 
 _ACCT-TYP_

 There are several clojure libraries that exist to improve the ease and
 safety of doing something like this. Amongst them are
 clojure.contrib.sql and ClojureQL, which take different approaches.
 They all should be sufficient to guard against SQL injection and
 should probably be the first place you look.

 For the more general question you were asking about how to generically
 replace a map of matches-to-replacements though, Daniel did a good job
 showing how to use a reduce over the map. That method will call
 replaceAll once per entry in the map, which is probably fine if you
 don't have many substitutions.

 Another way to do it is using clojure.string.replace, which has an
 often-overlooked third overload which matches with a regex and
 replaces with a mapping function.

 Starting with a simple example:
 user=(require '[clojure.string :as s])
 nil
 user=(s/replace a b a #a|b {a 1 b 2})
 1 2 1

 In the example, the map was being used as a replacement function.

 ---
 If you're willing to change your map to use strings as keys and
 values, then the previous example is good enough.

 Otherwise, because you're wanting to use keywords as your keys, and
 arbitratry values for your values, we'll need to use a slightly more
 sophisticated replacement function.

 (defn key-pattern
 Create a regex Pattern of the form 'key1|key2', the key names
 will be quoted in case they contain special regex characters
 [m]
 (- (keys m)
 (map #(java.util.regex.Pattern/**quote (name %)))
 (s/join |)
 java.util.regex.Pattern/**compile))

 (defn replace-map [text m]
 (s/replace text
(key-pattern m)
(fn [field-name]
   (java.util.regex.Matcher/**quoteReplacement (str (get m
 (keyword field-name)))

  -- 
 -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.comjavascript:
 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 javascript:
 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+u...@googlegroups.com javascript:.
 For more options, visit https://groups.google.com/groups/opt_out.
  
  




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

Re: Multiple replacements in string using a map

2013-04-19 Thread Mond Ray
Old thread but what the heck... it doesn't work in my REPL

user= (defn key-pattern
  #_= Create a regex Pattern of the form 'key1|key2', the key 
names
  #_= will be quoted in case they contain special regex characters
  #_= [m]
  #_= (- (keys m)
  #_= (map #(java.util.regex.Pattern/quote (name %)))
  #_= (s/join |)
  #_= java.util.regex.Pattern/compile))
#'user/key-pattern
user= 

user= (defn replace-map [text m]
  #_= (s/replace text
  #_=(key-pattern m)
  #_=(fn [field-name]
  #_=   (java.util.regex.Matcher/quoteReplacement (str (get m
  #_= (keyword field-name)))
#'user/replace-map
user= (replace-map /path/:p0/b/:p1 {:p0 1 :p1 2})
ClassCastException java.lang.String cannot be cast to clojure.lang.IFn 
 user/key-pattern/fn--408 (NO_SOURCE_FILE:6)

user= (key-pattern {:a 1})
ClassCastException java.lang.String cannot be cast to clojure.lang.IFn 
 user/key-pattern/fn--408 (NO_SOURCE_FILE:6)

Am I doing something wrong or is there a typo in your code?



On Tuesday, 15 March 2011 16:35:04 UTC+1, Aaron Cohen wrote:

 On Mon, Mar 14, 2011 at 2:17 PM, Daniel Solano Gomez
 clo...@sattvik.com javascript: wrote:
  On Mon Mar 14 13:02 2011, shuaybi2 shuaybi2 wrote:
  I have a string such as:
 
  select * from account where acctId = _ACCT-ID_ and acctTyp = 
 _ACCT-TYP_

 There are several clojure libraries that exist to improve the ease and
 safety of doing something like this. Amongst them are
 clojure.contrib.sql and ClojureQL, which take different approaches.
 They all should be sufficient to guard against SQL injection and
 should probably be the first place you look.

 For the more general question you were asking about how to generically
 replace a map of matches-to-replacements though, Daniel did a good job
 showing how to use a reduce over the map. That method will call
 replaceAll once per entry in the map, which is probably fine if you
 don't have many substitutions.

 Another way to do it is using clojure.string.replace, which has an
 often-overlooked third overload which matches with a regex and
 replaces with a mapping function.

 Starting with a simple example:
 user=(require '[clojure.string :as s])
 nil
 user=(s/replace a b a #a|b {a 1 b 2})
 1 2 1

 In the example, the map was being used as a replacement function.

 ---
 If you're willing to change your map to use strings as keys and
 values, then the previous example is good enough.

 Otherwise, because you're wanting to use keywords as your keys, and
 arbitratry values for your values, we'll need to use a slightly more
 sophisticated replacement function.

 (defn key-pattern
 Create a regex Pattern of the form 'key1|key2', the key names
 will be quoted in case they contain special regex characters
 [m]
 (- (keys m)
 (map #(java.util.regex.Pattern/quote (name %)))
 (s/join |)
 java.util.regex.Pattern/compile))

 (defn replace-map [text m]
 (s/replace text
(key-pattern m)
(fn [field-name]
   (java.util.regex.Matcher/quoteReplacement (str (get m
 (keyword field-name)))



-- 
-- 
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/groups/opt_out.




datomic + clojure + clojurescript examples

2013-02-23 Thread Mond Ray
My fellow Clojurians,

I am having a trouble hunting down an example of the above stack for 
clojure applications.

Can anyone point me in the right direction?

Thanks

Ray

-- 
-- 
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/groups/opt_out.




Re: another n00b defrecord combination question

2012-12-16 Thread mond
Great stuff Ben. Thanks.

ray

On Saturday, December 15, 2012 10:54:42 PM UTC+1, Benjamin R. Haskell wrote:

 On Sat, 15 Dec 2012, mond wrote: 

  Thanks for picking up the cudgels Ben! 

 Ha.  It's nice to have reached a point where I feel at-all confident 
 with any of this... happy to help. 


  To be honest I am struggling to repeat your advice in the REPL.  In 
  any case, I decided to change the data structures in line with your 
  advice and put the IDs into maps rather than the records. 
  
  (defrecord Item [name product quantity purchased]) 
  
  (defrecord Product [name description prices]) 
  
  (defrecord Price [price tax currency]) 
  
  (def items [ {:id 1 :item (-Item Brogues P-123 1 true)} 
   {:id 2 :item (-Item Underpants P-345 2 false)} 
   {:id 3 :item (-Item Shirt P-678 1 true)} ]) 
  
  (def carts [ (-Cart Birthday (first items)) 
   (-Cart Xmas (rest items)) ]) 
  
  (def products [ {:id P-1231 :product (-Product Table Coffee Table 
 (-Price 375 21 EURO))} 
  {:id P-3451 :product (-Product Chairs Set of 
 Four(4) chairs (-Price 200 21 EURO))} 
  {:id P-123 :product (-Product Brogues Men's 
 Leather Slip on Brogues (-Price 93.00 21 EURO))} 
  {:id P-345 :product (-Product Underpants CK Y 
 Fronts (-Price 23.50 21 EURO))} 
  {:id P-678 :product (-Product Shirt Slim Fit White 
 Vest Shirt (-Price 45.99 21 EURO))} 
  {:id P-6781 :product (-Product TableCloth Classic 
 red and white checks 2m x 2m (-Price 17.99 21 EURO))} ]) 
  
  Do you think the zipmap is still the way to go (to resolve the 
  'foreign key' or could there be an easier way? I guess the fact that 
  only items have to zipmapped is one advantage. 

 It seems like items and products should still have an :id property, so I 
 don't think you need to detach the ID from its entity.  The zipmap'ed 
 version is useful as an index, not really as its own structure.  So, it 
 ends up as just a single map (not an array of maps) with ID's as keys, 
 and the corresponding entity as the value: 


 (defrecord Item [id name product quantity purchased]) 

 (defrecord Product [id name description prices]) 

 (defrecord Price [price tax currency]) 

 (def items [ (-Item 1 Brogues P-123 1 true) 
  (-Item 2 Underpants P-345 2 false) 
  (-Item 3 Shirt P-678 1 true) ]) 

 (def products [ (-Product P-1231 Table Coffee Table (-Price 375 21 
 EURO)) 
 (-Product P-3451 Chairs Set of Four(4) chairs 
 (-Price 200 21 EURO)) 
 (-Product P-123 Brogues Men's Leather Slip on 
 Brogues (-Price 93.00 21 EURO)) 
 (-Product P-345 Underpants CK Y Fronts (-Price 
 23.50 21 EURO)) 
 (-Product P-678 Shirt Slim Fit White Vest Shirt 
 (-Price 45.99 21 EURO)) 
 (-Product P-6781 TableCloth Classic red and white 
 checks 2m x 2m (-Price 17.99 21 EURO)) ]) 

 ; two useful indexes(/indices?): 

 (def prod-item 
An index from Product ID to an Item 
(zipmap (map :product items) items)) 

 (def id-product 
An index from Product ID to a Product 
(zipmap (map :id products) products)) 

 ; Then you can use that to get the products joined with their items: 

 (defn product-with-item 
Find the Item info and merge it into the product, without overwriting 
 :id 
[product] 
(merge-with (fn [a b] a) product (prod-item (:id product 

 ; example usage: 

 (product-with-item (id-product P-345)) 
 ;= #user.Product{:id 2, 
 ; :name Underpants, 
 ; :description CK Y Fronts, 
 ; :prices #user.Price{:price 23.5, :tax 21, :currency 
 EURO}, 
 ; :purchased false, 
 ; :quantity 2, 
 ; :product P-345} 

 (def joined-products (map product-with-item products)) 
 ;= A list of all the products joined with their items 

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

Re: another n00b defrecord combination question

2012-12-16 Thread mond
One small thing Ben... when I try to use the final formulation I receive an 
error:

(def joined-products (map product-with-item products))

user= (joined-products)
ClassCastException clojure.lang.LazySeq cannot be cast to clojure.lang.IFn 
 user/eval241 (NO_SOURCE_FILE:9)

Any ideas?

Thanks in advance

ray

On Saturday, December 15, 2012 10:54:42 PM UTC+1, Benjamin R. Haskell wrote:

 On Sat, 15 Dec 2012, mond wrote: 

  Thanks for picking up the cudgels Ben! 

 Ha.  It's nice to have reached a point where I feel at-all confident 
 with any of this... happy to help. 


  To be honest I am struggling to repeat your advice in the REPL.  In 
  any case, I decided to change the data structures in line with your 
  advice and put the IDs into maps rather than the records. 
  
  (defrecord Item [name product quantity purchased]) 
  
  (defrecord Product [name description prices]) 
  
  (defrecord Price [price tax currency]) 
  
  (def items [ {:id 1 :item (-Item Brogues P-123 1 true)} 
   {:id 2 :item (-Item Underpants P-345 2 false)} 
   {:id 3 :item (-Item Shirt P-678 1 true)} ]) 
  
  (def carts [ (-Cart Birthday (first items)) 
   (-Cart Xmas (rest items)) ]) 
  
  (def products [ {:id P-1231 :product (-Product Table Coffee Table 
 (-Price 375 21 EURO))} 
  {:id P-3451 :product (-Product Chairs Set of 
 Four(4) chairs (-Price 200 21 EURO))} 
  {:id P-123 :product (-Product Brogues Men's 
 Leather Slip on Brogues (-Price 93.00 21 EURO))} 
  {:id P-345 :product (-Product Underpants CK Y 
 Fronts (-Price 23.50 21 EURO))} 
  {:id P-678 :product (-Product Shirt Slim Fit White 
 Vest Shirt (-Price 45.99 21 EURO))} 
  {:id P-6781 :product (-Product TableCloth Classic 
 red and white checks 2m x 2m (-Price 17.99 21 EURO))} ]) 
  
  Do you think the zipmap is still the way to go (to resolve the 
  'foreign key' or could there be an easier way? I guess the fact that 
  only items have to zipmapped is one advantage. 

 It seems like items and products should still have an :id property, so I 
 don't think you need to detach the ID from its entity.  The zipmap'ed 
 version is useful as an index, not really as its own structure.  So, it 
 ends up as just a single map (not an array of maps) with ID's as keys, 
 and the corresponding entity as the value: 


 (defrecord Item [id name product quantity purchased]) 

 (defrecord Product [id name description prices]) 

 (defrecord Price [price tax currency]) 

 (def items [ (-Item 1 Brogues P-123 1 true) 
  (-Item 2 Underpants P-345 2 false) 
  (-Item 3 Shirt P-678 1 true) ]) 

 (def products [ (-Product P-1231 Table Coffee Table (-Price 375 21 
 EURO)) 
 (-Product P-3451 Chairs Set of Four(4) chairs 
 (-Price 200 21 EURO)) 
 (-Product P-123 Brogues Men's Leather Slip on 
 Brogues (-Price 93.00 21 EURO)) 
 (-Product P-345 Underpants CK Y Fronts (-Price 
 23.50 21 EURO)) 
 (-Product P-678 Shirt Slim Fit White Vest Shirt 
 (-Price 45.99 21 EURO)) 
 (-Product P-6781 TableCloth Classic red and white 
 checks 2m x 2m (-Price 17.99 21 EURO)) ]) 

 ; two useful indexes(/indices?): 

 (def prod-item 
An index from Product ID to an Item 
(zipmap (map :product items) items)) 

 (def id-product 
An index from Product ID to a Product 
(zipmap (map :id products) products)) 

 ; Then you can use that to get the products joined with their items: 

 (defn product-with-item 
Find the Item info and merge it into the product, without overwriting 
 :id 
[product] 
(merge-with (fn [a b] a) product (prod-item (:id product 

 ; example usage: 

 (product-with-item (id-product P-345)) 
 ;= #user.Product{:id 2, 
 ; :name Underpants, 
 ; :description CK Y Fronts, 
 ; :prices #user.Price{:price 23.5, :tax 21, :currency 
 EURO}, 
 ; :purchased false, 
 ; :quantity 2, 
 ; :product P-345} 

 (def joined-products (map product-with-item products)) 
 ;= A list of all the products joined with their items 

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

another n00b defrecord combination question

2012-12-15 Thread mond
I have defined these types of records for modelling a simple shopping cart:

; An item in the cart
(defrecord Item [id name product quantity purchased])

; The product that relates to the item in the cart
(defrecord Product [id name description prices])

; A price definition
(defrecord Price [price tax-rate currency])


; Sample items
(def items [
 (-Item 1 Brogues P-123 1 true)
 (-Item 2 Underpants P-345 1 false)
 (-Item 3 Shirt P-678 1 true)
 ])

; Sample products
(def products [
(-Product P-123 Brogues Men's Leather Slip on 
Brogues (-Price 93.00 21 EURO))
(-Product P-345 Underpants CK Y Fronts (-Price 
23.50 21 EURO))
(-Product P-678 Shirt Slim Fit White Vest Shirt 
(-Price 45.99 21 EURO))
(-Product P-1231 Table Coffee Table (-Price 375 21 
EURO))
(-Product P-3451 Chairs Set of Four(4) chairs 
(-Price 200 21 EURO))
(-Product P-6781 TableCloth Classic red and white 
checks 2m x 2m (-Price 17.99 21 EURO))
])

My requirement is to combine data from the two collections such that I can 
show the detailed product information for any item, for example:

 (-Item 3 Shirt Slim Fit White Vest Shirt (-Price 45.99 
21 EURO) 1 true)

When I tried to use merge-with union I had an error which surprised me 
since I thought records are also maps:

user= (merge-with union items products)
ClassCastException user.Product cannot be cast to java.util.Map$Entry 
 clojure.core/key (core.clj:1465)

Although I will fight on, any tips from any of you that have done this 
before would be greatly helped.

Thanks

ray

-- 
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: another n00b defrecord combination question

2012-12-15 Thread mond
I think I am close to answering this myself (which is always nice!)

I have done the necessary joins with map and I can return a product 
description

user= (map (fn [item product] (= (:product item (:id product))) 
(:description product)) items products)
(Men's Leather Slip on Brogues CK Y Fronts Slim Fit White Vest Shirt)

Or a purchased status for the matching item

user= (map (fn [item product] (= (:product item (:id product))) 
(:purchased item)) items products)
(true false true)

But I have my knickers in a twist when I want to get more than one value 
out or a combination of values. 

user= (map (fn [item product] (= (:product item (:id product))) (:id item 
:description product)) items products)
(user= IllegalArgumentException Wrong number of args passed to keyword: 
:id  clojure.lang.Keyword.throwArity (Keyword.java:85)

So does anyone have the last bit of syntax to help me take back a list of 
values from the inputs?

Thanks

ray


On Saturday, December 15, 2012 5:25:59 PM UTC+1, mond wrote:

 I have defined these types of records for modelling a simple shopping cart:

 ; An item in the cart
 (defrecord Item [id name product quantity purchased])

 ; The product that relates to the item in the cart
 (defrecord Product [id name description prices])

 ; A price definition
 (defrecord Price [price tax-rate currency])


 ; Sample items
 (def items [
  (-Item 1 Brogues P-123 1 true)
  (-Item 2 Underpants P-345 1 false)
  (-Item 3 Shirt P-678 1 true)
  ])

 ; Sample products
 (def products [
 (-Product P-123 Brogues Men's Leather Slip on 
 Brogues (-Price 93.00 21 EURO))
 (-Product P-345 Underpants CK Y Fronts (-Price 
 23.50 21 EURO))
 (-Product P-678 Shirt Slim Fit White Vest Shirt 
 (-Price 45.99 21 EURO))
 (-Product P-1231 Table Coffee Table (-Price 375 21 
 EURO))
 (-Product P-3451 Chairs Set of Four(4) chairs 
 (-Price 200 21 EURO))
 (-Product P-6781 TableCloth Classic red and white 
 checks 2m x 2m (-Price 17.99 21 EURO))
 ])

 My requirement is to combine data from the two collections such that I can 
 show the detailed product information for any item, for example:

  (-Item 3 Shirt Slim Fit White Vest Shirt (-Price 45.99 
 21 EURO) 1 true)

 When I tried to use merge-with union I had an error which surprised me 
 since I thought records are also maps:

 user= (merge-with union items products)
 ClassCastException user.Product cannot be cast to java.util.Map$Entry 
  clojure.core/key (core.clj:1465)

 Although I will fight on, any tips from any of you that have done this 
 before would be greatly helped.

 Thanks

 ray


-- 
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: another n00b defrecord combination question

2012-12-15 Thread mond
OK - I have it now:

(map (fn [item product] (= (:product item (:id product))) [(:id item) 
(:description product)]) items products)

This feels OK to me but if anyone has a nicer / more idiomatic solution, I 
will be happy to take feedback.

ray

On Saturday, December 15, 2012 7:08:21 PM UTC+1, mond wrote:

 I think I am close to answering this myself (which is always nice!)

 I have done the necessary joins with map and I can return a product 
 description

 user= (map (fn [item product] (= (:product item (:id product))) 
 (:description product)) items products)
 (Men's Leather Slip on Brogues CK Y Fronts Slim Fit White Vest Shirt)

 Or a purchased status for the matching item

 user= (map (fn [item product] (= (:product item (:id product))) 
 (:purchased item)) items products)
 (true false true)

 But I have my knickers in a twist when I want to get more than one value 
 out or a combination of values. 

 user= (map (fn [item product] (= (:product item (:id product))) (:id item 
 :description product)) items products)
 (user= IllegalArgumentException Wrong number of args passed to keyword: 
 :id  clojure.lang.Keyword.throwArity (Keyword.java:85)

 So does anyone have the last bit of syntax to help me take back a list of 
 values from the inputs?

 Thanks

 ray


 On Saturday, December 15, 2012 5:25:59 PM UTC+1, mond wrote:

 I have defined these types of records for modelling a simple shopping 
 cart:

 ; An item in the cart
 (defrecord Item [id name product quantity purchased])

 ; The product that relates to the item in the cart
 (defrecord Product [id name description prices])

 ; A price definition
 (defrecord Price [price tax-rate currency])


 ; Sample items
 (def items [
  (-Item 1 Brogues P-123 1 true)
  (-Item 2 Underpants P-345 1 false)
  (-Item 3 Shirt P-678 1 true)
  ])

 ; Sample products
 (def products [
 (-Product P-123 Brogues Men's Leather Slip on 
 Brogues (-Price 93.00 21 EURO))
 (-Product P-345 Underpants CK Y Fronts (-Price 
 23.50 21 EURO))
 (-Product P-678 Shirt Slim Fit White Vest Shirt 
 (-Price 45.99 21 EURO))
 (-Product P-1231 Table Coffee Table (-Price 375 
 21 EURO))
 (-Product P-3451 Chairs Set of Four(4) chairs 
 (-Price 200 21 EURO))
 (-Product P-6781 TableCloth Classic red and white 
 checks 2m x 2m (-Price 17.99 21 EURO))
 ])

 My requirement is to combine data from the two collections such that I 
 can show the detailed product information for any item, for example:

  (-Item 3 Shirt Slim Fit White Vest Shirt (-Price 45.99 
 21 EURO) 1 true)

 When I tried to use merge-with union I had an error which surprised me 
 since I thought records are also maps:

 user= (merge-with union items products)
 ClassCastException user.Product cannot be cast to java.util.Map$Entry 
  clojure.core/key (core.clj:1465)

 Although I will fight on, any tips from any of you that have done this 
 before would be greatly helped.

 Thanks

 ray



-- 
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: another n00b defrecord combination question

2012-12-15 Thread mond
Thanks for picking up the cudgels Ben!

To be honest I am struggling to repeat your advice in the REPL.  In any 
case, I decided to change the data structures in line with your advice and 
put the IDs into maps rather than the records.

(defrecord Item [name product quantity purchased])

(defrecord Product [name description prices])

(defrecord Price [price tax currency])

(def items [ {:id 1 :item (-Item Brogues P-123 1 true)}
 {:id 2 :item (-Item Underpants P-345 2 false)}
 {:id 3 :item (-Item Shirt P-678 1 true)} ])

(def carts [ (-Cart Birthday (first items))
 (-Cart Xmas (rest items)) ])

(def products [ {:id P-1231 :product (-Product Table Coffee Table 
(-Price 375 21 EURO))}
{:id P-3451 :product (-Product Chairs Set of Four(4) 
chairs (-Price 200 21 EURO))}
{:id P-123 :product (-Product Brogues Men's Leather 
Slip on Brogues (-Price 93.00 21 EURO))}
{:id P-345 :product (-Product Underpants CK Y Fronts 
(-Price 23.50 21 EURO))}
{:id P-678 :product (-Product Shirt Slim Fit White 
Vest Shirt (-Price 45.99 21 EURO))}
{:id P-6781 :product (-Product TableCloth Classic red 
and white checks 2m x 2m (-Price 17.99 21 EURO))} ])

Do you think the zipmap is still the way to go (to resolve the 'foreign 
key' or could there be an easier way? I guess the fact that only items have 
to zipmapped is one advantage.

Thanks for your support

ray


On Saturday, December 15, 2012 7:53:32 PM UTC+1, Benjamin R. Haskell wrote:

 On Sat, 15 Dec 2012, Benjamin R. Haskell wrote: 

  Or if you really want a list, you need to quote it: 
  
  (map (fn [item product] '(:id item :description product)) items 
 products) 

 Oops, wrong.  Rather, you would want clojure.core/list: 

 (map (fn [item product] (list :id item :description product)) items 
 products) 

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

n00b question on embedded defrecord

2012-12-09 Thread mond
; I have these records defined

(defrecord Customer [firstName lastName emailAddress deliveryAddress 
invoiceAddress])
(defrecord Address [street number town postcode])

; I have this small sample data
(def customers
  {
(-Customer Drongo Bongo dro...@bongo.co
  (-Address Gongo 32A Fongo )
  (-Address Gongo 32B Fongo ))

  (-Customer Tinga Zinga qi...@zinga.co
(-Address Thinga 767 Dongo )
(-Address Jinga 828 Qongo ))
}
)

; and I want a small filter on an embedded property (deliveryAddress.number)
; this compiles but of course it's wrong so gives back an empty list

(prn (filter #(= 32A (:deliveryAddress :number %)) customers))

I have tried putting - and [] in various places and of course checked docs

In the end I thought it would be quicker just to ask here.

Thanks

Ray

 

-- 
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: n00b question on embedded defrecord

2012-12-09 Thread mond
Thanks Jordan - I just couldn't quite get that magic combination :)



On Sunday, December 9, 2012 11:38:50 PM UTC+1, Jordan wrote:




 On Sun, Dec 9, 2012 at 4:45 PM, mond r...@mcdermott.be javascript:wrote:

 ; I have these records defined

 (defrecord Customer [firstName lastName emailAddress deliveryAddress 
 invoiceAddress])
 (defrecord Address [street number town postcode])


 This looks fine
  


 ; I have this small sample data
 (def customers
   {
 (-Customer Drongo Bongo dro...@bongo.co javascript:
   (-Address Gongo 32A Fongo )
   (-Address Gongo 32B Fongo ))

   (-Customer Tinga Zinga qi...@zinga.co javascript:
 (-Address Thinga 767 Dongo )
 (-Address Jinga 828 Qongo ))
 }
 )


 You probably want this to be a vector of customers instead of a map?

 so, 

 (def customers
  [
   (-Customer Drongo Bongo dro...@bongo.co javascript:
(-Address Gongo 32A Fongo )
(-Address Gongo 32B Fongo ))

(-Customer Tinga Zinga qi...@zinga.co javascript:
 (-Address Thinga 767 Dongo )
 (-Address Jinga 828 Qongo ))
   ]
 )
  


 ; and I want a small filter on an embedded property 
 (deliveryAddress.number)
 ; this compiles but of course it's wrong so gives back an empty list

 (prn (filter #(= 32A (:deliveryAddress :number %)) customers))


 So you can write this a bunch of different ways:

 (prn (filter #(= (:number (:deliveryAddress %)) 32A) customers))

 or if you want to use -, you can:

 (prn (filter #(- % :deliveryAddress :number (= 32A)) customers))

 Cheers,

 Jordan


-- 
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: newbie question regarding maps

2012-09-27 Thread Mond Ray
Thanks for the support and especially the examples.  I will be back when I 
bump into the next level of complexity ;-)

On Monday, 24 September 2012 12:04:42 UTC+2, Mond Ray wrote:

 I am playing around with maps and using wish lists as a learning tool. I 
 have a list of items in wish lists like this:

 user= items
 [{:name Item 1, :cost 20.0} {:name Item 2, :cost 40.0}]

 user= wiggle-items
 [{:name Wiggle 1, :cost 20.0} {:name Wiggle 2, :cost 40.0} [:name 
 Item 3 :cost 10.0]]

 user= (def wish-lists [
 {:name WL1 :items items}
 {:name WL2 :items wiggle-items}
 ]
 )
 #'user/wish-lists

 user= wish-lists
 [{:name WL1, :items [{:name Item 1, :cost 20.0} {:name Item 2, :cost 
 40.0}]} {:name WL2, :items [{:name Wiggle 1, :cost 20.0} {:name Wiggle 
 2, :cost 40.0} [:name Item 3 :cost 10.0]]}]

 ---

 I now want to add an item to one of the Wish Lists but am struggling with 
 assoc and assoc-in (which seems to be the one I need).

 ---

 user= (def new-wi (conj wiggle-items [ :name Item 3 :cost 10.0 ]))

 #'user/new-wi

 user= (assoc-in wish-lists [:name WL1] [:name WL1 :items new-wi])
 IllegalArgumentException Key must be integer 
 clojure.lang.APersistentVector.assoc (APersistentVector.java:312)


 As you can see the REPL gives me an error stating that the keys must be 
 Integers. Is that right?  Or is my call process faulty?

 Thanks in advance for your support.


 ray


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

newbie question regarding maps

2012-09-25 Thread Mond Ray
I am playing around with maps and using wish lists as a learning tool. I 
have a list of items in wish lists like this:

user= items
[{:name Item 1, :cost 20.0} {:name Item 2, :cost 40.0}]

user= wiggle-items
[{:name Wiggle 1, :cost 20.0} {:name Wiggle 2, :cost 40.0} [:name Item 
3 :cost 10.0]]

user= (def wish-lists [
{:name WL1 :items items}
{:name WL2 :items wiggle-items}
]
)
#'user/wish-lists

user= wish-lists
[{:name WL1, :items [{:name Item 1, :cost 20.0} {:name Item 2, :cost 
40.0}]} {:name WL2, :items [{:name Wiggle 1, :cost 20.0} {:name Wiggle 
2, :cost 40.0} [:name Item 3 :cost 10.0]]}]

---

I now want to add an item to one of the Wish Lists but am struggling with 
assoc and assoc-in (which seems to be the one I need).

---

user= (def new-wi (conj wiggle-items [ :name Item 3 :cost 10.0 ]))

#'user/new-wi

user= (assoc-in wish-lists [:name WL1] [:name WL1 :items new-wi])
IllegalArgumentException Key must be integer 
clojure.lang.APersistentVector.assoc (APersistentVector.java:312)


As you can see the REPL gives me an error stating that the keys must be 
Integers. Is that right?  Or is my call process faulty?

Thanks in advance for your support.


ray

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