Re: Elegant but very slow

2008-12-04 Thread Mark Engelberg
A couple quick suggestions: On my machine, Peter's code runs in 120 seconds. Changing make-tree to return vectors rather than lists reduced time to 47 seconds. Taking out the nil? test (just (if tree branch1 branch2) shaved off a few more seconds. Removing the pattern-matching let in check-tree (

Re: Elegant but very slow

2008-12-04 Thread Mark Engelberg
I already tried bOR's two suggestions (replace anonymous function with + and type hinting), but they made no difference on my machine. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this

Re: Atoms

2008-12-04 Thread Mark Engelberg
Didn't commute essentially give this behavior for refs? How is this different? On Thu, Dec 4, 2008 at 5:02 PM, Rich Hickey <[EMAIL PROTECTED]> wrote: > > I've added a new reference type - atom. > > --~--~-~--~~~---~--~~ You received this message because you are s

Re: Atoms

2008-12-05 Thread Mark Engelberg
So, earlier, I asked how atoms differ from using commute on refs. It sounds like the answer is that if you use atoms in a larger transaction, then as soon as the atom set is encountered, it actually changes instantly, so if you rollback, and do the transaction again, it's already been set, and wi

Re: Elegant but very slow

2008-12-06 Thread Mark Engelberg
Has anyone been able to use type hints to successfully close the last bit of difference between the Clojure and Java version on this benchmark? On Sat, Dec 6, 2008 at 6:14 AM, PeterB <[EMAIL PROTECTED]> wrote: > Running in Clojure REPL for java 1.6.0_11 with -server option: > > result: -2 > E

Re: memory issue with nth

2008-12-06 Thread Mark Engelberg
It doesn't blow the heap on my machine, using the "Clojure in a Box" setup, and I only have 1GB of memory total. I added a couple 0s, and it didn't make a difference. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups

Re: Running out of memory when using filter?

2008-12-06 Thread Mark Engelberg
Except your version of filter doesn't do any filtering on the rest in the case where the first satisfies the predicate. On Sat, Dec 6, 2008 at 7:43 PM, Stephen C. Gilardi <[EMAIL PROTECTED]> wrote: > If you use a definition of filter like this in your test, I think it will > succeed: > > (defn fi

Re: Running out of memory when using filter?

2008-12-06 Thread Mark Engelberg
Well, part of the puzzle is to figure out why filter works just fine on the output of the range function, but not on the output of the map function. I'm starting to wonder whether there might be a fundamental bug in the java implementation of LazyCons. Maybe it doesn't implement "first" correctl

Emacs / Slime questions

2008-12-07 Thread Mark Engelberg
I'm using Clojure Box on Windows. It's working well for me, but I have a number of questions: 1. I see that you can use C-c C-c to feed the definition your cursor is on to the REPL. How do you feed the entire file to the REPL? 2. How do I restart the REPL, so that any definitions are erased a

Re: Emacs / Slime questions

2008-12-07 Thread Mark Engelberg
Thanks for all the info. I've searched my whole hard drive for a .emacs file, and can't find one. Can someone tell me where Clojure Box stores this file, or whether it's called something entirely different? --Mark --~--~-~--~~~---~--~~ You received this message

Re: Running out of memory when using filter?

