Re: Anyone want to take a crack at making the fasta shootout program faster?

2011-02-13 Thread Isaac Gouy
On Feb 12, 9:51 pm, Michael Gardner  wrote:
-snip-

> Those are all good, but ...

Before I pointed out that code size was already shown, you thought
just including such a measure would possibly "do the trick" but now
that seems not to be good enough for you.


> how many people actually look at anything beyond the fastest implementation 
> for a given language? To some extent this is their problem, sure, ...

Their problem? Isn't it their choice?


> but it's not helped by the prominence the shootout site gives to the fastest 
> implementations in various places.

The site name is given prominence - The Computer Language Benchmarks
Game - but if that isn't what someone wants to see they'll use a
different name.


> In the first place, I'm not even sure what the purpose of all that data is.

There's a response to that at the very top of the Help page.

-snip-
> If it's just for entertainment (as the word "game" seems to imply),

There's a response to that misunderstanding at the foot of the Help
page.



> In my opinion, it's simply not meaningful nor interesting to compare the 
> fastest implementations across different languages.
-snip-

You're welcome to your opinion.


> Perhaps each language could have one "best" implementation selected by the 
> community or by a panel of judges; then the default or most prominent 
> comparisons could use these "best" implementations rather than the fastest 
> ones.
-snip-

I look forward to seeing you publish the results of your proposed
project.

-- 
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: Textmash - another IDE for Clojure

2011-02-13 Thread Olek
Hi,

Yes I can reproduce the bug on MacOS too. I have added :main
textmash.core to project.clj. Now after: lein run, the GUI starts and
all implemented functionalities work fine, but in console I get:

Exception in thread "main" java.lang.NullPointerException
(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:5440)
at clojure.lang.Compiler.eval(Compiler.java:5415)
at clojure.lang.Compiler.eval(Compiler.java:5391)
at clojure.core$eval.invoke(core.clj:2382)
at clojure.main$eval_opt.invoke(main.clj:235)
at clojure.main$initialize.invoke(main.clj:254)
at clojure.main$null_opt.invoke(main.clj:279)
at clojure.main$main.doInvoke(main.clj:354)
at clojure.lang.RestFn.invoke(RestFn.java:422)
at clojure.lang.Var.invoke(Var.java:369)
at clojure.lang.AFn.applyToHelper(AFn.java:165)
at clojure.lang.Var.applyTo(Var.java:482)
at clojure.main.main(main.java:37)
Caused by: java.lang.NullPointerException
at user$eval5.invoke(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:5424)
... 12 more

Looks like a bug in Clojure, since when running textmash in REPL mode,
there are no execeptions.

And about other thoughts.
Yes, I will create seperate discussion group for Textmash.
I agree that extending textmash should be done via plugins.
I thing about communication between plugins and textmash via evetns,
so plugins can know when what happen with what arguments and
eventually can stop or override that event/arguments. Also there will
be core API for text manipulation, menu modification, REPL control,
window and files managment.

Some features posted in my previous e-mail could be done with that
plugins system (as for start text encoding?). So the plugins API would
solve real problems.

Regards,
Olek

On 12 Lut, 06:16, Shantanu Kumar  wrote:
> On Feb 12, 8:26 am, Olek  wrote:
>
> > Hi!
>
> > What is the status of paredit.clj in TextMash? Do you have any time
> > for integration?
> > I have moved project to lein in case anyone care.
>
> I ran TextMash on Windows 7 using Leiningen's (version 1.4) run task:
>
> D:\projects\checkout\textmash>lein run -m textmash.core
>
> It shows up the TextMash GUI window but throws exceptions at the
> terminal. Any pointers?
>
> > Editor pane is pretty much advanced, but still missing: encoding
> > support, files managment, text transformations, multiple repl support
> > and evaluation, brackets and hover highlighting, windows mangment,
> > events support, common edit opertations supoort (undo/redo, find,
> > select).
>
> Are you planning to add any plugin support - is there an interface I
> can look at? Things like files management can probably be implemented
> as plugins(?). Also, I think it perhaps makes sense to set up a
> discussion group for TextMash.
>
> Regards,
> Shantanu

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread James Reeves
On 12 February 2011 18:56, Seth  wrote:
> As the question states - why doesnt defrecord support java class
> inheritence? Is it just because people think it wouldnt be 'good' or
> are there true technical issues with doing it?

It's a design choice. Some languages tie together the concepts of
inheritance and polymorphism together. This tends not to be Clojure's
way of doing things; in Clojure, datatypes are just a mechanism for
polymorphism.

- James

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


Infinite seqs and testing

2011-02-13 Thread Andreas Liljeqvist
I have a statemap with a couple of infinite lazy-seq.
When doing testing I want to check states for equality.
This of course fails on the whole infinite thing.
The sane thing is taking only the first item from the seqs when doing the
equality check.

How would I accomplish this in the easiest way?
I am thinking about making the state a record and redefining equality from
Object.

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: Infinite seqs and testing

