fft1976 | 1 Jul 02:32 2009
Picon

Re: loneclojurian at ICFP programming contest


On Jun 30, 3:02 pm, igorrumiha <igorrum... <at> gmail.com> wrote:
> Greetings everyone,
>
> I didn't actually plan it but I ended up participating in the ICFP
> programming contest (http://www.icfpcontest.org). This year the task
> was to move satellites from one orbit to another, to meet with other
> satellites etc. Quite interesting, and to start it all you need to
> implement a virtual machine for the physics simulator.
>
> I used Clojure and managed to solve the first of the four problems,
> which means I didn't get really far. I was simply too slow to get to
> the really interesting stuff. I have written a rather long article
> describing my solution so I hope some of you may find it interesting:
>
> http://igor.rumiha.net/tag/icfp/
>
> I have also put the code on github:http://github.com/irumiha/icfp-loneclojurian/tree/master
>
> I hope someone has the interest and the time to take a look at the
> code. I consider myself a Clojure beginner and any suggestions are
> welcome, especially considering possible speed improvements to the
> virtual machine. According to some of the people on the #icfp-contest
> channel my VM implementation is 500x to 1000x slower than a typical
> implementation written in C. It is, on the other hand, in the same
> performance range as some VMs written in Python. Some people claim
> that the JVM can give you C-like performance, but I would be more than
> happy if I got my VM to be 10x slower than the C ones :)
>
> Igor.
(Continue reading)

Glen Stampoultzis | 1 Jul 02:45 2009
Picon

Re: subvec drops metadata

I couldn't say whether it should or not but I've faced similar issues with attaching metadata to various collections.  It becomes very easy to lose the metadata when you're dealing with collections since many high level functions tend to return new seqs.  I found myself having to reappy the metadata frequently.

2009/7/1 bayerf <google <at> bayerf.de>

