Generated schemas with clojure.spec

2017-06-07 Thread Petr
Hello,

I tried to use clojure.spec for validation and test data generation of 
requests for API described according OpenAPI (https://www.openapis.org/).
My code reads an OpenAPI specification, then constructs description that 
can be used with clojure.spec. 
The idea was to validate a request map according to spec or generate 
request structure that conform this spec.

I tried to generate regexp-like clojure.spec expressions that use tuples 
where first item selects specification sub-tree. 
For example:

(alt
  path-1-key (tuple path-1 (alt method-1-1-key (tuple method-1-1 (cat 
..params..))
method-1-2-key (tuple method-1-2 (cat 
..params..
  path-2-key (tuple path-2 (alt method-2-1-key (tuple method-2-1 (cat 
..params..))
method-2-2-key (tuple method-2-2 (cat 
..params..)
(cat is used for parameters so I can describe optional ones with `?`)

My experiment code is here 
https://github.com/PetrGlad/oasiege/blob/master/src/oasiege/core.clj
The approach turned out to be cumbersome, since generated structure has 
different structure than result I want.

It looks like clojure.spec's requirement for fully qualified spec names is 
made in assumption that all specs will be hand-crafted.
My first idea was to generate top level spec for paths, then for methods 
and so on. That would require qualified spec names that describe map keys. 
But names from OpenAPI description are unqualified. So to generate map of 
request parameters {:param-a val-a} I should somehow to work 
around the need to have specs like :api-specs.generated.spec1.param-a that 
should eventually produce [:param-a val-a] key-value pair
("spec1" name part would be generated for each loaded OpenAPI 
specification). Also it is not clear what to do if I want non keyword map 
keys. 

So the question: is there a more straightforward way to work with 
specifications that are known only at run time or this is out of intended 
clojure.spec usage?

-- 
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: Derefs broken after clojure.tools.namespace.repl/refresh

2017-04-11 Thread Petr
понедельник, 10 апреля 2017 г., 22:56:43 UTC+2 пользователь Didier написал:
>
> Hum, not sure why you would do this, but I'm guessing refresh goes in an 
> infinite async loop. You keep reloading a namespace which creates a thread 
> to reload itself.
>
> I can't really explain why the symbol exists, but is not bound. I would 
> have thought either the symbol would not exist, or the Var would contain 
> the correct value.
>

This is not what I really want to do. Just experimenting. But the behavior 
is unexpected. 

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


Derefs broken after clojure.tools.namespace.repl/refresh

2017-04-10 Thread Petr
What happens here?

> (send-off (agent {}) 
  (fn [_] 
  (clojure.tools.namespace.repl/refresh)))
> @(atom {})
=> {}
> (def s (atom {}))
=> #'user/s
> s
=> #object[clojure.lang.Atom 0x5f6a0fab {:status :ready, :val {}}]
> @s
=> ClassCastException clojure.lang.Var$Unbound cannot be cast to 
java.util.concurrent.Future  clojure.core/deref-future (core.clj:2206)

Same with agent 
> (def a (agent {}))
> @s
=> ClassCastException clojure.lang.Var$Unbound cannot be cast to 
java.util.concurrent.Future  clojure.core/deref-future (core.clj:2206)


PS: BTW atomic code reload is biggest missing feature in Clojure.

-- 
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] core.async 0.3.426

2017-02-23 Thread Petr
I have slightly unrelated question. Why core.async is still not 1.x 
version? Is there a feeling that API is still experimental and not mature 
enough? Or authors of core.async are not happy with implementation?

среда, 22 февраля 2017 г., 19:47:33 UTC+1 пользователь Alex Miller написал:
>
> core.async 0.3.426 is now available.
>
> Try it via:  [org.clojure/core.async "0.3.426"]
>
> 0.3.426 includes the following changes:
>
>- ASYNC-169  - handling 
>of catch and finally inside go blocks was broken, causing a number of 
> issues
>   - Related: ASYNC-100 
>   , ASYNC-173 , 
>   ASYNC-180 , ASYNC-179 
>   , ASYNC-122 
>   , ASYNC-78 
>   , ASYNC-168 
>   
>- ASYNC-138  - go blocks 
>do not allow closed over locals to be cleared which can lead to a memory 
>leak
>   - Related: ASYNC-32 
>- ASYNC-155  - preserve 
>loop binding metadata when inside a go block
>- ASYNC-54  - fix bad 
>type hint on MAX-QUEUE-SIZE
>- ASYNC-177  - fix 
>docstring typo in Buffer protocol full? method
>- ASYNC-70  - docstring 
>change in thread, thread-call
>- ASYNC-143  - assert 
>that fixed buffers must have size > 0
>- Update tools.analyzer.jvm dependency
>
>
> Many thanks to Kevin Downey and Nicola Mometto for their help on the go 
> block issues.
>
> Additionally, I've done some work to make the core.async build less weird 
> and to match all the other contrib projects (other than how the version is 
> computed). This will make core.async easier to use and manage for 
> development and CI and also lets us use our automated CI matrix test setup 
> to give us more coverage going forward.
>
>

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


[ANN] Compost 0.2 - replacement for stuartsierra/component

2017-02-14 Thread Petr
Hello,

This library https://github.com/petrglad/compost is a replacement 
for https://github.com/stuartsierra/component
The major differences are 

   - components' configuration is kept in plain Clojure maps instead of 
   metadata
   - lifecycle functions are plain functions.
   
My motivation was to have means to declare lighter ad-hoc components which 
I often need.
See also README.md for more details.

--
Petr

-- 
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: Casting bug in clojure

2016-02-26 Thread Petr Yanovich
Oh! It was added with an edit which I haven't seen :)

Thank you

пятница, 26 февраля 2016 г., 17:07:30 UTC+3 пользователь Alex Miller 
написал:
>
> That PR links to a stackoverflow which points to 
> http://dev.clojure.org/jira/browse/CLJ-445, so yes?
>
> On Friday, February 26, 2016 at 7:28:44 AM UTC-6, Petr Yanovich wrote:
>>
>> Hi, there is a minor bug described here
>>
>> https://github.com/clojure/clojure/pull/59 
>> <https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fclojure%2Fclojure%2Fpull%2F59=D=1=AFQjCNFny0CtXm16zDmoTgX-Z8icFY5ESQ>
>>
>> Is it a known bug?
>>
>

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


Casting bug in clojure

2016-02-26 Thread Petr Yanovich
Hi, there is a minor bug described here

https://github.com/clojure/clojure/pull/59 


Is it a known bug?

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


Is there a reason ns macro is not a function?

2014-12-30 Thread Petr
Hello.

Does anyone know why clojure.core/ns macro is not implemented as function? 
It seems that it should work that way except that probably it would be 
nicer to have in-ns call at the top level. 

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


tools.namespace ns parsing

2014-12-14 Thread Petr
 

 Hello. 

 I stumbled on a problem that caused me fair amount of frustration and I 
suspect that it could affect other users who start using tools.namespace. 

The problem is that clojure.code/ns macro is more lenient than what follows 
from it’s documentation. For example ‘use’ (or require) clause in vector is 
accepted e.g. “[:use ]” (see http://dev.clojure.org/jira/browse/TNS-21) 
or use clause with plain symbol (not keyword) is also OK, e.g. “(use ) 
(as in my case http://dev.clojure.org/jira/browse/TNS-30). And the way ns 
macro is expanded section in form “( )” would be replaced with 
“(clojure.core/ ... )”. However tools.namespace parses dependencies 
strictly according to doc. E.g.TNS-21 was closed as won’t fix for that 
reason. 

 This way there could be ns declarations  that have no compilation errors 
and work as one may think but are ignored by c.t.n’s parser.  Hence there 
could be actual dependencies that are silently ignored without any error or 
warning. 

 It seems that proper way to resolve this would be to rewrite ns macro to 
be more strict and accept only what’s described in documentation. But I 
suspect that this would lead to backward compatibility issues - some of 
existing code will break. 

 There is also possibility to rewrite 
clojure.tools.namespace.parse/deps-from-ns-decl so it does macroexpand and 
then looks for references to clojure.core/refer, clojure.core/require and 
clojure.core/use. This would match now ns actually works but one might not 
like this “implementation as specification”. 

 So I think that the good solution for now would be to have some validation 
function forns form. This function would issue a warning when some parts of 
namespace declaration could be ignored. This way unexpected behavior of 
clojure.tools.namespace.repl/refresh would not go unnoticed and this will 
make this library more user-friendly. 

What do you think would be a good solution for this?


-- 
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: How aliased require is different?

2014-05-04 Thread Petr Gladkikh
Thanks for the wrapper, that turned out to be the problem with 'user'
namespace I was REPLing in (other namespaces were refreshed as expected).

While resolving this I also found out that :after option of refresh
function is indeed useful (my server was re-created with older version of
ring handler otherwise). I also found out out that instead of passing
:after reference one can use
((ns-resolve 'namespace 'constructor)) to ensure that system constructor is
of latest version.

On Sun, May 4, 2014 at 3:24 PM, Ivan Kozik i...@ludios.org wrote:

 Here's a wrapper over (refresh) that updates the aliases in the user
namespace
 as well.  You can put it in the :repl-options in your project.clj.

 :init (do
 (require '[clojure.tools.namespace.repl :refer [refresh]])
 (defn r []
   (refresh)
   ;
https://github.com/clojure/tools.namespace#warnings-for-aliases
   (doseq [[sym target-ns] (ns-aliases 'user)]
 (ns-unalias 'user sym)
 ; (the-ns (ns-name ...)) gets the post-refresh namespace
object
 (.addAlias (the-ns 'user) sym (the-ns (ns-name target-ns))


 Ivan

 On Sun, May 4, 2014 at 7:40 AM, Ivan Kozik i...@ludios.org wrote:
  Hi Petr,
 
  Did you see this?
  https://github.com/clojure/tools.namespace#warnings-for-aliases
 
  Also mentioned: Aliases to reloaded namespaces will break if the
  namespace containing the alias is not reloaded also.
 
  I've been writing per-project wrappers over the refresh function to do
  the ns-unalias+alias calls.  I wonder if it would be feasible to have
  something that keeps track of just aliases in the user namespace, and
  do the re-aliasing after a refresh.
 
  Ivan
 
  On Sat, May 3, 2014 at 4:04 PM, Petr petrg...@gmail.com wrote:
  Hello.
 
  I experienced non obvious behavior of require namespace as alias when
  using clojure.tools.namespace.repl/refresh.
  If I use aliased namespace then after changing source files and call to
  refresh I seem to have old values in that alias. If I use plain
require all
  seem to work as expected, new definitions take effect after reload.
 
  For example, in first case:
  (ns a (require [b :as b-alias]))
  (println b-alias/x) ; original value
  ; change sources of b or it's dependencies
  (clojure.tools.namespace.repl/refresh)
  (println b-alias/x) ; original value
  ; and also
  (println b/x) ; new value
 
  In second case:
  (ns a (require [b]))
  (println b/x) ; original value
  ; change sources of b or it's dependencies
  (clojure.tools.namespace.repl/refresh)
  (println b/x) ; new value
 
  Can anyone explain why require acts like this? Does it mean that alias
is
  not just naming change but some data structure in memory?
 
  --

--
Petr Gladkikh

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


How aliased require is different?

2014-05-03 Thread Petr
Hello.

I experienced non obvious behavior of require namespace as alias when 
using clojure.tools.namespace.repl/refresh.
If I use aliased namespace then after changing source files and call to 
refresh I seem to have old values in that alias. If I use plain require all 
seem to work as expected, new definitions take effect after reload.

For example, in first case:
(ns a (require [b :as b-alias]))
(println b-alias/x) ; original value
; change sources of b or it's dependencies
(clojure.tools.namespace.repl/refresh)
(println b-alias/x) ; original value
; and also
(println b/x) ; new value

In second case:
(ns a (require [b]))
(println b/x) ; original value
; change sources of b or it's dependencies
(clojure.tools.namespace.repl/refresh)
(println b/x) ; new value

Can anyone explain why require acts like this? Does it mean that alias is 
not just naming change but some data structure in memory?

-- 
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: Factual is hiring Clojure engineers

2014-01-18 Thread Petr
Do you consider non US candidates? 
Wihout extensive data analysis experience?

On Saturday, January 18, 2014 7:34:38 AM UTC+7, Zach Tellman wrote:

 It's been six months since I last announced this, so: Factual is 
 perpetually hiring smart, driven engineers to solve deep problems in data 
 analysis and systems engineering.  We have offices in both SF and LA, and 
 remote candidates with a strong background in analysis will be considered.

 Factual provides an index of things in the world (businesses, places, and 
 products).  You can either use this directly via our API [1] to ask 
 questions like what are the three coffee shops nearest to me, or 
 indirectly by giving us your own real world data to enrich and refine [2] 
 [3].  Factual uses Clojure extensively [4], both for realtime systems and 
 batch data analysis.  Systems written purely in Clojure handle the 
 following:

 * realtime intake of 70k datapoints/sec from our partners
 * batch processing of 7 billion new datapoints every day, comprising 
 terabytes of compressed textual data
 * generating, storing, and serving 150 million records, refreshed daily

 Both of these numbers have at least doubled in the last six months, and 
 could very easily do so again.  There are also other projects that use 
 Clojure alongside Java to work at similar scales.

 Libraries developed in the course of working on these projects include:

 https://github.com/factual/skuld
 https://github.com/factual/clj-leveldb
 https://github.com/aphyr/merkle
 https://github.com/ztellman/clj-tuple
 https://github.com/ztellman/narrator

 Several other libraries are on the cusp of being open sourced, when we get 
 a chance.

 We need people who enjoy using technology as a lever to accomplish amazing 
 things (wasn't it Archimedes who said give me a large enough data 
 center...), who want to work alongside people they can continuously learn 
 from, and who want to be just a little uncomfortable with the scale and 
 scope of their responsibilities.

 If this sounds interesting, contact me at zach at factual.com.  I'm 
 heading up the small but growing SF office, and am happy to talk in person 
 with anyone in the Bay Area who'd like to know more.

 [1] http://developer.factual.com/working-with-factual-places/
 [2] http://www.factual.com/products/geopulse-audience
 [3] http://www.factual.com/products/resolve
 [4] http://www.factual.com/jobs/clojure


-- 
-- 
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: Looking to migrate thousands of Clojure applications from 1.2 to 1.5.

2014-01-16 Thread Petr
Yes, but this requires custom class loaders. AFAIK this is already 
implemented in Immutant project (http://immutant.org/)

On Thursday, January 16, 2014 11:13:11 PM UTC+7, solo...@gmail.com wrote:

 Quick update and question: We've put together a preliminary migration plan 
 and are considering options.
  
 Can someone share information or point me to where I can learn whether two 
 different versions of Clojure (1.2 and 1.5) can run in the same JVM?


-- 
-- 
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: How to generate a serializable java class in Clojure by gen-class?

2013-03-03 Thread Petr Gladkikh
If this question is still actual there is question on stackoverflow
http://stackoverflow.com/questions/1584054/declare-member-variables-with-gen-class-in-clojure

:state option to genclass is probably what you need
http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/gen-class


On Mon, Feb 25, 2013 at 10:05 PM, Mingli Yuan mingli.y...@gmail.com wrote:

 When using a java machine-learning package - mallet, we failed to
 serialize the class we generated by gen-class.

 Exception is as below:

 Exception in thread Thread-8 java.io.InvalidClassException:
 lancelot.SegmentorPipe; local class incompatible: stream classdesc
 serialVersionUID = 2583852145887230781, local class serialVersionUID =
 -2255006751011717591 at
 java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:579) at
 java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1601) at
 java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514) at
 java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
 at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347) at
 java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)

 *
 *
 Code snippet:
 *
 *

 (gen-class
  :name lancelot.Instance
  :extends cc.mallet.types.Instance)
 (gen-class
  :name lancelot.FilterPipe
  :extends cc.mallet.pipe.Pipe
  :prefix -filter-
  :methods [[pipe [cc.mallet.types.Instance] lancelot.Instance]])
 (defn -filter-pipe [this ^cc.mallet.types.Instance inst]
   (.setData inst (clojure.string/join  
 (filter normal-word? (clojure.string/split (.getData inst) #\s+
   inst)
 (gen-class
  :name lancelot.SegmentorPipe
  :extends cc.mallet.pipe.Pipe
  :prefix -segmentor-
  :methods [[pipe [cc.mallet.types.Instance] lancelot.Instance]])
 (defn -segmentor-pipe [this ^cc.mallet.types.Instance inst]
   (.setData inst (seg/seg (.getData inst)))
   inst)

 *
 *
 The problem is that serialVersionUID need to be static and final, but it
 seems that gen-class can only generate instance level final field.
 *
 *
 I posted this question on 
 SOhttp://stackoverflow.com/q/15062179/210629?sem=2,
 but it seems none answered it, so I reposted it here.

 Thanks a lot for your help.


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






-- 
Petr Gladkikh

-- 
-- 
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: Better ways to make time tansformations?

2012-03-31 Thread Petr Gladkikh
I'd suggest using java.util.concurrent.TimeUnit
for example TimeUnit.MILLISECONDS.toDays(1234543211);
or, since we're on Clojure list (.toDays
java.util.concurrent.TimeUnit/MILLISECONDS 1234543211)

For more complex tasks Joda Time is excellent choice.

On Sat, Mar 31, 2012 at 4:51 AM, Goldritter
marcus.goldritter.lind...@googlemail.com wrote:
 I wanted to track a program and set a maximum runtime for it in a way
 which is readable for the user. So I needed to write a time
 tansformation which I could use in my track function.
 First I wanted to use something like the 'defunits' macro from Let
 over Lambda from Doug Hoyte, but I'm not so fit in LISP and Clojure
 to write/understand it :(.

 So I use following two function:

 (defn unit-of-time
  [value unit]
  (unit-of-time value unit) returns the value in relation to the
 defined unit in seconds.
 Example: (unit-of-time 10 :minute) will return 600 and (unit-of-time
 3 :millisecond) 3/1000.
 following Keywords are supported:
 :second
 :minute
 :houre
 :day
 :millisecond
 :microsecond
 :nanosecond
  (* value (case unit
             :second 1
             :minute 60
             :houre 3600
             :day 86400
             :millisecond 1/1000
             :microsecond 1/100
             :nanosecond 1/10)))

 (defn transform-time
  [value from to]
  (transform-time value from to) calculates the value from the unit
 'from' to the unit 'to'
  (/ (unit-of-time value from)
     (unit-of-time 1 to)))

 The functions, but now I would know if there might be a better way to
 transform for example a time value given in minutes into the
 appropriate milli- or nanosecond value?

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



-- 
Petr Gladkikh

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


Source code as metadata

2012-03-29 Thread Petr Gladkikh
I am pondering on the idea of having more (or even a lot) of metadata
that could be useful for debugging and problem resolution.
Since we can store anything in metadata, can we store not only  source
file path and line number but whole source code that is associated
with piece of code?
For example, for a function we can store it's complete source code.
This way, when some piece of code throws NPE, tooling can show context
of that piece of code that caused problem (e.g. highlight it in
editor).
So, even if Clojure reader does not know actual file and line of
source code user can infer the location of code by matching structure
of code (and some tooling can do that automatically). Such information
could be especially helpful for anonymous functions.

Example:
; Source
(defn c [d]
  (#(/ % d) 13))

; REPL
(c nil)

; Ouput
NullPointerException
clojure.lang.Numbers.ops (Numbers.java:942)
clojure.lang.Numbers.divide (Numbers.java:154)
petrglad.bugs/c/fn--786 (bugs.clj:17)
petrglad.bugs/c (bugs.clj:17)
petrglad.bugs/eval791 (NO_SOURCE_FILE:1)
clojure.lang.Compiler.eval (Compiler.java:6465)
clojure.lang.Compiler.eval (Compiler.java:6431)
clojure.core/eval (core.clj:2795)
clojure.main/repl/read-eval-print--5967 (main.clj:244)
clojure.main/repl/fn--5972 (main.clj:265)
clojure.main/repl (main.clj:265)
clojure.tools.nrepl.middleware.interruptible-eval/evaluate
(interruptible_eval.clj:41)

In this case code is simple and I can guess what is happening by line
number. But what if there are more than one anonymous functions and
more operations in same line?

If source code is associated with that function it can be attached to
the exception and message could instead look like
NullPointerException in #(/ % d)
in (defn c [d] ( ... 13))
in bugs.clj line 17

I have been using Clojure occasionally for some development automation
so I could be missing something.
What do you think of this idea?

-- 
Petr Gladkikh

-- 
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]: cljsh repls 1.6.0 - so many repls, so little time…

2012-02-06 Thread Petr Gladkikh
On Wed, Feb 1, 2012 at 3:39 AM, Frank Siebenlist
frank.siebenl...@gmail.com wrote:
 We already have swank, lein repl, cake, nrepl, detour, clj, reply, … and now 
 we have the combo cljsh  repls.

 repls is a lein plugin, which is essentially leiningen's native lein repl 
 task with some default config options for the (not-)printing of eval results 
 and repl-prompt. It gives you a persistent repl-server that uses the basic 
 repl-protocol: clj-code as text for the reader thru stdin, and evaluated 
 results and printed side-effects thru stdout. Install it with lein plugin 
 install lein-repls 1.6.0, and run the repl-server in your project directory 
 with lein repls, and just leave it running...

 cljsh is a clojure shell bash-script that uses socat under the covers 
 to make the repl-server's stdin and stdout appear local to cljsh. It allows 
 for a relatively simple sending of clj-statements and clj-files to the 
 persistent repl-server. cljsh is also very lightweight, like cake/nailgun, 
 with a negligible startup time, which makes the evaluation of much of your 
 clj-code almost instant. You run cljsh like a normal shell script, like bash 
 or sed, and pass clj-code statements and files as command line directives or 
 piped-in thru stdin. That code is sent to the persistent repl-server, and the 
 printed results and output are available on cljsh's stdout. Almost sounds too 
 simple… Download the cljsh script from github: 
 https://raw.github.com/franks42/lein-repls/master/bin/cljsh.sh; and put it 
 somewhere in your path. An example command line invocation looks like:

  $ cljsh -c '(println hello)'
  hello
  $

 or a more contrived example:

  $ cat third.clj | cljsh -c '(println first)' -i second.clj - fourth.clj 
 -myargs

 The cljsh-test.sh script shows most of the supported features: 
 https://github.com/franks42/lein-repls/blob/master/bin/cljsh-test.sh;.

 cljsh also supports an interactive repl-mode with history and completion 
 support thru rlwrap - pretty much like the other repls, except that this one 
 starts-up instantly, and you can have multiple, concurrent repl-sessions 
 working with the same persistent repl-server. (not sure why you would do 
 that… but you can/could… ;-)). The completion words can easily be updated 
 with cljsh to reflect the evaluation context, but remember that rlwrap is a 
 poor-man's completion solution… even though it works pretty well most of the 
 time.

 Possible usage scenarios.
 Easy to write cljsh-scripts that can be used to select code in any text 
 editor or any app and send for evaluation. Select any symbol in any 
 app/browser and lookup the doc. Use growl notification to show you the doc or 
 the stack trace or eval result. Write your clj-based unix filter. Browse the 
 filesystem with fs by changing the repl-prompt to reflect your cwd. Write 
 your clj-based macosx automation service. Please let me know if you come up 
 with any others…

 Even though it all seems to work pretty well on my macosx… your mileage may 
 vary on the other unixes, and it most probably won't work on windows… maybe 
 cygwin(?). Please let me know of any compatibilty issues.

 Next step: extend it to support clojurescript. (…driving a browser UI thru 
 unix-style filters and scripts from the command line…)

 Please take a look at the README at: https://github.com/franks42/lein-repls; 
 for details.
 Any feedback is very much appreciated.

 Enjoy, FrankS.

Just tried it. This is what I have in Ubuntu
petr@host:~$ lein repls
Exception in thread main java.lang.Exception: Couldn't connect
(NO_SOURCE_FILE:0)
at clojure.lang.Compiler.eval(Compiler.java:5440)
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$script_opt.invoke(main.clj:270)
at clojure.main$main.doInvoke(main.clj:354)
at clojure.lang.RestFn.invoke(RestFn.java:457)
at clojure.lang.Var.invoke(Var.java:377)
at clojure.lang.AFn.applyToHelper(AFn.java:172)
at clojure.lang.Var.applyTo(Var.java:482)
at clojure.main.main(main.java:37)
Caused by: java.lang.Exception: Couldn't connect
at leiningen.repls$poll_repl_connection.invoke(repls.clj:129)
at leiningen.repls$repls.invoke(repls.clj:184)
at leiningen.repls$repls.invoke(repls.clj:155)
at clojure.lang.Var.invoke(Var.java:361)
at clojure.lang.AFn.applyToHelper(AFn.java:159)
at clojure.lang.Var.applyTo(Var.java:482)
at clojure.core$apply.invoke(core.clj:540)
at leiningen.core$apply_task.invoke(core.clj:260)
at leiningen.core$_main.doInvoke(core.clj:325)
at clojure.lang.RestFn.invoke(RestFn.java:410)
at clojure.lang.AFn.applyToHelper(AFn.java:161)
at clojure.lang.RestFn.applyTo(RestFn.java:132)
at clojure.core$apply.invoke(core.clj

Re: are non programmers the better programmers?

2012-01-18 Thread Petr Gladkikh
On Wed, Jan 18, 2012 at 4:46 AM, James Reeves jree...@weavejester.com wrote:
 On 17 January 2012 20:46, Dennis Haupt d.haup...@googlemail.com wrote:
 i've noticed this since i started to work as a programmer 10 years ago.
 programmers in general are supposed to be good at finding simple
 solutions, but my experience is: they are not. on the contrary, many
 suffer from their individual tunnel visions without being aware of it.
 to a hammer, everything looks like a nail.

 To borrow from Stuart Halloway: simplicity ain't easy.

 The example solutions you provide from non-programmers seem
 straightforward, but that's because they're instructions designed to
 be followed by a human being, who can infer and reason, rather than a
 programming language that is constructed around the idea of precise
 commands.

 For instance, you just make pairs hides a considerable amount of
 depth. How do you make pairs? You might select one number, then look
 for another number that is identical, but how do you ensure you don't
 pick the same number?

 Or what about count how often a number is in the list. Again, it
 seems a simple thing to do, but only if you're giving instructions to
 a human. In programming there are many additional questions, like
 where to store the numbers whilst you're counting them. Do you go
 through the whole list for each number, or do you go through the list
 once and keep a tally? If you keep a tally, which data structure
 should be used to implement this?

 It's often a lot easier to find a complex solution to a problem than a
 simple one. Simple solutions are hard work to find.

 - James


+1 That is what I wanted to answer. Just try to write program for
collecting pairs and see what happens.
That solution could be simpler for a human - but then why do you speak
of programmers?
Yes the hammer is the computer in this case and some tasks perhaps
cold be solved in more straightforward way given some creativity is
applied. But then, again, we are talking about (digital electronic)
computers here?

My observations show that over time I am able to find simpler
solutions and experience clearly amplifies this. Initially this starts
with just knowing more shortcuts or most effective ways of using some
given technology or tool. But then there is more: now I often can see
redundant or unnecessary aspects of some systems that account for say
20-30% of whole codebase. This is the thing that my colleagues with
little experience usually do not see since it is always obscured by
numerous implementation details. Experience allows to concentrate less
on details of particular technology and more on a problem itself.

-- 
Petr Gladkikh

-- 
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: Ending or closing malformed line in REPL

2011-12-05 Thread Petr Gladkikh
Clojure REPL in Counterclockwise
(http://code.google.com/p/counterclockwise/) is pretty nice.
And yes, allowing multi-line expressions and having no way to edit
them is annoying.

On Tue, Dec 6, 2011 at 11:45 AM, jlhouchin jlhouc...@gmail.com wrote:
 On 12/5/2011 7:19 PM, Stephen Compall wrote:
 On Mon, 2011-12-05 at 09:51 -0800, jlhouchin wrote:
 When I entered the closing  and then closing
 paren. I was fine.

 You may also try backspace; unusually for a REPL, that works.

 I tried that. But as I was on a new line after hitting the enter/
 return key. It wouldn't go back to the previous line.  I would have
 loved that it did. That the backspace could backspace over newlines.
 That would be sweet.

 Now, I do not know if there are any difference in repls or if there is
 only one repl. I was using the default 1.3 repl.

 Thanks.

 Jimmie

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



-- 
Petr Gladkikh

-- 
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: Thoughts on CUDA + Clojure

2011-09-12 Thread Petr Gladkikh
On Fri, Sep 9, 2011 at 1:43 AM, Timothy Baldridge tbaldri...@gmail.com wrote:
 I've been kicking around an idea for some time, of starting a
 Clojure-CUDA compiler. I would like to start a discussion about this
 to figure out what some possible solutions are. First of all let me
 start a simple fact list:

 CUDA (for those who don't know) is NVIDIA's technology for writing
 general use code for modern GPUs. The current system uses a subset of
 C++ as it's input. The code looks like small functions/classes that
 are executed for each thread of the GPU. These threads can number in
 the thousands, and the GPU commonly executes hundreds of these at one
 time. So, basically we're talking of running pmap on a system will
 512+ cores.

 CUDA 4.0 supports some very advanced C++ features. As of 4.0 CUDA
 supports virtual functions, and new/deleteyes...your GPU code can
 allocate memory on the fly (if you have a GeForce 4xx or greater).

 My idea is to make a subset of Clojure translatable to CUDA. So you
 would create input data in native memory, the the Clojure functions
 would be translated to CUDA C++, then to CUDA binaries where they
 would be executed in the CPU.

 A very simple approach would be to take the view that may Clojure-SQL
 frameworks do, and simply do a translation. In this method all CUDA
 Clojure functions would take only arrays and scalar values as inputs,
 and the functions would read data from arrays, and output them to
 arrays. No sequences, on-the-fly allocation, or any such thing would
 be allowed.  On top of that, all input and output data must be of the
 same type, so no mixing doubles and floats, or ints and longs. All
 data must be resolved to staticlly defined types, and mutating the
 variable's type on the fly is not allowed.

 The more complex approach would be to use something like ClojureScript
 to compile core.clj to CUDA, and actually run a subset of Clojure on
 the GPU. In this case we would have to come up with a simple type
 system, and then rewrite the ClojureScript compiler to output C++ code
 instead of JS. In addition, some sort of simple GC (reference
 counting?) would have to be developed.  The result would be slower
 than my first approach, but would be much more flexible.

 

 So in the first version we have a simple to create system, but we
 can't use many of the functions we are familiar with in CUDA.

 In the second method, we have a slower, but much more powerful system
 that would integrate much more tightly with existing code.

 

 Any thoughts? Besides that I'm crazy...

 Timothy


In my opinion It would be wiser to target OpenCL first. So this effort
will not depend on particular hardware vendor.
AFAIK OpenCL is already supported by Intel, AMD and Nvidia. Although
being a common denominator OpenCL may lack some toys already available
in CUDA.
Just saying ...

-- 
Petr Gladkikh

-- 
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: Errors in Clojure

2011-08-19 Thread Petr Gladkikh
This is postgresql exception so problem is there (it's not Clojure's
one). Just try to connect via some other tool to confirm this, use
psql in command line, for example. Maybe there is missing group role
that is linked to login role 'mgr'.

PS: I hope that password is not a confidential one :)

2011/8/19 J. Pablo Fernández pup...@pupeno.com:
 Hello Clojurians,
 I'm struggling to work with errors in Clojure. This is one example, one
 case, but I had many like these before:
 user= (load-file /Users/pupeno/Projects/mgr/src/lobos/migrations.clj)
 org.postgresql.util.PSQLException: FATAL: role lobos does not exist
 (config.clj:1)
 It just gives one line, no stack trace. config.clj looks like this:
 (ns lobos.config)
 (def db
   {:classname org.postgresql.Driver
    :subprotocol postgresql
    :subname //localhost:5432/mgr
    :user mgr
    :password chi1lie7Wai3ioP6aing3ooci})
 What now? how do I start to unravel this? How would you do it?
 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



-- 
Petr Gladkikh

-- 
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: Elegant tools deserve elegant solutions. -- L. E. Gant

2011-08-11 Thread Petr Gladkikh
On Mon, Aug 8, 2011 at 1:46 PM, Tuba Lambanog tuba.lamba...@gmail.com wrote:
 Hello,

 I’m doing a word stemmer for a non-English language. A stemmer parses
 a word into its word parts: prefixes, roots, suffixes. The input word
 is at least a root word (English example would be ‘cloud’), but can be
 any combination of  prefix(es) and a root (e.g., 'pre-nuptial'), or a
 root and suffix(es) (‘cloudy’), or all three ('unidirection'). A
 sequence of more than one prefix in a word is considered one
 occurrence of a prefix, and similarly for complex prefixes, thus,
 ‘directional’ is considered to have the ‘single’ suffix ‘ional’. The
 prefixes, roots, and suffixes are in their own set data structure.

 The approach I am pursuing is to create a set of potential suffixes
 that the input word contains. Asssume, for simplicity, that the suffix
 set consists of #{-or, -er, -al, -ion, -ional, able}. The input
 ‘directional’ would have the candidate suffix set #{-al –ional}. Now,
 drop the longest suffix (‘ional’) from the input then check the
 remaining string (‘direct’) if it is a root; if it is, done. If not,
 try the next suffix (‘-al’) in the potential suffix set.  Prefixes
 will be similarly processed. Input words with both prefixes and
 affixes will be fun to do ;)

 I’m having a hard time thinking through the process of generating the
 candidate suffix set using set forms, and I’m beginning to think I
 have selected an arduous path (for me).

 Thoughts?


Somehow offtopic maybe, but have you looked at Snowball
http://snowball.tartarus.org/ ?
Algorithm is different but language that is used to describe stemmers
there is almost lisp and may be useful at least as reference.

-- 
Petr Gladkikh

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


Predicate problem

2011-08-02 Thread Petr Gladkikh
I spent hour already and can not understand what is wrong here.

I want to filter collection based on a nested field of collection
element having some value:

(println pinned-ids)
; #{200, 210}

(println (contains? pinned-ids 200))
; true

(println (map #(.. % :field :id) new-states))
; (10 20 21 100 200 500 400 210) ; This collection contains both 200 and 210

However no test is true
(println (map #(contains? pinned-ids (.. % :field :id)) new-states))
; (false false false false false false false false)

Why contains? return false for all elements despite that set
pinned-ids contains some of elements?

-- 
Petr Gladkikh

-- 
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: Predicate problem

2011-08-02 Thread Petr Gladkikh
On Tue, Aug 2, 2011 at 4:27 PM, Stefan Kamphausen
ska2...@googlemail.com wrote:
 Hi,

 can't you just use the set itself as the function determining whether the
 items contains the ID.

Set-as-function was the first thing I tried but it did not work either.
Based on my tweaking, type mismatch is more likely the problem as
Oskar suggested.

Thank you all for suggestions.


 The following example uses just a vector of maps for the new-states and thus
 uses - instead of .. but it seems to work

  user= (def pinned-ids #{200, 210})
  #'user/pinned-ids
  user= (def new-states [{:field {:id 200}
   :test 1}
  {:field {:id 10}
   :test 2}])
  #'user/new-states
  user= new-states
  [{:field {:id 200}, :test 1} {:field {:id 10}, :test 2}]
  user= (map #(- % :field :id) new-states) ;; uses - instead of ..
  (200 10)
  user= (map #(pinned-ids (- % :field :id)) new-states)
  (200 nil)
  user= (filter #(pinned-ids (- % :field :id)) new-states)
  ({:field {:id 200}, :test 1})

 (Don't know how formatting will get through googles interface; no preview
 button around)


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



-- 
Petr Gladkikh

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


Re: Java object field access

2011-07-29 Thread Petr Gladkikh
On Tue, Jul 26, 2011 at 5:14 PM, Ken Wesson kwess...@gmail.com wrote:
 On Tue, Jul 26, 2011 at 6:02 AM, Petr Gladkikh petrg...@gmail.com wrote:
 On Tue, Jul 26, 2011 at 3:28 PM, Alan Malloy a...@malloys.org wrote:
 On Jul 25, 11:10 pm, Petr Gladkikh petrg...@gmail.com wrote:
 I am trying to construct java object and assign it's fields from a map.
 That is given Java object of class Something { long id; String name; }
 and Clojure map {:id 12, :name Impostor}
 I would like to set fields of java object to respective values from map.

 Now, this works
 (let [o (Something.)]
   (set! (. o :id) 12)
   (set! (. o :name) Impostor)
   o)

 But as soon as I use some value to get field expression compiler
 starts complaining Invalid assignment target.
 that is
 (let [o (Something.)
        ff :id]
   (set! (. o ff) 12)
   o)
 I do not understand why this problem occurs. Any variations that I
 tried to made it work do not do the trick.
 Including weird (or ridiculous) ones like (set! (. o (symbol (str :
 (name ff))) 12)

 I suspect that this has something to do with compiler that needs field
 names at compile time but Clojure could use reflection for this...

 Can anyone point to what is wrong here?

 By the way is there already some function that allows to set fields of
 an object from a map?

 Clojure *could* use reflection to do this...unless your object had a
 field named ff! It has to decide at compile time how to look up a
 field, and at that time it doesn't know your object won't have a .ff
 field, so it figures, sure, I'll set the ff field.

 If you really want to do this (hint: you don't), you can manually deal
 with the reflection that the compiler would generate, as Shantanu
 outlines.

 Could you elaborate on this? What would you use instead in this case?
 My motivation is need to construct list of Java objects and I would
 like to have some concise syntax to write them. So I decided to do
 this with maps.
 I wrote a function that acts as constructor. But long list of

 (set! (. obj :aa) (:aa props))
 (set! (. obj :bb) (:bb props))
 (set! (. obj :cc) (:cc props))
 (set! (. obj :dd) (:dd props))

 looks not very lispy. Maybe I should use macros instead?

 Untested! But should give a general idea how to do this sort of thing:

 (defmacro defsetter [class keys]
  (let [o (gensym)
        p (gensym)]
    `(defn ~(symbol (str set- (.toLowercase (str class
       [~o ~p]
       ~@(map
           (fn [k]
             `(set! (. ~o ~k) (~k ~p)))
           keys

 (defsetter Foo [:a :b])

 (set-foo a-foo {:a 0 :b 42})

 (defsetter Bar [:x :y :z])

 (ser-bar a-bar {:x 4 :y 8 :z 15})

 --
 Protege: What is this seething mass of parentheses?!
 Master: Your father's Lisp REPL. This is the language of a true
 hacker. Not as clumsy or random as C++; a language for a more
 civilized age.


I tried this since I have not used macroses for real problem so far.
And it actually works.
But I do not understand why it works.
I have class:
class Foo {
public String s;
public int v;
public String toString()  { return { + s + , + v + }; }
}

Then in Clojure:
 (defsetter abcde [:s :v])
 (let [afoo (actialpackage.Foo.)]
   (set-abcde afoo {:s S :v 42})
   (println afoo))

But at the moment  (defsetter abcde [:s :v]) is expanded nothing is
known about actual class.
So it is not clear to me why this works but giving field names at
runtime does not.

Can anyone clarify this?
Maybe this wokrs because in this case compiler can infer type of java
object at compile time?

-- 
Petr Gladkikh

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


Java object field access

2011-07-26 Thread Petr Gladkikh
I am trying to construct java object and assign it's fields from a map.
That is given Java object of class Something { long id; String name; }
and Clojure map {:id 12, :name Impostor}
I would like to set fields of java object to respective values from map.

Now, this works
(let [o (Something.)]
  (set! (. o :id) 12)
  (set! (. o :name) Impostor)
  o)

But as soon as I use some value to get field expression compiler
starts complaining Invalid assignment target.
that is
(let [o (Something.)
   ff :id]
  (set! (. o ff) 12)
  o)
I do not understand why this problem occurs. Any variations that I
tried to made it work do not do the trick.
Including weird (or ridiculous) ones like (set! (. o (symbol (str :
(name ff))) 12)

I suspect that this has something to do with compiler that needs field
names at compile time but Clojure could use reflection for this...

Can anyone point to what is wrong here?

By the way is there already some function that allows to set fields of
an object from a map?

-- 
Petr Gladkikh

-- 
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: Calling clojure from java.

2011-07-26 Thread Petr Gladkikh
On Tue, Jul 26, 2011 at 12:43 PM, mmwaikar mmwai...@gmail.com wrote:
 Hi,

 I am using the Lobos library - https://github.com/budu/lobos
 In it there's a function create, which is called like this - (create db
 (table :some-name)), where db is earlier defined as - (def db
  {:classname org.postgresql.Driver
   :subprotocol postgresql
   :user postgres
   :password 
   :subname //localhost:5432/flyway})

 If I have to call the above function from Java, how do I -
 1) define the def (do I have to load some variable)?
 2) I am loading the reqd. lobos libraries using [ RT.load(lobos/core,
 true); ] and then getting the reference to the create function like - [ Var
 foo = RT.var(lobos.core, create); ]
     But how do I pass the required arguments to this function. Do I need to
 create an array?
You might look at examples here
http://en.wikibooks.org/wiki/Clojure_Programming/Tutorials_and_Tips#Invoking_Clojure_from_Java

If i were doing same thing I would put all necessary initalization
into a Clojure script and wrapped necessary functions to have less
arguments to pass from Java.
This way later you'll need to load single script only. Say

(ns
  (use lobos.core))
(def db {.})
(defn create2 [table-name] (create db (table table-name)))

This might not be convenient in your case however.

-- 
Petr Gladkikh

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


Re: Java object field access

2011-07-26 Thread Petr Gladkikh
On Tue, Jul 26, 2011 at 3:28 PM, Alan Malloy a...@malloys.org wrote:
 On Jul 25, 11:10 pm, Petr Gladkikh petrg...@gmail.com wrote:
 I am trying to construct java object and assign it's fields from a map.
 That is given Java object of class Something { long id; String name; }
 and Clojure map {:id 12, :name Impostor}
 I would like to set fields of java object to respective values from map.

 Now, this works
 (let [o (Something.)]
   (set! (. o :id) 12)
   (set! (. o :name) Impostor)
   o)

 But as soon as I use some value to get field expression compiler
 starts complaining Invalid assignment target.
 that is
 (let [o (Something.)
        ff :id]
   (set! (. o ff) 12)
   o)
 I do not understand why this problem occurs. Any variations that I
 tried to made it work do not do the trick.
 Including weird (or ridiculous) ones like (set! (. o (symbol (str :
 (name ff))) 12)

 I suspect that this has something to do with compiler that needs field
 names at compile time but Clojure could use reflection for this...

 Can anyone point to what is wrong here?

 By the way is there already some function that allows to set fields of
 an object from a map?

 Clojure *could* use reflection to do this...unless your object had a
 field named ff! It has to decide at compile time how to look up a
 field, and at that time it doesn't know your object won't have a .ff
 field, so it figures, sure, I'll set the ff field.

 If you really want to do this (hint: you don't), you can manually deal
 with the reflection that the compiler would generate, as Shantanu
 outlines.

Could you elaborate on this? What would you use instead in this case?
My motivation is need to construct list of Java objects and I would
like to have some concise syntax to write them. So I decided to do
this with maps.
I wrote a function that acts as constructor. But long list of

(set! (. obj :aa) (:aa props))
(set! (. obj :bb) (:bb props))
(set! (. obj :cc) (:cc props))
(set! (. obj :dd) (:dd props))

looks not very lispy. Maybe I should use macros instead?

-- 
Petr Gladkikh

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


Re: Why take-last of empty collection is nil?

2011-02-03 Thread Petr Gladkikh
On Thu, Feb 3, 2011 at 1:31 PM, Meikel Brandmeyer m...@kotka.de wrote:
 Hi,

 On 3 Feb., 08:04, Petr Gladkikh petrg...@gmail.com wrote:

 Should not it be empty colection instead?
 It seems odd to me since it is inconsistent and forces to consider one
 more case (nil or collection).

 It is consistent. There is a difference between () and nil. () is the
 empty list. However there is no empty sequence. Either there is
 something or there is nothing. Why would you have to check for nil?
 You can pass nil to any of the sequence library functions without fear
 of harm. When you write such a function yourself, there is usually a
 single check in the beginning when realising the sequence. Something
 like (when-let [s (seq coll)] ...).

 I never encountered any problems with this. Do you have a concrete
 example where this causes trouble for you?

I have a vector that holds some history. I conj new items to it and to
save space I'd like to retain not more than n last items.
To do that I used (take-last n history). So: [] - (take-last n []) -
nil - (conj nil newItem) - '(newItem)

But list conj's at the beginning not at end of sequence as I would
like to. Of course I could use () from the beginning (with account for
reverse order).
But with [] I should do little more.

-- 
Petr Gladkikh

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


Why take-last of empty collection is nil?

2011-02-02 Thread Petr Gladkikh
Should not it be empty colection instead?
It seems odd to me since it is inconsistent and forces to consider one
more case (nil or collection).

And another question. I have written this function
(defn index-by
  Make map (f x) - x
  [f coll]
  (reduce #(assoc %1 (f %2) %2) {} coll))

I wonder, is there already such function somewhere in Clojure libraries?

-- 
Petr Gladkikh

-- 
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: Quicksort with accumulator

2010-12-28 Thread Petr Gladkikh
Why do you call qsort* inside of partify? I do not really grasp your
logic behind this.

On Tue, Dec 28, 2010 at 8:20 PM, Baishampayan Ghose b.gh...@gmail.com wrote:
 Hello,

 I tried writing a naive implementation of quicksort using an
 accumulator. Right now, the code is stack-consuming and returns a
 stackoverflowerror on large lists. Is there any way to prevent it from
 consuming stack with some changes? The code is as follows -

 (declare qsort qsort* partify)

 (defn partify
  [item coll [less equal greater] acc]
  (if (empty? coll)
    (qsort* less (concat equal (qsort* greater acc)))
    (let [[head  tail] coll]
      (cond
       ( head item) (recur item tail [(cons head less) equal greater] acc)
       ( head item) (recur item tail [less equal (cons head greater)] acc)
       :else (recur item tail [less (cons head equal) greater] acc)

 (defn qsort*
  [coll acc]
  (if-let [coll (seq coll)]
    (partify (first coll) (rest coll) [[] [(first coll)] []] acc)
    acc))

 (defn qsort
  Perform Quicksort, with apologies to C.A.R. Hoare
  [coll]
  (if-let [coll (seq coll)]
    (qsort* coll [])
    []))

 Regards,
 BG

 --
 Baishampayan Ghose
 b.ghose at gmail.com

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



-- 
Petr Gladkikh

-- 
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: Quicksort with accumulator

2010-12-28 Thread Petr Gladkikh
And also qsort may take up to n stack frames for collection of n
elements if you partition function is not optimal. In your case - if
input collection is sorted (as long as you split by first element).

On Tue, Dec 28, 2010 at 11:13 PM, Petr Gladkikh petrg...@gmail.com wrote:
 Why do you call qsort* inside of partify? I do not really grasp your
 logic behind this.

 On Tue, Dec 28, 2010 at 8:20 PM, Baishampayan Ghose b.gh...@gmail.com wrote:
 Hello,

 I tried writing a naive implementation of quicksort using an
 accumulator. Right now, the code is stack-consuming and returns a
 stackoverflowerror on large lists. Is there any way to prevent it from
 consuming stack with some changes? The code is as follows -

 (declare qsort qsort* partify)

 (defn partify
  [item coll [less equal greater] acc]
  (if (empty? coll)
    (qsort* less (concat equal (qsort* greater acc)))
    (let [[head  tail] coll]
      (cond
       ( head item) (recur item tail [(cons head less) equal greater] acc)
       ( head item) (recur item tail [less equal (cons head greater)] acc)
       :else (recur item tail [less (cons head equal) greater] acc)

 (defn qsort*
  [coll acc]
  (if-let [coll (seq coll)]
    (partify (first coll) (rest coll) [[] [(first coll)] []] acc)
    acc))

 (defn qsort
  Perform Quicksort, with apologies to C.A.R. Hoare
  [coll]
  (if-let [coll (seq coll)]
    (qsort* coll [])
    []))

 Regards,
 BG

 --
 Baishampayan Ghose
 b.ghose at gmail.com

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



 --
 Petr Gladkikh




-- 
Petr Gladkikh

-- 
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: Purpose of Macros

2010-11-29 Thread Petr Gladkikh
On Mon, Nov 29, 2010 at 3:29 PM, Ken Wesson kwess...@gmail.com wrote:
 In Java, you often have to pair things, e.g. opening a file and
 closing it, to avoid leaking resources like file handles.

 These pairings are among many cases where Java code contains structure
 that you can't extract and reify in your program.

Well, most things _can_ be reified in Java but resulting code would be
so bloated, cumbersome and often inefficient so I do not try to do
that anymore in Java. That is result of such refactoring only makes
program even worse (here I wrote about my attempts in this direction
http://petrglad.blogspot.com/2010/02/stream-close-template.html).
Sorry, just nitpicking.

-- 
Petr Gladkikh

-- 
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: Mobile Clojure

2010-11-13 Thread Petr Gladkikh
On Thu, Nov 11, 2010 at 5:49 AM, Glen Rubin rubing...@gmail.com wrote:
 Are there any mobile platforms that clojure runs well on?  I saw that
 clojure is available for Android, but runs very slowly.

There are some hacks (discussed on this list a while ago, IIRC) that
convert dalvik code converter to dalvik bytecode to load clojure
classes inside Android at run-time. If you do not need to load Clojure
code at run-time, then, I suppose, performance difference would be
about the same as Java vs Clojure on JVM.
See this, for example,
http://groups.google.com/group/clojure/browse_thread/thread/14725172c626642c?pli=1

-- 
Petr Gladkikh

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