I have a function
(defn f-test [c-width] (format "%-(c-width)s" "boogey"))
This is what happens when I try to evaluate it
(f-test 10)
FormatFlagsConversionMismatchException Conversion = c, Flags = ( java.util.Formatter$FormatSpecifier.failMismatch (Formatter.java:4041)
But this is what I am trying to do
(format "%-10s" "boogey")
"boogey "
how can I substitute in c-width inside the format call?
Try this one:
(defn f-test
[c-width]
(format (str "%-" c-width "s") "boogey"))
Related
I need to give an output like this:
/Fotos/Azoren-2018/buffet
/Fotos/Azoren-2018/restaurant/dc
/Fotos/Azoren-2018/restaurant/dc-41.jpg
/Fotos/Azoren-2018/restaurant/dc-42.jpg
/Fotos/Azoren-2018/restaurant/dc-43.jpg
/Fotos/Mallorca-2017/dc-10.jpg
/Fotos/Mallorca-2017/dc-11.jpg
/Fotos/Mallorca-2017/dc-19.jpg
But I think my output is a set:
#{"/Fotos/Mallorca-2017/dc-10.jpg" "/Fotos/Azoren-2018/buffet" "/Fotos/Azoren-2018/restaurant/dc-43.jpg" "/Fotos/Azoren-2018/restaurant/dc-42.jpg" "/Fotos/Mallorca-2017/dc-19.jpg" "/Fotos/Azoren-2018/restaurant/dc-41.jpg" "/Fotos/Mallorca-2017/dc-11.jpg" "/Fotos/Azoren-2018/restaurant/dc"}
Any ideas how can I convert that set into this output?
Use sort with clojure.string/join:
(->> #{"/Fotos/Mallorca-2017/dc-10.jpg" "/Fotos/Azoren-2018/buffet" "/Fotos/Azoren-2018/restaurant/dc-43.jpg" "/Fotos/Azoren-2018/restaurant/dc-42.jpg" "/Fotos/Mallorca-2017/dc-19.jpg" "/Fotos/Azoren-2018/restaurant/dc-41.jpg" "/Fotos/Mallorca-2017/dc-11.jpg" "/Fotos/Azoren-2018/restaurant/dc"}
sort
(clojure.string/join "\n")
println)
Output:
/Fotos/Azoren-2018/buffet
/Fotos/Azoren-2018/restaurant/dc
/Fotos/Azoren-2018/restaurant/dc-41.jpg
/Fotos/Azoren-2018/restaurant/dc-42.jpg
/Fotos/Azoren-2018/restaurant/dc-43.jpg
/Fotos/Mallorca-2017/dc-10.jpg
/Fotos/Mallorca-2017/dc-11.jpg
/Fotos/Mallorca-2017/dc-19.jpg
=> nil
People,
I started studying clojure a few time ago and i have one question.
In this function:
(defn print-coords [coords]
(let [lat (first coords)
lon (last coords)]
(println (str "Latitude: " lat " - " "Longitude: " lon))))
=> #'dev-projects.core/print-coords
I'm defining a global function with one parameter.
But, if i invoke these arguments:
(print-coords [54 32])
Latitude: 54 - Longitude: 32
=> nil
I will get a sucessful answer.
Why does it work? The original function shouldn't only work with one argument?
I think you confuse (print-coords [53 32]) to be (print-coords 54 32).
In the first case, coords is the vector [53 32] whose first is 54 -> lat and whose second indeed is lon 32. Return value is correctly nil, since println returns nil.
However:
user=> (print-coords 54 32)
Execution error (ArityException) at user/eval141 (REPL:1).
Wrong number of args (2) passed to: user/print-coords
The question is how [coords] in the defn is compatible with [53 32] in the call.
A defn's parameters are surrounded by square brackets, but a function call's arguments are not wrapped in any punctuation. Thus [53 32] is literally a vector passed as the 1 argument to the function call. [53 32] corresponds to coords.
By the way, print-coords could be defined more idiomatically like this:
(defn print-coords [[lat lon]]
(println (str "Latitude: " lat " - " "Longitude: " lon)))
It is still a function of 1 argument. The only difference is that the 1 argument is immediately destructured and names are bound to the first two members. Now it is pleasantly clear how [53 32] corresponds [lat lon].
I am trying usgin library clj-kafka.
Here my code
(use [clj-kafka.producer]
[clj-kafka.zk]
[clj-kafka.consumer.zk]
[clj-kafka.core]))
(brokers {"zookeeper.connect" "localhost:2181"})
(def p (producer {"metadata.broker.list" "localhost:9092"
"serializer.class" "kafka.serializer.DefaultEncoder"
"partitioner.class" "kafka.producer.DefaultPartitioner"}))
(send-message p (message "test" (.getBytes "this is my message")))
(def config {"zookeeper.connect" "localhost:2181"
"group.id" "clj-kafka.consumer"
"auto.offset.reset" "smallest"
"auto.commit.enable" "false"})
(with-resource [c (consumer config)]
shutdown
(take 2 (messages c "test"))) ;; return ()
I start zookepper-server and kafka itself with
bin/zookeeper-server-start.sh config/zookeeper.properties
bin/kafka-server-start.sh config/server.properties
The config/zookepper.properties:
dataDir=/tmp/zookeeper
clientPort=2181
maxClientCnxns=0
and config/server.properties:
broker.id=0
listeners=PLAINTEXT://:9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
zocket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=localhost:2181
zookeeper.connection.timeout.ms=6000
The 'problem' is when I execute:
(with-resource [c (consumer config)]
shutdown
(take 2 (messages c "test"))) ;; return empty ()
Any idea here?
Thanks in advance
See this github issue. Seems the documentation isn't great. You have to force the realization of the sequence (which is lazy) with doall. try this:
(with-resource [c (consumer config)]
shutdown
(doall (take 2 (messages c "test"))))
I'm trying to found a better way of handle exceptions in clojure, I'm using https://github.com/alaisi/postgres.async which thrown an exeption when fail, but I would prefer return false (because I'm using a validator) or even better something like a Either monad (or something more simple like this http://adambard.com/blog/acceptable-error-handling-in-clojure/ )
1) I'm trying to catch the exception and if exist return false, but the code doesnt works (doesnt return false and thrown the exception).
(try
(dosql [tx (<begin! db)
ucount (<execute! tx ["UPDATE groups SET garticlescounter=garticlescounter + 1 WHERE gid=$1"
groupid])
uartc (<execute! tx ["UPDATE subtopics SET starticlescount=starticlescount + 1 WHERE stid=$1"
sid])
uartins (<insert! tx
{:table "articles"}
{:aurl url :atitle title :asuttopicid sid :acommentcount 0 :alikescount 0 :auid uid})
ok? (<commit! tx)]
ok?)
(catch Exception _ false))
2)would be possible wrap in a way where if works return ok? and if doesnt works return false, or maybe [ok? nil] and [nil error] maybe a macro?
----thanks to swinn I did this
;must receive an optional parameter for error response or
; build a [nil ok] response but just know this works for me...
(defmacro async-try-block! [block]
`(let [chn# (!/chan 1)]
(!/go
(let [response# (try
~block
(catch Throwable e#))]
(if (instance? Throwable response#) (!/put! chn# false) (!/put! chn# response#))))
chn#))
(async-try-block!
(dosql [tx (<begin! db)
ucount (<execute! tx ["UPDATE groups SET garticlescounter=garticlescounter + 1 WHERE gid=$1"
groupid])
uartc (<execute! tx ["UPDATE subtopics SET starticlescount=starticlescount + 1 WHERE stid=$1"
sid])
uartins (<insert! tx
{:table "articles"}
{:aurl url :atitle title :asuttopicid sid :acommentcount 0 :alikescount 0 :auid uid})
ok? (<commit! tx)]
ok?))
I'm not familiar with the postgres.async library, butException is not the root of all Exceptions in the JVM, Throwable is.
Changing your catch to Throwable would be the first change I would suggest, but it seems that (<execute! ...) actually catches the exception for you and returns it, so you need to check the return value using (instance? Throwable)
I like to structure my org-mode projects using the :CATEGORY: property, however categories seem not to be recognised by org-agenda-filter-by-regexp (bound to =). When viewing a rather large list of TODO items, it would be helpful to be able to narrow the list to matching categories.
I know that I can use org-agenda-filter-by-category (<), but this command must be executed on the category entry.
Is there a way to regexp filter the Org Agenda including categories?
The function below performs what I'd like, however I can't get it to work from the org-agenda dispatcher as a custom agenda command.
(defun rnkn/org-category-match (str)
"Org Agenda match TODO headlines matching :CATEGORY: property STR."
(interactive "sCategory: ")
(org-tags-view t (concat "CATEGORY={" str "}")))
Type M-x org-agenda RET m and then enter the regex -- e.g., PropertyDrawerName={regex}
Previous Answer (before the original poster edited the question) -- the current version of org-element-property / org-element-headline-parser uses all UPPERCASE letters for the property drawer arguments:
(defun example-one ()
(interactive)
(require 'org-element)
(let* (
(org-todo-keywords '((sequence "Active(a)" "Next Action(n)" "Reference(r)" "Someday(s)" "Delegated(d)" "|" "None(N)") ))
(sample-todo (concat
"* TASKS\n\n"
"** Active [#A] smith # drawer-one (fishing) | drawer-two (tennis). :lawlist:\n"
" DEADLINE: <2013-12-21 Sat 17:00> SCHEDULED: <2013-12-21 Sat>\n"
" :PROPERTIES:\n"
" :DRAWER-ONE: fishing\n"
" :DRAWER-TWO: tennis\n"
" :END:\n\n"
"** Next Action [#B] doe # drawer-one (football) | drawer-two (bowling). :fred:\n"
" DEADLINE: <2013-12-22 Sun 08:30> SCHEDULED: <2013-12-22 Sun>\n"
" :PROPERTIES:\n"
" :DRAWER-ONE: football\n"
" :DRAWER-TWO: bowling\n"
" :END:\n\n"
"* EVENTS\n\n"
"** Reference [#C] john # drawer-one (fishing) | drawer-two (sky-diving). :george:\n"
" DEADLINE: <2013-12-23 Mon 10:15> SCHEDULED: <2013-12-23 Mon>\n"
" :PROPERTIES:\n"
" :DRAWER-ONE: fishing\n"
" :DRAWER-TWO: sky-diving\n"
" :END:\n\n"
"* UNDATED\n\n"
"** Someday [#D] jane # drawer-one (basket-ball) | drawer-two (bowling). :sam:\n"
" DEADLINE: <2013-12-24 Tues 12:00> SCHEDULED: <2013-12-24 Tues>\n"
" :PROPERTIES:\n"
" :DRAWER-ONE: basket-ball\n"
" :DRAWER-TWO: bowling\n"
" :END:")))
(if (get-buffer "foo.org")
(progn
(switch-to-buffer "foo.org")
(erase-buffer)
(delete-other-windows))
(switch-to-buffer (get-buffer-create "foo.org")))
(org-mode)
(insert sample-todo)
(goto-char (point-min))
(or (y-or-n-p (format "For this example work, you must save this buffer as a file. Proceed with example?"))
(error "Canceled."))
(write-file "~/Desktop/foo.org" t)
(let* (
(filename (buffer-file-name))
(org-agenda-files (list filename))
(org-agenda-only-exact-dates t)
(org-agenda-show-all-dates nil)
(org-deadline-warning-days 0)
(org-agenda-time-grid nil)
(org-agenda-span 'month)
(org-agenda-entry-types '(:deadline))
(month "12")
(year "2013")
(org-agenda-start-day (concat year "-" month "-" "01"))
(drawer-content (read-string "basket-ball | bowling | fishing | football | sky-diving | tennis: " nil))
(org-agenda-skip-function (lambda ()
(org-back-to-heading t)
(let* (
(element (org-element-at-point))
(drawer-one (org-element-property :DRAWER-ONE element))
(drawer-two (org-element-property :DRAWER-TWO element)))
(cond
((not (or
(equal drawer-one drawer-content)
(equal drawer-two drawer-content)))
(org-end-of-subtree t))
(t nil) )) )))
(org-agenda-list))))
(defun example-two ()
(interactive)
(let* (
(drawer (read-string "Select a property drawer (e.g., ToodledoFolder): "))
(drawer-content (read-string (concat "Select the content of the " drawer " property drawer (e.g., EVENTS): "))))
(org-tags-view nil (concat drawer "=\"" drawer-content "\"" ))))
I'm using the following function to filter by category (solves the problem of < ).
(defun stu/org-agenda-filter-by-category ()
"Filter lines in the agenda buffer that have a specific category."
(interactive)
(let ((cat (read-string "Select the category: ")))
;;(stu/show-agenda-and-todo)
(org-agenda-filter-apply
(setq org-agenda-category-filter
(list (concat "+" cat))) 'category)))
I use (stu/show-agenda-and-todo) to make sure it filters every possible task, not only the ones shown in the buffer.