2011-02-13 Thread Stuart Sierra
Redefining equality is not something to be taken lightly.  It can break in 
subtle ways with identity and hash codes.

You may want a custom equality function, like "equal up to n elements of a 
sequence," for use in your tests.

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

Re: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Stuart Sierra
I believe there are some technical issues with allowing re-definition of a 
class that inherits from a concrete (non-interface) superclass, but I don't 
know the details.

However, it was mainly a design decision.  Clojure is opinionated, and one 
of its opinions is that concrete inheritance is bad.  It leads to problems 
like fragile base classes, and (in Java at least) it limits you to a single 
inheritance hierarchy.  Even some Java developers will even tell you to use 
inheritance only from interfaces, and put shared code into static methods 
elsewhere.

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

Re: Type hinting question

2011-02-13 Thread Stuart Sierra
Modern JVMs like HotSpot are very good at eliminating unnecessary 
instructions.  Unfortunately, I haven't heard of a good tool for observing 
this process, so you either have to take it on faith or go read the OpenJDK 
source code.

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

Pimp my algorithm: finding repeating subsequences

2011-02-13 Thread Mark Fredrickson
Hello friends,

I am writing a program to generate directions for humans to read. The
directions are composed of a collection with a series of steps, some
of which may be duplicated:

[:a :b :c :a :b :c]

I would like to compress them by indicating repeats, using this
notation:

