This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-11-26
Channels
- # admin-announcements (70)
- # aws (1)
- # beginners (17)
- # boot (37)
- # business (1)
- # cider (2)
- # cljs-dev (56)
- # cljsrn (6)
- # clojure (151)
- # clojure-germany (1)
- # clojure-nl (5)
- # clojure-poland (5)
- # clojure-russia (34)
- # clojure-taiwan (1)
- # clojurescript (289)
- # clojurex (2)
- # cursive (16)
- # datavis (3)
- # datomic (12)
- # editors (10)
- # emacs (3)
- # hoplon (17)
- # ldnclj (5)
- # lein-figwheel (12)
- # leiningen (1)
- # liberator (1)
- # off-topic (23)
- # om (116)
- # onyx (39)
- # parinfer (44)
- # portland-or (1)
- # reagent (34)
- # yada (6)
Looking for some kind of parser or FSM. So far, I can’t use instaparse (I need to be able to update with individual input elements and test for accept state and I need to process inputs that aren’t chars). Automat will work, but it seems kind of difficult to compose reducers. Any options I should look at?
Or if you need just a FSM, you can simply code your own fairly easily (although you appear to need a parser): https://github.com/Day8/re-frame/blob/master/src/re_frame/router.cljs#L131-L170
@mikethompson: reduce-fsm seems pretty promising.
After playing with reduce-fsm, automat, and instaparse, I think a hand-rolled state machine is the best option.
How could I get a parent vector, which child has a specific string in it?
[vectortoget [„stringtosearchfor“]]
@danielgrosse: (map first (filter (fn [[parent [child]] (= child sought)) vectors))
Okay, thank you. I will try it.
Hello, I want to try to make my first toy programm where I can keep track what in my wallet
as far as I understand I can use a atom to keep track of the amount and swap! to change it when I give money out for something or put some money in my wallet
Now I wonder if I can use compare-and-set so I know every transition is take care the right way
@rauh https://github.com/clojure/clojure/blob/bc186508ab98514780efbbddb002bf6fd2938aee/src/jvm/clojure/lang/Iterate.java
@jaen: guard against that if I make it bigger so multiple people can work with it that the transactions will be in the right order.
@robert-stuttaford: Thanks. No IChunkedSeq
so I guess I'll assume it'll stay like that.
@roelof: can you define what right order means? For example by giving an example of what would be right order and what would not be. I'm just trying to understand what exactly are you after.
@jaen : just a example out of my head. Suppose you have a bank-account with amount 0 , Now one person makes a deposit of 100 euro and another person pays a bill of 50 euro. At this order no problem. 0 + 100 = 100 -50 = 50. If the system first pays the bill there is a problem because amount is not allowed to be less then zero.
This toy project is a very simple example of a accounting system which I want to make with clojure in the future
You can keep all accounts in a single atom. Like so
(def accounts (atom {12 {:amount 100} 14 {:amount 50}}))
(defn transfer [id-from id-to amount]
(swap! accounts (fn [accounts]
(-> accounts
(update-in [id-from :amount] - amount)
(update-in [id-to :amount] + amount)))))
Of course that's without any checking whether the transfer is valid, but I guess you can see where you can do that (inside the fn
given to swap).
First, this reduces concurrency - even if you have thousands of accounts, only one account can be updated at a time, so this won't scale to many accounts.
Another is it's a bit hard to return a value that signifies whether the transfer succeeded or not.
(def accounts (atom {12 {:balance 100} 14 {:balance 50}}))
(defn transfer-valid? [{:keys [balance] :as account} amount]
(>= (- balance amount) 0))
(defn transfer! [id-from id-to amount]
(let [successful? (atom false)]
(swap! accounts (fn [accounts]
(if (transfer-valid? (get accounts id-from) amount)
(do
(reset! successful? true)
(-> accounts
(update-in [id-from :balance] - amount)
(update-in [id-to :balance] + amount)))
accounts)))
@successful?))
Hey guys, is it possible to use [org.clojure/data.json "0.2.6”] to parse only the first level on a JSON object and not go for nested ones? For instance I’ve got this kind of incoming string:
{
"instance_ID": "%INSTANCE_ID",
"instance_IP": "%INSTANCE_IP",
"postDate": "%date{yyyy-MM-dd'T'HH:mm:ss.SSS}",
"body": "%m",
"level": "%-5level",
"thread": "%thread"
}
and “body” field content should be treated as a string without attempt to be parsed.
Is there a way to do this?@roelof: A somewhat better idea would be to use would be to use refs
and transactions (`dosync`) where accounts can be separate refs and it all behaves like SQL transactions with separate refs being separate rows and you have sort-of row-level locking.
You can look them up, if you have "Programming Clojure" or "Joy of Clojure" on hand both describe those concurrency primitives rather well.
Like here : https://www.refheap.com/112099 but I got confused by linenr 9
another this time beginners question : What do I have to change so this code https://www.refheap.com/112089 is outputtting [ 1 1 2 2 ] instead of [ ( 1 1 ) ( 2 2 )]
So if you conj [7 8]
it will end up there as the vector it is, it won't be splatted into the collection
So you can use concat
instead of conj
and it will work, yeah. concat
- joins collections/sequences, conj
- adds elements to a collection/sequence.
Correct, but it looks like I still have problems to find out which function I need for a particular case
@jaen: thanks, this one works fine : (reduce #(concat %1 (repeat amount %2)) [] list))
I have a function call with arguments in the format {:call-id 12, :options {}, :seq-args [1 2], :map-args nil}
where :seq-args the args contains, which the bound function needs. How can I bind the function, so I could use a normal definition like (defn fn[x y](println x y))
?
@roelof: yup it does, but I think the solution with mapcat
I suggested above is clearer. If you see why it works then I think it's better.
and I wanted to challenge myself to find a answer with reduce because the former challenge could also be done with it
Though clever is not always clearer, figuring the solution with reduce is smart, but I had to look at it twice before I understood what you wanted to do.
I wouldn't know, I always learned things on my own, but if you feel that way about here, then that's nice
Do I have to change something here : (some #{2 7 6} [5 6 7 8])) I get now a symbol some not found error message
You have one paren too many at the end, maybe you're closing something you shouldn't and that changes the scope somehow? Other than that looks good and works in my REPL.
Starting nREPL server... Connecting to local nREPL server... Clojure 1.7.0 nREPL server started on port 64691 on host 127.0.0.1 - <nrepl://127.0.0.1:64691> (some #{2 7 6} [5 6 7 8]) CompilerException java.lang.RuntimeException: Unable to resolve symbol: some in this context,
I don't know, you're using Windows, maybe Windows is doing something weird as it is known to do. Does it work if you run the repl from the command line?
you can find it here : https://www.refheap.com/112115
(some a-set collection)
is a common Clojure idiom due to the fact that Clojure collections can be called as a function to return an item.
([1 2 3 4] 2)
will return 3
(element of vector with index 2
), ({:a 1 :b 2} :b)
will return 2
(value of map under the key :b
) and (#{1 2 3} 3)
will return 3
(it returns the value if the set contains the value; if you did (#{1 2 3} 4)
you would get nil
).
But most Clojurists will think (some #{2 7 6} [5 6 7 8]))
is more concise (and clearer to read)
@roelof: Are you sending that form from an editor to the REPL, or typing it into the REPL window?
@roelof: @jaen: The problem is that Cursive will execute forms sent from the editor in the namespace of the editor, not the current namespace of the REPL.
If you haven’t loaded that namespace into the REPL yet it has not been correctly initialised and will not have clojure.core forms referred yet.