Clojure differences between Ref, Var, Agent, Atom, with examples

I'm very new to Clojure, Can you guys give me explanation with real world scenarios. I mean, where to use Ref, Var, Agent, Atom. I read book, but, still couldn't understand the real world examples. I highly recommend "The Joy of Clojure" or "programming Clojure" for a real answer to this question, I can reproduce a short snip-it of the motivations for each:start by watching this video on the notion of Identity and/or studying here.Refs are for Coordinated Synchronous access to "Many Identities".Atoms are for Uncoordinated synchronous access to a single Identity.Agents a...

how can macros be evaluated using the clojurescript repl

Once the browser is connected to the clojurescript repl, I previously had no way of calling macros from the repl. This is an issue that has put me off clojurescript in the past, preferring using javascript directly. Basically, I felt that the cljs-repl was kinda lame and I was going back to the compile/debug cycle that writing code in clojure was supposed to emancipate us from.Are there any good workarounds/workflows for pushing and testing code in clojurescript? Especially if macros can be evaluated?An example of my problem is:make a new cljs project lein new cljs-templa...

Clojure: slurping structs from file fails with string attributes containing whitespaces

I have just started playing with Clojure and the first thing I thought I'd try is storing and retrieving a list of structs, like in Suart Halloway's example here.My spit/slurp of a hash of structs works fine with, if I use struct instances without spaces in the attribute strings like the following:(struct customer "Apple" "InfiniteLoop")But if I use this:(struct customer "Apple" "Infinite Loop 1")I get an error:Exception in thread "main" clojure.lang.LispReader$ReaderException: java.lang.ArrayIndexOutOfBoundsException: 7 (test-storing.clj:19) at clojure.lang.Compiler$Inv...

Multiple assoc-in's in one swap! operation eg. to update x, y of point at the same time

