Re: Trait-like behavior with Protocols

2010-02-09 Thread Jeff Rose
I think the extend function is made exactly to support the concrete
implementation of protocols.  It takes a type, and then any number of
protocol + function map pairs, where keyword names map to functions.
Checkout the protocol docs on assembla and look for extend:

http://www.assembla.com/wiki/show/clojure/Protocols

or read a recent post by Rich where he talks about some of the design
decisions behind these constructs:

http://groups.google.com/group/clojure/msg/330c230e8dc857a9

-Jeff Rose

On Feb 9, 12:13 am, aria42 ari...@gmail.com wrote:
 Is it possible to have default implementations associated with
 functions in a protocol? This is most useful when some protocol
 functions are defined in terms of other. For instance,

 (defprotocol Span
   (start [self])
   (stop [self])
   (span-length [self]))

 Now I know I can just make span-length a function on Span as opposed
 to part of the protocol. Is that what one should do?

-- 
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: Please share your thoughts on dependency injection

2010-02-09 Thread Steve Purcell
On 8 Feb 2010, at 16:53, Boris Mizhen - 迷阵 wrote:

 Hello all,
 
 I am playing with the idea of a little library for dependency injection.
 The idea is to declare injectable values as metadata-to-function map.
 I started with a sketch of what the client code may look like.
 
 Please let me know what you think.


I think you could achieve the same effect much more simply using 
'binding'/'with-bindings' and 'memoize'.

-Steve

-- 
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: Prepping clojure for packaging (was: Re: Clojure for system administration)

2010-02-09 Thread Ramakrishnan Muthukrishnan
On Tue, Feb 9, 2010 at 12:27 PM, Meikel Brandmeyer m...@kotka.de wrote:
 Hi,

 maybe I don't understand the problem. Why can't the system provide
 some kind of local repository? The package system (deb, rpm, ports,
 whatever) just installs the dependencies there. A wrapper script reads
 in the dependencies and adds them to the classpath on program start.
 Nothing is downloaded. There might be several versions of a library
 installed. No global classpath. I think that's what maven/ivy do right
 now. Why wouldn't this work together with a packaging system? (I think
 FreeBSD shows the way to go: cooperation between the system and the
 language.)

That's exactly what Debian does. For every Java package also provide
the maven xml file and the jar is discoverable from maven. The
installed packages on the local system acts as a local maven repo.

 http://wiki.debian.org/Java/MavenRepoSpec

-- 
  Ramakrishnan

-- 
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: scope of binding

2010-02-09 Thread Alex
D'oh! Thanks.

I fall for that trap yet again. Sounds so simple when explained.

2010/2/9 Sean Devlin francoisdev...@gmail.com:
 The problem is that map returns a lazy seq, and the lazy seq is
 evaluated outside of the binding by the REPL.  If you add a doall
 inside the binding, it behaves as you expect.

 user= (binding [*v* 2] (doall (map f [1 1 1])))
 (3 3 3)

 Sean

I know I've omitted this detail, but the actual code in question is
actually *db* binding from clojure.contrib.sql, so I can't change it
either way. doall works fine though.

