Search Results

Search found 7019 results on 281 pages for 'learning clojure'.

Page 7/281 | < Previous Page | 3 4 5 6 7 8 9 10 11 12 13 14  | Next Page >

  • Porting a piece of Lisp code to Clojure (PAIP)

    - by Robert Brown
    I'm reading Paradigms of Artificial Intelligence Programming (PAIP) by Peter Norvig and I'm trying to write all the code in Clojure rather than common Lisp. However I'm stuck on this piece of code on page 39: (defparameter *simple-grammar* '((sentence -> (noun-phrase verb-phrase)) (noun-phrase -> (Article Noun)) (verb-phrase -> (Verb noun-phrase)) (Article -> the a) (Noun -> man ball woman table) (Verb -> hit took saw liked)) "A grammar for a trivial subset of English.") (defvar *grammar* *simple-grammar*) How can I translate this into Clojure? Thanks.

    Read the article

  • Defining a SPI in Clojure

    - by Joe Holloway
    I'm looking for an idiomatic way(s) to define an interface in Clojure that can be implemented by an external "service provider". My application would locate and instantiate the service provider module at runtime and delegate certain responsibilities to it. Let's say, for example, that I'm implementing a RPC mechanism and I want to allow a custom middleware to be injected at configuration time. This middleware could pre-process the message, discard messages, wrap the message handler with logging, etc. I know several ways to do this if I fall back to Java reflection, but feel that implementing it in Clojure would help my understanding. (Note, I'm using SPI in a general sense here, not specifically referring to the way it's defined in the JAR file specification) Thanks

    Read the article

  • Daemon with Clojure/JVM

    - by Isaac Copper
    I'd like to have a small (not doing too damn much) daemon running on a little server, watching a directory for new files being added to it (and any directories in the main one), and calling another Clojure program to deal with that new file. Ideally, each file would be added to a queue (a list represented by a ref in Clojure?) and the main process would take care of those files in the queue on a FIFO basis. My question is: is having a JVM up running this little program all the time too much a resource hog? And do you have any suggestions as to how go about doing this? Thank you very much!

    Read the article

  • Why is there a limit of max 20 parameters to a clojure function

    - by GuyC
    Hi, there seems to be a limit to the number of parameters a clojure function can take. When defining a function with more than 20 parameters I receive the following: Obviously this can be avoided, but I was hitting this limit porting the execution model of an existing DSL to clojure, and I have constructs in my DSL like the following, which by macro expansion can be mapped to functions quite easily except for this limit: (defAlias nn1 ((element ?e1) (element ?e2)) number "@doc features of the elements are calculated for entry into the first neural network, the result is the score computed by the latter" (nn1-recall (nn1-feature00 ?e1 ?e2) (nn1-feature01 ?e1 ?e2) ... (nn1-feature89 ?e1 ?e2))) which is a DSL statement to call a neural network with 90 input nodes. Can work around it of course, but was wondering where the limit comes from. Thanks.

    Read the article

  • Using type hints in Clojure for Java return values

    - by mikera
    I'm working on some Java / Clojure interoperability and came across a reflection warning for the following code: (defn load-image [resource-name] (javax.imageio.ImageIO/read (.getResource (class javax.imageio.ImageIO) resource-name))) => Reflection warning, clojure/repl.clj:37 - reference to field read can't be resolved. I'm surprised at this because getResource always returns a URL and I would therefore expect the compiler to use the appropriate static method in javax.imageio.ImageIO/read. The code works fine BTW so it is clearly finding the right method at run time. So two questions: Why is this returning a reflection warning? What type hint do I need to fix this?

    Read the article

  • Idiomatic approach for structuring Clojure source code

    - by mikera
    I'm interested in how people structure their Clojure source code. Being used to Java, I'm pretty familiar with the paradigm of one class per source code file, bundling all the data and method definitions with appropriate comments and annotations etc. However Clojure offers a lot more flexibility, and I'm not sure how I should structure my project (likely to end up as a medium sized app, maybe 5,000 lines with three or four distinct subsystems) In particular I'm wrestling with: What guidelines should I use to determine whether code should be in a single namespace vs. separated out into different namespaces? Should each protocol/datatype have it's own namespace + source file with associated set of functions? When should I require vs. use other namespaces?

    Read the article

  • Controlling symbol generation in Clojure macros

    - by mikera
    I'm trying (as a self-learning exercise) to create a Clojure macro that will generate code to apply a function to a sequence of integers and sum the result, e.g. f(0) + f(1) + f(2) + f(3) This is my attempt: (defmacro testsum [func n] `(fn [x#] (+ ~@( map (fn [i] `(~func x#)) (range n))))) However something seems to go wrong with the x# gensym and I end up with two different versions of x and hence the function doesn't work: (macroexpand '(testsum inc 3)) gives: (fn* ([x__809__auto__] (clojure.core/+ (inc x__808__auto__) (inc x__808__auto__) (inc x__808__auto__)))) This is pretty much exactly what I want apart from the different 809 and 808 versions of x..... What am I doing wrong? I thought that the auto gensym was meant to create a single unique symbol for exactly this kind of purpose? Is there a better way of doing this?

    Read the article

  • Dynamically generating high performance functions in clojure

    - by mikera
    I'm trying to use Clojure to dynamically generate functions that can be applied to large volumes of data - i.e. a requirement is that the functions be compiled to bytecode in order to execute fast, but their specification is not known until run time. e.g. suppose I specify functions with a simple DSL like: (def my-spec [:add [:multiply 2 :param0] 3]) I would like to create a function compile-spec such that: (compile-spec my-spec) Would return a compiled function of one parameter x that returns 2x+3. What is the best way to do this in Clojure?

    Read the article

  • Understanding Clojure concurrency example

    - by dusha
    Hello, I just go through various documentation on Clojure concurrency and came accross the example on the website (http://clojure.org/concurrent_programming). (import '(java.util.concurrent Executors)) (defn test-stm [nitems nthreads niters] (let [refs (map ref (replicate nitems 0)) pool (Executors/newFixedThreadPool nthreads) tasks (map (fn [t] (fn [] (dotimes [n niters] (dosync (doseq [r refs] (alter r + 1 t)))))) (range nthreads))] (doseq [future (.invokeAll pool tasks)] (.get future)) (.shutdown pool) (map deref refs))) I understand what it does and how it works, but I don't get why the second anonymous function fn[] is needed? Many thanks, dusha. P.S. Without this second fn [] I get NullPointerException.

    Read the article

  • Adding fields to a proxied class in Clojure

    - by mikera
    I'm using "proxy" to extend various Swing classes in a Clojure GUI application, generally with code that looks something like: (def ^JPanel mypanel (proxy [JPanel] [] (paintComponent [#^Graphics g] (.drawImage g background-image 0 0 nil)))) This works well but I can't figure out how to add additional fields to the newly extended class, for example making the background-image a field that could be subsequently updated. This would be pretty easy and common practice in Java. Is there a good way to do this in Clojure? Or is there another preferred method to achieve the same effect?

    Read the article

  • Organizing test hierarchy in clojure project

    - by Sergey
    There are two directories in a clojure project - src/ and test/. There's a file my_methods.clj in the src/calc/ directory which starts with (ns calc.my_methods...). I want to create a test file for it in test directory - test/my_methods-test.clj (ns test.my_methods-test (:require [calc.my_methods]) (:use clojure.test)) In the $CLASSPATH there are both project root directory and src/ directory. But the exception is still "Could not locate calc/my_methods__init.class or calc/my_methods.clj on classpath". What is the problem with requiring it in the test file? echo $CLASSPATH gives this: ~/project:~/project/src

    Read the article

  • how is a macro expanded in clojure?

    - by john wang
    In the book Programming Clojure(Stuart), when read how macros are expanded I got confused. user=> (defmacro chain ([x form] (list '. x form)) ([x form & more] (concat (list 'chain (list '. x form)) more))) #'user/chain The above macro can be expanded as: user=> (macroexpand '(chain a b c)) (. (. a b) c) But the following is only expanded to the first level: user=> (macroexpand '(and a b c)) (let* [and__3822__auto__ a] (if and__3822__auto__ (clojure.core/and b c) and__3822__auto__)) The and macro source: user=> (source and) (defmacro and([] true) ([x] x) ([x & next] `(let [and# ~x] (if and# (and ~@next) and#)))) Why is the chain macro expanded all the way but the and not ? Why is it not expanded to something like the following: user=> (macroexpand '(chain a b c d)) (. (chain a b c) d)

    Read the article

  • Difference in F# and Clojure when calling redefined functions

    - by Michiel Borkent
    In F#: > let f x = x + 2;; val f : int -> int > let g x = f x;; val g : int -> int > g 10;; val it : int = 12 > let f x = x + 3;; val f : int -> int > g 10;; val it : int = 12 In Clojure: 1:1 user=> (defn f [x] (+ x 2)) #'user/f 1:2 user=> (defn g [x] (f x)) #'user/g 1:3 user=> (g 10) 12 1:4 user=> (defn f [x] (+ x 3)) #'user/f 1:5 user=> (g 10) 13 Note that in Clojure the most recent version of f gets called in the last line. In F# however still the old version of f is called. Why is this and how does this work?

    Read the article

  • Database Functional Programming in Clojure

    - by Ralph
    "It is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail." - Abraham Maslow I need to write a tool to dump a large hierarchical (SQL) database to XML. The hierarchy consists of a Person table with subsidiary Address, Phone, etc. tables. I have to dump thousands of rows, so I would like to do so incrementally and not keep the whole XML file in memory. I would like to isolate non-pure function code to a small portion of the application. I am thinking that this might be a good opportunity to explore FP and concurrency in Clojure. I can also show the benefits of immutable data and multi-core utilization to my skeptical co-workers. I'm not sure how the overall architecture of the application should be. I am thinking that I can use an impure function to retrieve the database rows and return a lazy sequence that can then be processed by a pure function that returns an XML fragment. For each Person row, I can create a Future and have several processed in parallel (the output order does not matter). As each Person is processed, the task will retrieve the appropriate rows from the Address, Phone, etc. tables and generate the nested XML. I can use a a generic function to process most of the tables, relying on database meta-data to get the column information, with special functions for the few tables that need custom processing. These functions could be listed in a map(table name -> function). Am I going about this in the right way? I can easily fall back to doing it in OO using Java, but that would be no fun. BTW, are there any good books on FP patterns or architecture? I have several good books on Clojure, Scala, and F#, but although each covers the language well, none look at the "big picture" of function programming design.

    Read the article

  • Difference in calling redefined functions in F# and Clojure

    - by Michiel Borkent
    In F#: > let f x = x + 2;; val f : int -> int > let g x = f x;; val g : int -> int > g 10;; val it : int = 12 > let f x = x + 3;; val f : int -> int > g 10;; val it : int = 12 In Clojure: (defn f [x] (+ x 2)) (defn g [x] (f x)) (g 10) ;; => 12 (defn f [x] (+ x 3)) (g 10) ;; => 13 Note that in Clojure the most recent version of f gets called in the last line. In F# however still the old version of f is called. Why is this?

    Read the article

  • Getting the name of a Clojure struct type?

    - by j-g-faustus
    When defining a struct type and instance, I can print the value and get the "struct" implementation type: (defstruct person :name :age) (def p (struct person "peter" 30)) user=> p {:name "peter", :age 30} user=> (type p) clojure.lang.PersistentStructMap But is it possible to tell whether p is an instance of the struct type "person"?

    Read the article

  • How to go about reading a web page lazily in Clojure

    - by Rayne
    I and a friend recently implemented link grabbing in my Clojure IRC bot. When it sees a link, it slurp*s the page and grabs the title from the page. The problem is that it has to slurp* the ENTIRE page just to grab the link. How does one go about reading a page lazily until the first ?

    Read the article

  • How do I stop jetty server in clojure?

    - by Mad Wombat
    I am writing a web application using ring and clojure. I am using the jetty adapter for the development server and emacs/SLIME for IDE. While wrap-reload does help, run-jetty blocks my slime session and I would like to be able to start/stop it at will without having to run it in a separate terminal session. Ideally, I would like to define a server agent and functions start-server and stop-server that would start/stop the server inside the agent. Is this possible?

    Read the article

< Previous Page | 3 4 5 6 7 8 9 10 11 12 13 14  | Next Page >