2008-12-08 Thread Mark Engelberg
Has anyone made progress on this bug? The simplest form of the bug was this: (defn splode [n] (doseq [i (filter #(= % 20) (map inc (range n)))])) This blows the heap, but it shouldn't. I find this deeply troubling, because if this doesn't work, it undermines my faith in the implementation of

Re: Running out of memory when using filter?

2008-12-08 Thread Mark Engelberg
On Mon, Dec 8, 2008 at 5:56 PM, Stephen C. Gilardi <[EMAIL PROTECTED]> wrote: > I think I finally see the problem. The "rest expression" in filter's call to > lazy-cons has a reference to "coll" in it. That's all it takes for coll to > be retained during the entire calculation of the rest. > Well

Re: Running out of memory when using filter?

2008-12-08 Thread Mark Engelberg
On Mon, Dec 8, 2008 at 5:56 PM, Stephen C. Gilardi <[EMAIL PROTECTED]> wrote: > I think I finally see the problem. The "rest expression" in filter's call to > lazy-cons has a reference to "coll" in it. That's all it takes for coll to > be retained during the entire calculation of the rest. > Well

Java interop question

2008-12-11 Thread Mark Engelberg
I understand how Clojure lets you consume Java objects, and pass Clojure objects to Java programs. However, it is not uncommon for Java libraries to be designed in such a way that you need to create a subclass of something in the library in order to make use of the library. I don't understand wh

Re: Running out of memory when using filter?

2008-12-11 Thread Mark Engelberg
On Mon, Dec 8, 2008 at 6:51 PM, Rich Hickey wrote: I don't have the latest build of Clojure with atoms, so I reimplemented Rich's filter solution using refs, turning: > (defn filter > [pred coll] > (let [sa (atom (seq coll)) > step (fn step [] > (when-let [s @sa

Extending deref

2008-12-11 Thread Mark Engelberg
If you wanted to extend deref to new types, is there any way to do that? For example, deref of a delay could be equivalent to a force. Or deref of a Future could invoke the Future's get method. This would give you the handy @ reader macro for things that frequently need to be forced to evaluate

Re: Emacs / Slime questions

2008-12-12 Thread Mark Engelberg
On Sun, Dec 7, 2008 at 12:24 PM, Bill Clementson wrote: >> 3. If something in the REPL is running in an infinite loop, how do I >> interrupt evaluation? > > C-c C-c usually works for me (not sure about Windows) This isn't working for me on Clojure Box in Windows. Can anyone else tell me a way

Best style for internal variables

2008-12-12 Thread Mark Engelberg
Sometimes you use mutable state inside of a function to do some local temporary computation, and this state will never be visible outside of the function. So externally speaking, the function is still a pure function. Vars, Refs, and Atoms all seem to be reasonable choices for representing this

Re: Running out of memory when using filter?

2008-12-12 Thread Mark Engelberg
On Fri, Dec 12, 2008 at 6:37 AM, Rich Hickey wrote: > I'm appreciate the time you and others have spent on this, and will > improve filter, but I'm not sure where you are getting your > presumptions about lazy sequences. They are not a magic bullet that > makes working with data bigger than memor

Re: Running out of memory when using filter?

2008-12-12 Thread Mark Engelberg
On Fri, Dec 12, 2008 at 5:28 PM, Paul Mooser wrote: > > On Dec 12, 3:15 pm, "Mark Engelberg" wrote: >>And in fact, it turns out that in those languages, uncached lazy lists end up >>rarely used. > > Did you mean that the cached lazy lists are rarely used? Or

Re: Functional programming newbie question

2008-12-12 Thread Mark Engelberg
On Fri, Dec 12, 2008 at 6:51 PM, levand wrote: > The next implementation is simpler, and uses recursion quite > elegantly: > (defn copy-seq [source] >(if (nil? source) > '() > (cons (first source) (copy-seq (rest source) > What would one do in a language like Scheme, without l

Re: Running out of memory when using filter?

2008-12-12 Thread Mark Engelberg
On Fri, Dec 12, 2008 at 9:28 PM, Rich Hickey wrote: > I think it's very important not to conflate different notions of > sequences. Clojure's model a very specific abstraction, the Lisp list, > originally implemented as a singly-linked list of cons cells. It is a > persistent abstraction, first/s

Re: Emtpy (filter ...) Result Yields nil; Could / Should It Be An Empty List?

2008-12-14 Thread Mark Engelberg
When IS an appropriate time to use '()? It's in the language, so I assume there must be at least one good use case. Recently, I was writing a function that yields a sequence of all the subsequences of a given sequence, e.g., (subseqs '(1 2 3)) -> (nil (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3)) I ha

Re: On Lisp - porting problem

2008-12-14 Thread Mark Engelberg
#(fif even? (fn [x] (* 2 x))) has no % sign in it, so it is a function of zero arguments, which is not something that map can use. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this gro

Re: Running out of memory when using filter?

2008-12-14 Thread Mark Engelberg
On Sat, Dec 13, 2008 at 5:51 AM, Rich Hickey wrote: > No you can't, for the same reasons you can't for Iterator or > Enumeration seqs. Again it comes down to abstractions, and the > abstraction for (seq x) is one on persistent collections. It presumes > that (seq x) is referentially transparent,

Re: confused by Vars, Refs, Agents and Atoms

2008-12-16 Thread Mark Engelberg
On Tue, Dec 16, 2008 at 1:22 PM, falcon wrote: > > I am fairly familiar with basic functional programming. I've studied > a bit of scheme and Haskell. I don't quite understand Vars, Refs, > Agents and Atoms. I am a Java developer and have worked with > concurrency constructs: locks, synchroniz

Re: code golf

2008-12-17 Thread Mark Engelberg
This shaves 14 characters off of Chouser's solution: (defn enc[s e](apply str(map(apply hash-map(take-nth 2 e))s))) rzezeski: The problem statement is somewhat ambiguous about what to do if the character is missing from the mapping. It says you can just "skip it", and this short solution does

Re: code golf

2008-12-17 Thread Mark Engelberg
78? I count 72 after you take out all the superfluous spaces. But I agree that it says nothing significant about the language. It's a fun optimization puzzle, though, and can help understand whether the built-in library of functions is fairly rich. --~--~-~--~~~---~-

Re: Superficial barriers to entry

2008-12-18 Thread Mark Engelberg
On Thu, Dec 18, 2008 at 3:20 AM, Jan Rychter wrote: > I don't buy it. When you start using Python, nobody handholds you so > that you can pick an editor. You just use whatever you have. So what's > the deal here? At least on Windows, Python comes with IDLE, which is surprisingly full-featured gi

Re: recur rationale?

2008-12-20 Thread Mark Engelberg
On Sat, Dec 20, 2008 at 9:06 AM, Stuart Sierra wrote: > Leaving that aside, I actually find loop/recur more intuitive than > Scheme-style "helper functions." It really gets down to the essence > of what a loop is. loop/recur is almost exactly like Scheme's "named let" (basically loop/recur is a

Re: How to encapsulate local state in closures

2008-12-21 Thread Mark Engelberg
But if mk-counter is called twice because it's retried in part of a transaction, then you're in big trouble when you use atom. Better to use a ref here. atom needs to be reserved for the very few cases when retries don't matter (like a cache). --~--~-~--~~~---~--~---

Re: How to encapsulate local state in closures

2008-12-21 Thread Mark Engelberg
I misspoke; it's the call to counter that's the problem. Let's say you want to use a counter to count the number of times a ref is set, something like this: (dosync (counter) (ref-set r 1)) If your var-set causes the transaction to retry, an atom-based counter will increment twice. As I unders

Modulo

2008-12-22 Thread Mark Engelberg
Anyone know why there is no modulo or mod function in Clojure's core? I know there is a rem function, but that's not the same thing. mod and rem behave differently when the first number is negative. (mod -2 5) -> 3 (rem -2 5) -> -2 modulo n is important for making things stay in the range from

Numeric tower and Java's math library

2008-12-22 Thread Mark Engelberg
Clojure has pretty decent support for the so-called numeric tower. But most of the math operations in Java's math library only work on doubles. In PLT Scheme, I can do stuff like this: (sqrt 115792089237316195423570985008687907853269984665640564039457584007913129639936) and it outputs: 34028236

Re: How to encapsulate local state in closures

2008-12-22 Thread Mark Engelberg
On Mon, Dec 22, 2008 at 4:23 AM, Parth Malwankar wrote: > If I get it right, atoms are quite useful to maintain state > in the context of a single thread with memoization and > counter (within a thread) being two examples. No, RH said that atoms were definitely intended for multiple threads, not

Re: How to encapsulate local state in closures

2008-12-26 Thread Mark Engelberg
On Fri, Dec 26, 2008 at 8:35 PM, Adrian Cuthbertson wrote: > It's important to distinguish between updating atoms within > transactions and outside transactions. In the former case, one has to > ensure the update function can be retried without ill-effects. > However, outside a transaction, atoms

Re: distinct broken?

2008-12-27 Thread Mark Engelberg
So one thing I don't get is why would tristan's pow code sometimes generate a BigInteger, and sometimes a Long for the same resulting number? But if this is really the problem, it seems like before hashing a number, Clojure should apply some sort of consistent type conversion rule to make sure th

Re: take 1 element from each coll, make all possible strings?

2008-12-27 Thread Mark Engelberg
On Sat, Dec 27, 2008 at 2:18 PM, bOR_ wrote: > but > didn't see an obvious/elegant way to do it in there, and the only way > I'd write it now in clojure is going to be ugly. > > Anyone has a suggestion? This kind of problem is straightforward if you understand recursion, and the Clojure code is

Re: take 1 element from each coll, make all possible strings?

2008-12-28 Thread Mark Engelberg
On Sun, Dec 28, 2008 at 12:49 AM, bOR_ wrote: > > Annoying way to start a morning :P. Thanks though for the tip on > htdp.org I'm not trying to be annoying. I just don't know how valuable the answer will be to you, until you've got a better handle on recursion. The code below is the main idea;

Re: take 1 element from each coll, make all possible strings?

2008-12-28 Thread Mark Engelberg
I'm not surprised that the flexible version takes a bit more time than the one that's hardcoded to a for loop nested to 9 levels. The flexible version necessarily involves recursion, which means allocating some stack space with each call, and that's a bit of overhead. I am a bit surprised that y

Re: take 1 element from each coll, make all possible strings?

2008-12-28 Thread Mark Engelberg
I coded up a quickie iterative version for you so you can get an idea what that looks like. My timing tests indicate that this runs many times faster than even the hardcoded for loop nested to 9 levels. ; Convert to a vector, and pass off to step, which builds a lazy sequence by applying increme

Re: take 1 element from each coll, make all possible strings?

2008-12-28 Thread Mark Engelberg
This iterative version doesn't behave properly if one of the sequences is empty. Expand should probably check for this degenerate case before passing the vector off to step. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google G

Re: take 1 element from each coll, make all possible strings?

2008-12-28 Thread Mark Engelberg
I didn't know that you could name anonymous functions like that for recursive purposes. Thanks. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegro

Re: Modulo

2008-12-28 Thread Mark Engelberg
the shuffle of holiday time? On Mon, Dec 22, 2008 at 4:04 AM, Mark Engelberg wrote: > Anyone know why there is no modulo or mod function in Clojure's core? > I know there is a rem function, but that's not the same thing. mod > and rem behave differently when the first numbe

Re: How to encapsulate local state in closures

2008-12-29 Thread Mark Engelberg
On Mon, Dec 29, 2008 at 8:05 AM, Rich Hickey wrote: > It is certainly not the whole point of Clojure to make as much code as > possible safe for its software transactional memory. Clojure is a set > of tools. They are designed to allow for robust programs to be built, > including multithreaded pr

Re: How to encapsulate local state in closures

2008-12-29 Thread Mark Engelberg
On Mon, Dec 29, 2008 at 12:40 PM, Rich Hickey wrote: > People who know what they are doing can do these things right now with > Clojure's array support. There really isn't any more value for Clojure > to add to that, so no special primitives. I fully accept the necessity > of doing that at times,

Re: How to encapsulate local state in closures

2008-12-29 Thread Mark Engelberg
On Mon, Dec 29, 2008 at 2:57 PM, Dave Griffith wrote: > > It looks like the mutable locals use case is covered by the "with- > local-vars" binding form. Not really. with-local-vars has somewhat surprising semantics. For example, you'd expect this (contrived) function to generate an "add 2" fun

Understanding how to use namespaces

2008-12-29 Thread Mark Engelberg
So up until now, I've mainly been writing my code in a given file, using no namespaces, and then just compiling and executing it in a SLIME-based REPL. I've accumulated a few functions that seem worth making into a little library. So, for example, I've written a few permutation functions. I put

Re: How to encapsulate local state in closures

2008-12-30 Thread Mark Engelberg
On Tue, Dec 30, 2008 at 5:53 AM, Rich Hickey wrote: > Could you provide an example of when you would need/use that? > Sure. Use Case #1: Implementing classic imperative algorithms Consider the binary gcd algorithm on page 338 of The Art of Computer Programmiing, volume 2. This is a very clever

Re: How to encapsulate local state in closures

2008-12-30 Thread Mark Engelberg
On Tue, Dec 30, 2008 at 8:38 PM, Rich Hickey wrote: > There's simply no value for Clojure to add to a simple mutable box. > Clojure does provide the tools for low-level mutation - access to > Java. You can wrap that in whatever functions/macros you like. > There's no way to use the @ dereferenci

Re: Understanding how to use namespaces

2008-12-30 Thread Mark Engelberg
Thanks, that helps dramatically. It took me a while to figure out how to edit the SLIME startup to include my clojure files directory in the classpath, but I got it working. So it seems like you have to make sure the namespace always matches the file. That means if you change the name of the fi

Re: How to encapsulate local state in closures

2008-12-31 Thread Mark Engelberg
On Wed, Dec 31, 2008 at 6:27 AM, Rich Hickey wrote: > I also think that your use cases for atoms for local mutation are a > mismatch. atoms are about sharing. You really want something else for > private/local mutable references, and I have some ideas for that. You're right that I'm basically as

Exponentiation (expt / pow)

2008-12-31 Thread Mark Engelberg
As I posted a couple weeks ago, I've been finding it awkward that Clojure doesn't have too many math functions that know how to "do the right thing" with the various numeric types it uses. The one that's been bothering me the most is the lack of an expt function. Calling Math/pow automatically co

Re: implementing the Hofstadter Male and Female sequences

2009-01-02 Thread Mark Engelberg
I don't think you need the declare line in your example, since the recursive references are inside of closures. I think your code is already very elegant, in the sense that it almost directly mirrors the mathematical definition of the sequences. There are probably ways to make it more efficient

Math functions

2009-01-03 Thread Mark Engelberg
I've noticed that Clojure is missing several math functions that come standard with most programming languages, especially other Schemes/Lisps. Many of these functions are available in java's math library, but only for doubles. I am attaching a file that tries to "do the right thing" for Clojure'

Re: Math functions

2009-01-03 Thread Mark Engelberg
On Sat, Jan 3, 2009 at 7:06 PM, Mark H. wrote: > Would you > find a sqrt that returns complex numbers for negative inputs (it would > be the appropriate branch of the sqrt function in order to make it > single-valued) useful? Ideally I'd also like that, but since complex numbers aren't part of

Re: yet another Clojure snake

2009-01-06 Thread Mark Engelberg
One way to approach this without mutation is as follows: Decide on a data representation that represents the entire state of the game world at a given instant. Write a function which can draw this state. Write a function which takes the state and player keypresses as input, and returns a new upd

Re: update in place for unique references

2009-01-08 Thread Mark Engelberg
Clean doesn't allow mutation, so it has to do tricks like this or else you'd never be able to write a useful program. Clojure gives you a set of data structures that do very fast non-destructive update. Clojure also gives you tools like atoms, refs, and full access to Java's mutable behavior to

Clojure blog post about laziness

2009-01-08 Thread Mark Engelberg
A few days ago, Stuart Halloway and I had an offline discussion about some of the "gotchas" related to Clojure's laziness. He encouraged me to blog about my thoughts on the matter. On a related note, about a month ago, I posted comments about Clojure's laziness. Rich's response was: "The argum

Re: Clojure blog post about laziness

2009-01-08 Thread Mark Engelberg
On Thu, Jan 8, 2009 at 5:55 AM, Rich Hickey wrote: > The > promise of the abstraction is not merely that the nth item/rest will > be equal - it is that it will be identical. I.e. a seq is persistent > and immutable. I get that Clojure is making a promise of identity here, which is not possible i

Re: Clojure blog post about laziness

2009-01-08 Thread Mark Engelberg
On Thu, Jan 8, 2009 at 10:20 AM, Rich Hickey wrote: > On Thu, Jan 8, 2009 at 5:55 AM, Rich Hickey > I think the real test of non-cached seqs is to swap them in for > regular seqs, rebuild Clojure and some user libs and see what breaks > and why. Then you'll see the dependencies on caching that e

Re: sort behavior question

2009-01-08 Thread Mark Engelberg
Lists are not comparable (i.e., you can't do something like (< '(1 2 3) '(4 5 6))). So you can't sort a collection of lists, but you can sort a collection of vectors (provided the vectors contain comparable things). This is always the case; there is no inconsistency. The reason you are sometime

Re: Clojure blog post about laziness

2009-01-08 Thread Mark Engelberg
On Thu, Jan 8, 2009 at 5:55 AM, Rich Hickey wrote: > When you > ask for the nth element/rest, you get the identical item every time. I know this is nitpicking, but if this is really the promise that the seq abstraction is supposed to fulfill, then Clojure already violates this: (def a (seq [1 2

Re: Clojure blog post about laziness

2009-01-08 Thread Mark Engelberg
Oh, I mentioned this in my blog post, but perhaps it bears repating. If cycle, repeat, and replicate were implemented behind-the-scenes with LazySeq as opposed to LazyCons, they would still implement the promise of identical elements for separate traversals, but would be more efficient. Also, ran

Re: sort behavior question

2009-01-08 Thread Mark Engelberg
On Thu, Jan 8, 2009 at 11:20 PM, Christian Vest Hansen wrote: > Comparable implies that an Object can be reduced to a scalar value, if > only for the purpose of comparing. How do you imagine this should work > on list of arbitrary things? Lexicographic ordering. Compare the first elements, if e

Re: sort behavior question

2009-01-09 Thread Mark Engelberg
On Fri, Jan 9, 2009 at 8:28 AM, Chouser wrote: > So maybe leaving 'sort' undefined for lists makes sense. If you want > them to sort like vectors, then use vectors! But if the lists are nested within some aggregate structure, (e.g., in the original postr's use case, the lists were the second el

Re: REPL prints a ISeq as a list

2009-01-09 Thread Mark Engelberg
A sequence evaluates its elements on demand. The REPL needs to evaulate the elements in order to print them. You can control how many elements are printed by changing *print-length*. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the

Re: Clojure blog post about laziness

2009-01-10 Thread Mark Engelberg
Thanks Rich. I definitely appreciate the fuller explanation. It helped me understand what you've already tried, and what factors led to your design decision. You've given me a lot to think about. --~--~-~--~~~---~--~~ You received this message because you are su

Re: Simple Data Structure Question

2009-01-10 Thread Mark Engelberg
On Sat, Jan 10, 2009 at 6:57 PM, CuppoJava wrote: > ps: I'm doing a bit of reading about mutually-recursive data > structures on the net. Is Lisp's letrec supposed to handle this > situation? > > It would be handy just to be able to do: > > (letrec [bob (create_person "bob" bill) > bill

Re: Suggested functions for core: any? none?

2009-01-13 Thread Mark Engelberg
I also find the choice of some/not-any? as opposites to be hard to remember. I'd rather it be some/not-some? or any/not-any? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, se

Re: Can't lookup java.lang.Long keys in a map using int literal

2009-01-13 Thread Mark Engelberg
It might be nice if there were a function that automatically converted the number to the type that Clojure uses for that number if typed in as a literal. On Tue, Jan 13, 2009 at 7:27 PM, Stuart Sierra wrote: > > FYI, You can coerce to specific types with the functions int, long, > short, etc. Y

Re: Clojure blog post about laziness

2009-01-18 Thread Mark Engelberg
On Fri, Jan 9, 2009 at 6:00 AM, Rich Hickey wrote: > When I had lazy-seq in play, many of those were defined that way. The > real cost is explaining either why lazy-seq is private, or, if public, > when/how to use it. I'd certainly make use of lazy-seq if it were public. But assuming it's not m

Re: post on test-is and testing styles

2009-01-19 Thread Mark Engelberg
Seems like is/are are somewhat gratuitous. Why not leave them out completely in a deftest, and get the syntax as minimalist as possible? (deftest test-plus (= (+ 1 1) 2) (= (+ 2 3) 5)) --~--~-~--~~~---~--~~ You received this message because you are subscribed

Re: Proposal: "smarter" set operations that take size into account for speed

2009-01-20 Thread Mark Engelberg
I say "thumbs up" to this proposal. Sets really should test for the size of the two inputs, and adjust the algorithm accordingly. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this grou

struct question

2009-01-21 Thread Mark Engelberg
Is there any way to determine whether something is a "struct" (versus an ordinary hash map), and if so, determine which kind of struct it was built from? For example, in Scheme, consider (define-struct circle (x y radius)), you get a circle? predicate for free that knows how to distinguish betwee

Re: struct question

2009-01-21 Thread Mark Engelberg
Thanks for the thread links. This is basically what I suspected -- if you want to use structs in multimethods, you have to roll your own constructor which adds some kind of "type" tag to either the hashmap or the metadata. It just seems like a common case, so I was hoping there was a more conven

A nil puzzle

2009-01-23 Thread Mark Engelberg
The power set of a set S is defined as the set of all subsets of S. Here is one possible implementation of a powerset function. To stay true to the spirit of the above definition, I've written it in a way that manipulates the sets in set form as much as possible (not quite an easy task since thi

Re: Proposal: "smarter" set operations that take size into account for speed

2009-01-23 Thread Mark Engelberg
On Fri, Jan 23, 2009 at 12:23 PM, Jason Wolfe wrote: > > OK, if these are not wanted in core right now, will anyone sign off > for adding them to clojure.contrib? > Well, *I* want these changes you've proposed in the core, but of course, I'm not in charge. I guess the real question is, what is

Re: (clojure.contrib.lazy-seqs/combinations) should return [[]], not nil?

2009-01-23 Thread Mark Engelberg
On Fri, Jan 23, 2009 at 8:49 PM, Chouser wrote: > This is the best version? Depends on your definition of "best". I just posted a version at http://paste.lisp.org/display/74134 which is many times faster. Also, I have to say that calling this function "combinations" is fairly nonstandard. Usu

Re: A nil puzzle

2009-01-24 Thread Mark Engelberg
Now that everyone's had a day to mull this puzzle over, here are my thoughts. One way to think about this, is to think about what the statement of purpose / contract of a generalized powerset function would be. In general, most functions on seq-able objects return sequences. For example, (rest

Re: (clojure.contrib.lazy-seqs/combinations) should return [[]], not nil?

2009-01-24 Thread Mark Engelberg
On Fri, Jan 23, 2009 at 11:43 PM, wrote: > I think the usual mathematical name is be cartesian-product (or maybe > cross-product), although selections is OK with me too. Yes, now that you've reminded me, I agree that cartesian-product is the usual mathematical name, and that would probably be m

Re: A nil puzzle

2009-01-24 Thread Mark Engelberg
On Sat, Jan 24, 2009 at 9:06 AM, Rich Hickey wrote: > This is already wrong, right? Don't you mean (set s)? You haven't said > what (powerset [1 2 3 2 1]) is going to be. subsequences != subsets. Despite the name "powerset" being inspired by sets, when I posed the puzzle of generalizing the beha

Re: (clojure.contrib.lazy-seqs/combinations) should return [[]], not nil?

2009-01-25 Thread Mark Engelberg
For simple inputs, the two approaches have similar performance. On complex inputs, my tests show the iterative version tends to run about twice as fast. Try running on an extreme input like: ["ACDFG" "A" "B" "C" "D" "E" "F" "ABCD" "G" "H" "I" "ABEG" "J" "K" "BCDE" "L" "ABCDG" "M" "EF" "NABC" "AB

Re: (clojure.contrib.lazy-seqs/combinations) should return [[]], not nil?

2009-01-26 Thread Mark Engelberg
Also, it's worth pointing out that your newer version prints the combinations out in a non-standard order. I don't know whether people should really rely on the order, but I think most people would expect it to print out in the same order as a series of nested for loops. --~--~-~--~~

Re: (clojure.contrib.lazy-seqs/combinations) should return [[]], not nil?

2009-01-26 Thread Mark Engelberg
I've tested that already, and it takes even longer for all but trivial inputs, because rec now prevents the combinations sequence from being garbage collected, and the memory allocation bogs things down tremendously. On Mon, Jan 26, 2009 at 10:26 AM, Jason Wolfe wrote: > >> Also, it's worth poin

Re: Stream utilities in clojure.contrib

2009-01-26 Thread Mark Engelberg
On Mon, Jan 26, 2009 at 4:38 AM, Rich Hickey wrote: > Continuations, like TCO, will have to come from the JVM. Perhaps a better question to ask is whether it is possible to implement yield without user-available continuations. I assume it's possible since Python has yield, but not continuations

Re: (clojure.contrib.lazy-seqs/combinations) should return [[]], not nil?

2009-01-26 Thread Mark Engelberg
I updated the paste with the changes we've discussed. http://paste.lisp.org/display/74134#3 I'll keep playing around to see if I can get it faster, but so far, this is the fastest way I've found to generate the cartesian product in standard order. --~--~-~--~~~---~--

Re: (clojure.contrib.lazy-seqs/combinations) should return [[]], not nil?

2009-01-26 Thread Mark Engelberg
Hey, I just looked back at the original post that started the thread. At some point, I had changed my function so (cartesian-product) returned nil instead of (nil), but based on Jason's post, I've changed it back in another paste annotation. --~--~-~--~~~---~--~~ Y

Re: greatest and least

2009-01-26 Thread Mark Engelberg
I personally don't think I'd use the functions that return more than one min/max. I think in such contexts, I'm usually more interested in just sorting the whole collection. So I'd be happy just with the ones that return one result. --~--~-~--~~~---~--~~ You rece

Re: Got a Clojure library?

2009-01-29 Thread Mark Engelberg
Author: Mark Engelberg License: Public Domain Dependencies: None Category: Math clojure.contrib.math Common math functions for Clojure's numeric tower. clojure.contrib.combinatorics Efficient, functional implementations of common combinatorial functions such as permutations, combinations

Re: Alternatives to contains?

2009-01-29 Thread Mark Engelberg
On Thu, Jan 29, 2009 at 2:52 PM, Mark Volkmann wrote: >> What is the drawback of the (some #{:y} [:x :y :z]) idiom? Is it too >> verbose? Too slow? Too flexible? Too good a re-use of existing >> functionality? Too helpful in opening ones eyes to the possibilities >> of sets and higher order

Re: removing duplicates from combinatorics/selections

2009-02-01 Thread Mark Engelberg
I'm not sure exactly what you want to remove, but it sounds like you might be looking for something like: (set (map sort (selections [1 2 3 4 5] 3))) or: (for [i (range 1 6) j (range i 6) k (range j 6)] [i j k]) On Sun, Feb 1, 2009 at 8:12 PM, kyle smith wrote: > > The selections function in

Re: removing duplicates from combinatorics/selections

2009-02-01 Thread Mark Engelberg
On Sun, Feb 1, 2009 at 8:25 PM, Mark Engelberg wrote: > (set (map sort (selections [1 2 3 4 5] 3))) or (distinct (map sort (selections [1 2 3 4 5] 3))) if you want to generate the values lazily rather than all at once. --~--~-~--~~~---~--~~ You received t

Re: removing duplicates from combinatorics/selections

2009-02-01 Thread Mark Engelberg
I've written up a fast, iterative version of a "unique-selections" function, consistent with the approach taken in the other combinatorics functions. http://paste.lisp.org/display/74710 I haven't benchmarked it, but I would expect this version to be the fastest of the alternatives mentioned here

Re: Possible bug in preduce

2009-02-03 Thread Mark Engelberg
A month or so ago, I installed the ForkJoin library, and played around with the clojure.parallel wrapper library, and I wasn't able to get a single test to show a speed improvement on my dual core machine. In contrast, pmap, which doesn't rely on the ForkJoin library, works just fine. It makes m

Re: SVN branches

2009-02-03 Thread Mark Engelberg
One thing I couldn't tell from the "lazier" doc is whether rest is only being kept around for backward compatibility or whether there still might be reasons to actively prefer rest to more. --~--~-~--~~~---~--~~ You received this message because you are subscribed

Re: is mod correct?

2009-02-10 Thread Mark Engelberg
I was the source of this error, and I agree that the behavior is an error. I missed the case of a negative divisor and a 0 remainder among my test cases for the mod function. Thanks for noticing and fixing the problem. Although Chouser's version is slightly more compact than Pratley's, I wonder

Re: Fully lazy sequences are coming - feedback wanted!

2009-02-15 Thread Mark Engelberg
On Sun, Feb 15, 2009 at 1:44 PM, Chouser wrote: > (defn my-interpose [x coll] > (loop [v [x] coll coll] >(if (seq coll) ; Don't assume coll is a seq-or-nil > (recur (-> v (conj (first coll)) (conj x)) (rest coll)) > v))) You know, there is an empty? predicate. Why not wr

Re: Fully lazy sequences are coming - feedback wanted!

2009-02-15 Thread Mark Engelberg
My thoughts so far: 1. It always troubled me that filter, when written in the most natural way, had a "hang on to the head" problem when skipping over large numbers of items. I think this is something worth solving, and I'm glad that while developing the lazier branch, you came up with a compil

Re: Fully lazy sequences are coming - feedback wanted!

2009-02-15 Thread Mark Engelberg
On Sun, Feb 15, 2009 at 6:44 PM, Rich Hickey wrote: > I realize you are focused on filter, but that point of the fully lazy > branch is full laziness, which would not fall out of what you > describe. lazy-cons requires the lazy sequence function do all the > work that precedes the call to lazy-co

  1   2   3   4   5   6   7   8   9   10   >