Re: How aliased "require" is different?
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 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 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 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.
Re: How to generate a serializable java class in Clojure by gen-class?
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 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 > SO<http://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?
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 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
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…
: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:542) at leiningen.core$_main.invoke(core.clj:328) at user$eval42.invoke(NO_SOURCE_FILE:1) at clojure.lang.Compiler.eval(Compiler.java:5424) ... 11 more And runnung cljsh.sh gives petr@host:~$ cljsh.sh -c '(println "hello")' /home/petr/bin/cljsh.sh: 20: Syntax error: "(" unexpected -- 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: are non programmers the better programmers?
On Wed, Jan 18, 2012 at 4:46 AM, James Reeves wrote: > On 17 January 2012 20:46, Dennis Haupt 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
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 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
On Fri, Sep 9, 2011 at 1:43 AM, Timothy Baldridge 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
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 : > 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
On Mon, Aug 8, 2011 at 1:46 PM, Tuba Lambanog 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
Re: Predicate problem
On Tue, Aug 2, 2011 at 4:27 PM, Stefan Kamphausen 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
Predicate problem
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: Java object field access
On Tue, Jul 26, 2011 at 5:14 PM, Ken Wesson wrote: > On Tue, Jul 26, 2011 at 6:02 AM, Petr Gladkikh wrote: >> On Tue, Jul 26, 2011 at 3:28 PM, Alan Malloy wrote: >>> On Jul 25, 11:10 pm, Petr Gladkikh 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
Re: Java object field access
On Tue, Jul 26, 2011 at 3:28 PM, Alan Malloy wrote: > On Jul 25, 11:10 pm, Petr Gladkikh 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: Calling clojure from java.
On Tue, Jul 26, 2011 at 12:43 PM, mmwaikar 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
Java object field access
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: Why take-last of empty collection is nil?
On Thu, Feb 3, 2011 at 1:31 PM, Meikel Brandmeyer wrote: > Hi, > > On 3 Feb., 08:04, Petr Gladkikh 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?
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
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 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 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: Quicksort with accumulator
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 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: Purpose of Macros
On Mon, Nov 29, 2010 at 3:29 PM, Ken Wesson 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
On Thu, Nov 11, 2010 at 5:49 AM, Glen Rubin 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