Re: Arithmetic simplifier

2018-08-29 Thread bernardH
As an example of how to use intaparse to parse arithmetic expressions 
(among other things), you might be interested in this 
notebook 
https://mybinder.org/v2/gh/bhugueney/binder-test/master?filepath=Clojure.ipynb

On Saturday, August 18, 2018 at 2:02:25 PM UTC+2, ckry...@gmail.com wrote:
>
> Hi, 
> I am ne to Clojure, and trying to make arithmetic simplifier
> so it can take string in for `1x + 3y -4x + 8 -1y` and return "-3x + 2y +8"
> Currently, I am stuck with a `make-ast` function, where load-string loads.
> it can't load string with variables. 
>
> (def precedence '{* 0, / 0
>   + 1, - 1})
>
> (def ops {'* *
>   '+ +
>   '- -
>   '/ /})
>
> (defn order-ops
>   "((A x B) y C) or (A x (B y C)) depending on precedence of x and y"
>   [[A x B y C & more]]
>   (let [ret (if (<=  (precedence x)
>  (precedence y))
>   (list (list A x B) y C)
>   (list A x (list B y C)))]
> (if more
>   (recur (concat ret more))
>   ret)))
>
> (defn add-parens
>   "Tree walk to add parens.  All lists are length 3 afterwards."
>   [s]
>   (clojure.walk/postwalk
>#(if (seq? %)
>   (let [c (count %)]
> (cond (even? c) (throw (Exception. "Must be an odd number of forms"))
>   (= c 1) (first %)
>   (= c 3) %
>   (>= c 5) (order-ops %)))
>   %)
>s))
>
> (defn make-ast
>   "Parse a string into a list of numbers, ops, and lists"
>   [s]
>   (-> (format "'(%s)" s)
>   (.replaceAll , "([*+-/])" " $1 ")
>   load-string
>   add-parens))
>
>
> (defn simplify-seq
>   "Simplify sequence of vars"
>   [s]
>   (println s)
>   )
>
> (def eval-ast
>   (partial clojure.walk/postwalk
>#(if (seq? %)
>   ;(simplify-seq %)
>   (let [[a o b] %]
> ((ops o) a b))
>   %)))
>
> (defn evaluate [s]
>   "Parse and evaluate an infix arithmetic expression"
>   ;(eval-ast (make-ast s))
>   (def ast (make-ast s))
>   (println ">>> DEBUG AST:" ast)
>   (let [res (eval-ast ast)]
> (println ">>> DEBUG res:" res )))
>
>
> (defn -main
>   "Read from STDIN"
>   [& args]
>   ;
>   ;(loop [i 0]
>   ;  (when (< i n)
>   ;(def a (read-line))
>   ;(def new (split a #"\s+"))
>   ;(println ( + (Integer/parseInt (get new 0)) (Integer/parseInt (get new 
> 1)) ))
>   ;(recur (inc i))
>   ;))
>
>   (loop [input (read-line)]
> (if (= ":done" input)
>   (println (evaluate input))
>   (recur (read-line)
>
>

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Reflection needed ? More precise types returned from higher-order function (i.e. comp and partial) ?

2016-07-30 Thread bernardH
Hi Karsten,

Thank you for taking the time to walk me through some of the goodies that 
await me in thi.ng !

I do agree that matrix multiplications are the way to go for efficiently 
applying a bunch of geometric transformations on a number of points.
Actually, I intended to do it when the nb of transforms n more points that 
would be displayed i.e. when going 3D. I assumed that for 2D lines, the svg 
display would dwarf the transformations computation (aside : any way to 
assess this assumption ? I know nothing of profiling Clojurescript in a 
browser).

I also look forward to use your mix function to compute animations for 2d 
polygon games (a reimplementation of a game engine à la 
[[https://en.wikipedia.org/wiki/Another_World_%2528video_game%2529][Another 
World]] ).

However, for the task at hand, I want to decompose the transforms just the 
way I composed them to make apparent the composition itself. This is for a 
class when I want to teach programming as composing (a sort of intro to the 
spirit of functional programming, even if in Python because of the 
curriculum). The kind of result I wanted to achieve can be seen in 
https://scientific-coder.github.io/fractals/resources/public/fractals.html 
(I took model on your 
https://github.com/thi-ng/demos/blob/master/geom/src/physics_demos/strands.cljs,
 
hoping you would not mind).

In order to do that in a generic way (i.e. working on any kind of 
transformations composition), I have to work on the "AST" of the composite 
transformations, and walk it to adjust the parameters according to the step 
(I could/should also walk the "AST" to "compile it to matrix multiplication.
The thing is that I already have the "AST" as the composition of partial 
applications of the functions ( e.g. 
https://github.com/scientific-coder/fractals/blob/master/src/animations_2d/fractals.cljs#L128
 
) and I can easily walk (e.g. with multimethods 
https://github.com/scientific-coder/fractals/blob/master/src/animations_2d/fractals.cljs#L150
 
) if it  exposes the data the it has to hold anyway. The cleanest way might 
be to use a "real" DSL (like you do with your L-system interpreter in 
https://gist.github.com/postspectacular/47d62cc0386452fe3f9f0465c1fb1b73#file-lsys-clj
 
), but I think that better extensibility/interoperability would have been 
achieved with a standard access to partial, comp, juxt & al. attributes. 
Even python, not really the weapon of choice for functional programming, 
gives access to the bound arguments of partial ( 
https://docs.python.org/3/library/functools.html#partial-objects ).

Anyway, thanks again for the amazing thi.ng libraries, I'm having great fun 
with it, and intend to use them a lot .(Now, if only I found a way to have 
clojurescript code create/update svg / Webgl in my 
[[https://github.com/yjwen/org-reveal][org-reveal]] slides… )

I look forward to hooking students to generative programming thanks to you 
☺!

Best Regards,

bernard


On Tuesday, June 28, 2016 at 4:09:07 PM UTC+2, Karsten Schmidt wrote:
>
> Hi Bernard, 
>
> firstly, thanks for the kind words! I think you're overcomplicating 
> your problem and the extreme use of comp & partial would not help 
> making things run v.smoothly (obviously depending on number of 
> generated points / line segments). Instead I think you should look 
> into matrix based transformations, which, once constructed, have an 
> O(1) cost, no matter how complex the transformation. E.g. With 
> http://thi.ng/geom you can create a matrix, encoding a combined 
> translation, rotation and scale op like this: 
>
> ;; assumes [thi.ng/geom "0.0.1173-SNAPSHOT"] 
> (require 
>   '[thi.ng.geom.core :as g] 
>   '[thi.ng.geom.matrix :as mat] 
>   '[thi.ng.math.core :as m]) 
>
> ;; for 2D: M32 is a 3x2 identity matrix 
> (def tx (-> mat/M32 (g/translate [10 20]) (g/rotate Math/PI) (g/scale 
> 10))) 
>
> ;; for 3D: M44 is 4x4 identity matrix 
> (def tx (-> mat/M44 (g/translate [10 20 30]) (g/rotate-x m/HALF_PI) 
> (g/scale 10))) 
>
> Then to transform a point: 
> (g/transform-vector tx [x y z]) 
>
> To reverse the transformation, simply apply the matrix inverse to a 
> transformed point: 
> (g/transform-vector (m/invert tx) [x' y' z']) 
>
> This approach also makes it trivial to create animations, simply keep 
> a reference to the untransformed elements, then compute a matrix for 
> each animation frame, transform & visualize them... 
>
> If you just want to tween between a two sets of elements (e.g. the 
> original and transformed elements), you can use the `mix` protocol 
> function defined for all vector types (2D/3D) in thi.ng/geom: 
>
> (require '[thi.ng.geom.vector :as v]) 
>
> (defn tween-points 
>   [src dest t] (map (fn [a b] (m/mix a b t)) src dest)) 
>
> (tween-points [(v/vec2 0 0) (v/

Reflection needed ? More precise types returned from higher-order function (i.e. comp and partial) ?

2016-06-28 Thread bernardH

Hi,

Inspired by Karsten Schmidt's amazing work[0], I'm thinking about porting 
some generative
code from python to Clojure[script].

However, I would like to use the (expected) performance boost (from python
turtle module) to add some animation effects. I believe my current code is 
quite clean
and would like to avoid the second-system effect [1]

I currently have some very generic fractal generators taking some geometric 
transforms as arguments.
Those geometric transforms are composition of curryfied primitive 
operations (rotate, add, zoom), as in 
(def transfo (compose (partial rotate pi) (partial add [0. 1.]) (partial 
zoom (/ 1 3

My wish would be to be able to compute smooth animations by "scaling" 
smoothly
the composite transformations between identity (no transformation) and the 
full
(original) transformation (if fact I would also use the opposite 
transforms, the
one that would compose to identity). I would do so by scaling all the 
parameters involved 
with a scaling factor in [0 …1] ([0…-1] for opposite transforms) either by
multiplication (for "additive" transformations like rotate and add) or by
exponentiation (for "multiplicative" transformations like zoom).

"Half" of the above "transfo" would thus be :
(def transfo_half (comp (partial rotate (deep_multiply 0.5 pi)) (partial 
add (deep_multiply 0.5 [0. 1.])) (partial zoom (Math.pow (/ 1 3) 0.5

with

(defn deep_f [f x] (if (seq? x) (map deep_f x) (f x)))

(defn deep_multiply [k x] (deep_f (partial * k) x))

to unify meutiplication because for additive functions, some params are 
vectors (for add) while others are scalars (for rotate).

and the opposite of the original full transformation would be :
(def transfo_inv (comp (partial rotate (deep_multiply -1 pi)) (partial add 
(deep_multiply -1 [0. 1.])) (partial zoom (Math.pow (/ 1 3) -1


The ideal solution for me would be to be able to write a "scale" function 
so that :

(def transfo_half (scale 0.5 transfo))
(def transfo_inv (scale -1 transfo))

The scale function could look like

(def scale [k f]
  (condp is f
   comp (apply comp (map (partial scale k) (args f)))
   partial (let[ [ fun bound] (args f)] (if (or (is f add) (is f rotate)) 
(partial f (deep_multiply k bound )) (if (is f zoom) (partial f (Math.pow 
bound k)) f)))
   f)
)

 But that would require some help from the values returned by comp and 
partial : 
- the ability to identify them as such (above with an "is" function)
- the ability to access the bound values passed to them as arguments (above 
with an "args" function)

Of course, I would have to shadow clojure.core/comp and 
clojure.core/partial to
add those functionalities. I suppose I would have to create records and
protocols. However, I find this a bit cumbersome and thought that there 
might be
a simpler way to achieve my goal of keeping the transformation generation
separate from the animation code that must scale those functions. 
Piggiebacking metadata might be easier if more hackish.

Best would be to be able to write a higher order function (or macro) like:

(def comp (with-is-args comp))
(def partial (with-is-args partial))

Maybe using metadata on the elementary transformation functions (rotate, 
add,
zoom) to indicate that comp and partial should have specific results so 
that the generic case would not be changed.

But that is getting over my clojure-foo ☹.
How would you go about it ? I have a hard time to believe I'd be the first 
to want to modify composed and curryfied functions.

Any piece of advice would be greatly appreciated !

Best Regards,

b.

[0] 
https://medium.com/@thi.ng/workshop-report-generative-design-with-clojure-7d6d8ea9a6e8#.w6te331y0
[1] https://en.wikipedia.org/wiki/Second-system_effect .

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Puzzle solving in Clojure

2016-04-14 Thread bernardH
Hi !

On Friday, April 8, 2016 at 3:28:55 PM UTC+2, Olivier Scalbert wrote:
>
> Hello everybody !
>
> I just start learning Clojure and I am a complete newbie !
>
> I have tried to solve this classical small puzzle with Clojure.
> Assign one digit from {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} to each letter {o n e 
> t h r l v w y} so that
> one + one + one + three + three + eleven = twenty
> […]
> How can I change the code into a more idiomatic one ?
>
>
For what it's worth, this can be done easily with the great loco library 
[loco] :
;; straight port of cf 
https://github.com/aengelberg/loco/blob/master/test/loco/integer/sendmoremoney1.clj
;; (:use loco.constraints
;;  loco.core))

(defn initialize-digits [vars]
  (for [v vars]
($in v 0 9)))

(def letters
  [:o :n :e :t :h :r :l :v :w :y])

(def puzzle-model
  (concat (initialize-digits letters)
  [($distinct letters)
   ($> :o 0)
   ($> :t 0)
   ($> :e 0)
   ($= ($+ ($* 3 ($scalar [:o :n :e] [100 10 1]))
   ($* 2 ($scalar [:t :h :r :e :e] [1 1000 100 10 1]))
   ($scalar [:e :l :e :v :e :n] [10 1 1000 100 10 
1]))
   ($scalar [:t :w :e :n :t :y] [10 1 1000 100 10 
1]))]))
(time (solutions puzzle-model))

"Elapsed time: 61.496745 msecs"
({:y 8, :r 7, :v 1, :o 2, :n 3, :w 4, :e 5, :l 0, :h 9, :t 6})


Cheers,

bernard

[loco] https://github.com/aengelberg/loco
 

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ANN] dali SVG library 0.7.0

2016-02-14 Thread bernardH

On Thursday, February 11, 2016 at 1:49:54 AM UTC+1, Stathis Sideris wrote:
>
> Hello all,
>
> dali is a Clojure library for representing the SVG graphics format. It 
> allows the creation and manipulation of SVG files. The syntax 
>  used 
> to describe the graphical elements is based on hiccup 
>  with a few extensions.
>
> The main advantage of dali is that it provides facilities to perform 
> complex layouts 
>  
> without having to position elements explicitly.
>
>
Thanks, that looks great and I look forward to try using it to make 
presentations. For now, I am using other tools to make SVG graphs, and I 
was wondering if/how I could embed them. Would it be possible to include 
svg framengts ? Or even better would it ever be possible to round-trip form 
some existing svg to a dali data structure ?

Thanks again for this very interesting lib !

Best Regards. 

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ANN] pex, a powerful PEG parsing library

2015-11-19 Thread bernardH
This is interesting !
It reminds me of Parsnip from C.Grand [0], have you considered it when 
desining pex ? As your parser is focusing of characters, I am wondering : 
could the operations triggered by the execution of your pex code be simple 
enough to warrant actual compiling to JVM bytecode (at run time, with ASM 
[1]) for maximum performance ?

Best Regards,

Bernard

[0] https://github.com/cgrand/parsnip/
[1] http://asm.ow2.org/

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: My first implementation of Sieve of Eratosthenes. cons please

2014-11-26 Thread bernardH
Hi,
As has been said, this is not SoE. But my take would be something along :
(reduce
   (fn [primes number]
 (if (some zero? (map (partial mod number) (take-while #(= % 
(Math/sqrt number)) primes)))
   primes
   (conj primes number)))
   [2]
   (take n (iterate inc 3)))

But I'm a noob, so it migth be the bilnd leading the blind ☺

Best Regards,

b.

On Wednesday, November 26, 2014 3:52:44 PM UTC+1, Chernyshev Alex wrote:

 (defn not-divisible-by?[num denum]
   (not (= (mod num denum) 0)))

 (defn div-nums [denum bound]
 (for [x (range 2 bound) :when (not-divisible-by? x denum)] x))

 (defn divisible? [coll denum]
   (empty? (filter #(and (not= denum %) (not(not-divisible-by? denum %))) 
 coll)))

 (defn generate-primes 
   Sieve of Eratosthenes
   [n]
   (let [src (div-nums 2 n)]
 (cons 2 (for [x src :when (divisible? src x)] x
  



-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding up numbers, quickly

2014-10-17 Thread bernardH


On Friday, October 17, 2014 1:21:55 PM UTC+2, Jony Hudson wrote:

 Thanks all again for the advice. For the record, I ended up writing the 
 loop in Java - being constrained on the type of the data (it ultimately 
 comes from Java code), and not wanting to pay the (newly reduced!) price of 
 converting to vectorz for just this operation, this seemed like the best 
 way.


 Jony



If the data came from java (presumably as arrays), I would have considered 
using http://clojuredocs.org/clojure.core/areduce 

On a side note, not wanting to hijack your thread, I'm wondering if someone 
interested in summing many numbers in Clojure had considered implementing 
the https://en.wikipedia.org/wiki/Kahan_summation_algorithm.

Best Regards 

Bernard

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Optimizing code : Fun but now paining me

2014-10-17 Thread bernardH


On Friday, October 17, 2014 12:51:01 PM UTC+2, Ashish Negi wrote:

 Ok so i am back with this and rechanged the code and used another 
 algorithm : created by great Dijkstra Shunting Yard algo which creates the 
 infix to postfix conversion.

 My implementation here : 
 https://gist.github.com/ashishnegi/ef3c65182c09c154126b

 Now, i have only one test case unsolved which is giving timeout.. and i 
 think it would be largest test case.
 so, if you can find some performance bottleneck in the code like anything 
 using or not using doall or something.. it would be great to know.


Kuddos for the infix to postfix conversion, this is akin to what I had in 
mind to remove the recursion (sorry I did not have time to check my code 
and I did not want to send you off-tracks).

Have you inquired about 
http://clojuredocs.org/clojure.core/*unchecked-math* ?

You accumulate in lists '() to create your stack, have you tried with 
vectors ? (the conj add in last position, so you'd want to peek and pop 
http://clojuredocs.org/clojure.core/pop )
Not sure if it would be faster, however.


Best Regards,

Bernard 

PS: some of your functions are in CamelCase, is there a reason why ?

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Optimizing code : Fun but now paining me

2014-10-14 Thread bernardH
Sorry but I don't feel that I'm understanding your code enough to rewrite 
it (for instance, why substract 2 at 
https://gist.github.com/ashishnegi/a9ae3fb3c270c7d3742e#file-calculator-clj-L102
 
to add 2 at 
https://gist.github.com/ashishnegi/a9ae3fb3c270c7d3742e#file-calculator-clj-L16 
?).
But if I was to write it, I'd use a library to parse the expressions (see 
https://github.com/Engelberg/instaparse ). If you really want to do it 
yourself, the lexing step (

make-calulator-list)shouldn't return strings but numbers and keywords for 
operations imo. And from the list you could
build an Abstract Syntax Tree either with nested vectors or if speed is of 
essence with records (or maybe looking into
 https://github.com/ztellman/clj-tuple ). Processing the AST should be then 
much simpler than processing your strings list.

Try to avoid recursion if possible by expressing your proccessing with reduce 
for instance.

BTW, are you sure than int is a large enough type ? I'd use long and check that 
the mod is small enough.


My two cents.

On Monday, October 13, 2014 3:10:15 PM UTC+2, Ashish Negi wrote:

 I am competing in an online competition and the scenario is like this :
 I coded with my basic knowledge of clojure and got 11 out of 20 testcases 
 and others being timed out and one being runtime error..

 I started optimizing my code and read 
 https://groups.google.com/forum/#!searchin/clojure/performance/clojure/SxT2MaOsip8/xoMkDVHeOqMJ
 and other google articles for performance.. but after that i just managed 
 to solve only one more problem and i am stuck at 12 solved.. ( the time for 
 each case is 8 seconds max)
 The last 12th was completed in 7.05 secs :) .. just on the border huh... :)

 The problem is about that of Solving calculataor expressions like 
 2+3/2/2/(2+8) mod 10^8+7  gives answer 10^8+3..
 Nevertheless it is the concern but optimizing code is :)

 This is current condition of my code :

 (ns fpcontest.expression)

 (def Term)
 (def Factor)

 (def ToMod (int (+ 10 7)))

 ;; (int ToMod)

 (def toPrint false)

 (defn myprintln [ args]
   ;; (if toPrint
   ;;   (apply println args))
 )

 (defn EulerDivNonMemo [^long x ^long p]
   (let [ToMod (+ p 2)]
 (loop [num (long 1) toPow p numDouble x]
   (if (= 0 toPow) 
 ;;(do (myprintln  EulerDiv :  num  for  x p)) 
 num
 (let [numDouble2 (rem (* numDouble numDouble) ToMod)
   halfToPow (bit-shift-right toPow 1)]
   (if (odd? toPow)
 (recur (rem (* num numDouble) ToMod)
halfToPow
numDouble2)
 (recur num halfToPow numDouble2))
   
   
   )

 (def EulerDiv (memoize EulerDivNonMemo)
 )

 ;; (EulerDiv 2 2)
 ;; (defn TestEulerDiv []
 ;;   (and
 ;;(= 2 (mod (* 4 (EulerDiv 2 (- 3 2))) 3))
 ;;(= 1 (mod (* 2 (EulerDiv 2 (- 3 2))) 3))
 ;;(= 1 (int (mod (* 2 (EulerDiv 2 (- ToMod 2))) ToMod
 ;;   )

 ;; (= true (TestEulerDiv))

 (defn Expression [lst]
   (let [term (Term lst)
 ;; p (myprintln term  term for exp  lst)
 sizeTerm (count term)
 evaluateExpr 
 (fn [opr lst]
   (let [expr (Expression (drop 2 lst))
 ;; p (myprintln expr  expr for Expr  lst  and  opr)
 intLst (first lst)]
 (cons (rem (opr intLst
 (first expr)) 
ToMod) (drop 1 expr]
 (if ( sizeTerm 2)
   (if (= (second term) +)
 (evaluateExpr + term)
 (if (= (second term) -)
   (evaluateExpr - term)
   term)
 )
   ;; first of term is the answer
   term
   )
 ))


 (defn Term [lst]
   (let [factor (Factor lst)
 ;; p (myprintln factor  factor for term  lst)
 sizeFactor (count factor)
 evaluateExpr
 (fn [opr lst]
   (let [expr (Term (drop 2 lst))
 ;;p (myprintln expr  term for expr  
 lst  and  opr)
 intLst (first lst)]
 (cons (rem (opr intLst
 (first expr)) 
ToMod) (drop 1 expr) )))]
 (if ( sizeFactor 2)
   (if (= (second factor) *)
 (evaluateExpr * factor)
 (if (= (second factor) /)
   (let [expr (Term (drop 2 factor))
 fExpr (first expr)] 
 (cons (rem (* (first factor) (EulerDiv fExpr (- ToMod 2)))
ToMod) (drop 1 expr))
 )
   ;;(evaluateExpr / factor)
   factor)
 )
   factor
   )
 ))

 (defn Factor [lst]
   (let [fFactor (first lst)
 ;;p (myprintln  Factor has :  lst)
 ]
 (if (= fFactor +)
   (cons (int (Integer. (second lst))) (rest lst))
   (if (= fFactor -)
 (cons (- (int (Integer. (second lst (rest (rest lst)))
 (if (= fFactor ()
   ;; remove the '(' and ')'
   (let [expr (Expression 

Re: Teaching Clojure to students (how ?)

2013-10-09 Thread bernardH

Thanks for all the feedback.
I had already made sure that LightTable is already installed, and the 
plug-in for eclipse (that they already use). But I don't expect too much 
trouble on this front as they are already somewhat proficient (with even 
some converts to the church of Emacs ☺).

Cheers,

B.

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Teaching Clojure to students (how ?)

2013-10-05 Thread bernardH
Hi all,

I intend to (ab)use my authority as a teacher to enlighten unsuspecting 
students
about Clojure.

On the plus side, I may give them insights that they did not even know
they needed. On the minus side, I cannot expect (all of) them to be
curious about Clojure.

Hence, I want to make a demand driven introduction.

My goal is to :
1. identify what novelties Clojure brings to the table to Java developers
   - homoiconicity : macros
 - syntaxing sugar (e.g -, cond)
 - programming paradigms as libraries
   - core.logic
   - core.async
   - dynamic typing
   - simple concurrency handling :
 - immutable data structures
 - ref
 - atoms
 - STM
   - open-ended dynamic dispatching (protocols, namespaced vs. monkey 
patching)
   - multiple dispatching (multimethods)
   - maps instead of classes (no privacy  accessors needed thx to
 dynamic typing and immutable data) and composable libraries instead
 of frameworks.

2. For as many of those features as possible, I'd like to find a minimal 
use case that will be :
   - genuinely interesting (so that they find it beliveable that they
 might actually want to solve a similar problem)

   - complex (if possible hard!) / tedious to solve in Java (I will provide 
the Java code)

   - simple (if possible easy) to solve in Clojure.


The idea being that they would conclude from :
1. that the want to solve these problems
2. that Java won't help them much but Clojure would help them a lot

(+ 1. 2.) →
3. They want to learn Clojure ! ☺

I'd be most grateful for any help, either to complete/amend my list in 1., 
or to provide ideas for 2.


Best Regards,

B.

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [ANN] REST in Peace 0.1.0

2013-07-20 Thread bernardH


On Saturday, July 20, 2013 3:21:47 AM UTC+2, Steven Degutis wrote:

 https://github.com/sebastiansen/rip

  
Thanks !
I'll be implementing RESTful ws this summer and intended to use 
http://clojure-liberator.github.io/liberator/ .
How does your library compares to it ? Any advice on how to make the choice 
would be greatly appreciated.

Cheers,

B.

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




async.core : how to process (match) sequences of events ?

2013-07-20 Thread bernardH
Dear Clojurians,

I'm currently wrapping my head around core.async and it seems really great 
to process events from various souces.
However, I don't currently see how I could use it for my use case.

I want to make a kind of multiplayer action game with combos moves for
sequences of input events (i.e. down, down+forward, forward, punch )
with :
- timeout on each step of the would-be combo
- combo chaining (i.e. a combo move can be followed by other specific 
combos only available after it)
- combo breakers (events for outside -i.e. other player(s)- can 
interrupt/reset the sequence

My current understanding would drive me to make matcher channel for each
combo :
- with a buffer of the size of the max combo length
- reading from :
  - the player input stream
  - a control stream for combo breaker
- using a timeout channel

The input stream would be broadcast to all the combos matcher, and the 
select loop would alt! on all of them.
However, this would :
- perform greedy match of the shortest subsequence (I'd want the opposite : 
matching the longuest combo)
- not handle combo chaining

Should I really go back to a hand-written state machine ?

I'm looking forward to any insights.

Best Regards,

B.

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: direction of an arrow in core.async

2013-07-14 Thread bernardH


On Monday, July 15, 2013 1:39:00 AM UTC+2, Brandon Bloom wrote:

  When I read code like (- 1 2), I tranform it into (1 - 2) in my head

 

 […]
 In the absence of a mental infix transform, (! c) reads like redirecting 
 from a file in your shell.


Thx for the mnemonic !
It didn't make sense for me (either way) but this perfectly clicks.
Maybe this should be written somewhere  as a rationale/mnemonic for the 
naming convention to help people memorize the function names.

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




core.async ring middleware vs pedestal interceptors

2013-07-07 Thread bernardH
Dear clojurians,

Like many others here, I'm thrilled by the new core.async and wondering
about all the goodies that we will be able to reap from it.  Thinking
about how it decouples the abstracts threads of execution with the
concrete implementation thread(s) especially in the context of
non-blocking network io[*], it reminded me of transactors in pedestal.

A (the) barrier to entry for using pedestal is that it departs for the
established ring simplicity with interceptors. Tim Ewald convincingly
argues that this was unavoidable in order to scale to the a number of
connections that exceeds the number of available (OS) threads, as can be
the case with long polling ans Server Sent Events. And this is also
restated in the pedestal interceptor docs [***]

However, I was wondering if such goal could not be met now with
core.async and ring middlewares working on channels ?
Would the performance hit be too large ? []
Would it be still not flexible enough ?

Cheers,

B.


[*] 
http://martintrojer.github.io/clojure/2013/07/07/coreasync-and-blocking-io/
[**] http://thinkrelevance.com/blog/2013/03/18/pedestal-podcast-episode-027 
@ 15'
[***] http://pedestal.io/documentation/service-interceptors/
[] 
http://programming-puzzler.blogspot.fr/2013/07/stackless-clojure-with-coreasync_7.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
--- 
You received this message because you are subscribed to the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




No bit shift for clojure.lang.BigInt ?

2013-06-06 Thread bernardH
Hi,

I wanted to shave a few cycles of a (quot n 2) by using (bit-shift-right 
n 1) instead, with n being a bigint.
However, this fails with an IllegalArgumentException bit operation not 
supported for: class clojure.lang.BigInt  clojure.lang.Numbers.bitOpsCast 
(Numbers.java:1008).
Since the underlying java types  would support this [*], is there a good 
reason not to have those operators in clojure.lang.BigInt ?

Best Regards

B.


[*] 
http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html#shiftRight%28int%29

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: ANN: Iota 1.0.2 release (Reducers for text files)

2013-03-08 Thread bernardH
Hi Alan,


On Friday, March 8, 2013 4:02:18 PM UTC+1, Alan Busby wrote:

 Hi Bernard, 

 I'd certainly like to add support for binary files, but as I haven't had a 
 need for it myself I haven't had a good place to start.

 As Java NIO's mmap() doesn't support ranges over 2GB, I've had to paste 
 together multiple mmap's to cover files that are larger than 2GB. 
 So if a record ended up spanning two mmap()'s, you couldn't return the raw 
 data as a single object without copying it into a new buffer first.

 Also, if you provide a fixed record size in bytes for doing the idx 
 offset maths, why do you need the end idx for the current line as well?
 For example if you say file.bin is full of records each 100B in size, and 
 you ask for the 10th record; don't you already know that the length of the 
 record is 100B?


Indeed, the correlation between txt/binary and char (i.e \n) 
delimited/fixed length record is very strong. However in my case I want to 
first handle a \n delimited (txt) file as binary for performance reasons.
The context is that I have to consider all the lines of data, but might not 
have to do heavy processing on all of them, so I want to do as few work 
as possible on each line (i.e. not construct any java.lang.String).
This is in no way Clojure specific, I have two implementations in Java of a 
small Minimum Spanning Tree program :
- one is constructing Strings from all the lines: 
https://www.refheap.com/paste/12312
- one is using offsets from a raw ByteBuffer : 
https://www.refheap.com/paste/12313

As most of the lines are not really processed (just sorted according to the 
last field), being able to only peek at the relevant bytes instead of 
constructing full blown java.lang.Strings is a huge performance boost.
FWIW, as far as performance i concerned, I draw the line not between 
Clojure and Java but between objects (constructed by copying some data 
somewhere on the heap) and arrays of primitive data types, because 
nowadays, cache locality trumps everything (once you got rid of reflection 
calls in Clojure, obviously).

So ideally, maybe 2 x 2 combinations (String / offset in ByteArray) x (char 
delimited / fixed length) would be needed to cover all the needs.

Thanks again for sharing your library !

Cheers,

Bernard

PS: Is there a rationale for returning nil instead of empty String  on 
empty lines with iota/vec?

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: ANN: Iota 1.0.2 release (Reducers for text files)

2013-03-07 Thread bernardH
Hi,

On Wednesday, March 6, 2013 2:53:26 PM UTC+1, Alan Busby wrote:


 With the release of Clojure 1.5 and it's new reducers, I figured this 
 would be a good time to release a library to help with file IO for 
 reducers. As reducers can only operate in parallel over specific 
 collections, like vec, it requires some work to use them against files.

 Iota wraps a text files using Java NIO's mmap() so you can treat files 
 larger than memory like a vector. Clojure's normal collection functions 
 (first, last, nth, etc) work, while enabling 1.5's reducer's to operate in 
 parallel for fold.


Thanks,  I already love your lib !
However, in order to squeeze the last bits of performance, what I'd really 
need would to be directly access the raw data as byte arrays.
The ideal API would provide my function with things looking and quaking 
like bytes arrays but doing the idx offset maths to directly access the 
maped data (read-only ), the next-to ideal would provide my function the 
ref to whole bytes array and give me the start and end idx for current line.

(I'd like to port a Java code where the most significant performance boost 
was avoiding Strings creation when at all possible, which was possible by 
only peeking at most of the lines).

Cheers,

B.

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Clojure Performance For Expensive Algorithms

2013-02-25 Thread bernardH

Hi,

Nice solution, but don't we need to distinguish between the array types ? 
(cf. inline comment below )

Cheers,

B.

On Monday, February 25, 2013 12:08:39 AM UTC+1, Aria Haghighi wrote:

 […] Here, I think it's a macro you'll probably use all over the place, 
 arr-max, which will find the largest value of an expression looping over an 
 array's index and values. Then lcs is just nested uses of arr-max that I 
 think is pretty reasonable. The thing which clutters the remaining code are 
 the prev/cur swapping which I don't have a slick way of handling.   

 (defmacro arr-max 
   return maximum value of `expr` over the indices
and values of array `arr`, where `idx-symb` and `val-symb`
are bound to index and values of `arr`
   [arr idx-symb val-symb expr]
   `(let [arr# ~arr
  n# (alength arr#)]
  (loop [~idx-symb 0 max-val# java.lang.Long/MIN_VALUE]


Shouldn't we select java.lang.Double/NEGATIVE_INFINITY for arrays of 
Doubles ?

   (if (= ~idx-symb n#)
  max-val#
  (let [~val-symb (aget arr# ~idx-symb)
val# ~expr]
(recur (inc ~idx-symb)
   (if ( val# max-val#)
 val# max-val#)))
  



-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Clojure Performance For Expensive Algorithms

2013-02-24 Thread bernardH


On Thursday, February 21, 2013 10:27:11 PM UTC+1, Geo wrote:

 Man, this is exactly how I feel after all this tinkering! It was great for 
 learning Clojure a bit more in depth, but in the end I am going to stick 
 with the Java solution. Especially since it's so easy to mix Java and 
 Clojure in the same project! I just specify :java-source-paths [src/java] 
 in my project.clj and I just call that one method when I need it and the 
 rest of the project is in Clojure. I think when performance if critical 
 idiomatic Clojure is to just drop down to Java :) 

 Christophe's second function actually achieves Java speed or very close 
 (within 5-10%), but it's ugly and there's a bit more to my algorithm which 
 would make it even uglier if I were to go that route.


There would be no point in me to argue with you whether Chrispohe's version 
really is ugly or not for you, but I'd like to rephrase it as hard on 
the eye. Because I find this performance oriented Clojure vs Java fits 
quiet well in the easy vs simple mindeset.[*]. Adding Java code in a 
Clojure project is certainly easy if you happen to already know the 
language, while no amount of prior experience in idiomatic Clojure will 
help you in writing perfomance oriented Clojure as it can be a completely 
new idiom. But I do believe that the resulting code (as a whole, not just 
the small performance critical bit of code) is simpler with two idioms in 
one language than with two languages.

FWIW, I, for one, am really glad that Clojure allows us to select precisely 
which nice tools we want (have to) throw away (persistent data structures, 
dynamic typing, synchronized) when the need arises. Reminds me of the 
don't pay for what you don't use motto of C++ except done right (i.e. the 
other way around, because you don't want to pay wrt simplicity rather than 
performance, cf. premature optimization…)

Cheers,

Bernard

[*] https://www.youtube.com/watch?v=rI8tNMsozo0

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Building a REST API / efficient RPC interface in Clojure

2013-02-12 Thread bernardH


On Monday, February 11, 2013 2:48:30 AM UTC+1, Mikera wrote:

 Thanks bernardH - these are great links, probably pretty close to what I 
 was looking for.

 Will try some experiments with these over the next few days.


Hi,
I, for one, would be interested by the results of those experiments.

Also, you (amongst others !) might be interested by 
http://clojurewest.org/sessions#ashworth
Introducing Pedestal: Architecture and Services 

Pedestal is a set of libraries for building rich interactive Web 
applications using Clojure, ClojureScript and EDN. This talk starts with 
the big picture: problems Pedestal is designed to solve and the typical 
architecture of a Pedestal application. Then it dives into HTTP services, 
covering the details of the plumbing, including it's relationship to Ring, 
and how they enable collaborative applications.

by Brenton Ashworth (@brentonashworth https://twitter.com/brentonashworth) 
and Tim Ewald - Relevance, Inc.

I'm eager to discover this Pedestal !

Cheers,


Bernard


-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Building a REST API / efficient RPC interface in Clojure

2013-02-09 Thread bernardH
Hi,

On Saturday, February 9, 2013 4:47:26 AM UTC+1, Feng Shen wrote:

 Hi,  I did something similar during work (we are using Clojure)

1. Use HTTP as the transport:  browser ajax call,  mobile API call, 
intertal use
2. JSON as encoding.  Javascript support it natively, mobile can 
decode it easily,  Clojure has very good support for it

 Clojure can decode and encode json*[1]* very efficiently.  Nginx can be 
 configured to zip the data to save bandwidth.


I, for one,  would love to use fressian [*], for reasons explained by Rich 
Hickey [**]. Would you consider including this possibility in your lein 
template (which I'd very much like to use, considering the quality of your 
http-kit !)

As for REST, have you considered liberator [***] ?

Cheers,

Bernard

[*] https://github.com/Datomic/fressian
[**] 
http://skillsmatter.com/podcast/scala/the-language-of-the-system/mh-6213
[***] https://github.com/clojure-liberator/liberator

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: revisiting community funding?

2012-03-20 Thread bernardH


On 20 mar, 21:09, nchurch nchubr...@gmail.com wrote:

 But there are other people who work on Clojure and Clojurescript;
 great things could happen from the focus that comes from being able to
 work on them full time.  I know I'd be willing to give a couple
 hundred to fund such an effort, and given how much people spend to go
 to conferences, I'd be surprised if many others didn't feel the same
 way.
[…Is there still a
 concern about creating unreasonable expectations?  Are there people
 outside Clojure/core who would be willing to work on something?

FWIW, I'd also be willing to contribute on funding. I'd be glad to
contribute anonymously if that would avoid any concern of
unreasonable expectations (nobody, including Clojure/core member
would need to know that *I* contributed).

Best Regards,

B.

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


Clojure Propaganda, reaching out of the LISP niche market

2012-02-27 Thread bernardH
Hi,
I've been watching the Neal Ford video from Clojure/conj about World
Domination and thought about the propaganda part.
Clojure was sold to me as a practical Lisp on the JVM. This was good
because I had already decided to cure my parenthesophobia and the
contenders were Racket, Guile and Clojure.
However, I think it is also very important to reach people who do not
actively look for a language like Clojure (i.e. a Lisp) because most
people do not imagine what they miss with their current language of
choice.

As a result, I wanted to participate in very short rounds of
presentations where people get to hear about various 'exotic' (as in,
not java) languages. People with no prior interest into Clojure are
unlikely to attend to anything longer than a lightning talk.
IMO, for this target, the biggest (only) barrier to entry is being a
LISP and I thought maybe it could (should?) not be put in the
forefront, but only after enticing would-be Clojure programmers with
all the other goodies. Catching them off guard with a casual Oh, btw
it's a LISP : I'd call it sucker punch propaganda ☺.
Of course, it would also be very important to :
 - sympathize with the first replusion wrt lack of syntactic clues,
lack of infix math operators and of emphasis on the function called.
 - explain how the language  ide reduce the pain
 - hint at the reason for this choice (i.e. power of macros)

Then we could challenge the audience not to give up on all the goodies
just because of an initial repulsion toward the syntax.

In hindsight, it just seem usual salesman strategy : show the
obviously good parts before introducing the pain points.

While I won't be showing this presentation, I'd be very interested to
hear any comment wrt to this strategy in general, and my current
implementation outline at
 
https://github.com/scientific-coder/clj-pres/tree/master/sucker-punch-propaganda

Best Regards,

B.

[1]  
https://blip.tv/clojure/neal-ford-neal-s-master-plan-for-clojure-enterprise-mindshare-domination-5953926

-- 
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: Literate Programming example

2011-11-18 Thread bernardH


On Nov 18, 1:17 pm, daly d...@axiom-developer.org wrote:
 Many of you asked me to show an example of a literate
 program and demonstrate the use of the tangle function.

Thanks to your perseverance, I am looking into practicing literate
programming.

However, I decided to settle for emacs org-mode environment with the
literate elisp for the relevant code (abel'part of org-mode)
being here : http://eschulte.github.com/org-babel/org-babel.org.html
I found an example of clojure project (research on genetic
programming) written in literate programming using babel org-mode for
emacs
is hosted here :
http://gitweb.adaptive.cs.unm.edu/asm.git/tree

I do hope that others find those resources as useful as I found them.

Best Regards,

Bernard

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


Latest Bagwell paper for a new implementation of Clojure vectors ?

2011-11-12 Thread bernardH
hi,

I just stumbled upon a paper on en enhanced immutable data structure
[0] that could be useful for Clojure implementation.

Could it enable batter parallel programming semantics as discussed by
Guy Steel in this presentation [1] ?

I'd have to look deeper into it, but I'd be very interested in what
other Clojurian think of it.

Best Regards,

B.

[0] http://infoscience.epfl.ch/record/169879/files/RMTrees.pdf

Abstract. Immutable vectors are a convenient data structure for func-
tional programming and part of the standard library of modern
languages
like Clojure and Scala. The common implementation is based on wide
trees with a fixed number of children per node, which allows fast in-
dexed lookup and update operations. In this paper we extend the vector
data type with a new underlying data structure, Relaxed Radix Balanced
Trees (RRB-Trees), and show how this structure allows immutable vector
concatenation, insert-at and splits in O(logN ) time while maintaining
the
index, update and iteration speeds of the original vector data
structure.

[1] http://research.sun.com/projects/plrg/Publications/ICFPAugust2009Steele.pdf

-- 
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: Literate programming

2011-10-27 Thread bernardH
Thank you for your persistence on this subject : I'm sure that I'm not
the only lurker on this list that you manage to get interested in LP.

I wanted to share two thought that I had when looking at your LP take
on Clojure code :

1°) The infrsastructure should really lower the barrier to
participation as wiki did for wikipedia.
I gain some insight into the persistent data structures in Clojure
implementation as I ported persistent vector to C++ : I wish I could
just jump in and add that insight as a few paragraphs. Programs
nowadays can be massive hence the need of collaborative developpement.
A github account would be more appropriate imho that a http server.

2°) It was my understanding that Clojure implementation was both large
and poised for some overhaul (Clojure in Clojure ?). On the other
hand, I heard that Clojurescript implementation was damn small and
maybe the experience carried from Clojure implementation means that
its foundation will last : maybe it could be an opportunity for a LP
project ?

Best Regards,
B

PS: I'm seeing awsome potential in LP as a teaching tool ! I long for
the day where I can direct my students to LP projects to help them
understand the tought processes that went into great programs.

On Oct 27, 7:58 pm, daly d...@axiom-developer.org wrote:
 On Thu, 2011-10-27 at 00:17 -0700, Mark Engelberg wrote:
  Tim,

  I recall that at some point you described your setup for doing Clojure
  literate programming, and if I recall correctly, you were primarily
  working in LaTeX, relying on incremental compilation to test little
  snippets of code as you wrote them.

 Yes, the idea is to write a literate version of Clojure, 
 seehttp://daly.literatesoftware.com/clojure.pamphlethttp://daly.literatesoftware.com/clojure.pdf
 similar in style to Lisp in Small Pieces. (The effort has been
 stalled temporarily while I try to find new employment.)

 Open the source code. Stare at it. Ask yourself if you understand
 exactly why it was needed, why it is structured that way, what would
 happen if you changed it and what else depends on this code. Imagine
 your job is to maintain and modify it but Rich is not available for
 questions and answers.

 Ultimately that is what matters. In the long term the code will be
 the only remaining artifact after Rich leaves the project. Look at
 Sourceforge and you will see thousands of dead projects that will
 never be picked up because they are just trees of code, dead code.
 Et tu, Clojure?

 Literate programming is about making code live.
 I like Clojure and I really want it to live.

 Think long term. Imagine a better way.

 Tim Daly

Thank you for your persistence on this subject : I'm sure that I'm not
the only lurker on this list that you manage to get interested in LP.

I wanted to share two thought that I had when looking at your LP take
on Clojure code :

1°) The infrsastructure should really lower the barrier to
participation as wiki did for wikipedia.
I gain some insight into the persistent data structures in Clojure
implementation as I ported persistent vector to C++ : I wish I could
just jump in and add that insight as a few paragraphs. Programs
nowadays can be massive hence the need of collaborative developpement.
A github account would be more appropriate imho that a http server.

2°) It was my understanding that Clojure implementation was both large
and poised for some overhaul (Clojure in Clojure ?). On the other
hand, I heard that Clojurescript implementation was damn small and
maybe the experience carried from Clojure implementation means that
its foundation will last : maybe it could be an opportunity for a LP
project ?

Best Regards,
B

PS: I'm seeing awsome potential in LP as a teaching tool ! I long for
the day where I can direct my students to LP projects to help them
understand the tought processes that went into great programs.

PS2: I'd be willing to help fund LP work on Clojure[Script], now that
donations to Clojure are not possible anymore .

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


Clojure number crunching win ! was Re: Needing type hinting help for number crunching

2011-07-24 Thread bernardH
Thank you very much for the help !

On Jul 24, 2:30 pm, Chas Emerick cemer...@snowtide.com wrote:
 A couple of thoughts:

 You should be using 1.3.0-beta1 of Clojure 1.3 -- anything with 
 -master-SNAPSHOT is woefully out of date at
 this point.

Damned, I thought -master-SNAPSHOT was as snapshot of the current
HEAD.
Thanks for pointing that out.

 ...
 However, I wouldn't expect (much of) a speedup from those minor tweaks.  
 First, if that reduce with the `compute-step` fn is your hot loop, reduce 
 will box you into oblivion.  At least until primitives work across
 applications of HOFs, you'll need to unroll that by hand.  Second, you're 
 destructuring with `[next-put-value
 next-call-value]`.  Destructuring is convenient and usually not a bottleneck, 
 but it often is if you're
 attempting to avoid boxing overhead: what you have now is boxing the two 
 values you're binding, and then
 creating a vector to hold them, and then pulling that vector apart to get to 
 the original values.  You can just
 move your conditional out of the let's binding vector, and use it to choose 
 between two recur paths:
...

I had a wallclock time of 54094 msecs and two relection warnings about
the loop variables (plus the unimportnant one about the swap! return
type).
I first followed your advice to remove destructuring and was surprised
to see that the warnings for the two loop variables had disappeared !
However, the speedup was indeed minor, down to 50573 msecs.
I then removed the reduce as you suggested and went to this inner
loop :
(loop [put-value  0.
   call-value  0.]
  (if (neg? (swap! n-iters-todo dec))
  [put-value call-value]
  (let [delta-price (- strike-price
   (loop [price stock-price
  s n-steps]
  (if (== s 0)
   price
   (recur (* price (+ 1.
  (*
(.nextDouble double-normal-rng 0. volatility)
 
coeff)
  (* risk-
free-rate (/ years-to-maturity n-steps
   (dec s)]
(if (pos? delta-price)
(recur  (+ put-value delta-price) call-value)
(recur  put-value (- call-value delta-price)))

Quiet ugly by Clojure standard I confess, but *incredibly* efficient
with a run time down to 5993 msecs !
(No missing digit: a near x10 speedup !) : faster than both my C++
OpenMP and my C++ 0X multi-threaded implementations !

This is the first time that I witness this (discounting micro
benchmarks made on purpose to show off the JIT).

Thanks for making this happen :)

Best Regards,
Bernard

-- 
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: [incanter]State, how to do it right ? (rng state for parallel computing)

2011-07-24 Thread bernardH
(@cej38: thanks for bringing that up to my radar ! However, I'd have
to check the fine prints because I like the Open in OpenCL and
dispise vendor lock-ins, no matter how shiny the shackles :) )


On Jul 24, 1:11 pm, Lee Spector lspec...@hampshire.edu wrote:
 On Jul 22, 2011, at 11:20 AM, bernardH wrote:



  But there is a small catch : most of the time is spent in the Random
  Number Generator [3] so it would be sub-optimal to just have worker
  threads processing the output of a single threaded rng.

 I am not very confident in my solution to a similar problem, and my 
 application makes measurement difficult,
 but in case it's food for thought what I did was the following, using the 
 standard Java rng:

 ;; create a root binding for the thread-local-random-generator, although 
 this won't be used in
 the distributed code:

 (def thread-local-random-generator (new java.util.Random))

Thank you for your anwser. I cannot comment much on your particular
choice wrt to java.Util.Random because I wanted to use MersenneTwister
and was contraint by some of it's implementation details (in parallel
colt).

I had though about going the same root of a thread local binding but
the colt implementation was not synchronized when using generator
objects (but the static member functions are synchronized).
So I first had a root binding to nil, and a rng that checked if the
binding was nil (calling the static synchronized member functions) or
not (calling the member functions).

However, I gave up on having a generic solution because I'd have to
give up half of the performance if I wanted generic generators (i.e.
not tied to mean, sd) as the Box-Muller algorithm comptes pairs of
numbers (and the colt implementation caches the number if possible
(mean and sd same as given upon generator construction).

This is an ugly implementation detail but I really wanted the best
performance, efficient rng being of utmost importance for my Monte-
Carlo code. Otherwise, I can't think of anything better that your
approach (but hey, that is why I called the gurus here for help :) ).

One should however investigated the thread-safety of the rng because
either the root binding is not safe to share, or your are paying for
useless synchro in your thread-local rng.

Best Regards,

Bernard

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


[parallel][incanter]State, how to do it right ? (rng state for parallel computing)

2011-07-23 Thread bernardH
Context :

I'm currently comparing (ease of implementation *and* performance) C++
and Clojure (no flame war intented :  I *love* them both) trying to
find when / how one can code in Clojure rather than in C++.

Problem :

Rewriting some financial computing code, I was very pleased with my
first shot [0] (no type hinting yet, on Clojure 1.2) being only x6
slower than C++ with maximal optimizations (-march=native -O4) : the
JIT really shines here !

However, this is only for single core C++, as both OpenMP [1] and
C++0X [2] allow me to get a further x3 advantage on my netbook, as the
algorithm is embarrassingly parallel.

But there is a small catch : most of the time is spent in the Random
Number Generator [3] so it would be sub-optimal to just have worker
threads processing the output of a single threaded rng.
So each thread should have its own rng, but those rng should not be
clones of each other because there would be no point in running
multiple computations on the same sequences of pseudo-random numbers !

In my C++ implementations, I solve this easily with local copies of
the rng that reseed themselves (from a shared counter : the contention
upon seeding can easily be afforded) upon copy.

In Clojure, this means that a parallel outer reduce (is it even
possible ? I see a clojure.core/pmap but no preduce) over a single
random numbers sequence would be suboptimal. However, I have no clue
about how to tackle this problem : could someone clue me in ?

This key code from [0] is :
  (map f (reduce combine [0. 0.]
 (partition n-steps (sample-normal (* n-steps num-
trials)
   :mean 0. :sd
volatility)))
each combine invocation consuming n-steps random numbers.

I see how I could seed the thread local rng with local
cern.jet.random.tdouble.engine.DoubleMersenneTwister but not the
generic design in which I could have a pool of worker threads sharing
the work, each with its own rng (not just copies, but independently
seeded).
(I have an idea but it seems very ugly and required a complete rewrite
so I hope there would be a better way).

Any idea most welcome !
(I'd *love* to get back with 10x of the fastest C++, I find the
Clojure Way so joyful :-) )

Best Regards,

Bernard

PS: I'll move to Clojure 1.3 for type hinting, are there any pitfalls
in using Incanter lib with Clojure 1.3 ? (Is it at all
possible ? :-) )

PS2: My next implementation will be in OpenCL : is calx the best
OpenCL
wrapper ?

[0]
https://github.com/scientific-coder/Black-Scholes/blob/master/src/incanter-montecarlo.clj

[1]
https://github.com/scientific-coder/Black-Scholes/blob/master/src/openmp-montecarlo.cxx

[2]
https://github.com/scientific-coder/Black-Scholes/blob/master/src/cxx0x-montecarlo.cxx

[3] no hard numbers because g++ so agressively inlines the rng that
the random number generation in intermixed with further computations !
But perf reports 80% cpu usage in this spot.

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


Needing type hinting help for number crunching

2011-07-23 Thread bernardH
Hi,

I have rewritten some number crunching code from C++ to Clojure.
I was pleasantly surprised to find that the Clojure code is within 10x
of the C++ excecution time (both multicore implementations), but
always eager for more speed, I (reluctantly) moved away from incanter
(only used a couple of thin wrappers over Colt) to Clojure 1.3
(Clojure 1.3.0-master-SNAPSHOT) for type hinting.

However, no matter how hard I try, I fail to resolve some
recur arg for primitive local: put_value is not matching primitive,
had: Object, needed: double
leading to Auto-boxing loop arg: put-value in the most performance
sensitive code :(

( complete code on github [*]) the relevant parts are :

(let [double-normal-rng (Normal. 0. volatility
 
(DoubleMersenneTwister. (swap! seed inc)))]
 (loop [put-value  0.
  call-value  0.]
 (if (neg? (swap! n-iters-todo dec))
 [put-value call-value]
 (let [delta-price (- strike-price
(reduce compute-
step stock-price
  (repeatedly n-
steps
 
#(.nextDouble double-normal-rng
 
0. volatility
   [next-put-value next-call-value] (if
(pos? delta-price)
  [(+
put-value delta-price) call-value]
  [put-
value (- call-value delta-price)])]
   (recur  next-put-value next-call-
value)

And with
(set! *warn-on-reflection* true)
(set! *unchecked-math* true)
I get
Reflection warning, NO_SOURCE_PATH:2535 - call to
cern.jet.random.tdouble.engine.DoubleMersenneTwister ctor can't be
resolved.
NO_SOURCE_FILE:2548 recur arg for primitive local: put_value is not
matching primitive, had: Object, needed: double
NO_SOURCE_FILE:2548 recur arg for primitive local: call_value is not
matching primitive, had: Object, needed: double
Auto-boxing loop arg: put-value
Auto-boxing loop arg: call-value

When I eval the whole function def. :(
I should note that have type hinted the inner most function,
previously defined in a letfn
(compute-step
 ^:static ^double [^double price ^double rnd-number]
 (* price (+ 1.
 (* rnd-number (Math/sqrt (/ years-to-maturity
n-steps)))
 (* risk-free-rate (/ years-to-maturity n-
steps)

What is wrong with the DoubleMersenneTwister ctor ?
Shouldn' t the reduce call preserve the type information from
compute ? strike-price is type-hinted to double so shouldn't the
*unchecked-math* preserve this type info all the way to next-put-value
and next-call-value ?

Any help (even only a hint ;) on where to look for) *greatly*
appreciated !

Best Regards,

Bernard

[*] 
https://github.com/scientific-coder/Black-Scholes/blob/master/src/black-scholes/src/montecarlo.clj#L57

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