My question is about nested map's/key's in a clojure atom and how to update them at the same time. In my case I have a nested map in another map wich is a state holding atom of a little game.This is my atom:(def state (atom {:mousePos {:x 0 :y 0} :playArea {:width 10000 :height 10000} :player {:zoomOut 7.5 :cells [{:center {:x 1234 :y 5678} :radius 17.84124116 :area 1000}] :gravityCenter {:x 1234 :y 5678} ...

Compojure-api removes body-param after the first group of API routes

I am currently using the Luminus template with the +swagger option, which adds compojure-api support.My current setup is like so (only relevant bits):(ns my-project.routes.services (:require [compojure.api.sweet :refer :all] ,,,)(defapi service-routes (POST "/login" req :return Result :body [user UserLogin] :summary "Log the user in" (auth/login! req user))(defapi restricted-service-routes (POST "/login2" req :return Result :body [user UserLogin] :summary "Log the user in" (auth/login! req user)))and (ns my-project.handler) (:require ...

Reading Ring request body when already read

My question is, how can I idiomatically read the body of a Ring request if it has already been read?Here's the background. I'm writing an error handler for a Ring app. When an error occurs, I want to log the error, including all relevant information that I might need to reproduce and fix the error. One important piece of information is the body of the request. However, the statefulness of the :body value (because it is a type of java.io.InputStream object) causes problems.Specifically, what happens is that some middleware (the ring.middleware.json/wrap-json-body middlew...

Enclojure REPL can't find dependent clj file on Load

I have a problem with the Enclojure REPL and using clojure modules from it. The Load/Change REPL to file/ns works fine with an isolated clojure file, but breaks with a file which has references to another clojure file which I try to use from my project. Here are the exact steps:Create a new project.Create a clojure module foobar.clj (namespace com.acme.foobar)Define a function which returns a value in foobar.clj:(ns com.acme.foobar (:use com.acme.othermodule))(defn myfunc1 [] "a")Open a Netbeans IDE REPLFrom foobar.clj's context menu select:Change REPL to file/nsLoad From...

Are clojure forms data or type?

I am beginning learning clojure. Coming majorly from Java and some scripting languages, the code as data part of clojure (and all functional languages, I assume) is not so clear.Starting from the first pages of the book I see forms. And I see it mostly associated with data, like #{1 2}. But in some other places I saw Boolean labelled as form. Now, I was under the assumption that false is a form of type Boolean, but I didn't see type being mentioned more than a few times, so I am confused.Can someone give me a push to understand the concept of forms and also give me a push s...

Clojure - Hiccup Dropdown menu

I have a Clojure table in Hiccup that is populated from a database. In one column I have status which is set from the database. What I want to do is be able to click on this value, and have a dropdown menu appear where you can pick a new status from the dropdown menu.I am confused on how to do this. I have tried using a form with a placeholder where the value comes from the database but when I click on the status in the browser, I have to type instead of having a dropdown. How do I get the dropdown?(defn row-data [data] (for [x data] (let [[entity-id id date text status...

Efficient Datomic query to perform filtering on paginated sets

Given that Datomic does not support pagination I'm wondering how to efficiently support a query such as: Take the first 30 entities on :history/body, find entities whose :history/body matches some regex.Here's how I'd do regex matching alone:{:find [?e] :where [[?e :history/body ?body] [(re-find #"foo.*bar$" ?body)]]}Observations:I could then (take ...) from those, but that is not the same as matching against the first 30 entities.I could get all entities, take 30 then manually filter with re-find, but if I have 30M entities, getting all of them just to take 30 se...

Compojure-Api return vector of schema objects

I have started learning Luminus framework to get into Clojure(script) for web applications. For RESTful services framework suggests using swagger/compojure-api. Examples were helpful, but I can't find any for returning collections.Here is my model:CREATE TABLE thread(id INTEGER PRIMARY KEY AUTO_INCREMENT,name VARCHAR(30));And Conman query:-- :name get-threads :? :*-- :doc selects all threadsSELECT * FROM threadFor service there is a schema (it is called Threadd because of java's Thread):(s/defschema Threadd {:id s/Int :name s/Str})Finally, in service-ro...

How to best shut down a clojure core.async pipeline of processes

I have a clojure processing app that is a pipeline of channels. Each processing step does its computations asynchronously (ie. makes a http request using http-kit or something), and puts it result on the output channel. This way the next step can read from that channel and do its computation.My main function looks like this(defn -main [args] (-> file/tmp-dir (schedule/scheduler) (search/searcher) (process/resultprocessor) (buy/buyer) (report/reporter)))Currently, the scheduler step drives the pipeline (it hasn't got an input channel), and provides the chain with wor...

Can someone explain Clojure Transducers to me in Simple terms?

I have tried reading up on this but I still don't understand the value of them or what they replace. And do they make my code shorter, more understandable or what?UpdateAlot of people posted answers, but it would be nice to see examples of with and without transducers for something very simple, which even an idiot like me can understand. Unless of course transducers need a certain high level of understanding, in which case I will never understand them :( Transducers are recipes what to do with a sequence of data without knowledge what the underlying sequence is (how to d...

Clojure order dependency when calling functions in -main

I am new to Clojure and am just trying to build some sample apps to get used to the syntax. I noticed the following order dependency behaviour.I created a project called timex to calculate the time in weeks between two dates. I am using the clj-time functions for the date difference calculations.If my core.clj looks as follows: (ns timex.core (:gen-class)) (defn -main "Calculate weeks between dates." [& args] dp ) (require '[clj-time.core :as t]) (def d2 (t/date-time 1989 01 07)) (def dw (t/in-weeks (t/interval d2 (t/now)))) (def dp (str "The number of weeks bet...

Clojure form - Submit when press enter

I have a search-form that searches some text for me. When I type in the input box I have to manually press a button for it to search. Is there a way for me to hit enter on the keyboard and have that search, as well as the button?(defn search-form [] [:div [:p "What are you searching for? " [:input {:type :text :name :search :on-change #(swap! fields assoc :search (-> % .-target .-value)) :value (:search @fields)}]] [:input {:type :submit :value :Search :on-click #(do (search-function (:search @fields)))}] [@search-...

How well does Programming Clojure work on the Kindle?

Programming Clojure is currently available only in electronic form. I see it's available in .mobi format for the Kindle. My question is, how well does this work? I know some programming books can lose the formatting of the code on the Kindle. Has anyone tried this? Does this version work well? I have 3 books from the Pragmatic guys on my Kindle 2 right now and they render wonderfully.Although I'm upgrading to the Kindle DX purely for the native PDF reading functionality. I really can't wait. [XXX]The big thing is that they use a fair bit of color in the Prag boo...

Page 1 of 138  |  Show More Pages:  Top Prev Next Last