Re: Unable to build a simple project with clojure-ant-tasks

2009-08-31 Thread Ivan Chernetsky

2009/8/31 J. McConnell :

> The Ant tasks have only been tested against Clojure 1.0, since I assumed
> that's what most people would be using in a production context.

For my personal projects the HEAD version is stable enough. :)

> I am
> thinking about going the Contrib route and tagging a 1.0-compatible version
> and updating everything to work against HEAD. Feel free to watch the repo on
> GitHub if you'd be interested in that.

OK, thank you!

--~--~-~--~~~---~--~~
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: How do I add a new collection in Clojure?

2009-08-31 Thread Meikel Brandmeyer

Hi,

On Aug 31, 1:35 am, "jonathan.arrender.sm...@gmail.com"
 wrote:
> I was not sure from the documentation if conj, seq, etc are generic
> functions (multimetiod functions). Is there any easy way to define
> these for a new type?

Shameless commercial: http://bitbucket.org/kotarak/lazymap/

This library allows to define maps, which compute the value of a key
really only if necessary (ie. when the value is retrieved from the
map). It implements the IPersistentMap interface and provides a custom
ISeq implementation to ensure laziness of the resulting MapEntry
sequence. This is a real-life example plugging into the system in a
complete transparent way: you can drop a lazymap where ever a normal
map would work.

Sincerely
Meikel

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



Self-referencing map literals

2009-08-31 Thread Timo Mihaljov

When defining a map literal, is it possible to reference the map that is 
being defined?

I have some code that looks like this:

(let [radius 20
  diameter (* 2 radius)
  circumference (* pi diameter)]
  {:radius radius
   :diameter diameter
   :circumference circumference})

I would like to simplify it to something like:

{:radius 20
 :diameter (* 2 (% :radius))
 :circumference (* pi (% :diameter))}

where % is the map itself.

Is this possible with the {}-syntax, or is there a macro to do this in 
the standard library or contrib?

--
Timo

--~--~-~--~~~---~--~~
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: Self-referencing map literals

2009-08-31 Thread Meikel Brandmeyer

Hi,

On Aug 31, 11:27 am, Timo Mihaljov  wrote:
> When defining a map literal, is it possible to reference the map that is
> being defined?

I don't think this is possible.

> I have some code that looks like this:
>
>     (let [radius 20
>           diameter (* 2 radius)
>           circumference (* pi diameter)]
>       {:radius radius
>        :diameter diameter
>        :circumference circumference})
>
> I would like to simplify it to something like:
>
>     {:radius 20
>      :diameter (* 2 (% :radius))
>      :circumference (* pi (% :diameter))}
>
> where % is the map itself.
>
> Is this possible with the {}-syntax, or is there a macro to do this in
> the standard library or contrib?

Simply define a helper:

(defn make-circle
  [radius]
  {:radiusradius
   :diameter  (* 2 radius)
   :circumference (* 2 pi radius)})

If you are concerned about the double calculation of the diameter you
can use a let as you did. Or can have a look here:
http://github.com/francoisdevlin/devlinsf-clojure-utils/ and here the
discussion: 
http://groups.google.com/group/clojure-dev/browse_thread/thread/4b20e40d83095c67.
Look for a function named trans*.

Hope this helps.

Sincerely
Meikel

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



Re: Self-referencing map literals

2009-08-31 Thread Achim Passen

Hi!

Am 31.08.2009 um 11:27 schrieb Timo Mihaljov:

> I have some code that looks like this:
>
>(let [radius 20
>  diameter (* 2 radius)
>  circumference (* pi diameter)]
>  {:radius radius
>   :diameter diameter
>   :circumference circumference})
>
> I would like to simplify it to something like:
>
>{:radius 20
> :diameter (* 2 (% :radius))
> :circumference (* pi (% :diameter))}
>
> where % is the map itself.

You could define your own let-like construct for this:


(defmacro letmap [[m kvs & mkvs] & body]
   (if m
 `(let [~m {}
~@(mapcat (fn [[k v]] `(~m (assoc ~m ~k ~v))) kvs)]
   (letmap ~mkvs ~...@body))
 `(do ~...@body)))


Usage:

(letmap [a-map   {:a 12
   :b (* 2 (:a a-map))}
  another-map {:c (:a a-map)
   :d (+ (:b a-map) (:c another-map))}]
   [a-map, another-map])

-> [{:b 24, :a 12} {:d 36, :c 12}]


Hope this helps.

Kind Regards,
achim


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



Java security code in Clojure

2009-08-31 Thread Sam Hughes

Hey,

I'm trying to write a Clojure security library. My first step is
porting some working Java code into Clojure. The Java and Clojure
snippets below are more or less the same, but with the Clojure code,
I'm getting: "java.security.InvalidKeyException: IOException: null
[Thrown class java.security.spec.InvalidKeySpecException]," which I
can't seem to replicate with the Java code.

The goal of the code is to read in a DER file, use it to encrypt a
"Hello World" message, then output the encrypted message as a new
file.

Neither of these snippets necessarily follow good coding standards.
That said, here's the working Java code snippet:

final File keyFile = new File("public.der");
byte[] encodedKey = new byte[(int) keyFile.length()];

new FileInputStream(keyFile).read(encodedKey);
final byte[] newEncoded = encodedKey;

final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(newEncoded);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pk = kf.generatePublic(keySpec);

Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, pk);
OutputStream os = new CipherOutputStream(new FileOutputStream
("encrypted.rsa"), rsa);

Writer out = new OutputStreamWriter(os);
out.write("Hello World");
out.close();
os.close();

And here's the Exception throwing Clojure code:

(ns security
  (:import
   [java.io File FileInputStream IOException]
   [java.security.spec X509EncodedKeySpec]
   [java.security KeyFactory PublicKey
KeyPairGenerator NoSuchAlgorithmException KeyPair]
   [javax.crypto KeyGenerator Cipher]))

(defn byte-seq [rdr]
  (let [result (byte (. rdr read))]
(if (= result -1)
  (do (. rdr close) nil)
  (lazy-seq (cons result (byte-seq rdr))

(def stream (new FileInputStream (new File "public.der")))
(def byte-arr (into-array Byte/TYPE (byte-seq stream)))
(def pk-spec (new X509EncodedKeySpec byte-arr))
(def kf (. KeyFactory (getInstance "RSA")))
(def pk (. kf (generatePublic pk-spec)))  ; exception thrown
here

Does anyone have any suggestion for what could be causing the
exception? I'm perplexed because, right now, I'm just trying to
replicate Java code in Clojure -- nothing too fancy.

Thanks a lot,
Sam

--~--~-~--~~~---~--~~
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: vs. Python

2009-08-31 Thread Laurent PETIT
Hi,

Just one point:

2009/8/30 Dan Fichter 

> Read the contents of a file.
>
> [Clojure]
>
> (slurp filename)
>
> [Python]
>
> open(filename, 'r').read() # who cares about closing files opened in
> read-mode?
>

"who cares about closing files opened in read-mode" ?

I would say anybody concerned about blowing up the underlying OS if not
releasing files handles (especially if you open files in a tight loop), or
do I miss something ?

--~--~-~--~~~---~--~~
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: vs. Python

2009-08-31 Thread B Smith-Mannschott

On Mon, Aug 31, 2009 at 14:08, Laurent PETIT wrote:
> Hi,
>
> Just one point:
>
> 2009/8/30 Dan Fichter 
>>
>> Read the contents of a file.
>>
>> [Clojure]
>>
>> (slurp filename)
>>
>> [Python]
>>
>> open(filename, 'r').read() # who cares about closing files opened in
>> read-mode?
>
> "who cares about closing files opened in read-mode" ?
>
> I would say anybody concerned about blowing up the underlying OS if not
> releasing files handles (especially if you open files in a tight loop), or
> do I miss something ?

CPython will close the underlying file when its handle is garbage
collection. The collection itself will be prompt and deterministic due
to CPython's use of reference counting for GC. You shouldn't have a
problem, even in a tight loop.

Jython, on the other hand, ... uses Java's GC, which has many
advantages of Python's 70's style reference counter, but being
deterministic isn't one of them.

Incidentally, Python 2.6 provides something akin to Clojure's with-open macro:

with open( "somefile", "rb" ) as aFile:
do_something_with_contents_of(aFile)


// 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: vs. Python

2009-08-31 Thread Konrad Hinsen

On 31 Aug 2009, at 14:08, Laurent PETIT wrote:

> [Python]
>
> open(filename, 'r').read() # who cares about closing files opened in  
> read-mode?
>
> "who cares about closing files opened in read-mode" ?
>
> I would say anybody concerned about blowing up the underlying OS if  
> not releasing files handles (especially if you open files in a tight  
> loop), or do I miss something ?

In this particular case, there is no reason to worry: open() returns a  
file object that is fed to the method read(), but after that method  
returns, there is no more reference to the object, so it is garbage  
collected. Upon destruction of the file object, Python closes the  
file. All that is documented behaviour in Python, so it is safe to  
rely on it.

It is another question if relying on a list of reasonable but not  
evident behaviours is good style. Personally, I don't use such  
constructs in library code, but I do in scripts for personal use.

In Clojure, I would be much more careful because I know the Java  
libraries less well than the Python libraries. Which illustrates that  
"good style" also depends on someone's experience.

Konrad.


--~--~-~--~~~---~--~~
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: Self-referencing map literals

2009-08-31 Thread Chas Emerick


On Aug 31, 2009, at 6:56 AM, Achim Passen wrote:

>> I would like to simplify it to something like:
>>
>>   {:radius 20
>>:diameter (* 2 (% :radius))
>>:circumference (* pi (% :diameter))}
>>
>> where % is the map itself.
>
> You could define your own let-like construct for this:
>
>
> (defmacro letmap [[m kvs & mkvs] & body]
>   (if m
> `(let [~m {}
>~@(mapcat (fn [[k v]] `(~m (assoc ~m ~k ~v))) kvs)]
>   (letmap ~mkvs ~...@body))
> `(do ~...@body)))
>
>
> Usage:
>
> (letmap [a-map   {:a 12
>   :b (* 2 (:a a-map))}
>  another-map {:c (:a a-map)
>   :d (+ (:b a-map) (:c another-map))}]
>   [a-map, another-map])
>
> -> [{:b 24, :a 12} {:d 36, :c 12}]

Yeah, I needed something like this a while ago, and came up with this:

(defmacro let-map
   "Equivalent of (let [a 5 b (+ a 5)] {:a a :b b})."
   [kvs]
   (let [keys (keys (apply hash-map kvs))
 keyword-symbols (mapcat #(vector (keyword (str %)) %) keys)]
   `(let [...@kvs]
  (hash-map ~...@keyword-symbols

user=> (let-map [a 5 b (inc a) c [a b] d {:foo c :bar (* a b)}])
{:a 5, :c [5 6], :b 6, :d {:foo [5 6], :bar 30}}

The nice thing about it is that you no longer need to use all the  
keywords -- the definition of the map's slots becomes far more like  
defining sequential bindings in a let.

- Chas

--~--~-~--~~~---~--~~
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: vs. Python

2009-08-31 Thread Laurent PETIT
Usually the java libraries explicitly mention not to place OS resource
handles on the finalize() method called by the GC, that why I had the reflex
of thinking it was generally applicable to all languages with a GC.

-- 
Laurent

2009/8/31 Konrad Hinsen 

>
> On 31 Aug 2009, at 14:08, Laurent PETIT wrote:
>
> > [Python]
> >
> > open(filename, 'r').read() # who cares about closing files opened in
> > read-mode?
> >
> > "who cares about closing files opened in read-mode" ?
> >
> > I would say anybody concerned about blowing up the underlying OS if
> > not releasing files handles (especially if you open files in a tight
> > loop), or do I miss something ?
>
> In this particular case, there is no reason to worry: open() returns a
> file object that is fed to the method read(), but after that method
> returns, there is no more reference to the object, so it is garbage
> collected. Upon destruction of the file object, Python closes the
> file. All that is documented behaviour in Python, so it is safe to
> rely on it.
>
> It is another question if relying on a list of reasonable but not
> evident behaviours is good style. Personally, I don't use such
> constructs in library code, but I do in scripts for personal use.
>
> In Clojure, I would be much more careful because I know the Java
> libraries less well than the Python libraries. Which illustrates that
> "good style" also depends on someone's experience.
>
> Konrad.
>
>
> >
>

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



How to represents a Big text file using sequence?

2009-08-31 Thread wangzx

I just want to learn clojure by using it to parse log file and
generate reports. and one question is: for a large text file, can we
use it as a sequence effectively? for example, for a 100M log file, we
need to check each line for some pattern match.

I just using the (line-seq rdr) but it will cause
OutOfMemoryException.

demo code

(defn buffered-reader [file]
(new java.io.BufferedReader
(new java.io.InputStreamReader
(new java.io.FileInputStream file

(def -reader (buffered-reader "test.txt"))
(filter #(= "some" %) -reader)

even there is no lines match "some", the filter operation will cause
OutOfMemoryException.

Is there other APIs like the Sequence but provide stream-like API?
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Qi in Clojure (Shen) project underway

2009-08-31 Thread Fogus

It looks like there is a thrust to rewrite Qi (http://
lambdassociates.org) in Clojure.  With the impending departure of Dr.
Mark Tarver the effort could use some help from the Clojure
community.  There has been discussion in the past few weeks at
http://groups.google.co.uk/group/Qilang.  Likewise, the official Shen
repository is located on Github at http://github.com/snorgers/Shen/tree/master.
I know that the Clojure community has generally viewed Qi in a
positive light, so perhaps this is our chance to help 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
-~--~~~~--~~--~--~---



Re: Self-referencing map literals

2009-08-31 Thread Timo Mihaljov

On Mon, Aug 31, 2009 at 09:14:38AM -0400, Chas Emerick wrote:
> > You could define your own let-like construct for this:
> >
> >
> > (defmacro letmap [[m kvs & mkvs] & body]
> >   (if m
> > `(let [~m {}
> >~@(mapcat (fn [[k v]] `(~m (assoc ~m ~k ~v))) kvs)]
> >   (letmap ~mkvs ~...@body))
> > `(do ~...@body)))
> >
> >
> > Usage:
> >
> > (letmap [a-map   {:a 12
> >   :b (* 2 (:a a-map))}
> >  another-map {:c (:a a-map)
> >   :d (+ (:b a-map) (:c another-map))}]
> >   [a-map, another-map])
> >
> > -> [{:b 24, :a 12} {:d 36, :c 12}]
> 
> Yeah, I needed something like this a while ago, and came up with this:
> 
> (defmacro let-map
>"Equivalent of (let [a 5 b (+ a 5)] {:a a :b b})."
>[kvs]
>(let [keys (keys (apply hash-map kvs))
>  keyword-symbols (mapcat #(vector (keyword (str %)) %) keys)]
>`(let [...@kvs]
>   (hash-map ~...@keyword-symbols
> 
> user=> (let-map [a 5 b (inc a) c [a b] d {:foo c :bar (* a b)}])
> {:a 5, :c [5 6], :b 6, :d {:foo [5 6], :bar 30}}
> 
> The nice thing about it is that you no longer need to use all the  
> keywords -- the definition of the map's slots becomes far more like  
> defining sequential bindings in a let.

Thank you all for the answers!

I especially like Chas's solution. It didn't occur to me that the 
redundancy can be eliminated by removing the map and leaving in the let 
-- that's brilliant! :-) It's much easier to read because one doesn't 
have to dig into the map to get to the variables, and it looks exactly 
like the common idiom of interdependent let-bindings.

--
Timo

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



Two possible additions: non-reflective classobject calls & support for map-conj on arrays

2009-08-31 Thread Krukow

I have two minor minor suggestions for Clojure changes.

1) Consider this function:
user> (set! *warn-on-reflection* true)
true
user> (defn reader-from-classpath [s]
  (-> (.getResourceAsStream java.lang.String s)
  (java.io.InputStreamReader.)
  (java.io.BufferedReader.)))
Reflection warning, NO_SOURCE_PATH:2 - call to getResourceAsStream
can't be resolved.
#'user/reader-from-classpath

In general, I think every call of form (.instanceMember Classname
args*) will generate such a warning since it expands to, e.g.,

user> (macroexpand ' (.getResourceAsStream java.lang.String s))
(. (clojure.core/identity java.lang.String) getResourceAsStream s)
user>

And identity doesn't have any type information. A simple fix would be.
user> (defn #^Class class-identity [#^Class c] c)

#'user/class-identity

and then expanding (.instanceMember Classname args*) to

(. (class-identity Classname) instanceMember args*)

---

2) I can do:
user> (into {} '([:k :v]))
{:k :v}

This works for two-element vectors. However, I cannot do the same for
two-element arrays:

user> (def str_array (.split "k=v" "="))
#'user/str_array
user> (into {} (list str_array))
; Evaluation aborted.

It would be a simple addition to clojure.lang.ATransientMap:
import java.lang.reflect.Array;//change
...
public ITransientMap conj(Object o) {
ensureEditable();
if(o instanceof Map.Entry)
{
Map.Entry e = (Map.Entry) o;

return assoc(e.getKey(), e.getValue());
}
else if(o instanceof IPersistentVector)
{
IPersistentVector v = (IPersistentVector) o;
if(v.count() != 2)
throw new IllegalArgumentException("Vector arg 
to map conj must be
a pair");
return assoc(v.nth(0), v.nth(1));
}//begin change
else if(o != null && o.getClass().isArray())
{
if(Array.getLength(o) != 2)
throw new IllegalArgumentException("Array arg 
to map conj must
have exactly two elements");
return assoc(Array.get(o,0), Array.get(o,1));
}//end change

ITransientMap ret = this;
for(ISeq es = RT.seq(o); es != null; es = es.next())
{
Map.Entry e = (Map.Entry) es.first();
ret = ret.assoc(e.getKey(), e.getValue());
}
return ret;
}

After this change I have:
user> (def str_array (.split "k=v" "="))
#'user/str_array
user> (into {} (list str_array))
{"k" "v"}
user>

The use-case arose from wanting to create a map from a properties file
using split ("=") on each line of the properties file (and not wanting
to copy the array into []).

N.B. the cost is an additional null-check and o.getClass().isArray().

What do you guys think?
--~--~-~--~~~---~--~~
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: How to represents a Big text file using sequence?

2009-08-31 Thread Krukow

On Aug 31, 4:44 pm, wangzx  wrote:

> Is there other APIs like the Sequence but provide stream-like API?

You can try this, which I've used on short files recently:


(defn #^Class class-identity [#^Class c] c)
(defn #^java.io.BufferedReader reader-from-classpath [#^String s]
  (-> (. (class-identity java.lang.String) getResourceAsStream s)
  (java.io.InputStreamReader.)
  (java.io.BufferedReader.)))

;;perhaps this is the function you are looking for?
(defn buf2seq [#^java.io.BufferedReader b]
  (lazy-seq
(if-let [l (.readLine b)]
  (cons l (buf2seq b)

(defmacro with-open-stream [bindings body]
  (let [varname (bindings 0)
resource (bindings 1)]
`(let [s# (reader-from-classpath ~resource)
   ~varname (buf2seq s#)]
   (try ~body
(finally (if-not (nil? s#)
   (try (.close s#

(def prop-map
 (with-open-stream [s "/resources/myresource.properties"]
   (into {} (map #(into [] (.split #^String % "=")) s

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



Re: STM article

2009-08-31 Thread Howard Lewis Ship

Wow! That's a lot of great research.  I'm impressed.

My only concern is that some of the internals *could* change over time
(I see Rich's commit stream) and some indication in the doc
identifying what's design level (won't change) vs. implementation
level (might change) would be useful.

On Sun, Aug 30, 2009 at 10:42 AM, Mark
Volkmann wrote:
>
> I think we'd all agree that STM is a very important feature of
> Clojure. I trust that it works as advertised, but until recently I
> didn't feel that I understood exactly how it worked. This left me
> unable to defend it when pressed by developers that aren't using
> Clojure. I wanted to change that, for me and for the Clojure
> community. For the past few months I've been working on an article
> about STM in general and the Clojure implementation in particular. I
> hope the Clojure community derives some value from it.
>
> Consider this a beta version of the article. I have tried very hard to
> get all the facts straight and have reviewed it many times. It seems
> inevitable though that I may have stated some things incorrectly. This
> is complicated stuff ... which it must be in order to make concurrent
> programming easier.
>
> If you have an interest in this topic, I'd appreciated getting some
> feedback on the article. In particular, search for "Question:" in the
> article. I'll make suggested updates quickly to avoid spreading any
> misinformation.
>
> You can find the article at http://ociweb.com/mark/stm/article.html.
>
> Thanks!
>
> --
> R. Mark Volkmann
> Object Computing, Inc.
>
> >
>



-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

--~--~-~--~~~---~--~~
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: How to represents a Big text file using sequence?

2009-08-31 Thread Adrian Cuthbertson

I mostly revert to good ole loop/recur for these large file processing
exercises. Here's a template you could use (includes a try/catch so
you can see errors as you go);

(import '(java.io BufferedReader FileReader PrintWriter File))

(defn process-log-file
  "Read a log file tracting lines matching regx."
  [in-fp out-fp regx]
  (with-open [rdr (BufferedReader. (FileReader. (File. in-fp)))
  wtr (PrintWriter. (File. out-fp))]
  (loop [line (.readLine rdr) i 0]
(if line
  (try
(let [fnd (re-matches regx line)]
  (when-not (nil? fnd)
(.println wtr line))) ; or whatever
  (recur (.readLine rdr) (inc i))
(catch Exception e (prn line e)))
  

Regards, Adrian.



On Mon, Aug 31, 2009 at 4:44 PM, wangzx wrote:
>
> I just want to learn clojure by using it to parse log file and
> generate reports. and one question is: for a large text file, can we
> use it as a sequence effectively? for example, for a 100M log file, we
> need to check each line for some pattern match.
>
> I just using the (line-seq rdr) but it will cause
> OutOfMemoryException.
>
> demo code
>
> (defn buffered-reader [file]
>        (new java.io.BufferedReader
>                (new java.io.InputStreamReader
>                        (new java.io.FileInputStream file
>
> (def -reader (buffered-reader "test.txt"))
> (filter #(= "some" %) -reader)
>
> even there is no lines match "some", the filter operation will cause
> OutOfMemoryException.
>
> Is there other APIs like the Sequence but provide stream-like API?
> >
>

--~--~-~--~~~---~--~~
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: How to represents a Big text file using sequence?

2009-08-31 Thread Jonathan Smith

Look at clojure-contrib/duck_streams.clj

(specifically the read-lines function)

I think it should be sufficiently lazy to do the job that you are
looking for. (although I don't have any 100mb txt files to test with
handy right now...)

On Aug 31, 10:44 am, wangzx  wrote:
> I just want to learn clojure by using it to parse log file and
> generate reports. and one question is: for a large text file, can we
> use it as a sequence effectively? for example, for a 100M log file, we
> need to check each line for some pattern match.
>
> I just using the (line-seq rdr) but it will cause
> OutOfMemoryException.
>
> demo code
>
> (defn buffered-reader [file]
>         (new java.io.BufferedReader
>                 (new java.io.InputStreamReader
>                         (new java.io.FileInputStream file
>
> (def -reader (buffered-reader "test.txt"))
> (filter #(= "some" %) -reader)
>
> even there is no lines match "some", the filter operation will cause
> OutOfMemoryException.
>
> Is there other APIs like the Sequence but provide stream-like API?
--~--~-~--~~~---~--~~
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: How to represents a Big text file using sequence?

2009-08-31 Thread Jonathan Smith

Ooh, or maybe not. I just reread and line-seq and read-lines should
implement pretty much the same thing. (In the demo code it isn't clear
to me where you are using line-seq.)

 Are you holding on to the head of the sequence somewhere?

On Aug 31, 12:52 pm, Jonathan Smith 
wrote:
> Look at clojure-contrib/duck_streams.clj
>
> (specifically the read-lines function)
>
> I think it should be sufficiently lazy to do the job that you are
> looking for. (although I don't have any 100mb txt files to test with
> handy right now...)
>
> On Aug 31, 10:44 am, wangzx  wrote:
>
> > I just want to learn clojure by using it to parse log file and
> > generate reports. and one question is: for a large text file, can we
> > use it as a sequence effectively? for example, for a 100M log file, we
> > need to check each line for some pattern match.
>
> > I just using the (line-seq rdr) but it will cause
> > OutOfMemoryException.
>
> > demo code
>
> > (defn buffered-reader [file]
> >         (new java.io.BufferedReader
> >                 (new java.io.InputStreamReader
> >                         (new java.io.FileInputStream file
>
> > (def -reader (buffered-reader "test.txt"))
> > (filter #(= "some" %) -reader)
>
> > even there is no lines match "some", the filter operation will cause
> > OutOfMemoryException.
>
> > Is there other APIs like the Sequence but provide stream-like API?
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



for those who just can't get enough of monads

2009-08-31 Thread Raoul Duke

http://patryshev.com/monad/crashcourse.pdf
(via SVP)

--~--~-~--~~~---~--~~
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: Java security code in Clojure

2009-08-31 Thread Emeka
Sorry, I was too quick. I misunderstood your code.

Regards,
Emeka

On Mon, Aug 31, 2009 at 6:10 PM, Emeka  wrote:

>
> Hello Sam,
>
> From the Java version you used created array ,byte[] encodedKey = new
> byte[(int) keyFile.length()];, but in clojure version you did (into-array
> Byte/TYPE (byte-seq stream)) , why not  use make-array here.
>
> (byte-seq stream) returns list, is that the right argument for constructor
> call?
>
> Did  you try this;
>
> (make-array Byte/TYPE (int (. (java.io.File. "public.der") length)))
>
> Regards,
> Emeka
>   On Mon, Aug 31, 2009 at 11:19 AM, Sam Hughes 
> wrote:
>
>>
>> Hey,
>>
>> I'm trying to write a Clojure security library. My first step is
>> porting some working Java code into Clojure. The Java and Clojure
>> snippets below are more or less the same, but with the Clojure code,
>> I'm getting: "java.security.InvalidKeyException: IOException: null
>> [Thrown class java.security.spec.InvalidKeySpecException]," which I
>> can't seem to replicate with the Java code.
>>
>> The goal of the code is to read in a DER file, use it to encrypt a
>> "Hello World" message, then output the encrypted message as a new
>> file.
>>
>> Neither of these snippets necessarily follow good coding standards.
>> That said, here's the working Java code snippet:
>>
>> final File keyFile = new File("public.der");
>> byte[] encodedKey = new byte[(int) keyFile.length()];
>>
>> new FileInputStream(keyFile).read(encodedKey);
>> final byte[] newEncoded = encodedKey;
>>
>> final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(newEncoded);
>> KeyFactory kf = KeyFactory.getInstance("RSA");
>> PublicKey pk = kf.generatePublic(keySpec);
>>
>> Cipher rsa = Cipher.getInstance("RSA");
>> rsa.init(Cipher.ENCRYPT_MODE, pk);
>> OutputStream os = new CipherOutputStream(new FileOutputStream
>> ("encrypted.rsa"), rsa);
>>
>> Writer out = new OutputStreamWriter(os);
>> out.write("Hello World");
>> out.close();
>> os.close();
>>
>> And here's the Exception throwing Clojure code:
>>
>> (ns security
>>  (:import
>>   [java.io File FileInputStream IOException]
>>   [java.security.spec X509EncodedKeySpec]
>>   [java.security KeyFactory PublicKey
>>KeyPairGenerator NoSuchAlgorithmException KeyPair]
>>   [javax.crypto KeyGenerator Cipher]))
>>
>> (defn byte-seq [rdr]
>>  (let [result (byte (. rdr read))]
>>(if (= result -1)
>>  (do (. rdr close) nil)
>>  (lazy-seq (cons result (byte-seq rdr))
>>
>> (def stream (new FileInputStream (new File "public.der")))
>> (def byte-arr (into-array Byte/TYPE (byte-seq stream)))
>> (def pk-spec (new X509EncodedKeySpec byte-arr))
>> (def kf (. KeyFactory (getInstance "RSA")))
>> (def pk (. kf (generatePublic pk-spec)))  ; exception thrown
>> here
>>
>> Does anyone have any suggestion for what could be causing the
>> exception? I'm perplexed because, right now, I'm just trying to
>> replicate Java code in Clojure -- nothing too fancy.
>>
>> Thanks a lot,
>> Sam
>>
>> >>
>>
>

--~--~-~--~~~---~--~~
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: STM article

2009-08-31 Thread Lau

Hey Mark,

Congratulations on a very good article - I helped me get a lot of
facts
straight.

The final part of the article where you comment on the various
functions like
add-watcher etc, is not very insightful and the UML charts didnt help
me all
that much. But with such a small amount of critisism, its actually a
compliment :)

I loved it! Thanks.

/Lau

On Aug 30, 7:42 pm, Mark Volkmann  wrote:
> I think we'd all agree that STM is a very important feature of
> Clojure. I trust that it works as advertised, but until recently I
> didn't feel that I understood exactly how it worked. This left me
> unable to defend it when pressed by developers that aren't using
> Clojure. I wanted to change that, for me and for the Clojure
> community. For the past few months I've been working on an article
> about STM in general and the Clojure implementation in particular. I
> hope the Clojure community derives some value from it.
>
> Consider this a beta version of the article. I have tried very hard to
> get all the facts straight and have reviewed it many times. It seems
> inevitable though that I may have stated some things incorrectly. This
> is complicated stuff ... which it must be in order to make concurrent
> programming easier.
>
> If you have an interest in this topic, I'd appreciated getting some
> feedback on the article. In particular, search for "Question:" in the
> article. I'll make suggested updates quickly to avoid spreading any
> misinformation.
>
> You can find the article athttp://ociweb.com/mark/stm/article.html.
>
> Thanks!
>
> --
> R. Mark Volkmann
> Object Computing, Inc.
--~--~-~--~~~---~--~~
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: Java security code in Clojure

2009-08-31 Thread Emeka
Hello Sam,

>From the Java version you used created array ,byte[] encodedKey = new
byte[(int) keyFile.length()];, but in clojure version you did (into-array
Byte/TYPE (byte-seq stream)) , why not  use make-array here.

(byte-seq stream) returns list, is that the right argument for constructor
call?

Did  you try this;

(make-array Byte/TYPE (int (. (java.io.File. "public.der") length)))

Regards,
Emeka
On Mon, Aug 31, 2009 at 11:19 AM, Sam Hughes wrote:

>
> Hey,
>
> I'm trying to write a Clojure security library. My first step is
> porting some working Java code into Clojure. The Java and Clojure
> snippets below are more or less the same, but with the Clojure code,
> I'm getting: "java.security.InvalidKeyException: IOException: null
> [Thrown class java.security.spec.InvalidKeySpecException]," which I
> can't seem to replicate with the Java code.
>
> The goal of the code is to read in a DER file, use it to encrypt a
> "Hello World" message, then output the encrypted message as a new
> file.
>
> Neither of these snippets necessarily follow good coding standards.
> That said, here's the working Java code snippet:
>
> final File keyFile = new File("public.der");
> byte[] encodedKey = new byte[(int) keyFile.length()];
>
> new FileInputStream(keyFile).read(encodedKey);
> final byte[] newEncoded = encodedKey;
>
> final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(newEncoded);
> KeyFactory kf = KeyFactory.getInstance("RSA");
> PublicKey pk = kf.generatePublic(keySpec);
>
> Cipher rsa = Cipher.getInstance("RSA");
> rsa.init(Cipher.ENCRYPT_MODE, pk);
> OutputStream os = new CipherOutputStream(new FileOutputStream
> ("encrypted.rsa"), rsa);
>
> Writer out = new OutputStreamWriter(os);
> out.write("Hello World");
> out.close();
> os.close();
>
> And here's the Exception throwing Clojure code:
>
> (ns security
>  (:import
>   [java.io File FileInputStream IOException]
>   [java.security.spec X509EncodedKeySpec]
>   [java.security KeyFactory PublicKey
>KeyPairGenerator NoSuchAlgorithmException KeyPair]
>   [javax.crypto KeyGenerator Cipher]))
>
> (defn byte-seq [rdr]
>  (let [result (byte (. rdr read))]
>(if (= result -1)
>  (do (. rdr close) nil)
>  (lazy-seq (cons result (byte-seq rdr))
>
> (def stream (new FileInputStream (new File "public.der")))
> (def byte-arr (into-array Byte/TYPE (byte-seq stream)))
> (def pk-spec (new X509EncodedKeySpec byte-arr))
> (def kf (. KeyFactory (getInstance "RSA")))
> (def pk (. kf (generatePublic pk-spec)))  ; exception thrown
> here
>
> Does anyone have any suggestion for what could be causing the
> exception? I'm perplexed because, right now, I'm just trying to
> replicate Java code in Clojure -- nothing too fancy.
>
> Thanks a lot,
> Sam
>
> >
>

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



Easily add a Java Listener

2009-08-31 Thread rb

Hi,

After using Jwt from Clojure, I did it with Jruby and discovered that
Jruby has what they call Closure Conversion (http://kenai.com/projects/
jruby/pages/CallingJavaFromJRuby#Closure_conversion ) where a Ruby
block or closure is converted to an appropriate Java interface. From
the wiki: "When calling a method that expects an interface, JRuby
checks if a block is passed and automatically converts the block to an
object implementing the interface".

I found this to be unbelievably easy to use and efficient (for the
developer) as the listener is added this way:
button.clicked.add_listener(self) do
  greeting_.setText(nameEdit_.getText)
end

There's no need for the developer to implement any interface or manage
any proxy object.
I wondered if something similar is possible in Clojure. If not, would
this be considered a valuable addition to Clojure?

Thanks

Raphaël




--~--~-~--~~~---~--~~
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: STM article

2009-08-31 Thread Mark Volkmann

On Mon, Aug 31, 2009 at 1:10 PM, Lau wrote:
>
> Hey Mark,
>
> Congratulations on a very good article - I helped me get a lot of
> facts straight.

Thanks!

> The final part of the article where you comment on the various
> functions like
> add-watcher etc, is not very insightful and the UML charts didnt help
> me all
> that much. But with such a small amount of critisism, its actually a
> compliment :)
>
> I loved it! Thanks.

Yeah, the "Low Level" section is mainly useful if you want to go
through the source code (especially Ref.java and
LockingTransaction.java) and you want some assistance figuring out the
purpose of each field and the functionality of each method. Most
readers will probably get what they want from the first half of the
article which is everything before that section.

-- 
R. Mark Volkmann
Object Computing, Inc.

--~--~-~--~~~---~--~~
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: STM article

2009-08-31 Thread Mark Volkmann

On Mon, Aug 31, 2009 at 11:23 AM, Howard Lewis Ship wrote:
>
> Wow! That's a lot of great research.  I'm impressed.

Thanks!

> My only concern is that some of the internals *could* change over time
> (I see Rich's commit stream) and some indication in the doc
> identifying what's design level (won't change) vs. implementation
> level (might change) would be useful.

That's a very valid concern and that's why I say this near the
beginning of the "High Level" section:

"My intention is to update this article whenever the Clojure STM
implementation changes so it remains an accurate source of information
that is easier to digest than simply reading the source code."

I expect that we'll see all the STM-related code rewritten in Clojure
instead of Java, perhaps within the next year, so I'll have a lot of
updating to do.

-- 
R. Mark Volkmann
Object Computing, Inc.

--~--~-~--~~~---~--~~
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: Java security code in Clojure

2009-08-31 Thread Sam Hughes

Hey Emeka,

No problem. Yeah, the reason I used the above configuration is because
it ultimately returned the same primitive byte array, to be passed
into the X509EncodedKeySpec constructor.

Sam

On Aug 31, 1:27 pm, Emeka  wrote:
> Sorry, I was too quick. I misunderstood your code.
>
> Regards,
> Emeka
>
> On Mon, Aug 31, 2009 at 6:10 PM, Emeka  wrote:
>
> > Hello Sam,
>
> > From the Java version you used created array ,byte[] encodedKey = new
> > byte[(int) keyFile.length()];, but in clojure version you did (into-array
> > Byte/TYPE (byte-seq stream)) , why not  use make-array here.
>
> > (byte-seq stream) returns list, is that the right argument for constructor
> > call?
>
> > Did  you try this;
>
> > (make-array Byte/TYPE (int (. (java.io.File. "public.der") length)))
>
> > Regards,
> > Emeka
> >   On Mon, Aug 31, 2009 at 11:19 AM, Sam Hughes 
> > wrote:
>
> >> Hey,
>
> >> I'm trying to write a Clojure security library. My first step is
> >> porting some working Java code into Clojure. The Java and Clojure
> >> snippets below are more or less the same, but with the Clojure code,
> >> I'm getting: "java.security.InvalidKeyException: IOException: null
> >> [Thrown class java.security.spec.InvalidKeySpecException]," which I
> >> can't seem to replicate with the Java code.
>
> >> The goal of the code is to read in a DER file, use it to encrypt a
> >> "Hello World" message, then output the encrypted message as a new
> >> file.
>
> >> Neither of these snippets necessarily follow good coding standards.
> >> That said, here's the working Java code snippet:
>
> >> final File keyFile = new File("public.der");
> >> byte[] encodedKey = new byte[(int) keyFile.length()];
>
> >> new FileInputStream(keyFile).read(encodedKey);
> >> final byte[] newEncoded = encodedKey;
>
> >> final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(newEncoded);
> >> KeyFactory kf = KeyFactory.getInstance("RSA");
> >> PublicKey pk = kf.generatePublic(keySpec);
>
> >> Cipher rsa = Cipher.getInstance("RSA");
> >> rsa.init(Cipher.ENCRYPT_MODE, pk);
> >> OutputStream os = new CipherOutputStream(new FileOutputStream
> >> ("encrypted.rsa"), rsa);
>
> >> Writer out = new OutputStreamWriter(os);
> >> out.write("Hello World");
> >> out.close();
> >> os.close();
>
> >> And here's the Exception throwing Clojure code:
>
> >> (ns security
> >>  (:import
> >>   [java.io File FileInputStream IOException]
> >>   [java.security.spec X509EncodedKeySpec]
> >>   [java.security KeyFactory PublicKey
> >>    KeyPairGenerator NoSuchAlgorithmException KeyPair]
> >>   [javax.crypto KeyGenerator Cipher]))
>
> >> (defn byte-seq [rdr]
> >>  (let [result (byte (. rdr read))]
> >>    (if (= result -1)
> >>      (do (. rdr close) nil)
> >>      (lazy-seq (cons result (byte-seq rdr))
>
> >> (def stream (new FileInputStream (new File "public.der")))
> >> (def byte-arr (into-array Byte/TYPE (byte-seq stream)))
> >> (def pk-spec (new X509EncodedKeySpec byte-arr))
> >> (def kf (. KeyFactory (getInstance "RSA")))
> >> (def pk (. kf (generatePublic pk-spec)))          ; exception thrown
> >> here
>
> >> Does anyone have any suggestion for what could be causing the
> >> exception? I'm perplexed because, right now, I'm just trying to
> >> replicate Java code in Clojure -- nothing too fancy.
>
> >> Thanks a lot,
> >> Sam
--~--~-~--~~~---~--~~
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: Clojure for game programming?

2009-08-31 Thread hoeck

Hi,

I'm using Clojure together with Processing and JBox2D to write small,
2D physics-powered games.
The constraint given by JBox2D is, that the physics simulation has to
be run in and only accessed from the same thread.
So the whole game simulation is confined to the physics simulation
thread and a Java HashMap to keep track of player bodies.
But Clojure helps splitting the game into several threads, for
rendering, simulation and networking.
Currently I'm using Clojure vectors to share the state of the
simulated world (JBox2D) with the rendering thread (Processing).

Processing also provides a way to use OpenGL as a renderer, but I
haven't figured out yet how to setup it correctly.



On 30 Aug., 06:01, Elliott Slaughter 
wrote:
> Hi,
>
> I'm visiting from the Common Lisp game-dev crowd and wanted to try out
> Clojure for writing games.
>
> I saw some JOGL examples posted in this group, in addition to the
> cloggle library which wraps some JOGL functionality. Cloggle is pretty
> thin right now and uses glFunctionNames as they are, so I've added
> some patches to convert glFunctionNames to Lispier function-names, and
> tried to Lispify the interface in general [1].
>
> I think I'd be fairly comfortable writing a graphics engine with my
> patched version of cloggle. What I'm not so sure about is writing the
> game simulation model.
>
> All game simulation models I've seen used graphs of mutable objects;
> I'm not entirely sure how to move to a more functional model. One the
> one hand, reallocating the game world on every frame seems excessive
> (even if Java's GC is fast), and on the other hand putting refs
> everywhere a value could potentially change seems equally excessive
> (and probably detrimental to performance).
>
> I just saw zippers on the other libraries page, but haven't had the
> time to read it and don't know if it meets my needs or not.
>
> If anyone has suggestions on simulating interactions between trees of
> objects (especially on the Clojure way to do it), I'd appreciate it.
> Comments on my cloggle patches also welcome.
>
> [1]http://github.com/slaguth/cloggle/tree/master
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



A complete documentation (downloadable)

2009-08-31 Thread freddi301

are there a complete clojure documentation ?

--~--~-~--~~~---~--~~
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: vs. Python

2009-08-31 Thread Brian Hurt
On Sun, Aug 30, 2009 at 9:31 AM, Jason Baker  wrote:

>
> On Aug 30, 2:24 am, Dan Fichter  wrote:
> > The Clojure version is more concise and radically safer but a little more
> > conceptually packed.  Is it worth your trouble?
>
> Being primarily a Python programmer, I can say that the first thing my
> co-workers would say is that Clojure isn't as readable as Python is.
>
>
>
Any language you are familiar and comfortable with is going to seem much
more readable and much more intuitive than a language you are unfamiliar
with.  Even similarity to English presupposes a familiarity with and comfort
with English- something most people on this planet don't have.  A native
English speaker would find a programming language whose syntax was based on,
say, Mandarin or Swahili, very "unintuitive".

The point here is that arguing in favor of a new language on the basis of
intuitiveness and readability is a losing argument.

Instead, I'd concentrate on the advantages Clojure has- things like
incredibly good parallelism capabilities, tight integration with Java (for
example, can you extend Lucene's HitCollector abstract base class to
implement your own hit collector in Jython?  This is an honest question- I
really don't know), etc.

Brian

--~--~-~--~~~---~--~~
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: STM article

2009-08-31 Thread sirpi


 I remember a discussion about the Ant example in Lisp and Clojure,
the response for Rich shows some important point that establishes the
difference with a naive implementation of the Ant example.

 I think you can add a link to the Ant example and the incorrect Lisp
implementation of it.

http://groups.google.com/group/comp.lang.lisp/msg/a86c91091ae16970

 I expect to get time to read your article, it seems very interesting.
Congratulations for such a good work.


--~--~-~--~~~---~--~~
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: vs. Python

2009-08-31 Thread Brian Hurt
On Mon, Aug 31, 2009 at 9:03 AM, Konrad Hinsen
wrote:

>
>
> In this particular case, there is no reason to worry: open() returns a
> file object that is fed to the method read(), but after that method
> returns, there is no more reference to the object, so it is garbage
> collected. Upon destruction of the file object, Python closes the
> file. All that is documented behaviour in Python, so it is safe to
> rely on it.
>
>
If I recall correctly (and correct me if I'm wrong), Python uses a reference
counting garbage collector.  Which means as soon as the reference to the
object goes away, the object gets collected and the handle closed.  Most
JVMs use some form of mark & sweep algorithm, which means it may be some
time before the object gets collected (and the resource freed).  This is
especially the case in a generational GC system, where long-lived objects
get collected much less frequently.  So, for long-running programs, it's
possible to pile up uncollected resources to the point where you run out of
the resource, simply because unused objects haven't been collected yet.

Generally, when you open a file descriptor, you should always make sure it's
gets closed when you're done with it.

Brian

--~--~-~--~~~---~--~~
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: How to represents a Big text file using sequence?

2009-08-31 Thread Laurent PETIT
Hello,

Can you give the exact REPL session, with the exception you have, and also
the clojure version you're using ?

8/31 wangzx 

>
> I just want to learn clojure by using it to parse log file and
> generate reports. and one question is: for a large text file, can we
> use it as a sequence effectively? for example, for a 100M log file, we
> need to check each line for some pattern match.
>
> I just using the (line-seq rdr) but it will cause
> OutOfMemoryException.
>
> demo code
>
> (defn buffered-reader [file]
>(new java.io.BufferedReader
>(new java.io.InputStreamReader
>(new java.io.FileInputStream file
>
> (def -reader (buffered-reader "test.txt"))
> (filter #(= "some" %) -reader)
>
> even there is no lines match "some", the filter operation will cause
> OutOfMemoryException.
>
> Is there other APIs like the Sequence but provide stream-like API?
> >
>

--~--~-~--~~~---~--~~
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: How to represents a Big text file using sequence?

2009-08-31 Thread Stuart Sierra

On Aug 31, 10:44 am, wangzx  wrote:
> I just want to learn clojure by using it to parse log file and
> generate reports. and one question is: for a large text file, can we
> use it as a sequence effectively? for example, for a 100M log file, we
> need to check each line for some pattern match.

You have to be quite careful to avoid "holding on to the head" of the
sequence when you do this sort of thing.  You can create a sequence of
lines from a Reader (clojure.core/line-seq or duck-streams/read-lines)
but you have to be careful that you deal with the sequence only
through lazy functions such as "map", without keeping any references
to the start of the sequence itself.

-SS
--~--~-~--~~~---~--~~
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: Easily add a Java Listener

2009-08-31 Thread Stuart Sierra

That's a clever trick.  How does the block know which interface method
was invoked?
-SS

On Aug 31, 2:41 pm, rb  wrote:
> Hi,
>
> After using Jwt from Clojure, I did it with Jruby and discovered that
> Jruby has what they call Closure Conversion (http://kenai.com/projects/
> jruby/pages/CallingJavaFromJRuby#Closure_conversion ) where a Ruby
> block or closure is converted to an appropriate Java interface. From
> the wiki: "When calling a method that expects an interface, JRuby
> checks if a block is passed and automatically converts the block to an
> object implementing the interface".
>
> I found this to be unbelievably easy to use and efficient (for the
> developer) as the listener is added this way:
> button.clicked.add_listener(self) do
>   greeting_.setText(nameEdit_.getText)
> end
>
> There's no need for the developer to implement any interface or manage
> any proxy object.
> I wondered if something similar is possible in Clojure. If not, would
> this be considered a valuable addition to Clojure?
>
> Thanks
>
> Raphaël
--~--~-~--~~~---~--~~
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: Easily add a Java Listener

2009-08-31 Thread Kevin Downey

I think this would necessitate an added layer of indirection and
reflection, which would mean taking a performance hit.

On Mon, Aug 31, 2009 at 2:54 PM, Stuart
Sierra wrote:
>
> That's a clever trick.  How does the block know which interface method
> was invoked?
> -SS
>
> On Aug 31, 2:41 pm, rb  wrote:
>> Hi,
>>
>> After using Jwt from Clojure, I did it with Jruby and discovered that
>> Jruby has what they call Closure Conversion (http://kenai.com/projects/
>> jruby/pages/CallingJavaFromJRuby#Closure_conversion ) where a Ruby
>> block or closure is converted to an appropriate Java interface. From
>> the wiki: "When calling a method that expects an interface, JRuby
>> checks if a block is passed and automatically converts the block to an
>> object implementing the interface".
>>
>> I found this to be unbelievably easy to use and efficient (for the
>> developer) as the listener is added this way:
>> button.clicked.add_listener(self) do
>>   greeting_.setText(nameEdit_.getText)
>> end
>>
>> There's no need for the developer to implement any interface or manage
>> any proxy object.
>> I wondered if something similar is possible in Clojure. If not, would
>> this be considered a valuable addition to Clojure?
>>
>> Thanks
>>
>> Raphaël
> >
>



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



Re: Easily add a Java Listener

2009-08-31 Thread Christophe Grand

Rhino provides a similar facility
https://developer.mozilla.org/en/Scripting_Java#JavaScript_Functions_as_Java_Interfaces
but AFAIK it uses reflection.

On Tue, Sep 1, 2009 at 12:03 AM, Kevin Downey wrote:
>
> I think this would necessitate an added layer of indirection and
> reflection, which would mean taking a performance hit.
>
> On Mon, Aug 31, 2009 at 2:54 PM, Stuart
> Sierra wrote:
>>
>> That's a clever trick.  How does the block know which interface method
>> was invoked?
>> -SS
>>
>> On Aug 31, 2:41 pm, rb  wrote:
>>> Hi,
>>>
>>> After using Jwt from Clojure, I did it with Jruby and discovered that
>>> Jruby has what they call Closure Conversion (http://kenai.com/projects/
>>> jruby/pages/CallingJavaFromJRuby#Closure_conversion ) where a Ruby
>>> block or closure is converted to an appropriate Java interface. From
>>> the wiki: "When calling a method that expects an interface, JRuby
>>> checks if a block is passed and automatically converts the block to an
>>> object implementing the interface".
>>>
>>> I found this to be unbelievably easy to use and efficient (for the
>>> developer) as the listener is added this way:
>>> button.clicked.add_listener(self) do
>>>   greeting_.setText(nameEdit_.getText)
>>> end
>>>
>>> There's no need for the developer to implement any interface or manage
>>> any proxy object.
>>> I wondered if something similar is possible in Clojure. If not, would
>>> this be considered a valuable addition to Clojure?
>>>
>>> Thanks
>>>
>>> Raphaël
>> >
>>
>
>
>
> --
> And what is good, Phaedrus,
> And what is not good—
> Need we ask anyone to tell us these things?
>
> >
>



-- 
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)

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



Re: Java security code in Clojure

2009-08-31 Thread Timothy Pratley

security=> (count byte-arr)
115
tprat...@neuromancer:~$ wc public.der
  5  11 294 public.der

your byte-seq does not do what the java version does :)



On Aug 31, 9:19 pm, Sam Hughes  wrote:
> Hey,
>
> I'm trying to write a Clojure security library. My first step is
> porting some working Java code into Clojure. The Java and Clojure
> snippets below are more or less the same, but with the Clojure code,
> I'm getting: "java.security.InvalidKeyException: IOException: null
> [Thrown class java.security.spec.InvalidKeySpecException]," which I
> can't seem to replicate with the Java code.
>
> The goal of the code is to read in a DER file, use it to encrypt a
> "Hello World" message, then output the encrypted message as a new
> file.
>
> Neither of these snippets necessarily follow good coding standards.
> That said, here's the working Java code snippet:
>
> final File keyFile = new File("public.der");
> byte[] encodedKey = new byte[(int) keyFile.length()];
>
> new FileInputStream(keyFile).read(encodedKey);
> final byte[] newEncoded = encodedKey;
>
> final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(newEncoded);
> KeyFactory kf = KeyFactory.getInstance("RSA");
> PublicKey pk = kf.generatePublic(keySpec);
>
> Cipher rsa = Cipher.getInstance("RSA");
> rsa.init(Cipher.ENCRYPT_MODE, pk);
> OutputStream os = new CipherOutputStream(new FileOutputStream
> ("encrypted.rsa"), rsa);
>
> Writer out = new OutputStreamWriter(os);
> out.write("Hello World");
> out.close();
> os.close();
>
> And here's the Exception throwing Clojure code:
>
> (ns security
>   (:import
>    [java.io File FileInputStream IOException]
>    [java.security.spec X509EncodedKeySpec]
>    [java.security KeyFactory PublicKey
>     KeyPairGenerator NoSuchAlgorithmException KeyPair]
>    [javax.crypto KeyGenerator Cipher]))
>
> (defn byte-seq [rdr]
>   (let [result (byte (. rdr read))]
>     (if (= result -1)
>       (do (. rdr close) nil)
>       (lazy-seq (cons result (byte-seq rdr))
>
> (def stream (new FileInputStream (new File "public.der")))
> (def byte-arr (into-array Byte/TYPE (byte-seq stream)))
> (def pk-spec (new X509EncodedKeySpec byte-arr))
> (def kf (. KeyFactory (getInstance "RSA")))
> (def pk (. kf (generatePublic pk-spec)))          ; exception thrown
> here
>
> Does anyone have any suggestion for what could be causing the
> exception? I'm perplexed because, right now, I'm just trying to
> replicate Java code in Clojure -- nothing too fancy.
>
> Thanks a lot,
> Sam
--~--~-~--~~~---~--~~
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: vs. Python

2009-08-31 Thread John Harrop
On Mon, Aug 31, 2009 at 5:15 PM, Brian Hurt  wrote:

> If I recall correctly (and correct me if I'm wrong), Python uses a
> reference counting garbage collector.  Which means as soon as the reference
> to the object goes away, the object gets collected and the handle closed.
> Most JVMs use some form of mark & sweep algorithm, which means it may be
> some time before the object gets collected (and the resource freed).  This
> is especially the case in a generational GC system, where long-lived objects
> get collected much less frequently.  So, for long-running programs, it's
> possible to pile up uncollected resources to the point where you run out of
> the resource, simply because unused objects haven't been collected yet.
>

This suggests that when low-level JVM functions that try to get a file
handle, socket, or what-not from the OS fail they should invoke the garbage
collector in a full stop-the-world collection and then retry, just as the
memory allocator already does, and throw the IOException only if they still
fail afterward. (These resources tend to have finalizers, so the GC should
be run twice back-to-back to collect them, or even repeatedly until no
garbage was collected.)

Then most cases of this would cause the occasional very slow file handle
acquisition instead of a crash or other error.

Generally, when you open a file descriptor, you should always make sure it's
> gets closed when you're done with it.
>

But I do agree with this. Finalizers and gc of objects holding native
resources are a safety net; it's better not to fall into it even when it's
there. You might not die but the judges will be holding up placards reading
0.0, 0.1, 0.0, 1.2, 0.3 or some such after your performance. :)

--~--~-~--~~~---~--~~
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: vs. Python

2009-08-31 Thread John Harrop
On Mon, Aug 31, 2009 at 5:04 PM, Brian Hurt  wrote:

> On Sun, Aug 30, 2009 at 9:31 AM, Jason Baker  wrote:
>
>> On Aug 30, 2:24 am, Dan Fichter  wrote:
>> > The Clojure version is more concise and radically safer but a little
>> more
>> > conceptually packed.  Is it worth your trouble?
>>
>> Being primarily a Python programmer, I can say that the first thing my
>> co-workers would say is that Clojure isn't as readable as Python is.
>>
>>
>>
> Any language you are familiar and comfortable with is going to seem much
> more readable and much more intuitive than a language you are unfamiliar
> with.  Even similarity to English presupposes a familiarity with and comfort
> with English- something most people on this planet don't have.  A native
> English speaker would find a programming language whose syntax was based on,
> say, Mandarin or Swahili, very "unintuitive".
>
> The point here is that arguing in favor of a new language on the basis of
> intuitiveness and readability is a losing argument.
>

That may depend on the audience. If the audience is a bunch of Python
programmers, similarities to Python may be quite relevant and not comprise a
losing argument.

--~--~-~--~~~---~--~~
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: A complete documentation (downloadable)

2009-08-31 Thread John Harrop
On Mon, Aug 31, 2009 at 3:45 PM, freddi301  wrote:

>
> are there a complete clojure documentation ?


There's the documentation at clojure.org; you could spider it with wget,
though with some sites you need to spoof the user-agent and/or hack wget to
disable retrieving robots.txt to do that. (Ethical, IMO, if your intent is
to save hammering that server for bandwidth in the future by having a local
copy.)

There's also the (doc foo) form at the repl. All the API documentation is
there. Unfortunately, *only* the API documentation is there; it would be
nice if, at the very least, (doc a-special-form-name) provided a brief
synopsis instead of just the web site's URL, for the convenience of having
more of it in one place and zero task-switches away from your open project
windows, and also more information findable via find-doc.

--~--~-~--~~~---~--~~
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: Java security code in Clojure

2009-08-31 Thread Timothy Pratley

The reason your byte-seq fails is because you coerce the int result to
a byte before comparing to -1. You should compare the int result to -1
and coerce to a byte after:
(defn byte-seq [rdr]
  (let [result (. rdr read)]
(if (= result -1)
  (do (. rdr close) nil)
  (lazy-seq (cons (byte result) (byte-seq rdr))

Because
user=> (byte 255)
-1

-1 is a valid byte to read in the file and will be returned as int 255
by read

security=> pk
# wrote:
> security=> (count byte-arr)
> 115
> tprat...@neuromancer:~$ wc public.der
>   5  11 294 public.der
>
> your byte-seq does not do what the java version does :)
>
> On Aug 31, 9:19 pm, Sam Hughes  wrote:
>
> > Hey,
>
> > I'm trying to write a Clojure security library. My first step is
> > porting some working Java code into Clojure. The Java and Clojure
> > snippets below are more or less the same, but with the Clojure code,
> > I'm getting: "java.security.InvalidKeyException: IOException: null
> > [Thrown class java.security.spec.InvalidKeySpecException]," which I
> > can't seem to replicate with the Java code.
>
> > The goal of the code is to read in a DER file, use it to encrypt a
> > "Hello World" message, then output the encrypted message as a new
> > file.
>
> > Neither of these snippets necessarily follow good coding standards.
> > That said, here's the working Java code snippet:
>
> > final File keyFile = new File("public.der");
> > byte[] encodedKey = new byte[(int) keyFile.length()];
>
> > new FileInputStream(keyFile).read(encodedKey);
> > final byte[] newEncoded = encodedKey;
>
> > final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(newEncoded);
> > KeyFactory kf = KeyFactory.getInstance("RSA");
> > PublicKey pk = kf.generatePublic(keySpec);
>
> > Cipher rsa = Cipher.getInstance("RSA");
> > rsa.init(Cipher.ENCRYPT_MODE, pk);
> > OutputStream os = new CipherOutputStream(new FileOutputStream
> > ("encrypted.rsa"), rsa);
>
> > Writer out = new OutputStreamWriter(os);
> > out.write("Hello World");
> > out.close();
> > os.close();
>
> > And here's the Exception throwing Clojure code:
>
> > (ns security
> >   (:import
> >    [java.io File FileInputStream IOException]
> >    [java.security.spec X509EncodedKeySpec]
> >    [java.security KeyFactory PublicKey
> >     KeyPairGenerator NoSuchAlgorithmException KeyPair]
> >    [javax.crypto KeyGenerator Cipher]))
>
> > (defn byte-seq [rdr]
> >   (let [result (byte (. rdr read))]
> >     (if (= result -1)
> >       (do (. rdr close) nil)
> >       (lazy-seq (cons result (byte-seq rdr))
>
> > (def stream (new FileInputStream (new File "public.der")))
> > (def byte-arr (into-array Byte/TYPE (byte-seq stream)))
> > (def pk-spec (new X509EncodedKeySpec byte-arr))
> > (def kf (. KeyFactory (getInstance "RSA")))
> > (def pk (. kf (generatePublic pk-spec)))          ; exception thrown
> > here
>
> > Does anyone have any suggestion for what could be causing the
> > exception? I'm perplexed because, right now, I'm just trying to
> > replicate Java code in Clojure -- nothing too fancy.
>
> > Thanks a lot,
> > Sam
--~--~-~--~~~---~--~~
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: Easily add a Java Listener

2009-08-31 Thread Luc Prefontaine
Hi guys,

Am I missing something here?

We define SWING listeners with proxies and find that it's already short
in terms of code lines:

(.addMouseListener
  (proxy [MouseAdapter] []
(mouseClicked [event]
(if (= 2 (.getClickCount event))
(display-details (.locationToIndex (.getPoint event)))
)
)
  )
)

Does the removal of the proxy call warrants significant efforts to
implement a "transparent" way of calling Java code ?
For us most of the programming time is spent in looking at the Javadoc
of the Java code to find which interface to use and how, not typing the
proxy call
itself.
Hope you do not hit too many member functions with the same name and
number of arguments but with different classes/interfaces :)))
You would then now a way to solve the ambiguity...

Again did I miss something ?

Luc



On Tue, 2009-09-01 at 00:59 +0200, Christophe Grand wrote:

> Rhino provides a similar facility
> https://developer.mozilla.org/en/Scripting_Java#JavaScript_Functions_as_Java_Interfaces
> but AFAIK it uses reflection.
> 
> On Tue, Sep 1, 2009 at 12:03 AM, Kevin Downey wrote:
> >
> > I think this would necessitate an added layer of indirection and
> > reflection, which would mean taking a performance hit.
> >
> > On Mon, Aug 31, 2009 at 2:54 PM, Stuart
> > Sierra wrote:
> >>
> >> That's a clever trick.  How does the block know which interface method
> >> was invoked?
> >> -SS
> >>
> >> On Aug 31, 2:41 pm, rb  wrote:
> >>> Hi,
> >>>
> >>> After using Jwt from Clojure, I did it with Jruby and discovered that
> >>> Jruby has what they call Closure Conversion (http://kenai.com/projects/
> >>> jruby/pages/CallingJavaFromJRuby#Closure_conversion ) where a Ruby
> >>> block or closure is converted to an appropriate Java interface. From
> >>> the wiki: "When calling a method that expects an interface, JRuby
> >>> checks if a block is passed and automatically converts the block to an
> >>> object implementing the interface".
> >>>
> >>> I found this to be unbelievably easy to use and efficient (for the
> >>> developer) as the listener is added this way:
> >>> button.clicked.add_listener(self) do
> >>>   greeting_.setText(nameEdit_.getText)
> >>> end
> >>>
> >>> There's no need for the developer to implement any interface or manage
> >>> any proxy object.
> >>> I wondered if something similar is possible in Clojure. If not, would
> >>> this be considered a valuable addition to Clojure?
> >>>
> >>> Thanks
> >>>
> >>> Raphaël
> >> >
> >>
> >
> >
> >
> > --
> > And what is good, Phaedrus,
> > And what is not good—
> > Need we ask anyone to tell us these things?
> >
> > >
> >
> 
> 
> 

Luc Préfontaine

Armageddon was yesterday, today we have a real problem...

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



Re: Easily add a Java Listener

2009-08-31 Thread Timothy Pratley

If I understand correctly it could be implemented with the following
change to Reflector.java:
static Object boxArg(Class paramType, Object arg){
if(paramType.isInterface() && arg instanceof IFn)
return makeAProxy( findMethodMatch( paramType, arg ),
arg );

Which would then allow us to instead of using proxy:
> (.addMouseListener
>   (proxy [MouseAdapter] []
>     (mouseClicked [event]
>         (if (= 2 (.getClickCount event))
>                 (display-details (.locationToIndex (.getPoint event)))

Write it using a function which will discover what interface/method to
create a proxy for:
(.addMouseListener
  #(if (= 2 (.getClickCount %))
 (display-details (-> % .getPoint .locationToIndex)))
  something)

The alternative solution for doing that if we are willing to create a
macro or helper function:
(defmacro add-action-listener
  "Attaches an ActionListener to a Component"
  [#^java.awt.Component obj, evt & body]
  `(.addActionListener
 ~obj (proxy [java.awt.event.ActionListener] []
(actionPerformed [~evt] ~...@body

(add-action-listener something evt
  (if (= 2 (.getClickCount evt))
(display-details (-> evt .getPoint .locationToIndex

The only difference being of course that you'd need a separate helper
for all the different interfaces want to proxy, and would have to
explicitly choose which interface the function implements. I don't
have enough experience to know of how many anonymous single function
interface proxies are useful.


Regards,
Tim.

--~--~-~--~~~---~--~~
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: Easily add a Java Listener

2009-08-31 Thread Luc Prefontaine
It could be handy to have this shortcut implemented as you suggest.
Solving ambiguities may be done through an explicit proxy or with some
meta data to point to the proper
method match.

Luc


On Mon, 2009-08-31 at 19:34 -0700, Timothy Pratley wrote:

> If I understand correctly it could be implemented with the following
> change to Reflector.java:
> static Object boxArg(Class paramType, Object arg){
> if(paramType.isInterface() && arg instanceof IFn)
> return makeAProxy( findMethodMatch( paramType, arg ),
> arg );
> 
> Which would then allow us to instead of using proxy:
> > (.addMouseListener
> >   (proxy [MouseAdapter] []
> > (mouseClicked [event]
> > (if (= 2 (.getClickCount event))
> > (display-details (.locationToIndex (.getPoint event)))
> 
> Write it using a function which will discover what interface/method to
> create a proxy for:
> (.addMouseListener
>   #(if (= 2 (.getClickCount %))
>  (display-details (-> % .getPoint .locationToIndex)))
>   something)
> 
> The alternative solution for doing that if we are willing to create a
> macro or helper function:
> (defmacro add-action-listener
>   "Attaches an ActionListener to a Component"
>   [#^java.awt.Component obj, evt & body]
>   `(.addActionListener
>  ~obj (proxy [java.awt.event.ActionListener] []
> (actionPerformed [~evt] ~...@body
> 
> (add-action-listener something evt
>   (if (= 2 (.getClickCount evt))
> (display-details (-> evt .getPoint .locationToIndex
> 
> The only difference being of course that you'd need a separate helper
> for all the different interfaces want to proxy, and would have to
> explicitly choose which interface the function implements. I don't
> have enough experience to know of how many anonymous single function
> interface proxies are useful.
> 
> 
> Regards,
> Tim.
> 
> > 
> 

Luc Préfontaine

Armageddon was yesterday, today we have a real problem...

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