Monad newbie question

2011-01-13 Thread Saul Hazledine
Hello,
  I've never used monads but I have a problem that feels like it could
be solved elegantly with them.

 I have a sequence of functions of arbitary size and an input sequence
s. Each function is given a sequence and returns a sequence that can
be bigger than the input sequence. I want the output of one function
to be operated on using a mapcat of the next function:

(let [seq-of-fns [f1 f2 f3 ... fm]]
 (mapcat fm ... (mapcat f2 (mapcat f1 s)))

If any of the functions return nil, I'd like the computation to stop.
This seems like something that should be possible with the maybe-m in
clojure.contrib.monad but I don't understand how to do the following:

1. How do I handle an arbitary seq-of-fns [f1 f2 ... fm]?
2. How can I specify that I want a function to mapcat the return value
of the previous function?

I've read the monad tutorials linked at 
http://richhickey.github.com/clojure-contrib/monads-api.html
which have been very useful but any extra help would be appreciated.

Thanks in advance
Saul


-- 
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: Monad newbie question

2011-01-13 Thread Saul Hazledine
On Jan 13, 9:18 am, Saul Hazledine shaz...@gmail.com wrote:
 Hello,
   I've never used monads but I have a problem that feels like it could
 be solved elegantly with them.

  I have a sequence of functions of arbitary size and an input sequence
 s. Each function is given a sequence and returns a sequence that can
 be bigger than the input sequence. I want the output of one function
 to be operated on using a mapcat of the next function:


Sorry, I explained that wrong. Each function takes a single item and
returns a sequence - this is why mapcat is used.

Saul

-- 
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: When to use #'

2011-01-13 Thread Laurent PETIT
One common case also, is if you want to store in a datastructure not the
current function of a global var, but rather the var itself, so that
everytime you want to use it, you'll get the current value bound to the var,
and not the value bound that happened to be bound to the var when at the
point in time it was stored in the datastructure.

By example:

(defn toupper [s] (.toLowerCase s))
#'user/toupper
(def str-funs {:to-upper toupper})
#'user/str-funs
((:to-upper str-funs) Foo)
foo
; arg, let's change toupper definition (e.g. the root value associated to
the toupper var)
(defn toupper [s] (.toUpperCase s))
#'user/toupper
; now let's call the to-upper function ...
((:to-upper str-funs) Foo)
foo
; arg, still the old definition. Oh wait ! In the str-fun defs, toupper has
been evaluated to its root value, and I've stored a function in the
datastructure
; let's store the toupper var itself
; let's retry all this again, to be sure
(defn toupper [s] (.toLowerCase s))
#'user/toupper
(def str-funs {:to-upper #'toupper}) ; or (var toupper), if you don't like
the #' reader macro
#'user/str-funs
((:to-upper str-funs) Foo)
foo
; ok so now let's redefine toupper
(defn toupper [s] (.toUpperCase s))
#'user/toupper
((:to-upper str-funs) Foo)
FOO
; Yay !




2011/1/13 rob levy r.p.l...@gmail.com

 One common use is for referring to private functions in other namespaces.
  For example, say you want to write tests for foo.core/p, a privately
 defined function.  It is private in terms of your intent as expressed in
 your API, but you can still access the var from foo.core-test and call the
 function in your tests as #'foo.core/p.


 On Wed, Jan 12, 2011 at 10:29 PM, gaz jones gareth.e.jo...@gmail.comwrote:

 its a reader macro equivalent to the var special form:

 (var symbol)
 The symbol must resolve to a var, and the Var object itself (not its
 value) is returned. The reader macro #'x expands to (var x).

 from:

 http://clojure.org/special_forms#var

 On Wed, Jan 12, 2011 at 9:11 PM, Alex Baranosky
 alexander.barano...@gmail.com wrote:
  Hi,  I find it extremely hard to google this to learn more!  I'd like to
  know some good sources of further information on when to use #' .  It is
 a
  bit mysterious to me at this point.
 
  --
  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.comclojure%2bunsubscr...@googlegroups.com
  For more options, visit this group at
  http://groups.google.com/group/clojure?hl=en

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To 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.comclojure%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en


  --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To 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.comclojure%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en


-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To 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: Compiling dynamically created namespaces, without on-disk source code?

2011-01-13 Thread Robert McIntyre
On a related note, is there any way to completely clear all the
bytecode that's being stored internally by the jvm, forcing it to fall
back to the hard-disk version of the classfiles available on the
classpath?

sincerely,
--Robert McIntyre

On Tue, Jan 11, 2011 at 5:04 PM, Stuart Sierra
the.stuart.sie...@gmail.com wrote:
 You would have to capture the source form of each definition before it's
 compiled. Not too hard if you hack the REPL a bit; someone has probably
 already worked on it.
 -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

-- 
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 find implemented protocols in Clojure object?

2011-01-13 Thread Maurits
I tried to do it the other way around: find all symbols in a given
namespace and use your protocol? function to find out which of these
is a protocol, and next check with extenders if the class of my object
is included.

However this fails because my code to get all symbols from a
namespace:

(map #(% 0) (ns-publics *ns*))

exactly does that: it gives the symbol names. The protocol? function
doesn't work for a symbol, but needs the actual protocol as parameter.
To make myself hopefully a little bit more clear:

(defprotocol P (bar [x]))
(println (isprotocol? P))

prints true

(map #(isprotocol? (% 0)) (ns-publics *ns*)))

just returns sequence which only contains false values.

Maurits

On Jan 12, 10:48 pm, Bob Hutchison hutch-li...@recursive.ca wrote:
 On 2011-01-12, at 8:38 AM, Stuart Sierra wrote:

  One way:

      (ancestors (type the-object))

  This will include every interface implemented by the object, including 
  protocols.

 This seems to only work if the protocol is mentioned in the defrecord. If 
 it's applied using the extend function it doesn't seem to show the protocol.

 The functions bases and supers work similarly.

 Unless I'm doing something really stupid, which is a distinct possibility.

 Any idea how to tell which of the classes returned by ancestors/bases/supers 
 are protocols? I copied a function from the source that can tell if something 
 is a protocol if written directly in clojure, but does not work on the 
 results from those methods.

 (defn protocol?
   [maybe-p]
   (boolean (:on-interface maybe-p)))

 Cheers,
 Bob



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

 
 Bob Hutchison
 Recursive Design Inc.http://www.recursive.ca/
 weblog:http://xampl.com/so

-- 
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 find implemented protocols in Clojure object?

2011-01-13 Thread Meikel Brandmeyer
Hi,

Try this:
(map #(isprotocol? @(val %)) (ns-publics *ns*)))

However just checking extenders is not enough, because it does not
contain classes, which implement the protocol directly.

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: [ANN] fs - file system utilities for Clojure

2011-01-13 Thread Tim Visher
I'm missing something blindingly obvious. Where can I download this?

On Wed, Jan 12, 2011 at 3:48 PM, Miki miki.teb...@gmail.com wrote:
 [fs 0.2.0-SNAPSHOT] is out, featuring:

 abspath
     Return absolute path
 basename
     Return the last part of path
 copy
     Copy a file
 cwd
     Return the current working directory
 delete
     Delete path
 directory?
     True if path is a directory
 dirname
     Return directory name
 executable?
     Check if path is executable
 exists?
     Check if path exists
 file?
     True if path is a file
 glob
     `ls` like operator
 join
     Join part to path
 listdir
     List files under directory
 mkdir
     Create directory
 mtime
     File modification time
 mkdirs
     Create directory tree
 normpath
     Return normalized (canonical) path
 readable?
     Check if path is readable
 rename
     Rename path
 separator
     Path separator
 size
     File size
 split
     Split path to parts
 tempdir
     Create temporary directory
 tempfile
     Create temporary file
 walk
     Walk over directory structure, calling function on every step
 writeable?
     Check if path is writable

 Have fun,
 --
 Miki

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

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To 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 find implemented protocols in Clojure object?

2011-01-13 Thread Maurits
Thanks for your answer. I guess the right approach is to check with
extends? against the list of protocols that I found in the namespace.

Maurits

On Jan 13, 2:00 pm, Meikel Brandmeyer m...@kotka.de wrote:
 Hi,

 Try this:
 (map #(isprotocol? @(val %)) (ns-publics *ns*)))

 However just checking extenders is not enough, because it does not
 contain classes, which implement the protocol directly.

 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: How do I find implemented protocols in Clojure object?

2011-01-13 Thread Meikel Brandmeyer
Hi,

On 13 Jan., 14:15, Maurits maurits.r...@gmail.com wrote:
 Thanks for your answer. I guess the right approach is to check with
 extends? against the list of protocols that I found in the namespace.

Actually satisfies? with an instance of the class in question.

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: fs - file system utilities for Clojure

2011-01-13 Thread Meikel Brandmeyer
Hi,

On 12 Jan., 21:48, Miki miki.teb...@gmail.com wrote:

 [fs 0.2.0-SNAPSHOT] is out

A SNAPSHOT is out? Please don't do this. If it is a release, get rid
of the SNAPSHOT. :(

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: [ANN] fs - file system utilities for Clojure

2011-01-13 Thread gaz jones
probably clojars.org (or by putting [fs 0.2.0-SNAPSHOT] in your
project.clj if you're using lein...

On Thu, Jan 13, 2011 at 7:11 AM, Tim Visher tim.vis...@gmail.com wrote:
 I'm missing something blindingly obvious. Where can I download this?

 On Wed, Jan 12, 2011 at 3:48 PM, Miki miki.teb...@gmail.com wrote:
 [fs 0.2.0-SNAPSHOT] is out, featuring:

 abspath
     Return absolute path
 basename
     Return the last part of path
 copy
     Copy a file
 cwd
     Return the current working directory
 delete
     Delete path
 directory?
     True if path is a directory
 dirname
     Return directory name
 executable?
     Check if path is executable
 exists?
     Check if path exists
 file?
     True if path is a file
 glob
     `ls` like operator
 join
     Join part to path
 listdir
     List files under directory
 mkdir
     Create directory
 mtime
     File modification time
 mkdirs
     Create directory tree
 normpath
     Return normalized (canonical) path
 readable?
     Check if path is readable
 rename
     Rename path
 separator
     Path separator
 size
     File size
 split
     Split path to parts
 tempdir
     Create temporary directory
 tempfile
     Create temporary file
 walk
     Walk over directory structure, calling function on every step
 writeable?
     Check if path is writable

 Have fun,
 --
 Miki

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

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

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To 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: fs - file system utilities for Clojure

2011-01-13 Thread Miki


  [fs 0.2.0-SNAPSHOT] is out 

 A SNAPSHOT is out? Please don't do this. If it is a release, get rid 
 of the SNAPSHOT. :( 

 Done, please use [fs 0.2.0] :)

All the best,
--
Miki

-- 
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 find implemented protocols in Clojure object?

2011-01-13 Thread Maurits
Thanks for all the feedback and answers.

I have added my implementation below.

Maurits
-

; test protocol and record

(defprotocol Log
  (log [this] Method to log))

(defrecord Foo [f1 f2]
  Log
  (log [this] (println (:f1 this) (:f2 this

(def foo (Foo. 13 42))
(log foo)

; the relevant stuff

(defn protocol? [maybe-p]
  (boolean (:on-interface maybe-p)))

(defn all-protocols []
  (filter #(protocol? @(val %)) (ns-publics *ns*)))

(defn implemented-protocols [sym]
  (filter #(satisfies? @(val %) sym) (all-protocols)))

(println (implemented-protocols foo))

-- 
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: [ANN] fs - file system utilities for Clojure

2011-01-13 Thread Miki
 I'm missing something blindingly obvious. Where can I download this?
 probably clojars.org (or by putting [fs 0.2.0-SNAPSHOT] in your
 project.clj if you're using lein...
Yup, it's in clojars. However if you prefer to download the jar manually, 
you can
get it from https://bitbucket.org/tebeka/fs/downloads/fs-0.2.0.jar

All the best,
--
Miki

-- 
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: [ANN] fs - file system utilities for Clojure

2011-01-13 Thread Laurent PETIT
2011/1/13 Miki miki.teb...@gmail.com

  I'm missing something blindingly obvious. Where can I download this?
  probably clojars.org (or by putting [fs 0.2.0-SNAPSHOT] in your
  project.clj if you're using lein...
 Yup, it's in clojars. However if you prefer to download the jar manually,
 you can
 get it from https://bitbucket.org/tebeka/fs/downloads/fs-0.2.0.jar



Too bad the artifact name f was already taken, and you had to resort to
using a sooo long name as fs :-p

Just kidding ;)

-- 
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: [ANN] fs - file system utilities for Clojure

2011-01-13 Thread Steve Miner
Thanks for shaing.  I was just about to write several functions along these 
lines.  I have a couple of comments.

First, I suggest that you look at the standard clojure.java.io package for some 
useful functions that are already in Clojure 1.2.  In particular, you could use 
io/file instead of (File. xxx) in your code to add some flexibility to the 
kinds of things that can be treated as a file.  I think io/file could replace 
your fs/join. Also, io/copy is very flexible so it's worth a look, too.

Second, fs is using a singe segment namespace.  I remember that there have been 
some cautions against doing that. (But not everyone agrees.)  My understanding 
is that it's best for Java interop to have a multi-segment namespace.  
(Reference links below.)

http://clojure.org/libs

 A lib name is a symbol that will typically contain two or more parts 
 separated by periods.

http://groups.google.com/group/clojure-dev/browse_frm/thread/00b1c6971c3b3394

Chas Emerick wrote:
 First, namespacing is good.  Your foobar library won't have the same name as 
 my foobar library -- and while you might think who else would put code in 
 the same oddly-named namespace as mine?, it's a big world out there and an 
 ounce of prevention is worth a pound of cure. More strictly speaking, one 
 cannot use any class in the default package from any Java class that is in a 
 package.  That is the practical issue that leads to default package use being 
 nonexistent in the Java space. And, you may not care about Java interop now, 
 but either (a) you might later, or (b) your users might, now.  Finally, 
 gen-class will simply not work (last I checked) from a single-segment 
 namespace. 


Best Regards,
Steve Miner
stevemi...@gmail.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


Question On Implicit Recursion

2011-01-13 Thread Vitaly Peressada
The following solution by bmtgred/b for a href=http://clojure-
euler.wikispaces.com/Project Euler Clojure/a problem 003 uses
implicit recursion.

pre
(use '[clojure.contrib.lazy-seqs :only (primes)])
(defn prime-factors [n]
  (let [f (some #(if (= 0 (rem n %)) %) primes)]
(if (= f n) #{f} (conj (prime-factors (/ n f)) f
(apply max (prime-factors 600851475143))
/pre

Here is above with added println

(defn prime-factors [n]
  (let [f (some #(if (= 0 (rem n %)) %) primes)]
(println n: n , f: f)
(if (= f n)
  #{f}
  (conj (prime-factors (/ n f)) f

Which produces

n: 600851475143 , f: 71
n: 8462696833 , f: 839
n: 10086647 , f: 1471
n: 6857 , f: 6857
#{71 839 6857 1471}

Can anybody explain why 6857 comes 3rd? I would expect to be the last.

-- 
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: Question On Implicit Recursion

2011-01-13 Thread Armando Blancas
A literal set is a unordered hash-set. To get the factors in order
change #{f} for (sorted-set f).

On Jan 13, 7:09 am, Vitaly Peressada vit...@ufairsoft.com wrote:
 The following solution by bmtgred/b for a href=http://clojure-
 euler.wikispaces.com/Project Euler Clojure/a problem 003 uses
 implicit recursion.

 pre
 (use '[clojure.contrib.lazy-seqs :only (primes)])
 (defn prime-factors [n]
   (let [f (some #(if (= 0 (rem n %)) %) primes)]
     (if (= f n) #{f} (conj (prime-factors (/ n f)) f
 (apply max (prime-factors 600851475143))
 /pre

 Here is above with added println

 (defn prime-factors [n]
   (let [f (some #(if (= 0 (rem n %)) %) primes)]
     (println n: n , f: f)
     (if (= f n)
       #{f}
       (conj (prime-factors (/ n f)) f

 Which produces

 n: 600851475143 , f: 71
 n: 8462696833 , f: 839
 n: 10086647 , f: 1471
 n: 6857 , f: 6857
 #{71 839 6857 1471}

 Can anybody explain why 6857 comes 3rd? I would expect to be the last.

-- 
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: [ANN] fs - file system utilities for Clojure

2011-01-13 Thread gaz jones
there is also this:

https://github.com/jashmenn/clj-file-utils

which seems to be very similar

On Thu, Jan 13, 2011 at 9:04 AM, Steve Miner stevemi...@gmail.com wrote:
 Thanks for shaing.  I was just about to write several functions along these 
 lines.  I have a couple of comments.

 First, I suggest that you look at the standard clojure.java.io package for 
 some useful functions that are already in Clojure 1.2.  In particular, you 
 could use io/file instead of (File. xxx) in your code to add some flexibility 
 to the kinds of things that can be treated as a file.  I think io/file 
 could replace your fs/join. Also, io/copy is very flexible so it's worth a 
 look, too.

 Second, fs is using a singe segment namespace.  I remember that there have 
 been some cautions against doing that. (But not everyone agrees.)  My 
 understanding is that it's best for Java interop to have a multi-segment 
 namespace.  (Reference links below.)

 http://clojure.org/libs

 A lib name is a symbol that will typically contain two or more parts 
 separated by periods.

 http://groups.google.com/group/clojure-dev/browse_frm/thread/00b1c6971c3b3394

 Chas Emerick wrote:
 First, namespacing is good.  Your foobar library won't have the same name as 
 my foobar library -- and while you might think who else would put code in 
 the same oddly-named namespace as mine?, it's a big world out there and an 
 ounce of prevention is worth a pound of cure. More strictly speaking, one 
 cannot use any class in the default package from any Java class that is in a 
 package.  That is the practical issue that leads to default package use 
 being nonexistent in the Java space. And, you may not care about Java 
 interop now, but either (a) you might later, or (b) your users might, now.  
 Finally, gen-class will simply not work (last I checked) from a 
 single-segment namespace.


 Best Regards,
 Steve Miner
 stevemi...@gmail.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

-- 
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 namespaces, Cobol, and Hierarchical file systems

2011-01-13 Thread Tim Daly

  Second, fs is using a singe segment namespace. I remember that
 there have been some cautions against doing that. (But not everyone
 agrees.) My understanding is that it's best for Java interop to have a
 multi-segment namespace. (Reference links below.) 
http://clojure.org/libs

A lib name is a symbol that will typically contain two or more parts separated 
by periods.

http://groups.google.com/group/clojure-dev/browse_frm/thread/00b1c6971c3b3394

Chas Emerick wrote:

First, namespacing is good.  Your foobar library won't have the same name as my foobar 
library -- and while you might think who else would put code in the same 
oddly-named namespace as mine?, it's a big world out there and an ounce of 
prevention is worth a pound of cure. More strictly speaking, one cannot use any class in 
the default package from any Java class that is in a package.  That is the practical 
issue that leads to default package use being nonexistent in the Java space. And, you may 
not care about Java interop now, but either (a) you might later, or (b) your users might, 
now.  Finally, gen-class will simply not work (last I checked) from a single-segment 
namespace.

As a greybeard in the game, this all sounds so familiar.
The point of this post is to suggest that Clojure should
NOT adopt java naming conventions.

Back in the days of Cobol, before it got renamed to Java,
we used to construct hierarchical databases. For those who
don't know about them, they would specifiy the path to every
element in the database in the name of the element. Thus, you
would say 
world.country.company.site.disk.dbname.dept.bldg.aisle.office.person

or some such.

It was argued that this was necessary to ensure that
you always got a unique element and that programs would not clash.
It was also very efficient since the worldwide path to a point
was always unique (as opposed to the horribly inefficient idea
of using table and relational algebra to look up stuff at runtime!
Why the ambiguity will cause world confusion and the runtime will
be impossibly long! Just write out the long name! Sigh.)

There are a couple of items worth mentioning. First, notice
that everything is fine until the world changes. Now there
are hardcoded legacy assumptions built into the name (e.g.
the person is in a different building so we have to reformat
the database to pay them). Consider that the Java naming
convention does that... java.sun.com ... who is sun?
I never heard of them.

Second, but similar, should I name my classes java.daly.tim.freesource?
Suppose I get married and change my name? Suppose someone with a
similar name writes a package. Does my son have to write
java.jr.daly.tim.freesource?

Third, suppose I refactor my program. Now I have to rename every
class to fit the new hierarchy. But that will break old programs
that use my package. Who could possibly want to refactor deep
enough to shuffle classes?

Fourth, this hierarchical thinking strongly encourages making
functions that are class-specific. Thus you find that java has
hundreds of sorts, one per class.

Fifth, one writes the long.drawn.out.name to satify the
compiler's inability to find anything despite the class name
being unique. In fact, the compiler is insisting that your
class name match the name of the file and it STILL insists
that you use unique names so it doesn't have to search the
filesystem. The side-effects of this are that your class hierarchy
is rigid (see refactoring above), the name assumes a hierarchical
file system, and the ultimate issue... the user has to use an IDE
to find something simple as a sort.

Sixth, programs rarely need to deal with the whole world.
Why would you have qualifiers on a per-company basis just
to guarantee uniqueness of the name of your sort function?

In lisp one ends up with hundreds of functions in the same
namespace. Because the functions are not type-specific
they tend to be very broad and general. It is rare to have
more than one sort and people don't generally write sort
functions since they are too standard.

A simple namespace qualifier, like fs, is sufficient to
handle large, general purpose packages which contain functions
to handle file systems.

Since lisp uses naked functions there is no natural map from
functions to filenames.

In sum, I'm suggesting that it isn't very lispy to use
hierarchical namespace naming conventions.

Tim Daly

--
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: Question On Implicit Recursion

2011-01-13 Thread Vitaly Peressada
Armando, thanks for a plausible explanation. Here is what happened
after I made the suggested change:

user= (prime-factors 600851475143)
n: 600851475143 , f: 71
n: 8462696833 , f: 839
n: 10086647 , f: 1471
n: 6857 , f: 6857
#CompilerException java.lang.ClassCastException: java.lang.Integer
cannot be cast to clojure.lang.IFn (REPL:91)

I guess this something subtle with unordered vs. sorted-set as
implicit factors accumulator. May be that is why the author of the
solution had to use unordered set followed by (apply max ...).

On Jan 13, 11:23 am, Armando Blancas armando_blan...@yahoo.com
wrote:
 A literal set is a unordered hash-set. To get the factors in order
 change #{f} for (sorted-set f).

 On Jan 13, 7:09 am, Vitaly Peressada vit...@ufairsoft.com wrote:







  The following solution by bmtgred/b for a href=http://clojure-
  euler.wikispaces.com/Project Euler Clojure/a problem 003 uses
  implicit recursion.

  pre
  (use '[clojure.contrib.lazy-seqs :only (primes)])
  (defn prime-factors [n]
    (let [f (some #(if (= 0 (rem n %)) %) primes)]
      (if (= f n) #{f} (conj (prime-factors (/ n f)) f
  (apply max (prime-factors 600851475143))
  /pre

  Here is above with added println

  (defn prime-factors [n]
    (let [f (some #(if (= 0 (rem n %)) %) primes)]
      (println n: n , f: f)
      (if (= f n)
        #{f}
        (conj (prime-factors (/ n f)) f

  Which produces

  n: 600851475143 , f: 71
  n: 8462696833 , f: 839
  n: 10086647 , f: 1471
  n: 6857 , f: 6857
  #{71 839 6857 1471}

  Can anybody explain why 6857 comes 3rd? I would expect to be the last.

-- 
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 find implemented protocols in Clojure object?

2011-01-13 Thread Daniel Werner
On 13 January 2011 15:52, Maurits maurits.r...@gmail.com wrote:
 (defn all-protocols []
  (filter #(protocol? @(val %)) (ns-publics *ns*)))

 (defn implemented-protocols [sym]
  (filter #(satisfies? @(val %) sym) (all-protocols)))

Of course, this will restrict your search to protocols in a given namespace.

It might make sense to also add defrecords and deftypes to the
protocol's :impls map if they explicitly extend the protocol at
definition time, or to a new :direct-impls set, and make (extenders P)
return a union of these so it could always return all implementors of
protocol P. Or would this entail problems I don't see at the moment?

Daniel

-- 
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: Question On Implicit Recursion

2011-01-13 Thread Benny Tsai
Armando's suggested change worked fine for me.

(use '[clojure.contrib.lazy-seqs :only (primes)])

(defn prime-factors [n]
  (let [f (some #(if (= 0 (rem n %)) %) primes)]
(println n: n , f: f)
(if (= f n)
  (sorted-set f)
  (conj (prime-factors (/ n f)) f

user= (prime-factors 600851475143)
n: 600851475143 , f: 71
n: 8462696833 , f: 839
n: 10086647 , f: 1471
n: 6857 , f: 6857
#{71 839 1471 6857}

On Jan 13, 10:01 am, Vitaly Peressada vit...@ufairsoft.com wrote:
 Armando, thanks for a plausible explanation. Here is what happened
 after I made the suggested change:

 user= (prime-factors 600851475143)
 n: 600851475143 , f: 71
 n: 8462696833 , f: 839
 n: 10086647 , f: 1471
 n: 6857 , f: 6857
 #CompilerException java.lang.ClassCastException: java.lang.Integer
 cannot be cast to clojure.lang.IFn (REPL:91)

 I guess this something subtle with unordered vs. sorted-set as
 implicit factors accumulator. May be that is why the author of the
 solution had to use unordered set followed by (apply max ...).

 On Jan 13, 11:23 am, Armando Blancas armando_blan...@yahoo.com
 wrote:







  A literal set is a unordered hash-set. To get the factors in order
  change #{f} for (sorted-set f).

  On Jan 13, 7:09 am, Vitaly Peressada vit...@ufairsoft.com wrote:

   The following solution by bmtgred/b for a href=http://clojure-
   euler.wikispaces.com/Project Euler Clojure/a problem 003 uses
   implicit recursion.

   pre
   (use '[clojure.contrib.lazy-seqs :only (primes)])
   (defn prime-factors [n]
     (let [f (some #(if (= 0 (rem n %)) %) primes)]
       (if (= f n) #{f} (conj (prime-factors (/ n f)) f
   (apply max (prime-factors 600851475143))
   /pre

   Here is above with added println

   (defn prime-factors [n]
     (let [f (some #(if (= 0 (rem n %)) %) primes)]
       (println n: n , f: f)
       (if (= f n)
         #{f}
         (conj (prime-factors (/ n f)) f

   Which produces

   n: 600851475143 , f: 71
   n: 8462696833 , f: 839
   n: 10086647 , f: 1471
   n: 6857 , f: 6857
   #{71 839 6857 1471}

   Can anybody explain why 6857 comes 3rd? I would expect to be the last.

-- 
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: Question On Implicit Recursion

2011-01-13 Thread Vitaly Peressada
Benny, thanks for your explanation. It does make sense and clarifies
the issue. What DOES NOT make sense is

Clojure 1.2.0
user=  (conj (conj (conj #{4} 3) 2) 1)
#{1 2 3 4}
user=  (conj (conj (conj #{6857} 1471) 839) 71)
#{71 839 6857 1471}

Any ideas?

On Jan 13, 11:36 am, Benny Tsai benny.t...@gmail.com wrote:
 (conj) will add items to different places in the collection, depending
 on the type of the collection.  For a set, this can be at either the
 end OR the beginning.

 For this problem, the output is built by:

 (conj (conj (conj #{6857} 1471) 839) 71)

 which is equivalent to:

 (- #{6857} (conj 1471) (conj 839) (conj 71))

 The innermost/first conj, (conj #{6857} 1471), sticks 1471 at the end
 and produces #{6857 1471}.  The next conj sticks 839 at the beginning
 and produces #{839 6857 1471}.  The final conj sticks 71 at the
 beginning and produces the final result of #{71 839 6857 1471}.

 When ordering matters, I usually use either (cons), which will always
 add to the beginning regardless of the collection type, or use (conj)
 with a list (will always add to the beginning) or vector (will always
 add to the end).

 user= (- '(6857) (conj 1471) (conj 839) (conj 71))
 (71 839 1471 6857)
 user= (- [6857] (conj 1471) (conj 839) (conj 71))
 [6857 1471 839 71]

 On Jan 13, 8:09 am, Vitaly Peressada vit...@ufairsoft.com wrote:







  The following solution by bmtgred/b for a href=http://clojure-
  euler.wikispaces.com/Project Euler Clojure/a problem 003 uses
  implicit recursion.

  pre
  (use '[clojure.contrib.lazy-seqs :only (primes)])
  (defn prime-factors [n]
    (let [f (some #(if (= 0 (rem n %)) %) primes)]
      (if (= f n) #{f} (conj (prime-factors (/ n f)) f
  (apply max (prime-factors 600851475143))
  /pre

  Here is above with added println

  (defn prime-factors [n]
    (let [f (some #(if (= 0 (rem n %)) %) primes)]
      (println n: n , f: f)
      (if (= f n)
        #{f}
        (conj (prime-factors (/ n f)) f

  Which produces

  n: 600851475143 , f: 71
  n: 8462696833 , f: 839
  n: 10086647 , f: 1471
  n: 6857 , f: 6857
  #{71 839 6857 1471}

  Can anybody explain why 6857 comes 3rd? I would expect to be the last.

-- 
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: Question On Implicit Recursion

2011-01-13 Thread Vitaly Peressada
Replying to myself :-)

After some pondering, it DOES make sense if I pay attention to what
you guys saying: for unordered set conj can nondeterministically add
either at beginning OR end.

On Jan 13, 12:40 pm, Vitaly Peressada vit...@ufairsoft.com wrote:
 Benny, thanks for your explanation. It does make sense and clarifies
 the issue. What DOES NOT make sense is

 Clojure 1.2.0
 user=  (conj (conj (conj #{4} 3) 2) 1)
 #{1 2 3 4}
 user=  (conj (conj (conj #{6857} 1471) 839) 71)
 #{71 839 6857 1471}

 Any ideas?

 On Jan 13, 11:36 am, Benny Tsai benny.t...@gmail.com wrote:







  (conj) will add items to different places in the collection, depending
  on the type of the collection.  For a set, this can be at either the
  end OR the beginning.

  For this problem, the output is built by:

  (conj (conj (conj #{6857} 1471) 839) 71)

  which is equivalent to:

  (- #{6857} (conj 1471) (conj 839) (conj 71))

  The innermost/first conj, (conj #{6857} 1471), sticks 1471 at the end
  and produces #{6857 1471}.  The next conj sticks 839 at the beginning
  and produces #{839 6857 1471}.  The final conj sticks 71 at the
  beginning and produces the final result of #{71 839 6857 1471}.

  When ordering matters, I usually use either (cons), which will always
  add to the beginning regardless of the collection type, or use (conj)
  with a list (will always add to the beginning) or vector (will always
  add to the end).

  user= (- '(6857) (conj 1471) (conj 839) (conj 71))
  (71 839 1471 6857)
  user= (- [6857] (conj 1471) (conj 839) (conj 71))
  [6857 1471 839 71]

  On Jan 13, 8:09 am, Vitaly Peressada vit...@ufairsoft.com wrote:

   The following solution by bmtgred/b for a href=http://clojure-
   euler.wikispaces.com/Project Euler Clojure/a problem 003 uses
   implicit recursion.

   pre
   (use '[clojure.contrib.lazy-seqs :only (primes)])
   (defn prime-factors [n]
     (let [f (some #(if (= 0 (rem n %)) %) primes)]
       (if (= f n) #{f} (conj (prime-factors (/ n f)) f
   (apply max (prime-factors 600851475143))
   /pre

   Here is above with added println

   (defn prime-factors [n]
     (let [f (some #(if (= 0 (rem n %)) %) primes)]
       (println n: n , f: f)
       (if (= f n)
         #{f}
         (conj (prime-factors (/ n f)) f

   Which produces

   n: 600851475143 , f: 71
   n: 8462696833 , f: 839
   n: 10086647 , f: 1471
   n: 6857 , f: 6857
   #{71 839 6857 1471}

   Can anybody explain why 6857 comes 3rd? I would expect to be the last.

-- 
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: Question On Implicit Recursion

2011-01-13 Thread B Smith-Mannschott
On Thu, Jan 13, 2011 at 17:36, Benny Tsai benny.t...@gmail.com wrote:
 (conj) will add items to different places in the collection, depending
 on the type of the collection.  For a set, this can be at either the
 end OR the beginning.

For a (hashed) set, this can be *anywhere* (between existing elements too).

FTFY

// Ben

 For this problem, the output is built by:

 (conj (conj (conj #{6857} 1471) 839) 71)

 which is equivalent to:

 (- #{6857} (conj 1471) (conj 839) (conj 71))

 The innermost/first conj, (conj #{6857} 1471), sticks 1471 at the end
 and produces #{6857 1471}.  The next conj sticks 839 at the beginning
 and produces #{839 6857 1471}.  The final conj sticks 71 at the
 beginning and produces the final result of #{71 839 6857 1471}.

 When ordering matters, I usually use either (cons), which will always
 add to the beginning regardless of the collection type, or use (conj)
 with a list (will always add to the beginning) or vector (will always
 add to the end).

 user= (- '(6857) (conj 1471) (conj 839) (conj 71))
 (71 839 1471 6857)
 user= (- [6857] (conj 1471) (conj 839) (conj 71))
 [6857 1471 839 71]

 On Jan 13, 8:09 am, Vitaly Peressada vit...@ufairsoft.com wrote:
 The following solution by bmtgred/b for a href=http://clojure-
 euler.wikispaces.com/Project Euler Clojure/a problem 003 uses
 implicit recursion.

 pre
 (use '[clojure.contrib.lazy-seqs :only (primes)])
 (defn prime-factors [n]
   (let [f (some #(if (= 0 (rem n %)) %) primes)]
     (if (= f n) #{f} (conj (prime-factors (/ n f)) f
 (apply max (prime-factors 600851475143))
 /pre

 Here is above with added println

 (defn prime-factors [n]
   (let [f (some #(if (= 0 (rem n %)) %) primes)]
     (println n: n , f: f)
     (if (= f n)
       #{f}
       (conj (prime-factors (/ n f)) f

 Which produces

 n: 600851475143 , f: 71
 n: 8462696833 , f: 839
 n: 10086647 , f: 1471
 n: 6857 , f: 6857
 #{71 839 6857 1471}

 Can anybody explain why 6857 comes 3rd? I would expect to be the last.

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

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To 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: Question On Implicit Recursion

2011-01-13 Thread Vitaly Peressada
Right, my bad. It was a typo. (sorted-set (f))
Thanks for your help.

On Jan 13, 12:16 pm, Benny Tsai benny.t...@gmail.com wrote:
 Armando's suggested change worked fine for me.

 (use '[clojure.contrib.lazy-seqs :only (primes)])

 (defn prime-factors [n]
   (let [f (some #(if (= 0 (rem n %)) %) primes)]
     (println n: n , f: f)
     (if (= f n)
       (sorted-set f)
       (conj (prime-factors (/ n f)) f

 user= (prime-factors 600851475143)
 n: 600851475143 , f: 71
 n: 8462696833 , f: 839
 n: 10086647 , f: 1471
 n: 6857 , f: 6857
 #{71 839 1471 6857}

 On Jan 13, 10:01 am, Vitaly Peressada vit...@ufairsoft.com wrote:







  Armando, thanks for a plausible explanation. Here is what happened
  after I made the suggested change:

  user= (prime-factors 600851475143)
  n: 600851475143 , f: 71
  n: 8462696833 , f: 839
  n: 10086647 , f: 1471
  n: 6857 , f: 6857
  #CompilerException java.lang.ClassCastException: java.lang.Integer
  cannot be cast to clojure.lang.IFn (REPL:91)

  I guess this something subtle with unordered vs. sorted-set as
  implicit factors accumulator. May be that is why the author of the
  solution had to use unordered set followed by (apply max ...).

  On Jan 13, 11:23 am, Armando Blancas armando_blan...@yahoo.com
  wrote:

   A literal set is a unordered hash-set. To get the factors in order
   change #{f} for (sorted-set f).

   On Jan 13, 7:09 am, Vitaly Peressada vit...@ufairsoft.com wrote:

The following solution by bmtgred/b for a href=http://clojure-
euler.wikispaces.com/Project Euler Clojure/a problem 003 uses
implicit recursion.

pre
(use '[clojure.contrib.lazy-seqs :only (primes)])
(defn prime-factors [n]
  (let [f (some #(if (= 0 (rem n %)) %) primes)]
    (if (= f n) #{f} (conj (prime-factors (/ n f)) f
(apply max (prime-factors 600851475143))
/pre

Here is above with added println

(defn prime-factors [n]
  (let [f (some #(if (= 0 (rem n %)) %) primes)]
    (println n: n , f: f)
    (if (= f n)
      #{f}
      (conj (prime-factors (/ n f)) f

Which produces

n: 600851475143 , f: 71
n: 8462696833 , f: 839
n: 10086647 , f: 1471
n: 6857 , f: 6857
#{71 839 6857 1471}

Can anybody explain why 6857 comes 3rd? I would expect to be the last.

-- 
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: Question On Implicit Recursion

2011-01-13 Thread Benny Tsai
Good to know, thank you Ben :)

On Jan 13, 10:49 am, B Smith-Mannschott bsmith.o...@gmail.com wrote:
 On Thu, Jan 13, 2011 at 17:36, Benny Tsai benny.t...@gmail.com wrote:
  (conj) will add items to different places in the collection, depending
  on the type of the collection.  For a set, this can be at either the
  end OR the beginning.

 For a (hashed) set, this can be *anywhere* (between existing elements too).

 FTFY

 // Ben







  For this problem, the output is built by:

  (conj (conj (conj #{6857} 1471) 839) 71)

  which is equivalent to:

  (- #{6857} (conj 1471) (conj 839) (conj 71))

  The innermost/first conj, (conj #{6857} 1471), sticks 1471 at the end
  and produces #{6857 1471}.  The next conj sticks 839 at the beginning
  and produces #{839 6857 1471}.  The final conj sticks 71 at the
  beginning and produces the final result of #{71 839 6857 1471}.

  When ordering matters, I usually use either (cons), which will always
  add to the beginning regardless of the collection type, or use (conj)
  with a list (will always add to the beginning) or vector (will always
  add to the end).

  user= (- '(6857) (conj 1471) (conj 839) (conj 71))
  (71 839 1471 6857)
  user= (- [6857] (conj 1471) (conj 839) (conj 71))
  [6857 1471 839 71]

  On Jan 13, 8:09 am, Vitaly Peressada vit...@ufairsoft.com wrote:
  The following solution by bmtgred/b for a href=http://clojure-
  euler.wikispaces.com/Project Euler Clojure/a problem 003 uses
  implicit recursion.

  pre
  (use '[clojure.contrib.lazy-seqs :only (primes)])
  (defn prime-factors [n]
    (let [f (some #(if (= 0 (rem n %)) %) primes)]
      (if (= f n) #{f} (conj (prime-factors (/ n f)) f
  (apply max (prime-factors 600851475143))
  /pre

  Here is above with added println

  (defn prime-factors [n]
    (let [f (some #(if (= 0 (rem n %)) %) primes)]
      (println n: n , f: f)
      (if (= f n)
        #{f}
        (conj (prime-factors (/ n f)) f

  Which produces

  n: 600851475143 , f: 71
  n: 8462696833 , f: 839
  n: 10086647 , f: 1471
  n: 6857 , f: 6857
  #{71 839 6857 1471}

  Can anybody explain why 6857 comes 3rd? I would expect to be the last.

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

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To 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: Monad newbie question

2011-01-13 Thread Ken Wesson
On Thu, Jan 13, 2011 at 3:18 AM, Saul Hazledine shaz...@gmail.com wrote:
 Hello,
  I've never used monads but I have a problem that feels like it could
 be solved elegantly with them.

  I have a sequence of functions of arbitary size and an input sequence
 s. Each function is given a sequence and returns a sequence that can
 be bigger than the input sequence. I want the output of one function
 to be operated on using a mapcat of the next function:

    (let [seq-of-fns [f1 f2 f3 ... fm]]
         (mapcat fm ... (mapcat f2 (mapcat f1 s)))

 If any of the functions return nil, I'd like the computation to stop.

I don't see any real reason not to use

(reduce #(if %1 (mapcat %2 %1)) s seq-of-fns)

unless you have another requirement you haven't told us.

-- 
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: Monad newbie question

2011-01-13 Thread Saul Hazledine
On Jan 13, 7:35 pm, Ken Wesson kwess...@gmail.com wrote:

     (let [seq-of-fns [f1 f2 f3 ... fm]]
          (mapcat fm ... (mapcat f2 (mapcat f1 s)))

  If any of the functions return nil, I'd like the computation to stop.

 I don't see any real reason not to use

 (reduce #(if %1 (mapcat %2 %1)) s seq-of-fns)



That seems to work nicely thanks. I hadn't thought of using reduce to
produce a sequence before.

Saul

-- 
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: Monad newbie question

2011-01-13 Thread Meikel Brandmeyer
Hi,

depending on the length of your function sequence you might run into stack 
overflows, though. Just keep that in mind (or add a tactically placed doall).

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: Java namespaces, Cobol, and Hierarchical file systems

2011-01-13 Thread Chas Emerick

On Jan 13, 2011, at 11:54 AM, Tim Daly wrote:

  Second, fs is using a singe segment namespace. I remember that
  there have been some cautions against doing that. (But not everyone
  agrees.) My understanding is that it's best for Java interop to have a
  multi-segment namespace. (Reference links below.) http://clojure.org/libs
 A lib name is a symbol that will typically contain two or more parts 
 separated by periods.
 http://groups.google.com/group/clojure-dev/browse_frm/thread/00b1c6971c3b3394
 
 Chas Emerick wrote:
 First, namespacing is good.  Your foobar library won't have the same name 
 as my foobar library -- and while you might think who else would put code 
 in the same oddly-named namespace as mine?, it's a big world out there and 
 an ounce of prevention is worth a pound of cure. More strictly speaking, 
 one cannot use any class in the default package from any Java class that is 
 in a package.  That is the practical issue that leads to default package 
 use being nonexistent in the Java space. And, you may not care about Java 
 interop now, but either (a) you might later, or (b) your users might, now.  
 Finally, gen-class will simply not work (last I checked) from a 
 single-segment namespace.
 As a greybeard in the game, this all sounds so familiar.
 The point of this post is to suggest that Clojure should
 NOT adopt java naming conventions.

...

 Since lisp uses naked functions there is no natural map from
 functions to filenames.
 
 In sum, I'm suggesting that it isn't very lispy to use
 hierarchical namespace naming conventions.

Just to clarify my position (it's funny to see one's self quoted out of the 
blue from an old thread!), I'm not at all suggesting java naming conventions 
when it comes to namespacing.  (Although I'd note that Clojure *is* a 
fundamentally hosted language, and so certain idioms do flow from that host; 
CamelCase naming conventions for records, types, and protocols comes to mind.)  
No one wants to work with com.foo.bar.baz.factory.factory.factories.Factory, 
etc.

While there is no natural mapping between functions and filenames, surely 
there's *some* mapping for every project that won't run afoul of the real 
issues that truly can crop up with single-segment namespaces[1] but still feels 
natural to you.  Functionally logical grouping of code never goes out of style. 
 I'd humbly submit that single-segment namespaces are a non-solution to any 
naming problem you'd like to solve (outside of maximal concision perhaps, which 
I'd suggest is a low-value problem).

Cheers,

- Chas

[1] If you really, really don't care about host interop, and you never, *ever* 
will, and you're a maestro at naming projects (yup, I'm looking at you, Phil! 
:-), then maybe single-segment namespaces are OK.  Even then, it's worth noting 
that even e.g. leiningen, robert hooke, etc. don't use single-segment 
namespaces.

-- 
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 namespaces, Cobol, and Hierarchical file systems

2011-01-13 Thread Steve Miner

On Jan 13, 2011, at 3:16 PM, Chas Emerick wrote:

 Just to clarify my position (it's funny to see one's self quoted out of the 
 blue from an old thread!), I'm not at all suggesting java naming 
 conventions when it comes to namespacing.

By the way, I didn't mean to put Chas on the spot.  Google led me to what 
seemed like a good quote, and I thought he deserved the credit. Please take 
that as a compliment.

One suggested compromise (from the old thread) was to use a short first segment 
so that your namespace is technically multi-segment (good for Java interop), 
but still short enough to be esthetically pleasing.  For example, clj.foo 
instead of plain foo.  Seems like a good compromise to me.

Now, one could say that the problem is in the Clojure compiler.  Maybe single 
segment names should be explicitly disallowed.  (They're bad, don't do that.)  
Or maybe the compiler should silently prepend us.technomancy. to 
single-segment namespaces to make the world safe for Java interop with minimal 
danger of conflicts.  That would make everybody happy. :-)

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


Why is DISsoc being DIScriminated?

2011-01-13 Thread Eduardo Julian
I noticed that although you can use assoc with sequences and vectors
to modify them, you could not use dissoc to eliminate elements from
them. Why is this so?

-- 
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: Why is DISsoc being DIScriminated?

2011-01-13 Thread David Nolen
On Thu, Jan 13, 2011 at 4:05 PM, Eduardo Julian eduardo...@gmail.comwrote:

 I noticed that although you can use assoc with sequences and vectors
 to modify them, you could not use dissoc to eliminate elements from
 them. Why is this so?


You cannot efficiently remove items from the middle of sequences or vectors.

David

-- 
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 namespaces, Cobol, and Hierarchical file systems

2011-01-13 Thread Chas Emerick

On Jan 13, 2011, at 3:59 PM, Steve Miner wrote:

 On Jan 13, 2011, at 3:16 PM, Chas Emerick wrote:
 
 Just to clarify my position (it's funny to see one's self quoted out of the 
 blue from an old thread!), I'm not at all suggesting java naming 
 conventions when it comes to namespacing.
 
 By the way, I didn't mean to put Chas on the spot.  Google led me to what 
 seemed like a good quote, and I thought he deserved the credit. Please take 
 that as a compliment.

I did, thanks. :-)

 One suggested compromise (from the old thread) was to use a short first 
 segment so that your namespace is technically multi-segment (good for Java 
 interop), but still short enough to be esthetically pleasing.  For example, 
 clj.foo instead of plain foo.  Seems like a good compromise to me.

That seems perfectly fine, as long as the prefix or suffix isn't standard -- 
otherwise, for purposes of runtime differentiation, such namespaces are 
functionally a single segment.  i.e. if the standard is clj.foo or baz.core, 
then someone else's clj.foo or baz.core will conflict just as well.

FWIW, I use cemerick as a prefix for all personal stuff (though I do little 
programming that is both personal and significant enough to warrant worrying 
about namespaces).  I assume that everyone has their preferred irc/IM/email 
handle; using it as a prefix seems reasonable to me, anyway.

If your name changes, then modifying some namespaces is surely not the most 
difficult part of that process!

- 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


Enhanced Primitive Support Syntax

2011-01-13 Thread RJ Nowling
Hi all,

I've been reading about the changes to Clojure that are occurring with
regards to improving performance of arithmetic operations.  For
example, + no longer auto promotes and +' has been introduced as an
auto-promoting addition operator.

The idea of introducing additional syntax is a bit odd to me, so I had
thought that it could be solved by setting an environmental variable
for the namespace and the use of macros.  For example, maybe set
*enhanced-primitive-support* true or false will determine the behavior
of the operators (+, -, etc.) for an entire namespace.  If a user
wants a different set of behaviors for a block of code in that
namespaces, they could wrap the code in a macro called something like
enhanced-primitive-support or old-primitive-support:

(enhanced-primitive-support (+ a b) )

I was wondering why an approach like this was not taken?

Thanks,
RJ

-- 
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: Why is DISsoc being DIScriminated?

2011-01-13 Thread Ken Wesson
On Thu, Jan 13, 2011 at 4:05 PM, Eduardo Julian eduardo...@gmail.com wrote:
 I noticed that although you can use assoc with sequences and vectors
 to modify them

I get an exception trying to assoc a sequence in 1.2.

 you could not use dissoc to eliminate elements from
 them. Why is this so?

Deletion in the middle is not easy with lists or vectors. (Interesting
that (dissoc '(1 2 3) 0) and (dissoc [1 2 3] 2) don't work, though;
(assoc [1 2 3] 3 4) works to extend by one so why not allow dissoc to
contract by one? Although it would just be synonymous with pop. And
(assoc '(1 2 3) 0 7) doesn't work, though (cons 7 '(1 2 3)) would do
the same thing anyway, and (rest '(1 2 3)) the same as (dissoc '(1 2
3) 0).)

Treating a vector as a map from integers to objects, perhaps dissoc
could quasi-work on vectors by having (dissoc v n) act like (assoc v n
nil); then again, it wouldn't cause (contains? v n) to go from true to
false, which people might want from dissoc.

It's probably best to leave things as they are. Need to shorten by
one? Use pop (vectors) or rest (lists). Need to really dissoc? Use a
sparse vector, that is, an actual map with integer keys, like {0 1,
1 2, 2 3} instead of [1 2 3].

-- 
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: [ANN] fs - file system utilities for Clojure

2011-01-13 Thread Miki


 First, I suggest that you look at the standard clojure.java.io package for 
 some useful functions that are already in Clojure 1.2.  In particular, you 
 could use io/file instead of (File. xxx) in your code to add some 
 flexibility to the kinds of things that can be treated as a file.  I think 
 io/file could replace your fs/join. Also, io/copy is very flexible so it's 
 worth a look, too.

 Yeah, I wasn't aware of the many things in clojure.java.io (used just 
reader from there). I do plan switch to clojure.java.io soon.
 

 Second, fs is using a singe segment namespace.  I remember that there have 
 been some cautions against doing that. (But not everyone agrees.)  My 
 understanding is that it's best for Java interop to have a multi-segment 
 namespace.  (Reference links below.)

(and many other mails in this thread).

Coming from a Python background, single segment namespaces are natural to me 
and have never been a problem. I added :gen-class and ran lein compile and 
it worked, so I don't see a problem here as well. Changing it to 
com.mikitebeka.fs or lazy1.fs has too much of an ego trip for me :)

-- 
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: Why is DISsoc being DIScriminated?

2011-01-13 Thread Stuart Sierra
The result of `assoc` on a vector is well-defined, by treating the vector as 
a map with integer indices as keys.

But the result of `dissoc` on a vector is not so clear. `dissoc` removes a 
key from an associative thing. But you can't remove an index from the middle 
of a vector. Even if you could efficiently remove a single value, the index 
still exists, with a different value. In short, the definition of `dissoc` 
makes it impossible on a vector.

Some other data structures, such as finger trees [1], support efficient 
insertion and removal from the middle, but it's still not a `dissoc` 
operation.

Sequences in general support neither `assoc` nor `dissoc`.

[1] https://github.com/clojure/data.finger-tree

-- 
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: Enhanced Primitive Support Syntax

2011-01-13 Thread Stuart Sierra
The goal of primitive math is better performance in the common case. The 
implementation makes the assumptions that Java long is big enough for nearly 
all cases, and that auto-promotion to BigInteger (and the resulting 
performance hit) is rarely desirable.

The + function still promotes, it just promotes everything to long or 
double. If you want BigInts or BigDecimals, just insert one of them into the 
calculation, and everything else will get promoted to BigInt or BigDecimal.

The only difference between + and +' is that +' will promote to BigInt if 
you overflow a long, whereas + will thrown an exception.

-- 
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: Enhanced Primitive Support Syntax

2011-01-13 Thread Ken Wesson
On Thu, Jan 13, 2011 at 7:28 PM, Stuart Sierra
the.stuart.sie...@gmail.com wrote:
 The goal of primitive math is better performance in the common case.

Of course, this better performance is not needed in the common case,
IMO, but only in hotspots that do number crunching, where people
already optimize using primitive locals, coercion, and unchecked-foo.

 The implementation makes the assumptions that Java long is big enough for
 nearly all cases

It also makes the assumption that the Java long is as fast as native
arithmetic. Which, on a lot of 32-bit hardware, it won't be.

 and that auto-promotion to BigInteger (and the resulting performance hit) is
 rarely desirable.

Debatable. I, for one, prefer to have unadorned arithmetic be correct
at the expense of a little speed, while still having a way to get the
speed in performance-critical parts of my code.

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

2011-01-13 Thread Alex Baranosky
So I am converting some Ruby code I have into CLojure for practice/fun and I
am having trouble finding info via Google.

I want to take something like this from Ruby and do it in Clojure:

DATE_REGEX = /^\s*(\d{4})\s+(\d{1,2})\s+(\d{1,2})/

token =~ DATE_REGEX
[$1, $2, $3]

So far my best guess has been:

(defonce date-regex #^\s*(\d{4})\s+(\d{1,2})\s+(\d{1,2}))
(re-find date-regex line)

but I'm not sure how to access $1, $2, and $3


Another example I'd like to convert is this:

DAYS_OF_WEEK_REGEX = /^\s*(#{Date::DAYS_OF_WEEK.join('|')})/i

token =~ DAYS_OF_WEEK_REGEX
$1.downcase.to_sym

Best attempt is:

(defonce days-of-week-regex #mon|tue|wed|thu|fri|sat|sun(?i))
(re-find days-of-week-regex line)

Here I'd love to know the syntax for ignore-case, along with how to access $1.

Thanks,
Alex

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

2011-01-13 Thread Eric Lavigne
 So I am converting some Ruby code I have into CLojure for practice/fun and I
 am having trouble finding info via Google.

Clojure uses the same regex style as Java, so you'll need to search
for information on Java regexes rather than Clojure regexes.

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

2011-01-13 Thread .Bill Smith
Here is one way:

user= (import 'java.util.regex.Pattern)
java.util.regex.Pattern
user= (def p (Pattern/compile mon|tue|wed|thu|fri|sat|sun 
Pattern/CASE_INSENSITIVE))
#'user/p
user= (re-find p I have a dentist appoint on Monday morning, so you'll 
need to take the kids to school.)
Mon

Bill


On Thursday, January 13, 2011 8:35:17 PM UTC-6, Alex Baranosky wrote:


 Here I'd love to know the syntax for ignore-case



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

2011-01-13 Thread gaz jones
i think you want to do something like this:

  (let [_ year month day] (re-find date-regex line))


On Thu, Jan 13, 2011 at 8:50 PM, Eric Lavigne lavigne.e...@gmail.com wrote:
 So I am converting some Ruby code I have into CLojure for practice/fun and I
 am having trouble finding info via Google.

 Clojure uses the same regex style as Java, so you'll need to search
 for information on Java regexes rather than Clojure regexes.

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

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To 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 regexs

2011-01-13 Thread Alex Baranosky
I see.  So I may have to use some kind of clunky syntax instead of a nice
$1, $2, $3 syntax.  I can handle that I guess :)

Thanks.

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

2011-01-13 Thread Alex Baranosky
A blog entry I read (and now can't find) mentioned being able to use syntax
like (?i) to do the ignore case.

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

2011-01-13 Thread Benny Tsai
For accessing groups in a match, you can use (re-matches).  It will
always give the full match as the first element though:

user= (re-matches date-regex 2011 1 13)
[2011 1 13 2011 1 13]

So to replicate the Ruby code's behavior maybe you'll just want (rest
(re-matches date-regex line)).

For ignoring case, putting (?i) at the beginning of the regex worked
for me.  (re-find) should give you $1 just fine.

user= (require '[clojure.string :as str])
nil
user= (str/lower-case (re-find #(?i)mon|tue|wed|thu|fri|sat|sun
asdf MON tue))
mon

Hope this helps!

On Jan 13, 7:35 pm, Alex Baranosky alexander.barano...@gmail.com
wrote:
 So I am converting some Ruby code I have into CLojure for practice/fun and I
 am having trouble finding info via Google.

 I want to take something like this from Ruby and do it in Clojure:

 DATE_REGEX = /^\s*(\d{4})\s+(\d{1,2})\s+(\d{1,2})/

 token =~ DATE_REGEX
 [$1, $2, $3]

 So far my best guess has been:

 (defonce date-regex #^\s*(\d{4})\s+(\d{1,2})\s+(\d{1,2}))
 (re-find date-regex line)

 but I'm not sure how to access $1, $2, and $3

 Another example I'd like to convert is this:

 DAYS_OF_WEEK_REGEX = /^\s*(#{Date::DAYS_OF_WEEK.join('|')})/i

 token =~ DAYS_OF_WEEK_REGEX
 $1.downcase.to_sym

 Best attempt is:

 (defonce days-of-week-regex #mon|tue|wed|thu|fri|sat|sun(?i))
 (re-find days-of-week-regex line)

 Here I'd love to know the syntax for ignore-case, along with how to access $1.

 Thanks,
 Alex

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

2011-01-13 Thread Ken Wesson
On Thu, Jan 13, 2011 at 9:54 PM, Alex Baranosky
alexander.barano...@gmail.com wrote:
 I see.  So I may have to use some kind of clunky syntax instead of a nice
 $1, $2, $3 syntax.  I can handle that I guess :)

Hey, it's a Lisp! You can use almost any syntax you want. How about %1, %2, %3:

(defn re-do [f regex input]
  (apply f (rest (re-find regex input

(re-do #(do-stuff-with %3 %1 %2) regex input)

Or even

(defmacro re-let [names regex input  body]
  `(let [[_ ~@names] (re-find ~regex ~input)]
 ~@body))

(re-let [year month day] date-regex line
  (do-something-with year month day))

This one doesn't even have the bug that gaz jones's let has. ;)

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

2011-01-13 Thread John Svazic
They do seem to allow whatever you like if you upload your own
package.  I'm hooked on using their built-in editor, so in some cases
I copy-and-paste code until they get around to updating their version
of Clojure.  The one nice thing about the site is that they do seem
rather responsive to questions, bugs and feature requests!

I'm jsvazic on the site if anyone is interested.  :-)  Based on the
list of recent submissions lately, it seems like Clojure is getting a
lot of exposure!  It's funny to see some language turf wars showing
up, since I've seen Clojure dominate recent submissions list, then
Scala gets a kick, then people go on a PHP binge for some reason.  :-)

In any case, I'm finding the site absolutely great, and it's been an
absolute boon for updating my Clojure skills.  I fully expect that in
the next 6 months I'll look back at earlier submissions and say what
was I thinking?!  :-)

On Jan 13, 2:50 am, Robert McIntyre r...@mit.edu wrote:
 They seem to allow you to include anything in a lib directory that you'd want.

 I sometimes include apache commons-io and clojure-contrib1.2 without
 any problems.
 I also included a sql connection library for one of the problems, so
 it seems fine :)

 --Robert McIntyre







 On Thu, Jan 13, 2011 at 2:00 AM, Stuart Campbell stu...@harto.org wrote:
  On 12 January 2011 14:07, Robert McIntyre r...@mit.edu wrote:

  You can use the latest version of clojure if you include it as a
  dependency in your submission, so even though they say they only
  support clojure1.0 they really support all of them.

  Are other 3rd-party libs allowed, too?

  Cheers,
  Stuart

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

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To 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 Quizzes?

2011-01-13 Thread Ken Wesson
On Thu, Jan 13, 2011 at 10:04 PM, John Svazic jsva...@gmail.com wrote:
 They do seem to allow whatever you like if you upload your own
 package.  I'm hooked on using their built-in editor, so in some cases
 I copy-and-paste code until they get around to updating their version
 of Clojure.  The one nice thing about the site is that they do seem
 rather responsive to questions, bugs and feature requests!

 I'm jsvazic on the site if anyone is interested.  :-)  Based on the
 list of recent submissions lately, it seems like Clojure is getting a
 lot of exposure!  It's funny to see some language turf wars showing
 up, since I've seen Clojure dominate recent submissions list, then
 Scala gets a kick, then people go on a PHP binge for some reason.  :-)

PHP? Isn't that a web page generation engine, rather than a
general-purpose application programming language?

 In any case, I'm finding the site absolutely great, and it's been an
 absolute boon for updating my Clojure skills.  I fully expect that in
 the next 6 months I'll look back at earlier submissions and say what
 was I thinking?!  :-)

I think we all get that at first. It may be because functional/Lisp is
somewhat alien to most other things you're likely to have had
experience with beforehand, or just because C++ is to chess as Lisp is
to go -- fewer moves and simpler rules, but oh so much depth as your
Lisp-fu grows strong.

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

2011-01-13 Thread gaz jones
bah! good catch.
(let [[_ year month day]] (re-find date-regex line))

fixed!

On Thu, Jan 13, 2011 at 9:03 PM, Ken Wesson kwess...@gmail.com wrote:
 On Thu, Jan 13, 2011 at 9:54 PM, Alex Baranosky
 alexander.barano...@gmail.com wrote:
 I see.  So I may have to use some kind of clunky syntax instead of a nice
 $1, $2, $3 syntax.  I can handle that I guess :)

 Hey, it's a Lisp! You can use almost any syntax you want. How about %1, %2, 
 %3:

 (defn re-do [f regex input]
  (apply f (rest (re-find regex input

 (re-do #(do-stuff-with %3 %1 %2) regex input)

 Or even

 (defmacro re-let [names regex input  body]
  `(let [[_ ~@names] (re-find ~regex ~input)]
     ~@body))

 (re-let [year month day] date-regex line
  (do-something-with year month day))

 This one doesn't even have the bug that gaz jones's let has. ;)

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

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To 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 regexs

2011-01-13 Thread Ken Wesson
On Thu, Jan 13, 2011 at 10:22 PM, gaz jones gareth.e.jo...@gmail.com wrote:
 bah! good catch.
 (let [[_ year month day]] (re-find date-regex line))

 fixed!

Oh, gaz. I'm so so sorry. :(

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

2011-01-13 Thread gaz jones
lol oh noes! i should really stop doing this while watching 30rock...

On Thu, Jan 13, 2011 at 9:38 PM, Ken Wesson kwess...@gmail.com wrote:
 On Thu, Jan 13, 2011 at 10:22 PM, gaz jones gareth.e.jo...@gmail.com wrote:
 bah! good catch.
 (let [[_ year month day]] (re-find date-regex line))

 fixed!

 Oh, gaz. I'm so so sorry. :(

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

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To 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 in Small Pieces

2011-01-13 Thread Tim Daly

 The latest version of Clojure in Small Pieces is up.
http://daly.axiom-developer.org/clojure.pamphlet
http://daly.axiom-developer.org/clojure.pdf

A few people have volunteered to write sections.
More volunteers are welcome.

There are new sections. The LispReader is split apart
and is undergoing documentation. The index now covers
all of the classes and interfaces with hyperlinks to
the defining pages. The Appendix A has external hyperlinks
to browser pages describing standard Java classes that
are referenced by the code.

There are some graphics for bit-partitioning and
red-black trees.

And, on a technical note, the clojure.sty file is
no longer needs as I rewrote the latex macros.

Tim Daly

P.S. the tangle.c code is still up on the website at:
http://daly.axiom-developer.org/tangle.c
although you don't need it as it is in the pamphlet.

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

2011-01-13 Thread Robert McIntyre
As HTML-y as it it is, PHP is Turing complete and thus a real
programming language.

It's got foreach loops, mutable arrays, and boolean logic, and that's
one particular set of operations to simulate a Turing machine.

http://en.wikipedia.org/wiki/Turing_machine
http://aturingmachine.com/

sincerely,
--Robert McIntyre

On Thu, Jan 13, 2011 at 10:07 PM, Ken Wesson kwess...@gmail.com wrote:
 On Thu, Jan 13, 2011 at 10:04 PM, John Svazic jsva...@gmail.com wrote:
 They do seem to allow whatever you like if you upload your own
 package.  I'm hooked on using their built-in editor, so in some cases
 I copy-and-paste code until they get around to updating their version
 of Clojure.  The one nice thing about the site is that they do seem
 rather responsive to questions, bugs and feature requests!

 I'm jsvazic on the site if anyone is interested.  :-)  Based on the
 list of recent submissions lately, it seems like Clojure is getting a
 lot of exposure!  It's funny to see some language turf wars showing
 up, since I've seen Clojure dominate recent submissions list, then
 Scala gets a kick, then people go on a PHP binge for some reason.  :-)

 PHP? Isn't that a web page generation engine, rather than a
 general-purpose application programming language?

 In any case, I'm finding the site absolutely great, and it's been an
 absolute boon for updating my Clojure skills.  I fully expect that in
 the next 6 months I'll look back at earlier submissions and say what
 was I thinking?!  :-)

 I think we all get that at first. It may be because functional/Lisp is
 somewhat alien to most other things you're likely to have had
 experience with beforehand, or just because C++ is to chess as Lisp is
 to go -- fewer moves and simpler rules, but oh so much depth as your
 Lisp-fu grows strong.

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

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To 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 Quizzes?

2011-01-13 Thread Ken Wesson
On Fri, Jan 14, 2011 at 1:13 AM, Robert McIntyre r...@mit.edu wrote:
 As HTML-y as it it is, PHP is Turing complete and thus a real
 programming language.

So are LaTeX, Javascript, and a few others, but from what I've seen
they're all meant for specific purposes rather than general-purpose
software engineering.

 It's got foreach loops, mutable arrays, and boolean logic, and that's
 one particular set of operations to simulate a Turing machine.

I read somewhere that it suffices to have a program counter, a
register, four stacks, jump-back-or-forth-N-steps with a small bounded
N, if register zero then next instruction otherwise skip one
instruction, increment register (which wraps at some value), and peek
or pop for each of the four stacks (to the register). Alternatively, a
program counter, a stack, a cursor into an infinite deque, and these
eight instructions: increment at cursor, decrement at cursor, jump
amount at cursor, if zero at cursor next instruction else skip, cursor
left one, cursor right one, push program counter, pop program counter.
The universal Turing machine concept suggests that there's an
instruction set for which only the infinite deque is needed, with
cursor-left, cursor-right, and some additional instructions, but no
long range jumps within the deque, and instructions also fetched from
the deque with no separate program counter. It would be a real bitch
to program, though. The general rule seems to be that a) there has to
be at least one if-something condition that alters flow, say by
optionally skipping one instruction that can be a longer-range jump
and may be followed by another such; b) there has to be a backward
jump possibility; and c) there has to be some kind of memory -- either
bidirectionally infinite and cursor-navigable like a giant ArrayList,
or at least four stacks that can be separately peeked and popped. With
just one stack you can apparently get a big subset of fully general
computation but not the whole deal. There also has to be d) arithmetic
of some kind, on the memory content and interacting in some way with
the if condition.

 http://en.wikipedia.org/wiki/Turing_machine
 http://aturingmachine.com/

I see your Turing machine and raise you this:

http://www.rezmason.net/wireworld/

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