Re: Combinatorics partitions that preserves adjacency?

2017-03-15 Thread Mark Engelberg
Think in terms of numbering the possible split locations:

a b c
 1 2

So the possible partitions are represented by [1], [2], [1 2], i.e., all
the non-empty subsets of [1 2].

So write a function that goes from this numbering scheme to partitions
(e.g., using subvec) and use combinatorics' subsets function.



On Wed, Mar 15, 2017 at 8:35 PM, Paul Gowder  wrote:

> Hi everyone,
>
> Does anyone know of a straightforward way to get something like
> clojure.math/combinatorics/partitions that works more like partition in
> the core library, that is, that only selects partitions with adjacent
> elements?
>
> In other words, right now this is the problem:
>
> (require '[clojure.math.combinatorics :as c])
> (c/partitions [:a :b :c] :min 2)
>
> => (([:a :b] [:c]) ([:a :c] [:b]) ([:a] [:b :c]) ([:a] [:b] [:c]))
>
> But that ([:a :c] [:b]) there in the second position isn't a proper
> partition because :a and :c aren't adjacent in the original vector.
>
> I feel like there's got to be a standard, canonical solution for this, or
> some existing sequence or combinatorics function with a funny name that
> just returns (([a :b] [:c]) ([:a] [:b :c]) ([:a] [:b] [:c])) in this
> situation.  I just don't know it...
>
> The best I can come up with is kind of a hackish workaround that only
> works when the original vector is sorted, namely, flattening all the
> partitions and testing to see whether they are sorted too, i.e.:
>
> (require '[clojure.math.combinatorics :as c])
>
> (defn test-fn [part]
>   (let [f (flatten part)]
> (= f (sort f
>
> (filter test-fn (c/partitions [:a :b :c] :min 2))
>
> => (([:a :b] [:c]) ([:a] [:b :c]) ([:a] [:b] [:c]))  ; Yay! :-)
>
> And that works, but, as noted, only when the original vector is sorted.
> What if someone wanted to preserve adjacencies in an unsorted vector?
>
> All thoughts appreciated, thanks!
>
> Cheers,
>
> -Paul
>
> --
> 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.
>

-- 
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: Combinatorics partitions that preserves adjacency?

2017-03-15 Thread Mikera
Filtering and sorting each partition is going to be pretty expensive! If 
the list is long you will be discarding most of the results anyway.

I found a recursive way to do this that is fairly efficient, by observing 
that you either want to join two adjacent elements together in a partition 
or split at this point.

(defn ordered-partitions 
 ([[fst & more]]
   (ordered-partitions (list [fst]) more))
 ([init more]  
   (if (empty? more)
  (list init)
  (let [more-partitions (ordered-partitions more)
start (butlast init)
join (last init)]
(concat
  (map #(concat init %) more-partitions)
  (map #(let [[more-fst & more-more] %]
  (concat start (list (vec (concat join more-fst))) 
more-more)) more-partitions))

(time (count (ordered-partitions (range 20
"Elapsed time: 822.939961 msecs"
524288

i.e. you can compute half a million partitions in less than a second, which 
I think is decent?

On Thursday, 16 March 2017 11:35:55 UTC+8, Paul Gowder wrote:
>
> Hi everyone, 
>
> Does anyone know of a straightforward way to get something like 
> clojure.math/combinatorics/partitions that works more like partition in the 
> core library, that is, that only selects partitions with adjacent elements?
>
> In other words, right now this is the problem:
>
> (require '[clojure.math.combinatorics :as c])
> (c/partitions [:a :b :c] :min 2)
>
> => (([:a :b] [:c]) ([:a :c] [:b]) ([:a] [:b :c]) ([:a] [:b] [:c]))
>
> But that ([:a :c] [:b]) there in the second position isn't a proper 
> partition because :a and :c aren't adjacent in the original vector.  
>
> I feel like there's got to be a standard, canonical solution for this, or 
> some existing sequence or combinatorics function with a funny name that 
> just returns (([a :b] [:c]) ([:a] [:b :c]) ([:a] [:b] [:c])) in this 
> situation.  I just don't know it... 
>
> The best I can come up with is kind of a hackish workaround that only 
> works when the original vector is sorted, namely, flattening all the 
> partitions and testing to see whether they are sorted too, i.e.: 
>
> (require '[clojure.math.combinatorics :as c])
>
> (defn test-fn [part]
>   (let [f (flatten part)]
> (= f (sort f
>
> (filter test-fn (c/partitions [:a :b :c] :min 2))
>
> => (([:a :b] [:c]) ([:a] [:b :c]) ([:a] [:b] [:c]))  ; Yay! :-)
>
> And that works, but, as noted, only when the original vector is sorted. 
>  What if someone wanted to preserve adjacencies in an unsorted vector?
>
> All thoughts appreciated, thanks!
>
> Cheers,
>
> -Paul
>
>

-- 
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: What would be the easiest way for people's Rails app, PHP app, NodeJS app, etc, to ping my Clojure app on the same server?

2017-03-15 Thread Denis Fuenzalida
If you have several web apps in different languages, the common denominator 
is: they talk HTTP and probably they can consume data from 3rd party 
endpoints in JSON format.

It could be interesting if your Clojure app was distributed as an 
executable Jar installer that dealt with configuring Apache or Nginx for 
you.

-- Denis

El miércoles, 15 de marzo de 2017, 15:47:27 (UTC-7), piast...@gmail.com 
escribió:
>
> Not especially a Clojure question, though my app is in Clojure, so I have 
> all the tools of the JVM to work with. 
>
> Suppose I release a small Clojure app that people should run on the 
> servers, and it releases some data over some port. Let's say port 4. 
> The idea is that I want this data from them, and so I'm trying to make it 
> easy for them to publish this data. I release this app as an open source, 
> and I hope people will run in on their servers. I assume these people will 
> be running all kinds of apps on their servers: PHP apps, Rails apps, NodeJS 
> apps, Java apps, WordPress, Django, etc. 
>
> What's the easiest way to let those apps ping my app? I don't want to 
> assume port 80 because I assume they already have some app using port 80. 
> And if we are talking about a typical WordPress developer, they might lack 
> the skill to know how to establish a reverse proxy. So I thought perhaps 
> the easiest thing would be to listen on a file socket? Because I think even 
> a beginner PHP programmer would know how to write data to a file socket? 
> There are lots of examples of 10 lines snippets teaching them how to do it. 
>
> I could also ask them to simply send a HTTP request to my server, though 
> I'm wondering if there is anything I can do that would be even easier and 
> more automatic than that. I suppose I could write an app that watches their 
> log files, though that would mean keeping in mind the different formats of 
> Rails logs versus Apache logs, and knowing where the crucial data would 
> appear. And even Apache and Nginix logs can be configured to show more or 
> less data on different servers. 
>
> But I am just guessing. Does anyone have a thought about this?  I'm trying 
> to think of the most automatic way to get data about certain activities 
> that happen on their server, which they may wish to advertise. 
>
> I might just go with a traditional "they should ping my API" but if I 
> could think of something even more automatic, that would be ideal. Watching 
> their log files would be the easiest thing, though the logs are not 100% 
> standard. 
>

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


Combinatorics partitions that preserves adjacency?

2017-03-15 Thread Paul Gowder
Hi everyone, 

Does anyone know of a straightforward way to get something like 
clojure.math/combinatorics/partitions that works more like partition in the 
core library, that is, that only selects partitions with adjacent elements?

In other words, right now this is the problem:

(require '[clojure.math.combinatorics :as c])
(c/partitions [:a :b :c] :min 2)

=> (([:a :b] [:c]) ([:a :c] [:b]) ([:a] [:b :c]) ([:a] [:b] [:c]))

But that ([:a :c] [:b]) there in the second position isn't a proper partition 
because :a and :c aren't adjacent in the original vector.  

I feel like there's got to be a standard, canonical solution for this, or some 
existing sequence or combinatorics function with a funny name that just returns 
(([a :b] [:c]) ([:a] [:b :c]) ([:a] [:b] [:c])) in this situation.  I just 
don't know it... 

The best I can come up with is kind of a hackish workaround that only works 
when the original vector is sorted, namely, flattening all the partitions and 
testing to see whether they are sorted too, i.e.: 

(require '[clojure.math.combinatorics :as c])

(defn test-fn [part]
  (let [f (flatten part)]
(= f (sort f

(filter test-fn (c/partitions [:a :b :c] :min 2))

=> (([:a :b] [:c]) ([:a] [:b :c]) ([:a] [:b] [:c]))  ; Yay! :-)

And that works, but, as noted, only when the original vector is sorted.  What 
if someone wanted to preserve adjacencies in an unsorted vector?

All thoughts appreciated, thanks!

Cheers,

-Paul

-- 
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: go local variable binding

2017-03-15 Thread Timothy Baldridge
Yes, that should work fine, do your tests confirm otherwise? Also if you're
not doing a recur there's no reason to use `go-loop` you can just use `go`.

On Wed, Mar 15, 2017 at 4:44 PM, Eran Levi  wrote:

> Hi,
> can I bind variables, same as I do for threads, for go routines, to be
> local only for a particular go routine, and if I can't, how would you mimic
> this behavior ?
>
> (defn print-stuff [s]
>   (println s))
>
> (go-loop []
>(binding [*out* (clojure.java.io/writer "foo.txt")]
>   (print-stuff)))
>
> (go-loop []
>(binding [*out* (clojure.java.io/writer "bar.txt")]
>   (print-stuff)))
>
>
> --
> 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.
>



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

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
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.


go local variable binding

2017-03-15 Thread Eran Levi
Hi,
can I bind variables, same as I do for threads, for go routines, to be 
local only for a particular go routine, and if I can't, how would you mimic 
this behavior ?

(defn print-stuff [s]
  (println s))

(go-loop []
   (binding [*out* (clojure.java.io/writer "foo.txt")]
  (print-stuff)))

(go-loop []
   (binding [*out* (clojure.java.io/writer "bar.txt")]
  (print-stuff)))


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


What would be the easiest way for people's Rails app, PHP app, NodeJS app, etc, to ping my Clojure app on the same server?

2017-03-15 Thread piastkrakow
Not especially a Clojure question, though my app is in Clojure, so I have 
all the tools of the JVM to work with. 

Suppose I release a small Clojure app that people should run on the 
servers, and it releases some data over some port. Let's say port 4. 
The idea is that I want this data from them, and so I'm trying to make it 
easy for them to publish this data. I release this app as an open source, 
and I hope people will run in on their servers. I assume these people will 
be running all kinds of apps on their servers: PHP apps, Rails apps, NodeJS 
apps, Java apps, WordPress, Django, etc. 

What's the easiest way to let those apps ping my app? I don't want to 
assume port 80 because I assume they already have some app using port 80. 
And if we are talking about a typical WordPress developer, they might lack 
the skill to know how to establish a reverse proxy. So I thought perhaps 
the easiest thing would be to listen on a file socket? Because I think even 
a beginner PHP programmer would know how to write data to a file socket? 
There are lots of examples of 10 lines snippets teaching them how to do it. 

I could also ask them to simply send a HTTP request to my server, though 
I'm wondering if there is anything I can do that would be even easier and 
more automatic than that. I suppose I could write an app that watches their 
log files, though that would mean keeping in mind the different formats of 
Rails logs versus Apache logs, and knowing where the crucial data would 
appear. And even Apache and Nginix logs can be configured to show more or 
less data on different servers. 

But I am just guessing. Does anyone have a thought about this?  I'm trying 
to think of the most automatic way to get data about certain activities 
that happen on their server, which they may wish to advertise. 

I might just go with a traditional "they should ping my API" but if I could 
think of something even more automatic, that would be ideal. Watching their 
log files would be the easiest thing, though the logs are not 100% 
standard. 

-- 
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: Leiningen, AOT compilation, and classloaders

2017-03-15 Thread Kevin Downey
On 03/07/2017 09:07 PM, 'Tianxiang Xiong' via Clojure wrote:
> I recently ran into an issue with AOT compilation that I'd like to
> understand better. A minimal working example (MWE) can be found here
> . 
> 
> Basically, using `:aot :all` in `project.clj` with `lein trampoline
> test` results in different classloaders for class `Foo`.  
> 
> |
> Classloaderforlein_trampoline_aot.core.Foo: 
> #object[clojure.lang.DynamicClassLoader
> 0x4982cc36 clojure.lang.DynamicClassLoader@4982cc36]
> Classloaderfor(->Foo): #object[sun.misc.Launcher$AppClassLoader
> 0x55f96302 sun.misc.Launcher$AppClassLoader@55f96302]
> |
> 
> While the same classloaders are used with `lein test`:
> 
> |
> Classloader
> forlein_trampoline_aot.core.Foo: #object[sun.misc.Launcher$AppClassLoader 
> 0x55f96302
> sun.misc.Launcher$AppClassLoader@55f96302]
> Classloaderfor(->Foo): #object[sun.misc.Launcher$AppClassLoader
> 0x55f96302 sun.misc.Launcher$AppClassLoader@55f96302]
> |
> 
> When is one classloader used instead of another, and why do `lein
> trampoline test` and `lein test` behave differently with `:aot :all`?


When you aot compile you generate a class file on disk that is visible
to the system classloader. When the reference to the Foo class is
compiled, it is compiled as a call something like
clojure.lang.RT.classForName("Foo"); RT.classForName uses different
class loaders depend on this that an the other, and in this case it is
using the system classloader (the AppClassLoader) when using lein
trampoline. When you loaded your clojure code it ran through the
compiler which generated bytecode which is loaded via clojure's
DynamicClassLoader.


The way RT.classForName determines which classloader to use is something
like: if there is a DynamicClassloader available from the compiler, use
it, otherwise use the system classloader.

Once you finish loading code the DynamicClassloader the compiler uses
is, uh, for lack of a better word, popped, so it isn't available to
RT.classForName.

So given that and the error we can deduce that `lein trampoline` runs in
to phases a code loading phase, and then an execution phase. Which is a
perfectly reasonable thing to do and easy to do with the command line
options to clojure.main.

I suspect adding an import of the Foo class would work around this
issue, but I strongly recommend you consider not aot compiling instead.
AOT compilation is a source of weird behavior and just isn't worth it.

The #1 reason people aot compile is because they don't know how else to
start their clojure programs, so I have a little write up here
https://github.com/hiredman/clojure-site/blob/df56aef005d5d867213a51c2d3bbec5a86b0acad/content/guides/running_a_clojure_program.adoc



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


-- 
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

-- 
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: Leiningen, AOT compilation, and classloaders

2017-03-15 Thread 'Tianxiang Xiong' via Clojure
Thanks for investigating--any luck?

On Friday, 10 March 2017 07:16:32 UTC-8, Mike Rodriguez wrote:
>
> I haven't been able to get to the bottom of this as of yet.  Primarily the 
> problem is I need to investigate how `lein trampoline` works compared to 
> without it, from an implementation perspective.
>
> I'll note that `lein test` does do a :reload option to `require` when 
> running tests.  Typically forced reloads of namespaces doesn't mix well 
> with AOT compilation, due to classloader issues like you are reporting here.
> However, I'm still missing key parts to understanding this.  I want to 
> look a bit closer at it, but just responding here in case these thoughts 
> are meaningful to anyone else that is looking at it.
>
> On Wednesday, March 8, 2017 at 12:07:31 AM UTC-5, Tianxiang Xiong wrote:
>>
>> I recently ran into an issue with AOT compilation that I'd like to 
>> understand better. A minimal working example (MWE) can be found here 
>> . 
>>
>> Basically, using `:aot :all` in `project.clj` with `lein trampoline test` 
>> results in different classloaders for class `Foo`.  
>>
>> Classloader for lein_trampoline_aot.core.Foo:  
>> #object[clojure.lang.DynamicClassLoader 
>> 0x4982cc36 clojure.lang.DynamicClassLoader@4982cc36]
>> Classloader for (->Foo):  #object[sun.misc.Launcher$AppClassLoader 
>> 0x55f96302 sun.misc.Launcher$AppClassLoader@55f96302]
>>
>> While the same classloaders are used with `lein test`:
>>
>> Class loader for lein_trampoline_aot.core.Foo:  
>> #object[sun.misc.Launcher$AppClassLoader 
>> 0x55f96302 sun.misc.Launcher$AppClassLoader@55f96302]
>> Classloader for (->Foo):  #object[sun.misc.Launcher$AppClassLoader 
>> 0x55f96302 sun.misc.Launcher$AppClassLoader@55f96302]
>>
>> When is one classloader used instead of another, and why do `lein 
>> trampoline test` and `lein test` behave differently with `:aot :all`?
>>
>

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