The subject says all: Should subvec keep the parent vector's metadata
(it currently doesn't) or not?
Similar functions like conj, assoc, dissoc DO keep the metadata of the
collection.




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure <at> 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+unsubscribe <at> googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

mmwaikar | 1 Jul 04:55 2009
Picon

Problem with clojure code on .net.


Hi,

I am learning clojure these days, but on .Net. I have the following
code -

(import '(System.IO Path Directory File DirectoryNotFoundException))

(defn starts-with-hmorx [name]
	(if (or (= (.ToUpper (.ToString name)) "H")
		  (= (.ToUpper (.ToString name)) "M")
		  (= (.ToUpper (.ToString name)) "X")) true false))

(defn GetSubfolderName [filename]
	((def name-wo-extn (Path/GetFileNameWithoutExtension filename))
	 (def first-char (aget (.ToCharArray (.ToString name-wo-extn)) 0))
	 (if (Char/IsDigit first-char) (Convert/ToInt32 first-char) (if
(starts-with-hmorx first-char) (.ToLower (.ToString first-char))
"other"))))

But when I call (GetSubfolderName "D:\\CsEx\\Manoj.cs"), I get this
exception -

System.InvalidCastException: Unable to cast object of type
'System.String' to type 'clojure.lang.IDeref'.
   at lambda_method(Closure , Object )
   at AFunction_impl.invoke(Object )
   at lambda_method(Closure , Object )
   at AFunction_impl.invoke(Object )
   at lambda_method(Closure )
   at AFunction_impl.invoke()
   at REPLCall(Closure )

Any help is greatly appreciated.

Thanks,
Manoj.
Daniel Lyons | 1 Jul 06:50 2009

Re: Problem with clojure code on .net.


Manoj,

On Jun 30, 2009, at 8:55 PM, mmwaikar wrote:

> I am learning clojure these days, but on .Net. I have the following
> code -
>
> (import '(System.IO Path Directory File DirectoryNotFoundException))
>
> (defn starts-with-hmorx [name]
> 	(if (or (= (.ToUpper (.ToString name)) "H")
> 		  (= (.ToUpper (.ToString name)) "M")
> 		  (= (.ToUpper (.ToString name)) "X")) true false))
>
> (defn GetSubfolderName [filename]
> 	((def name-wo-extn (Path/GetFileNameWithoutExtension filename))
> 	 (def first-char (aget (.ToCharArray (.ToString name-wo-extn)) 0))
> 	 (if (Char/IsDigit first-char) (Convert/ToInt32 first-char) (if
> (starts-with-hmorx first-char) (.ToLower (.ToString first-char))
> "other"))))

I think you want something like this instead:

(defn GetSubfolderName [filename]
   (let [name-wo-extn (Path/GetFileNameWithoutExtension filename)
	first-char (aget (.ToCharArray (.ToString name-wo-extn)) 0)]
     (if (Char/IsDigit first-char) (Convert/ToInt32 first-char)
	(if (starts-with-hmorx first-char) (.ToLower (.ToString first-char))  
"other"))))

The nested if is a little unusual though. I would probably rewrite it  
to use cond. If you can get a repl going, try to step through each  
expression and see where the problem is that way. I don't have  
Clojure .NET set up to give a more precise answer unfortunately.

—
Daniel Lyons

Jeff Foster | 1 Jul 07:34 2009
Picon

Re: loneclojurian at ICFP programming contest


I looked at the ICFP Contest too.  I didn't even get as far as solving
the first problem, but I did implement a virtual machine that appeared
to work.  I really enjoyed the coding, though I didn't get very far
with the physics!  I tried a couple of approaches but settling on the
functional side.  Performance was not bad (from what I've seen it was
vaguely comparable to the Python implementations, but was completely
blown away by C/C++ implementations).  I really wish I'd had the time
to do a visualizer!

My code is on github too (http://github.com/fffej/ClojureProjects/tree/
1494815e83febebe9af28b0cb08b812a63df9e96/icfp/uk/co/fatvat) and
there's a write-up on my blog (http://www.fatvat.co.uk/2009/06/icfp-
contest-this-time-its-functional.html).

Again, I'd appreciate any guidance on anything that I could improve!

Cheers

jeff

On Jun 30, 11:02 pm, igorrumiha <igorrum... <at> gmail.com> wrote:
> Greetings everyone,
>
> I didn't actually plan it but I ended up participating in the ICFP
> programming contest (http://www.icfpcontest.org). This year the task
> was to move satellites from one orbit to another, to meet with other
> satellites etc. Quite interesting, and to start it all you need to
> implement a virtual machine for the physics simulator.
>
> I used Clojure and managed to solve the first of the four problems,
> which means I didn't get really far. I was simply too slow to get to
> the really interesting stuff. I have written a rather long article
> describing my solution so I hope some of you may find it interesting:
>
> http://igor.rumiha.net/tag/icfp/
>
> I have also put the code on github:http://github.com/irumiha/icfp-loneclojurian/tree/master
>
> I hope someone has the interest and the time to take a look at the
> code. I consider myself a Clojure beginner and any suggestions are
> welcome, especially considering possible speed improvements to the
> virtual machine. According to some of the people on the #icfp-contest
> channel my VM implementation is 500x to 1000x slower than a typical
> implementation written in C. It is, on the other hand, in the same
> performance range as some VMs written in Python. Some people claim
> that the JVM can give you C-like performance, but I would be more than
> happy if I got my VM to be 10x slower than the C ones :)
>
> Igor.
Krukow | 1 Jul 07:50 2009
Picon

Re: ants.clj and render thread starvation


On Jun 30, 6:01 pm, Rich Hickey <richhic... <at> gmail.com> wrote:
> MVCC history in Clojure's STM is dynamic, created by need. There is no
> read tracking, and more important for this case, no transaction
> tracking. So, if a read transaction is unable to satisfy its snapshot
> view from history, it will flag the offending ref with a fault and
> retry. When a writer sees a ref with a read fault it will grow history
> for that ref. In this way only as much history is created as is needed
> to satisfy the dynamic contention patterns, and
> tracking/synchronization is minimized.

Ok - I understand better now. I guess I had an "idealized" system in
mind where each ref (in principle) had a complete history associated
with it so reads would never need to be retried (of course, unneeded
entries would somehow be discarded when the system decides it can
never be read.) Certainly that would imply tracking transactions. I
guess you don't do this because of the overhead.

I like the pragmatics of :min-history, and I believe it would be
sufficient for many scenarios. However, I suspect we are now moving
closer to the situation that Cliff Click was predicting [1] where as a
programmer you need more precise knowledge about the STM
implementation to understand the behavior and tune its performance.

Kind Regards,
- Karl

[1] http://groups.google.com/group/clojure/browse_frm/thread/5c7a962cc72c1fe7/a1a0cb35070e9558
Daniel Lyons | 1 Jul 07:58 2009

Re: Problem with clojure code on .net.


Manoj,

In case this helps, I've had a bit longer to dwell on this problem and  
I have a couple of ideas. I do think this raises the general question  
of, are Clojure .NET and Clojure Java two different languages or two  
implementations of the same language? Normally in a situation like  
this (say, Jython and Python) I would say that they should be two  
implementations of the same language but the situation is complicated  
by the fact that Clojure code relies heavily on Java objects since the  
policy is not to wrap everything. I'm not sure how the subject of  
Clojure .NET / Java portability has been approached but my naïve guess  
is that it would revolve around separating Java dependencies and .NET  
dependencies from pure Clojure code. Is anyone else addressing these  
issues in their own code at this time?

Back to your code. First of all, to simplify things, it's probably a  
good idea to decouple your I/O from your parsing, since the parsing  
can be functional. First I rewrote starts-with-hmorx into this:

(defn starts-with-hm-or-x [name]
   (#{\H \M \X} (.charAt name 0)))

Which I think is simpler, building on the fact that sets can be used  
as functions, but then I thought it would probably be better to  
rewrite the functions into one that gets the information from the  
filesystem and another one that gets the information you want out:

(defn parse-subfolder-name
   [name]
   (or
    (when-let [[_ digit] (re-find #"(\d).*" name)]
      (Integer/parseInt digit))
    (when-let [[_ hmx] (re-find #"([hHmMxX]).*" name)]
      (.toLowerCase hmx))
    "other"))

Unfortunately I'm still dependent on some Java API stuff in there, but  
I think you can probably translate to the .NET equivalents. This is  
easier to test at the REPL:

user> (parse-subfolder-name "0stuf")
0
user> (parse-subfolder-name "Hstuf")
"h"
user> (parse-subfolder-name "stuf")
"other"

Then I'd set about handling the loading of the filename and  
dispatching it:

(defn get-subfolder-name
   [filename]
   (parse-subfolder-name
    (.ToString (Path/GetFileNameWithoutExtensions filename))))

Now it should be easier to determine where the problem is, either in  
the I/O code or in the functional code which is easier to test at the  
REPL.

As an aside, I'd recommend that you follow the naming convention of  
using lowercase function names with words separated by hypens (get- 
subfolder-name) rather than CamelCase (GetSubfolderName). Also, it  
doesn't look to me like GetSubfolderName is really returning a  
subfolder's name but I'm not quite sure what it is doing either. Are  
you working on an app with specific meaning tied to the first  
character of a path name or is this a .NET filesystem thing? Just  
curious.

Thanks and hope this helps,

—
Daniel Lyons

Michael Wood | 1 Jul 09:03 2009
Picon

Re: Problem with clojure code on .net.


Hi

2009/7/1 mmwaikar <mmwaikar <at> gmail.com>:
> Hi,
>
> I am learning clojure these days, but on .Net. I have the following
> code -

I only have a tiny bit to add to what Daniel's already said.

[...]
> (defn GetSubfolderName [filename]
>        ((def name-wo-extn (Path/GetFileNameWithoutExtension filename))

First, def creates basically global variables (or something similar).
You want let instead.
Second, you can't just add parentheses as you see fit. :)

(def name-wo-extn "something")

is defining name-wo-extn as the string "something".

((def name-wo-extn "something"))

is trying to define name-wo-extn as the string "something" and then
call it as a function.  Since "something" is a String and not a
function (or something that acts as a function), this will fail.

So, for ((def name-wo-extn (Path/GetFileNameWithoutExtension
filename)) ...) implies that (Path/GetFileNameWithoutExtension
filename) returns a Clojure function (or something that can be used as
one, like a map/set/etc.).  I suppose Path/GetFileNameWithoutExtension
is a .Net method, in which case it is unlikely to work as a Clojure
function.  Java methods can't be used in place of Clojure functions
and I assume it's the same for the .Net port.

>         (def first-char (aget (.ToCharArray (.ToString name-wo-extn)) 0))
>         (if (Char/IsDigit first-char) (Convert/ToInt32 first-char) (if
> (starts-with-hmorx first-char) (.ToLower (.ToString first-char))
> "other"))))
>
> But when I call (GetSubfolderName "D:\\CsEx\\Manoj.cs"), I get this
> exception -
>
> System.InvalidCastException: Unable to cast object of type
> 'System.String' to type 'clojure.lang.IDeref'.

I think the extra parentheses are the reason for this exception.

>   at lambda_method(Closure , Object )
>   at AFunction_impl.invoke(Object )
>   at lambda_method(Closure , Object )
>   at AFunction_impl.invoke(Object )
>   at lambda_method(Closure )
>   at AFunction_impl.invoke()
>   at REPLCall(Closure )
[...]

--

-- 
Michael Wood <esiotrot <at> gmail.com>

Raoul Duke | 1 Jul 09:43 2009
Picon

agents + repl?


(i googled but didn't find anything about this yet...)

while trying to understand the graphics animation code from ants.clj,
i tried this simpler code, but when i run the code below in the repl,
i don't see the "."s until i evaluate something afterwards; then they
get flushed to the console. is this something about agents+repl? or
something else i'm not grokking? many thanks for edification.

(defn new-animator []
  (agent 0))
(def running true)
(def animation-sleep-ms 250)
(defn animation [i]
  (when running
    (send-off *agent* #'animation))
  (print ".")
  (. Thread (sleep animation-sleep-ms))
  nil)
(send-off (new-animator) animation)

Laurent PETIT | 1 Jul 10:52 2009
Picon

Re: pprint


Hi Tom,

Thanks for the answer. I already have some embryonic antlr grammar for
clojure, but I'm willing to give pprint a thourough try.

I'll play with your code. Do you have a first pass over the clojure
reader to attach other meta information as you go, or do you directly
consume the clojure data structures the reader passes to you ? If so,
then certainly there would be the need to have an intermediate
structure that would be the output of both "clojure reader parsed
code" and "source code preserving reader parsed code" ? So that the
rest of the formatting code is decoupled from that ? (But that will
certainly be lots of changes, and in all parts I guess).

As far as IDE integration is concerned, i would not bother (at first)
about incremental thing. I rather intend to always parse the entire
edited file content (of course if this causes a performance problem, I
might rethink about it). For performance testing, I've found that
clojure/core.clj is a good candidate :-)

Regards,

-- 
Laurent

2009/6/30 Tom Faulhaber <tomfaulhaber <at> gmail.com>:
>
> Hi Laurent,
>
> I think that pprint might be a good foundation for what you are doing,
> but there are a couple of issues that need to be dealt with first.
>
> First, pprint works directly on Clojure objects and not strings, so
> the code will need to be read first.
>
> Second, the Clojure reader is lossy - it strips comments, metadata
> tags (#^{}) and expands forms like backquote and reader time
> evaluation before passing you the result.
>
> The first thing I would do is look at making a modified reader that
> could read chunks of s-expression and pass back raw data. In a perfect
> world, this reader would be much more resistant to syntax problems
> (for example by "assuming" extra opening or closing brackets of
> various types).
>
> Then I would look at how I was going to attach the pprint to the
> editor. In practice, I think you want the reformatting to be very
> incremental, working on small chunks of code at a time, but you know
> better than I.
>
> Using the code dispatch in pretty printing should be pretty easy once
> you have this infrastructure. You'll probably want to customize it a
> little, but that's fairly straightforward. You'll also probably want
> to "clean up" any syntax corrections you made. I might think about
> adding metadata to the read structure and then using that in the
> dispatch to skip writing open parens that weren't really there, for
> instance.
>
> I've been thinking about doing all this, but it's not at the top of my
> list right now (working on a new contrib autodoc tool and making
> pprint cleaner and faster plus my real job!). But I'm happy to discuss/
> help in the short term.
>
> HTH,
>
> Tom
>
> On Jun 30, 7:08 am, Laurent PETIT <laurent.pe... <at> gmail.com> wrote:
>> Hi all,
>>
>> I want to add source code formatting / auto-indenting to the clojure
>> plugin for eclipse.
>>
>> I had asked other IDE plugin creators, and it seems that they
>> leveraged the specifics of their respective platforms to do so. So I
>> have nothing substantial to "steal from them" not requiring a fair
>> amount of rework :-)
>>
>> I'm now facing the following two paths :
>>
>>  * try to leverage Eclipse's way of doing clojure source code
>> formatting / auto-indenting and create yet another lib for doing that
>> very specifically
>>
>>  * try to do it in clojure so that it can also be used as a standalone
>> command-line / whatever utility for mass source code reformatting ...
>>
>> So what is my question ? :-)
>>
>> Do you know if pprint lib now is (or will in a near future) be able to
>> handle this use case : that is not only formatting clojure code
>> returned by the reader, but also clojure code as string, and
>> potentially broken code (and so it would have to preserve comments,
>> literal metadata in the exact form they were written, etc.)
>>
>> Do you know whether there is another existing effort to do this in
>> plain clojure, or at least sufficiently independtly from an existing
>> IDE framework so that I can painlessly reuse it ?
>>
>> Thanks in advance,
>>
>> --
>> Laurent
> >
>


Gmane