[[2 [:a :b :c]]

Since the directions will be read by humans (after some formatting), I
am limiting nesting to one level. So the following would be an invalid
transformation:

[:a :b :a :b :c :a :b :a :b :c] => [[2 [2 [:a :b]] :c]]

My current algorithm for the compress has O(2^n)  complexity, which
makes it useless except for the most trivial cases. I'm looking for
something that runs in polynomial time or better. To give some
context, my current algorithm is as follows:

1. Generate the set indices for the power set of the directions (e.g.
(powerset [:a :b :c]) => [[] [1 0 0] [1 1 0] ...]).
2. For each index, use partition-by to generate a set of groups of the
vector (e.g. [1 1 0] => [[:a :b] [:c]]). Since [1 1 0] and [0 0 1] are
identical for my needs, take the distinct groupings (only generating
the distinct groups would only be O(2^{n-1]), so I haven't bothered to
shave computations here).
3. For each grouping, merge identical groups from left to right.
4. Score all the groupings by counting the number of steps (symbols in
these examples). The grouping with the fewest steps is the "best".

I've looked at some string searching algorithms (Boyer-Moore, e.g.),
but these seem geared towards finding a specific substring in a larger
string. Perhaps these algorithms would apply to my problem, but I'm
just not seeing it.

Thanks in advance for your help. All suggestions are welcome.

-Mark

-- 
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: better error messages > smaller stack traces

2011-02-13 Thread Jeff Rose
Here's one:

user=> {:a 1 :b}
#

Instead how about a parse error exception that specifically says
something like, "Map literal requires an even number of forms."

Thanks,
Jeff

On Feb 8, 3:01 pm, Stuart Halloway  wrote:
> This conversation began on Twitter [1] but I want to continue it on the 
> mailing list as it may be of broader interest.
>
> The core team is very interested in improving error messages, and relatively 
> less interested in more code to manipulate stack traces, for the following 
> reasons:
>
> (1) The language sits at the bottom, and must solve the problems nobody 
> higher up can solve. Error messages are created at the point of an error, and 
> if the language gets it wrong you may not have the information to fix it 
> later. OTOH, any old library code can reduce a stacktrace.
>
> (2) Better error messages are easy to implement [2]. Most oddball errors are 
> in macroexpansion, so there is no runtime cost to doing extra work to improve 
> error messages. These can be implemented as pure functions, so even a 
> relative beginner could contribute a patch.
>
> (3) The clojure.repl/pst macro in 1.3 already provides better control of 
> stack trace spewage at the REPL.
>
> Please let us know when you get a misleading error message from a 
> macroexpansion, so we can make it better. Or contribute a patch along the 
> lines of [2].
>
> Thanks,
> Stu
>
> Stuart Halloway
> Clojure/corehttp://clojure.com
>
> [1]http://twitter.com/marick/statuses/33760838540070912
> [2]https://github.com/clojure/clojure/commit/d694d6d45fb46195ae4de01aab9...

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


Request for review: GAHelloWorld

2011-02-13 Thread John Svazic
Thanks again to Alex for helping me with my Clojure mojo in order to
get around a ClassCastException.  I'm writing to request a code review
of my simple "Hello, world!" genetic algorithm simulator written in
Clojure.  I've written an implementation in Java and I have used that
as the basis for my Clojure implementation.  I realize there is more
that I can do with it (using Datatypes and Protocols for example, and
I still need to write up some unit tests), but I figured now would be
a good a time as any to get feedback and any suggestions/enhancements
before I go any further.

The Clojure project can be found here:

https://github.com/jsvazic/GAHelloWorld/tree/master/clojure

The main project can be found here:

https://github.com/jsvazic/GAHelloWorld

This is my first "real" application, aside from some puzzles I've
written for Coderloop.com, so I'm looking forward to any advice/
feedback.  TIA.

-- 
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: Pimp my algorithm: finding repeating subsequences

2011-02-13 Thread Mark Engelberg
This is essentially a compression problem.  I think you want to research the
topic of "Run-length encoding", and then look at algorithms like LZ77 which
handle runs of repeated characters.  LZ77 finds repeated blocks that are
some distance apart; so, for example, you could restrict the algorithm to
look at distances which match the block size, to locate the blocks that
immediately follow one another.

I think you're right that finding the truly optimal instruction set would be
hopelessly time consuming, but it should be possible to draw inspiration
from compression algorithms and write a program that finds a satisfyingly
good instruction set in polynomial time.

I hope that gives you some ideas to explore!

-- 
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: Pimp my algorithm: finding repeating subsequences

2011-02-13 Thread Andy Fingerhut


On Feb 13, 2011, at 10:11 AM, Mark Fredrickson wrote:


Hello friends,

I am writing a program to generate directions for humans to read. The
directions are composed of a collection with a series of steps, some
of which may be duplicated:

[:a :b :c :a :b :c]

I would like to compress them by indicating repeats, using this
notation:

[[2 [:a :b :c]]

Since the directions will be read by humans (after some formatting), I
am limiting nesting to one level. So the following would be an invalid
transformation:

[:a :b :a :b :c :a :b :a :b :c] => [[2 [2 [:a :b]] :c]]

My current algorithm for the compress has O(2^n)  complexity, which
makes it useless except for the most trivial cases. I'm looking for
something that runs in polynomial time or better. To give some
context, my current algorithm is as follows:

1. Generate the set indices for the power set of the directions (e.g.
(powerset [:a :b :c]) => [[] [1 0 0] [1 1 0] ...]).
2. For each index, use partition-by to generate a set of groups of the
vector (e.g. [1 1 0] => [[:a :b] [:c]]). Since [1 1 0] and [0 0 1] are
identical for my needs, take the distinct groupings (only generating
the distinct groups would only be O(2^{n-1]), so I haven't bothered to
shave computations here).
3. For each grouping, merge identical groups from left to right.
4. Score all the groupings by counting the number of steps (symbols in
these examples). The grouping with the fewest steps is the "best".

I've looked at some string searching algorithms (Boyer-Moore, e.g.),
but these seem geared towards finding a specific substring in a larger
string. Perhaps these algorithms would apply to my problem, but I'm
just not seeing it.

Thanks in advance for your help. All suggestions are welcome.

-Mark


Here are some ideas that won't necessarily give an optimal answer,  
i.e. a shortest one, but sometimes it will, and even when it doesn't  
it should give something reasonable.


There are O(n^2) subsequences of your original sequence.

Step 1. For each one, you can in linear time determine whether that  
subsequence repeats immediately afterwards, and if so, how many times  
it repeats.  Calculate those answers R[i,j] for each of the O(n^2)  
subsequences and save them, e.g. in a 2-d array indexed by start  
position i and end position j.  That should take O(n^3) time in the  
worst case, but often closer to O(n^2) depending upon the sequence you  
have.


Example: I'll use just strings of characters to represent the  
sequences, to save typing.  So your sequence  
[:a :b :a :b :c :a :b :a :b :c] above I will write as: ababcababc


If we use [i,j] to represent a subsequence starting at index i (0- 
based, so the first element of the sequence is at 0) and up to but not  
including the element at index j, then [1,4] represents the  
subsequence "bab".


Here is part of the table calculated in part 1:

R[0,1] 1 (i.e. "a" repeats 1 time starting at index 0, consecutively.   
It does not immediately repeat after that)

R[0,2] 2 (i.e. "ab" repeats 2 times starting at index 0, consecutively.)
R[0,3] 1 ("aba" appears only 1 time starting at index 0, consecutively.)
R[0,4] 1
R[0,5] 2
R[0,6] 1
etc.

Step 2. Now go over every subsequence again, this time looking for the  
shortest way to represent that subsequence using your notation.  This  
will depend upon exactly what you consider the quantitative "savings"  
is for your repetition notation.  In this example, I'll use the number  
of characters used to write down the notation.


So ababcababc has "cost" 10
but 2[ababc] has cost 8 (I'm counting the digits and the square  
brackets, too -- you can choose differently in whatever algorithm you  
choose to implement)


For [0,10], we can either represent it as its original 10-element  
sequence (cost 10), or maybe as 2 repetitions of [0,5], or maybe as 5  
repetitions of [0,2].  In general, a sequence of length N can either  
be represented as itself, or for each value K that evenly divides N,  
as (N/K) repetitions of the sequence of length K at its beginning.   
For each one of those possibilities, use table R to tell in constant  
time which of those is possible and which is not.


[0,10] - cost 10
2 repetitions of [0,5]? - Yes, this is possible, because R[0,5] >= 2.
5 repetitions of [0,2]? - No, impossible, because R[0,2] < 5.

So for [0,10], there are two ways to represent it: cost 10 in its  
original unrepeated form, or cost 8 for "2[ababc]".


Let us make a table S[i,j] for all subsequences that tells us how much  
we can save by representing [i,j] (the whole thing, not just part of  
it) as a repetition.


So S[i,j]=10-8=2, because we can save 2 characters by using the  
repetition 2[ababc] in place of writing out the full sequence.


Part of table S would look like this:

S[0,1]=0   No shorter way than writing out "ab", so no savings
S[0,2]=0  No way to write _all_ of "aba" as a repetition, so no  
savings possible.
S[0,3]=0  We could write "abab" as that or as "2[ab]", but both have  
cost 

Re: Pimp my algorithm: finding repeating subsequences

2011-02-13 Thread Fred Concklin
DEFLATE is a compression method that combines LZ77 with Huffman
coding. It is implemented in java.util.zip. 

Read about it here:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/package-summary.html

fpc

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


Append a # and a string

2011-02-13 Thread Christopher Maggio
Hi, clojure newbie here;

I have a quick question, I am writing a small pattern matching program
and I can't figure out how to append a string to a # for re-matcher:

(.replaceAll (re-matcher #"ATGCCC" my_String) replacement_String)

When I try (string? #"ATGCCC") it returns false.

# isnt a macro symbol is it? Thanks in advace

Chris

-- 
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: Infinite seqs and testing

2011-02-13 Thread Andreas Liljeqvist
Roger that.

Thanks for the help.

2011/2/13 Stuart Sierra 

> Redefining equality is not something to be taken lightly.  It can break in
> subtle ways with identity and hash codes.
>
> You may want a custom equality function, like "equal up to n elements of a
> sequence," for use in your tests.
>
> -Stuart Sierra
> 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

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

noob Echo Server coded, need help to figure out if there is better/idiomatic way of doing things.

2011-02-13 Thread Jigar Gosar
* I think io-loop function can be improved/simplified.
* I usually don't like duplication (.readLine in) is being repeated in
io-loop.
* any other comments/links/reference to some other idiomatic code are
welcome.

(ns echo.server
  [:use clojure.contrib.server-socket clojure.java.io
clojure.contrib.logging])

(defn line-received [line out]
  (info line)
  (doto out (.write line) (.newLine))
  (.flush out))

(defn connection-closed [out]
  (info "Client connection closed."))

(def running (ref true))

(defn io-loop [in out]
  (loop [line (.readLine in)]
(when @running
  (if (= line nil)
(connection-closed out)
(do
  (line-received line out)
  (recur (.readLine in)))


(defn client-handler [in-stream out-stream]
  (try
(io-loop (reader in-stream) (writer out-stream))
(catch Throwable e (.printStackTrace e)))
  )
(info "starting server ...")
(create-server 8080 client-handler)
(info "server started")

-- 
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: Append a # and a string

2011-02-13 Thread Fred Concklin
Christopher Maggio  writes:

> Hi, clojure newbie here;
>
> I have a quick question, I am writing a small pattern matching program
> and I can't figure out how to append a string to a # for re-matcher:
Not quite following, but check out re-pattern
(http://clojuredocs.org/clojure_core/clojure.core/re-pattern). 
>
> (.replaceAll (re-matcher #"ATGCCC" my_String) replacement_String)
>
> When I try (string? #"ATGCCC") it returns false.
It should return false; it is not a string.

  user=> (string? #"ATGCCC")
  false
  user=> (type #"ATGCCC")
  java.util.regex.Pattern
  user=> (string? "ATGCCC")
  true
>
> # isnt a macro symbol is it? Thanks in advace
Check out the Macro characters -> Dispatch section on this page:

http://clojure.org/reader

fpc

-- 
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: Request for review: GAHelloWorld

2011-02-13 Thread Stefan Kamphausen
Hi,

you may be interested in reading the code from our book which a) includes 
the code for doing exactly what you did (evolve a string) and b) shows how 
multithreading can change your way of doing GAs (get rid of the concept of 
generations).

The book and the code are written in German which may pose a problem.

However, you can find the code at 
http://clojure-buch.de/listings/concurrency.txt starting with Snippet #33.

Kind regards,
Stefan

-- 
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: Append a # and a string

2011-02-13 Thread Christopher Maggio
Thanks, just what i was looking for!

-Chris

On Feb 13, 9:04 pm, Fred Concklin  wrote:
> Christopher Maggio  writes:
> > Hi, clojure newbie here;
>
> > I have a quick question, I am writing a small pattern matching program
> > and I can't figure out how to append a string to a # for re-matcher:
>
> Not quite following, but check out re-pattern
> (http://clojuredocs.org/clojure_core/clojure.core/re-pattern).
>
> > (.replaceAll (re-matcher #"ATGCCC" my_String) replacement_String)
>
> > When I try (string? #"ATGCCC") it returns false.
>
> It should return false; it is not a string.
>
>   user=> (string? #"ATGCCC")
>   false
>   user=> (type #"ATGCCC")
>   java.util.regex.Pattern
>   user=> (string? "ATGCCC")
>   true
>
> > # isnt a macro symbol is it? Thanks in advace
>
> Check out the Macro characters -> Dispatch section on this page:
>
> http://clojure.org/reader
>
> fpc

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Seth
Cool, thats what I thought. Sometimes, i wish i could inherit from a
class, but then i just use proxy to do it instead :) For example, i
made a function which acted like a fn? , but had some metadata. At
first i was going to use records, but couldnt because i needed to
inherit from Afn, but proxies worked beautifully!

On Feb 13, 9:00 am, Stuart Sierra  wrote:
> I believe there are some technical issues with allowing re-definition of a
> class that inherits from a concrete (non-interface) superclass, but I don't
> know the details.
>
> However, it was mainly a design decision.  Clojure is opinionated, and one
> of its opinions is that concrete inheritance is bad.  It leads to problems
> like fragile base classes, and (in Java at least) it limits you to a single
> inheritance hierarchy.  Even some Java developers will even tell you to use
> inheritance only from interfaces, and put shared code into static methods
> elsewhere.
>
> -Stuart Sierra
> 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


Best method for storing dynamic data in database

2011-02-13 Thread Seth
I have an application which contains a number of running threads, each
which has a state to it. A state is a single record. It contains some
random stuff, but most importantly it a has a :var slot which contains
a map in which the keys are vars and the values are whatever the vars
are (if they can be dynamically updated, they are atoms/refs types
otherwise they are simple constants).

I have tried a simple print-dup method. This works fine when I dont
have java objects in it. But when i have things like DateTimes and
such, it doesnt work. On the other side of things, serializing wont
work for things like functions  (i think) that are a value  in the
map. When i print-dup, functions work fine , because they output
something like (find-symbol ). So, i need a combination of print-
dup for clojure objects and serializing for java objects.

Is there any solution out there that does this, saving it all to a
database?  I was thinking that a special record could be produced
which wraps around the clojure objects when serializing and then when
deserializing, it could do what one does with print-dup method - and
for regular java objects, simple serialization could be used.

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Ken Wesson
On Sun, Feb 13, 2011 at 7:00 PM, Seth  wrote:
> Cool, thats what I thought. Sometimes, i wish i could inherit from a
> class, but then i just use proxy to do it instead :) For example, i
> made a function which acted like a fn? , but had some metadata. At
> first i was going to use records, but couldnt because i needed to
> inherit from Afn, but proxies worked beautifully!

If you controlled the testing code, could you have used ifn? instead
of fn?, or would ifn? returning true on things like vectors, maps, and
sets have caused a problem?

The ifn? function tests only for the interface IFn, which deftype can
be used to implement (and defrecord implements by default as map
lookup).

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Seth
I already have code which uses fn? everywhere. So i needed to inherit
Fn interface. Adding metadata to the function (basically, an id) was a
necessary afterthought. Basically, im using an emacs like hook
interface for the gui end of my application, so its hightly
configurable - but i need to know which namespace the function can
from (or id, if supplied), so that i can overwrite the hook when
adding a hook with similar ns/id.

On Feb 13, 9:08 pm, Ken Wesson  wrote:
> On Sun, Feb 13, 2011 at 7:00 PM, Seth  wrote:
> > Cool, thats what I thought. Sometimes, i wish i could inherit from a
> > class, but then i just use proxy to do it instead :) For example, i
> > made a function which acted like a fn? , but had some metadata. At
> > first i was going to use records, but couldnt because i needed to
> > inherit from Afn, but proxies worked beautifully!
>
> If you controlled the testing code, could you have used ifn? instead
> of fn?, or would ifn? returning true on things like vectors, maps, and
> sets have caused a problem?
>
> The ifn? function tests only for the interface IFn, which deftype can
> be used to implement (and defrecord implements by default as map
> lookup).

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Seth
oh, and of course the funuction needs to be executable (they are after
all just functions with metadata) - so we cant use ifn?, which would
return true on record!

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Seth
And just for the heck of it, if anyones interested - heres the hook
code.

(ns forex.util.emacs))
(defmacro is [val & message]
  `(let [result# ~val]
 (if (not result#)
   (throw (Exception. ~(or (and (first message) `(format
~@message)) (format "assert: %s" (str val)
   result#)))

(defn fn-meta [function meta]
  (let [ns-from *ns*]
(proxy [clojure.lang.AFn clojure.lang.Fn  clojure.lang.IMeta] []
  (invoke [& args] (apply function args))
  (meta [] (merge (meta function) (merge {:ns ns-from} meta))

(defn- val-of [a] (if (var? a) (var-get a) a))
;;TODO: add log to this
(defn- apply-fn [a args]
  (try (apply (val-of a) args)
   (catch Exception e
 (println (format "error in hook %s %s: %s" a (val-of a)
e)
(defmacro- run-fn [a & args]
  `(let [a# ~a]
 (try ((val-of a#) ~@args)
  (catch Exception e# (println
   (format "error in hook %s %s: %s" a#
(val-of a#) e#))

(defn- as-ns [a]
  (condp = (class a)
  String (find-ns (symbol a))
  clojure.lang.Symbol (find-ns a)
  a))
(defn ns-metas
  ([fn] (ns-metas *ns* fn))
  ([ns fn]
 (is (as-ns ns) "%s is not a ns, or cant find it" ns)
 (let [vars (filter fn (vals (ns-interns (as-ns ns]
   (apply hash-map (interleave vars (map var-get vars))
(defn ns-vars
  ([] (ns-vars *ns*))
  ([ns]
 (is (as-ns ns) "%s is not an ns, or cant find it" ns)
 (let [vars (filter #(:var (meta  %)) (vals (ns-interns (as-ns
ns]
   (apply hash-map (interleave vars (map var-get vars))

(defmacro defvar
  ([name]
 (let [new-name (with-meta name (assoc (meta name) :var true))]
   `(defonce ~new-name nil)))
  ([name init]
 (let [new-name (with-meta name (assoc (meta name) :var true))]
   `(defonce ~new-name ~init
(defmacro defhook [& args] `(defvar ~@args))
(defmacro setq [& args]
  `(do ~@(map
  (fn [[var val]]
`(alter-var-root #'~var (fn [a#] ~val)))
  (group args


(defn- member
  ([value list] (member value list =))
  ([value list test]
 (some #(test % value) list)))

(defn- pushnew* [hook arg id replace]
  (if replace
(alter-var-root hook (fn [old]
   (doall (concat
   (list arg)
   (filter #(and (if id (not (= (:id (meta %)) 
id)) true)
(not (= % arg))) old)
(alter-var-root hook (fn [old]
   (doall (if (empty? (take 1 (filter #(or (when id (= 
(:id (meta
%)) id)) (= % arg)) old)))
(concat (list arg) old)
old))
(defmulti pushnew (fn [a b & args] [(if (fn? a) ::fn (type a)) (if
(fn? b) ::fn (type b))]))
(defmethod pushnew [clojure.lang.Var ::fn]
  ([hook function] (pushnew hook function *ns* true))
  ([hook function id] (pushnew hook function id true))
  ([hook function id replace]
 (let [new-function (if (:id (meta function)) function (if id (fn-
meta function {:id id}) function))]
   (pushnew* hook new-function id replace
(defmethod pushnew [clojure.lang.Var clojure.lang.Var]
  ([hook var] (pushnew hook var nil false))
  ([hook var id] (pushnew hook var id false))
  ([hook var id replace] (pushnew* hook var id replace)))


(defn- add-to-list* [hook arg id replace]
  (if replace
(alter-var-root hook (fn [old]
   (doall (concat
   (filter #(and (if id (not (= (:id (meta %)) 
id)) true)
(not (= % arg))) old)
   (list arg)
(alter-var-root hook (fn [old]
   (doall (if (empty? (take 1 (filter #(or (when id (= 
(:id (meta
%)) id)) (= % arg)) old)))
(concat old (list arg))
old))
(defmulti add-to-list (fn [a b & args] [(if (fn? a) ::fn (type a)) (if
(fn? b) ::fn (type b))]))
(defmethod add-to-list [clojure.lang.Var ::fn]
  ([hook function] (add-to-list hook function *ns* true))
  ([hook function id] (add-to-list hook function id true))
  ([hook function id replace]
 (let [new-function (if (:id (meta function)) function (if id (fn-
meta function {:id id}) function))]
   (add-to-list* hook new-function id replace
(defmethod add-to-list [clojure.lang.Var clojure.lang.Var]
  ([hook var] (add-to-list hook var nil false))
  ([hook var id] (add-to-list hook var id false))
  ([hook var id replace] (add-to-list* hook var id replace)))

(comment
  (defn push [var val]
(alter-var-root var (fn [it] (concat (list val) it)

(defn add-hook [hook function] (pushnew hook function))
(defn add-hooks [hook functions] (doall (map #(pushnew hook %)
functions)))

;;RUNNING hooks
(defn run-hooks [& hooks]
  (mapc (fn [hook] (mapc #(run-fn %) hook)) hooks))

(defn run-hook-with-args [hook & args]
  (mapc #(ap

Re: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Seth
and of course i forgot to include some utility functions. oh well...

-- 
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: Best method for storing dynamic data in database

2011-02-13 Thread Ken Wesson
On Sun, Feb 13, 2011 at 8:39 PM, Seth  wrote:
> I have an application which contains a number of running threads, each
> which has a state to it. A state is a single record. It contains some
> random stuff, but most importantly it a has a :var slot which contains
> a map in which the keys are vars and the values are whatever the vars
> are (if they can be dynamically updated, they are atoms/refs types
> otherwise they are simple constants).
>
> I have tried a simple print-dup method. This works fine when I dont
> have java objects in it. But when i have things like DateTimes and
> such, it doesnt work. On the other side of things, serializing wont
> work for things like functions  (i think) that are a value  in the
> map. When i print-dup, functions work fine , because they output
> something like (find-symbol ). So, i need a combination of print-
> dup for clojure objects and serializing for java objects.
>
> Is there any solution out there that does this, saving it all to a
> database?  I was thinking that a special record could be produced
> which wraps around the clojure objects when serializing and then when
> deserializing, it could do what one does with print-dup method - and
> for regular java objects, simple serialization could be used.

Is print-dup a multi-method or a protocol and your set of Java objects
closed? If so, you can extend print-dup to the Java objects to output
#=(JavaClass. constructor args) or whatnot; if the set of Java objects
is open-ended you may have to resort to #=(.readObject
(ObjectInputStream. (ByteArrayInputStream. (binhex/decode
"4JK09bX38dQ1jl...". :)

(Implementation of binhex/decode, and of the encoding half of all of
this, is left as an exercise for the reader.)

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Ken Wesson
On Sun, Feb 13, 2011 at 10:14 PM, Seth  wrote:
> oh, and of course the funuction needs to be executable (they are after
> all just functions with metadata) - so we cant use ifn?, which would
> return true on record!

The map? function also returns true on a record, and false on a deftype, so

#(and (ifn? %) (not (or (map? %) (vector? %) (set? %) (keyword? %

looks like it might fit the bill as a predicate that excludes records
and normal maps (along with sets, vectors, and keywords) and includes
"true" functions and deftypes that implement IFn (but not
IPersistentMap, that makes map? return true).

As for fn? being all through your code -- might I suggest search and
replace? Failing that there's always

(ns foo
  (:refer-clojure :exclude [fn?])
  ...)

(defn fn? [x]
  (and (ifn? x) (not (or (map? x) (vector? x) (set? x) (keyword? x)

or something of the sort.

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Ken Wesson
As for being able to attach metadata, you just have to implement IObj
on your functoids.

-- 
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: Pimp my algorithm: finding repeating subsequences

2011-02-13 Thread Mark Fredrickson
Thanks for the hint on LZ77. That was exactly what I was looking for.

Thank you also to the others who provided this and other answers.

Thanks!
-Mark


On Feb 13, 1:00 pm, Mark Engelberg  wrote:
> This is essentially a compression problem.  I think you want to research the
> topic of "Run-length encoding", and then look at algorithms like LZ77 which
> handle runs of repeated characters.  LZ77 finds repeated blocks that are
> some distance apart; so, for example, you could restrict the algorithm to
> look at distances which match the block size, to locate the blocks that
> immediately follow one another.
>
> I think you're right that finding the truly optimal instruction set would be
> hopelessly time consuming, but it should be possible to draw inspiration
> from compression algorithms and write a program that finds a satisfyingly
> good instruction set in polynomial time.
>
> I hope that gives you some ideas to explore!

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Seth
whats wrong with implementing Fn and IMeta?

On Feb 13, 9:35 pm, Ken Wesson  wrote:
> As for being able to attach metadata, you just have to implement IObj
> on your functoids.

-- 
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: Help - .class files and GAE's "too many files" problem

2011-02-13 Thread Constantine Vetoshev
On Feb 10, 7:47 pm, Edgar Gonçalves  wrote:
> So for future reference, if you come up with the same issue, the solution is
> to compile your gen'ed-classes and delete all the others from the
> war/WEB-INF directory, making sure you have a standalone jar along the
> remaining used jars (a uberjar may also work, can't see why not). If you
> happen to have a file-size problem on your jars, you can use a flag on the
> upload script. Google for "--enable_jar_splitting" for more details.

I realize you already solved the problem in your app, but for the
benefit of others reading this thread: appengine-magic takes care of
this for you. It bundles loose class files into you
application's .jar. This has the added benefit of making deployment
much faster. Unless you need AOT compilation for gen-class, you never
have to worry about managing compiled files.

http://github.com/gcv/appengine-magic

-- 
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: Best method for storing dynamic data in database

2011-02-13 Thread Seth
any serializable java object should be supported. hmmm... i guess that
would work - simply output the object as a byte string and then decode
that.

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Ken Wesson
On Sun, Feb 13, 2011 at 11:26 PM, Seth  wrote:
> whats wrong with implementing Fn and IMeta?

I don't know. But when I checked a normal function's supers I saw IFn,
AFn, and AFunction; and when I checked a vector's I saw only IFn of
those three. I also saw IObj, did a .getMethods on it, and the methods
there were obviously for getting and setting metadata.

If there's also a Fn and an IMeta then there seems to be some
redundancy, on the one hand between IMeta and IObj and on the other
some among IFn, Fn, AFn, and AFunction -- perhaps the first two and
the latter two. I mean, either objects take metadata or they don't, so
a single metadata-enabling interface seems to suffice, and either
objects are "true functions" that should produce true from fn?, are
callable and produce true from ifn? but are not "true functions", or
are neither, which the IFn interface (ifn?) and either the AFn or the
AFunction class (fn?) would seem to suffice to distinguish.

Perhaps someone who knows more about Clojure's internals can shed some
light on why all these interfaces and classes exist and whether and
how they serve distinct purposes.

-- 
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: any good reason why defrecord/deftype doesn't support java class inheritence?

2011-02-13 Thread Seth
I know that IMeta is used with the (meta) function and with IObj, you
can also implement with-meta. And fn? tests for 'true functions' ,
i.e. only the function created by (fn). Not sure about the rest...

-- 
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: Best method for storing dynamic data in database

2011-02-13 Thread Seth
not fast, but it works:) any ideas on better code?

;code from 
http://www.mail-archive.com/clojure@googlegroups.com/msg19378.html
;;saving objects to strings and going back -slow, but it works
(import (java.io ByteArrayOutputStream ObjectOutputStream
 ByteArrayInputStream ObjectInputStream))

(use 'clojure.contrib.duck-streams)

(defn str-to-bytes [s]
  (.getBytes s))

(defn str-from-bytes [b]
  (new String b))

; encode a raw array of bytes as a base64 array of bytes
(defn encode64 [b]
  (. (new sun.misc.BASE64Encoder) encode b))

; decode a string encoded in base 64, result as array of bytes
(defn decode64 [s]
  (let [decoder (new sun.misc.BASE64Decoder)]
(. decoder decodeBuffer s)))

(defn obj-to-bytes
   "convert object to string"
   [obj]
   (with-open [bos (ByteArrayOutputStream.)
   stream (ObjectOutputStream. bos)]
 (.writeObject stream obj)
 (.flush stream)
 (.toByteArray bos)))

(defn bytes-to-obj
  "convert string to object"
  [s]
  (.readObject
   (ObjectInputStream.
(ByteArrayInputStream.
 s

; compress human readable string and return it as base64 encoded
(defn encode [s]
  (let [b (obj-to-bytes s)
output (new java.io.ByteArrayOutputStream)
deflater (new java.util.zip.DeflaterOutputStream

  output
  (new java.util.zip.Deflater) 1024)]
(. deflater write b)
(. deflater close)
(str-from-bytes (encode64 (. output toByteArray)

; take an object that was compressed & base64 encoded.. and undo
allthat
(defn decode [s]
  (let [b (decode64 s)
input (new java.io.ByteArrayInputStream b)
inflater (new java.util.zip.InflaterInputStream
  input
  (new java.util.zip.Inflater) 1024)
result (to-byte-array inflater)]
(. inflater close)
(bytes-to-obj result)))
(defmethod print-dup :default [o w]
  (.write w (str "#=(forex.module.ea/decode \"" (encode o) "\")")))

-- 
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: Best method for storing dynamic data in database

2011-02-13 Thread Seth
or i could have just used base64:) I love leiningen +maven+central
repository!

(import (java.io ByteArrayOutputStream ObjectOutputStream
 ByteArrayInputStream ObjectInputStream)
org.apache.commons.codec.binary.Base64)
(defn bytes-to-obj
  "convert string to object"
  [s]
  (.readObject
   (ObjectInputStream.
(ByteArrayInputStream.
 s
(defn obj-to-bytes
   "convert object to string"
   [obj]
   (with-open [bos (ByteArrayOutputStream.)
   stream (ObjectOutputStream. bos)]
 (.writeObject stream obj)
 (.flush stream)
 (.toByteArray bos)))
(defn encode [o]
  (Base64/encodeBase64String (obj-to-bytes o)))
(defn decode [o]
  (bytes-to-obj (Base64/decodeBase64 o)))
(defmethod print-dup :default [o w]
  (.write w (str "#=(forex.module.ea/decode \"" (encode o) "\")")))

-- 
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: Newbie: What's your opinion about this Clojure code?

2011-02-13 Thread Nick Wiedenbrueck
Thanks a lot.

I'll have to get used to closing all parentheses of a function on a
single line, cause this makes finding the right position when editing
the code afterwards a little harder. Also now I got the clojure mode
for emacs (instead of lisp mode) to get the indentation right.

-- 
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: Handling of unsigned bytes

2011-02-13 Thread Rasmus Svensson
To turn a signed byte (-128 to 127) into an unsigned one:

(bit-and the-byte 0xff)

The byte (for example 0x80, which is negative) will be extended to an
int (0xff80) and anded with 0x00ff (and you get 0x0080,
which is positive).

The javadoc for the methods of DataInput[1] contain formulas for how
to convert shorts and ints and longs too. DataOutput[2] contains
comments for the opposite opereration.

1. http://download.oracle.com/javase/6/docs/api/java/io/DataInput.html
2. http://download.oracle.com/javase/6/docs/api/java/io/DataOutput.html

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