Fork me on GitHub
#clojure
<
2017-11-19
>
tvalerio01:11:07

I’m trying to read a json from stdin but I’m having some problems.. I’m using cheshire trying to convert the entry but I’m getting a “JsonEOFException: Unexpected end-of-input” Does anyone tried to do this?

tvalerio01:11:48

(ns job-queue.core
  (:gen-class)
  (:require
    [job-queue.job-queue-handler :as handler]
    [cheshire.core :refer :all]))

(defn handle-json
  [json]
  (prn "json: " json)
  (prn "json decoded: " (decode json true)))


(defn -main
  [& args]
  (println "Hi, enter the JSON!")
  (doseq [in (line-seq (java.io.BufferedReader. *in*))]
   (handle-json in)))

tvalerio01:11:37

perhaps that’s because stdin is bringing one line at a time?

grzm01:11:31

probably, yes

tvalerio01:11:56

hmm Is there a way to bring the lines all together?

grzm01:11:24

slurp is a common way of reading a file into a string

tvalerio01:11:56

I have tried to do with slurp but it doesn’t print anything

(ns job-queue.core
  (:gen-class)
  (:require
    [job-queue.job-queue-handler :as handler]
    [cheshire.core :refer :all]))

(defn handle-json
  [json]
  (prn "json: " json)
  (prn "json decoded: " (decode json true)))


(defn -main
  [& args]
  (println "Hi, enter the JSON!")
  (let [in (slurp *in*)]
    (handle-json in)))

tvalerio01:11:27

am I doing something wrong?

grzm01:11:01

how are you calling it from the command line?

tvalerio02:11:35

I’m executing the command

lein run

tvalerio02:11:40

and then pasting a json that is correctly formatted

grzm02:11:39

try echo '"string"' | lein run

grzm02:11:35

I suspect slurp needs and EOF, which it's not getting if you're running just lein run and typing

grzm02:11:20

(well, ctrl-d will send EOF, but it'll also quit the program. that works for me.)

tvalerio02:11:06

yeah, if I executes

echo '"string"' | lein run
it works

tvalerio02:11:32

well, It’s an exercice and I have more things to do. I’m going to move on this way

grzm02:11:06

glad to help!

qqq02:11:28

This is a common code pattern I use:

(case (:tag obj)
  :foo (let [{:keys [a b c]} obj]
         ...)
  :bar (let [{:keys [x y z]} obj]
         ...)
  :cat (let [{:keys [aa dd ff]} obj]
         ...))
Is there a nice macro to simplify this? basically, I want to 1. dispatch on the :tag field, and 2. do a destructuring bind on keys

alexmiller02:11:03

You could just use a multimethod

qqq02:11:24

(defn rule->sexp [obj [[tag & vars] & body]]
  `[~tag
    (let [{:keys ~(vec  vars)} ~obj]
      [email protected])])

(defmacro tdbind [obj & rules]
  (let [obj-var (gensym "obj")] 
    `(let [~obj-var ~obj] 
       (case (:tag ~obj-var) 
         [email protected](mapcat #(rule->sexp obj-var %) rules)))))


(macroexpand-1
 '(tdbind (make some obj)
         [[:rect x y]
          (* x y)]
         [[:circle r]
          (* r r 3.14)]
         [[:triangle a b c]
          (assert false "no idea")]))

(comment
 (clojure.core/let
 [obj9109 (make some obj)]
 (clojure.core/case
  (:tag obj9109)
  :rect
  (clojure.core/let [{:keys [x y]} obj9109] (* x y))
  :circle
  (clojure.core/let [{:keys [r]} obj9109] (* r r 3.14))
  :triangle
  (clojure.core/let
   [{:keys [a b c]} obj9109]
   (assert false "no idea")))))


is closer to what . I had in mind 🙂

alexmiller14:11:20

A multimethod would be a lot simpler

alexmiller14:11:46

(defmulti area :tag) (defmethod area :rect [{:keys [x y]}] (* x y))

zignd03:11:01

cross-posting this link here from #datomic, would appreciate any help https://stackoverflow.com/questions/47373356/query-result-pagination-in-datomic

yonatanel08:11:16

What's your way of introducing new bindings as you test some conditions? I see that let bindings in cond was rejected. https://dev.clojure.org/jira/browse/CLJ-200?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel https://github.com/Engelberg/better-cond

cmal09:11:22

Hi, how to get the min value's index of a vector? shall i use (let [v [1 2 3 4]] (.indexOf v (apply min v)))?

bronsa10:11:04

you can (apply min-key v (range (count v)))

3Jane10:11:16

Here’s my try, bit verbose; would the use of reduce make it more or less efficient than using ranges?

(let [v [7 2 4 3]]
  (reduce-kv
    (fn [[min-k min-v] k v]
      (if (< v min-v)
        [k v]
        [min-k min-v]))
    [0 (first v)]
    v))

3Jane10:11:23

(also, doesn’t work correctly for an empty vector but gives you both key and value)

Andrew20:11:09

Hello everyone. Did anyone of you ever wrote a logging into a clojure application? I am currently building a clojure app with postgresql and one of main features should be activity log. This feature was actualy designed after i wrote a lot of code, so customizing every database action to also make a activity log would be time counsuming and to be honest not very clean solution.

Andrew20:11:05

Therefore I am asking if anyone of you know any good clean solution to this? I was thinking about some posgres triggers or stuff like that, but i need to know which user in my application is doing such action

qqq23:11:39

realistically, how big can a *.cljc file get before it becomes too big ?

vemv23:11:05

@qqq depends on who you ask 😜

vemv23:11:09

personally I go by these lines in general

Andrew23:11:28

Anyone who might help me with my issue 🙂?

vemv23:11:19

> Did anyone of you ever wrote a logging into a clojure application? I'm confused by the wording

Andrew23:11:44

Sorry, i will try to clarify it as much as possible

Andrew23:11:40

We have appplication where several users perform basic CRUD actions on postgresql database

Andrew23:11:54

the point is, we would like to log each of those actions with coresponding users

Andrew23:11:12

for example USER A added entity B with name C , or USER D edited entity (fields E F G) with values ( H I J) .. basicaly an activity log

Andrew23:11:24

that is readable for non-programmer admin user

vemv23:11:26

ah got you! interesting feature. sincerely IDK. the Rails equivalent would be https://github.com/airblade/paper_trail which wraps every CRUD action with an object to be persisted, including a whodunnit column

vemv23:11:58

switching to Datomic just for this might be overkill

vemv23:11:06

otherwise dunno!

Andrew23:11:31

Yea .. well since I am using sqlkorma with postgres i even thought about somehow forking korma and writing this kind of logic. But it's not the clean solution im looking for. 😕

vemv23:11:37

sql log analysis? turn INSERT into ... text data into new data to be inserted would seem fragile-ish to me, can be a last resource