2010/2/9 Richard Newman holyg...@gmail.com:
 You can also capture the binding. This looks a little ugly, but it works: it
 grabs the binding eagerly, and returns a closure that dynamically binds it
 when the function is invoked.

 (binding [*v* 2]
  (map (let [v *v*]
         (fn [n]
           (binding [*v* v]
             (f n
       [1 1 1]))

 Obviously you wouldn't use it in this instance -- use doall, or better yet
 rewrite your function to not use dynamic bindings -- but for larger jobs it
 works fine.

-- 
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: error reporting for macro expansion

2010-02-09 Thread Jeff Rose
I agree, the error reporting from the compiler can often be hard to
dig through.  Besides showing both the location of the macro
definition and its usage, it would be nice to hide all of the
clojure.lang.* calls in the stack trace by default, or fold them into
a single line.  That way the user code in the callstack would be
separated by 1 line rather 10 or 20, so you could more easily trace
the execution path.

On Feb 8, 8:11 pm, John R. Williams shponglesp...@gmail.com wrote:
 The Clojure compiler is not very helpful when it comes to debugging
 exceptions that occur while macros are being expanded. As an example,
 consider this code:

 ;; macro-fail.clj
 (defmacro broken [] (/ 0 0))
 (broken)

 Here's the stack trace I get when I compile this file:

 Exception in thread main java.lang.ArithmeticException: Divide by
 zero (macro-fail.clj:0)
         at clojure.lang.Compiler.eval(Compiler.java:5365)
         at clojure.lang.Compiler.load(Compiler.java:5759)
         at clojure.lang.Compiler.loadFile(Compiler.java:5722)
         at clojure.main$load_script__5893.invoke(main.clj:213)
         at clojure.main$script_opt__5922.invoke(main.clj:265)
         at clojure.main$main__5940.doInvoke(main.clj:346)
         at clojure.lang.RestFn.invoke(RestFn.java:409)
         at clojure.lang.Var.invoke(Var.java:365)
         at clojure.lang.AFn.applyToHelper(AFn.java:165)
         at clojure.lang.Var.applyTo(Var.java:482)
         at clojure.main.main(main.java:37)
 Caused by: java.lang.ArithmeticException: Divide by zero
         at clojure.lang.Numbers.divide(Numbers.java:138)
         at user$broken__1.invoke(macro-fail.clj:2)
         at clojure.lang.Var.invoke(Var.java:369)
         at clojure.lang.AFn.applyToHelper(AFn.java:167)
         at clojure.lang.Var.applyTo(Var.java:482)
         at clojure.lang.Compiler.macroexpand1(Compiler.java:5212)
         at clojure.lang.Compiler.macroexpand(Compiler.java:5267)
         at clojure.lang.Compiler.eval(Compiler.java:5335)
         ... 10 more

 As you can see, line 3, where the macro is used, appears nowhere in
 the stack trace. I've made some progress addressing this issue by
 adding an exception handler in Compiler.macroexpand1. I also
 discovered that, although the reader attaches line numbers to the
 forms it reads, it does not attach file names. I've added some code in
 LispReader.java that attaches the file name, but it does so by getting
 the value of Compiler.SOURCE_PATH. I suspect a less hackish fix would
 involve passing a filename to the reader some other way.

-- 
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: Trait-like behavior with Protocols

2010-02-09 Thread Konrad Hinsen
On 09.02.2010, at 02:14, Stuart Sierra wrote:

 On Feb 8, 6:13 pm, aria42 ari...@gmail.com wrote:
 (defprotocol Span
   (start [self])
   (stop [self])
   (span-length [self]))
 
 Now I know I can just make span-length a function on Span as opposed
 to part of the protocol. Is that what one should do?
 
 Yes.

I would say it depends.

I have a similar situation in my multiarray package 
(http://code.google.com/p/clj-multiarray/). In the multiarray protocol, I have 
two functions, shape and rank, with the latter being by definition the same 
as (comp count shape). However, I still have rank in the protocol, because 
for some implementations it is more efficient to compute the rank directly, 
rather than construct a shape vector just for computing its length afterwards.

In such situations it is useful to provide a default implementation and leave 
it up to each type to implement a more efficient alternative or not. With 
extend and the maps that go with it, this is easy to achieve: make a map with 
the default implementations, and merge this with the type-specific 
implementations fed to extend.

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


clojure gen-class questions

2010-02-09 Thread Аркадий Рост
Hi!
For example:

(ns exaple_class
(:gen-class))

(defn -main [args*] (...do smth...))
(defn -method_name1 [args*] (...do smth...))
(defn- -method_name2 [args*] (...do smth...))
(def -a 5)

(defn fun1 [args*] (...do smth...))
(defn- fun2 [args*] (...do smth...))
(def b 5)

As I understand, in this example we'll get java class. It's name test
and it contains methods: main, method_name1, method_name2.
What does it mean that method_name2 is declared as private?

Then what classes will contain functions fun1, fun2?
fun2 defined with defn- so it will be private, but for what does it
mean?

Also is there difference beetwen declaration -a and b? what classes
are they containing in?



-- 
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 gen-class questions

2010-02-09 Thread Meikel Brandmeyer
Hi,

On Feb 9, 4:22 pm, Аркадий Рост arkr...@gmail.com wrote:

 For example:

 (ns exaple_class
     (:gen-class))

 (defn -main [args*] (...do smth...))
 (defn -method_name1 [args*] (...do smth...))
 (defn- -method_name2 [args*] (...do smth...))
 (def -a 5)

 (defn fun1 [args*] (...do smth...))
 (defn- fun2 [args*] (...do smth...))
 (def b 5)

 As I understand, in this example we'll get java class. It's name test
 and it contains methods: main, method_name1, method_name2.

No. It just has a static method main (which is specially treated). To
make method_name1 and method_name2 you must declare so in the gen-
class clause.

(ns example.class
  (:gen-class
:methods [[method_name1 [... arg types here ...] return type here]
  [method_name1 [... arg types here ...] return type here]
  #^{:static true} [some_static_method ]]))

 What does it mean that method_name2 is declared as private?

The methods are just plain old clojure function. The real methods
are stubs which call these functions. So you can basically call the
method functions directly. If method_name2 is private this can only be
done from the same namespace. For (.method_name2 obj ...) form of
calling the private flag is not of interest and works from everywhere.

 Then what classes will contain functions fun1, fun2?

No class will contain those functions, because they are not declared
in the :gen-class clause.

 fun2 defined with defn- so it will be private, but for what does it
 mean?

See above for method_name2.

 Also is there difference beetwen declaration -a and b?

It's just a different name. Otherwise they will behave the same.

 what classes are they containing in?

No classes will contain -a or b. gen-class'd classes cannot contain
fields. You can only specify a state (via :state in the :gen-class
clause). However the state can be a map, which can hold different
other values. However it is immutable. So if you want to modify the
state you have to wrap into a ref or atom.

See also: http://clojure.org/compilation

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: clojure gen-class questions

2010-02-09 Thread Аркадий Рост
Thaks for yor answer. It's really help me with studying.

-- 
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 gen-class questions

2010-02-09 Thread Аркадий Рост
Oh...So to make methods I must declare them in gen-class clause and if
some function wasn't include in gen-class clause, it wouldn't be a
method even if it had a prefix from gen-class clause.
I mean:
(ns example.class
  (:gen-class
   :prefix pre-
   :methods [[method_name1 [... arg types here ...] return type
here]]))

(defn -main [args*] (...do smth...))
(defn pre-method_name1 [args*] (...do smth...))
(defn pre-method_name2 [args*] (...do smth...))

In example: method_name1 is a method because it is included in gen-
class clause, -method_name2 is clojure function. But what about main?
Is it a function(not method) because prefix was changed or not?

And what function's names can be used to declare class method without
including them to gen-class clause? (main and what else?)

-- 
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 gen-class questions

2010-02-09 Thread Аркадий Рост
And one more, when and why methods should be used? (except main)

-- 
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 gen-class questions

2010-02-09 Thread Brian Wolf


Hi,

I also am confused about gen-class, looking for a pointer to some code 
examples.


Thanks,

Brian

???  wrote:

Oh...So to make methods I must declare them in gen-class clause and if
some function wasn't include in gen-class clause, it wouldn't be a
method even if it had a prefix from gen-class clause.
I mean:
(ns example.class
  (:gen-class
   :prefix pre-
   :methods [[method_name1 [... arg types here ...] return type
here]]))

(defn -main [args*] (...do smth...))
(defn pre-method_name1 [args*] (...do smth...))
(defn pre-method_name2 [args*] (...do smth...))

In example: method_name1 is a method because it is included in gen-
class clause, -method_name2 is clojure function. But what about main?
Is it a function(not method) because prefix was changed or not?

And what function's names can be used to declare class method without
including them to gen-class clause? (main and what else?)

  


--
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: Detecting Number of Available CPU Threads

2010-02-09 Thread Chouser
On Mon, Feb 8, 2010 at 8:37 PM, Wardrop t...@tomwardrop.com wrote:
 That seems like what I'm after, thanks. I assume this would be pretty
 reliable across all platforms running the JVM.

 By the way, I did google the Java API with various keywords but never
 cam across this object property.

It may be worth noting that using 'send' to dispatch actions to
an agent already takes into account the number of CPUs available.
This essentially means it's safe to queue up sends on as many
agents as you want -- hundreds, even thousands of agents --
regardless of how many CPUs are on your current host.

The same is *not* true of 'send-off' or 'future', which do not
have bounds on their thread pool.

--Chouser
http://joyofclojure.com/

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


Re: Prepping clojure for packaging (was: Re: Clojure for system administration)

2010-02-09 Thread Peter Schuller
 That's exactly what Debian does. For every Java package also provide
 the maven xml file and the jar is discoverable from maven. The
 installed packages on the local system acts as a local maven repo.

  http://wiki.debian.org/Java/MavenRepoSpec

I see they also solved the problem of not downloading during build.

One should probably look into doing something similar for other
packaging systems then.

Guess I need to bite the bullet and learn Maven ;)

-- 
/ Peter Schuller

-- 
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: Trait-like behavior with Protocols

2010-02-09 Thread aria42
If this situation is common enough, shouldn't defprotocol support
optional implementations which are implicitly merged?

On Feb 9, 6:01 am, Konrad Hinsen konrad.hin...@fastmail.net wrote:
 On 09.02.2010, at 02:14, Stuart Sierra wrote:

  On Feb 8, 6:13 pm, aria42 ari...@gmail.com wrote:
  (defprotocol Span
    (start [self])
    (stop [self])
    (span-length [self]))

  Now I know I can just make span-length a function on Span as opposed
  to part of the protocol. Is that what one should do?

  Yes.

 I would say it depends.

 I have a similar situation in my multiarray package 
 (http://code.google.com/p/clj-multiarray/). In the multiarray protocol, I 
 have two functions, shape and rank, with the latter being by definition 
 the same as (comp count shape). However, I still have rank in the protocol, 
 because for some implementations it is more efficient to compute the rank 
 directly, rather than construct a shape vector just for computing its length 
 afterwards.

 In such situations it is useful to provide a default implementation and leave 
 it up to each type to implement a more efficient alternative or not. With 
 extend and the maps that go with it, this is easy to achieve: make a map with 
 the default implementations, and merge this with the type-specific 
 implementations fed to extend.

 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: clojure gen-class questions

2010-02-09 Thread Meikel Brandmeyer
Hi,

I wrote up a post: http://tr.im/NwCL

Hope it 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: clojure gen-class questions

2010-02-09 Thread Meikel Brandmeyer
Hi,

Am 09.02.2010 um 18:10 schrieb Аркадий Рост:

 Oh...So to make methods I must declare them in gen-class clause and if
 some function wasn't include in gen-class clause, it wouldn't be a
 method even if it had a prefix from gen-class clause.
 I mean:
 (ns example.class
  (:gen-class
   :prefix pre-
   :methods [[method_name1 [... arg types here ...] return type
 here]]))
 
 (defn -main [args*] (...do smth...))
 (defn pre-method_name1 [args*] (...do smth...))
 (defn pre-method_name2 [args*] (...do smth...))
 
 In example: method_name1 is a method because it is included in gen-
 class clause, -method_name2 is clojure function.

In fact both are clojure functions. However you can call (.method_name1 obj) 
but not (.method_name2 obj).

 But what about main?
 Is it a function(not method) because prefix was changed or not?

main is special. You can tell whether to generate a static main method via 
:main true or :main false in the :gen-class clause. true is the default. So 
in you example a main method will be generated, but it can't be used, because 
there is no function called pre-main. -main is a normal clojure function (as 
would be pre-main if it was defined).

 And what function's names can be used to declare class method without
 including them to gen-class clause? (main and what else?)

Only main is of that sort. And methods from interfaces and super classes don't 
have to be declared. In fact they are not allowed to be declared.

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: Trait-like behavior with Protocols

2010-02-09 Thread Konrad Hinsen

On 9 Feb 2010, at 22:29, aria42 wrote:


If this situation is common enough, shouldn't defprotocol support
optional implementations which are implicitly merged?


Yes, if it is common enough. It's perhaps too early to decide.

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