Fork me on GitHub
#clojure
<
2016-12-11
>
sgrove00:12:06

Hey all, I have some code that’s similar to:

(Thread. 
    (fn [] (let [a (agent nil)] 
        (println “beginning”)
        (dorun 
          (map-indexed 
             (fn [i _] 
             (println "About to send”)
             (send-off a (fn [_]
                                   (println “success”)))
           (range 0 10))
          (println "finished mapindexed”)))
but “success” never gets printed - everything else does

bronsa00:12:05

@sgrove not sure if this is your problem but send-off takes a function of one arg

sgrove00:12:29

Suppose I could have formatted that better...

sgrove00:12:35

@bronsa Sorry, will edit

octahedrion00:12:22

anyone used lein checkouts ? my project only works with them if I dont put their deps in the project.clj deps vector

octahedrion00:12:45

but then it wont compile in clojurescript

octahedrion00:12:58

only works in a clojure repl

sgrove00:12:27

@bronsa As you can see in my immaculately-formatted gist there, I’m taking one argument 😉

sgrove00:12:36

Literally no errors, and no output

bronsa00:12:53

(doto
 (Thread.
  (fn [] (let [a (agent nil)]
           (println "beginning")
           (dorun (map-indexed (fn [i _]
                                   (println "About to send")
                                   (send-off a (fn [_] (println "success"))))
                               (range 0 10)))
           (println "finished mapindexed"))))
 (.start))

bronsa00:12:06

^ that works for me

sgrove00:12:16

Thanks, I’ll work from that and see what’s going on

sgrove00:12:25

I owe you yet more unbought-beers, @bronsa

flyboarder02:12:28

Hello clojurians! If any of you need additional help/support this holiday season feel free to shoot me a message, I am donating my time as a gift 🎁 https://medium.com/degree9/pre-clojureremote-17-year-end-cleanup-e829757a1f89?source=linkShare-a2d5aa46d2e3-1481424862

spacepluk09:12:19

can I make pprint break lines after every key/value pair and disregard the width limits?

spacepluk10:12:17

actually what I want to do is write an indented edn but the default indentation I get with pprint is a bit messy

spacepluk12:12:45

nevermind, I figured it out

leov16:12:53

hello everyone

leov16:12:20

question - why when I comment out or remove last line, second-to-last line stop working?

(let [uri "datomic:"]
           (d/create-database uri)
           (doto (d/connect uri)
             ((fn [db] (->> (fs/glob "resources/datomic-migrations/*.edn")
                            sort
                            (map #(-> % str (clojure.string/split #"resources/") last
                                      conformity/read-resource))
                            (map #(conformity/ensure-conforms db %))
                            (#(doto % prn)))))))

leov16:12:06

is something about doto construct special compared to (let [x ..] (..) (..) x)?

robert-stuttaford16:12:11

@leov your intent is to print out the result of ensure-conforms?

leov16:12:21

no, just to execute it

leov16:12:36

I just want to execute ensure-conforms, but return connection

leov16:12:11

last line appeared during debugging. when I add it - it works. when I remove it - it doesn't execute ensure-conforms

robert-stuttaford16:12:14

a faithful rewrite of your code, in (my personal take on) idiomatic Clojure:

robert-stuttaford16:12:10

the essence of it is that you use let to grab conn, do a side-effecting action with doseq, and then return conn. the rest is just my OCD 🤓

leov16:12:27

your code is very nice

leov16:12:42

but why does doto fail?

robert-stuttaford16:12:43

doto is designed for use with mutable interop interactions http://clojure.org/reference/java_interop

robert-stuttaford16:12:24

Datomic connections are not mutable objects for you to invoke methods on

leov16:12:30

and you used (let [x ..] .. x)

leov16:12:40

which should be === doto

robert-stuttaford16:12:50

your actual question’s answer is that map is lazy

robert-stuttaford16:12:05

and prn is “realising" the collection produced by map

leov16:12:11

oh. my. god.

robert-stuttaford16:12:19

catches us all 🙂

leov16:12:22

you saved my I don't know what

leov16:12:50

let me think for 5 mins what's happening due to map laziness

leov16:12:04

is it that it's just doing nothing at all because no one realized it?

leov16:12:26

why did it work in all my other cases - because lazy wrapper was used?

robert-stuttaford16:12:31

it’s typically a smell to be using doto outside of interop (e.g. java things), or wrapping function declarations directly inside invocations (such as ((fn [] …)) or (#(…)) )

robert-stuttaford16:12:03

that’s right. prn is eager. you could replace your prn line with a simple doall as well

leov16:12:21

well, I'm just trying to inline as much unnecessary fun defs as possible, and trying to have Object#tap and Object#try after ruby...

leov16:12:34

1000 thanks!

robert-stuttaford16:12:50

turns out that’s actually an anti-pattern. for performance and for easy code reading a month from now

leov16:12:54

Object#tap should be doto

robert-stuttaford16:12:01

lots of little single purpose functions is best

leov16:12:39

well, I can't agree just now, but yes, smaller functions are self-documenting by their name, with that I agree

robert-stuttaford16:12:56

you’re right, doto is tap-like

leov16:12:01

I just wanted 3 operations - create db, connect, and Dir.glob -> migrate

leov16:12:12

but laziness got me

robert-stuttaford16:12:25

glad you’re on the journey. it’s lots of fun!

leov16:12:29

is there an extensive article on how laziness actually implemented?

leov16:12:33

you just saved me

leov16:12:37

thank you very much

tbaldridge18:12:13

@leov the gist? Imagine a cons cell of "first" and "next", now make "next" a function with no arguments. Now memoize that function...that's about it.

tbaldridge18:12:30

So instead of a list being (:a (:b (:c, nil))) we now have (:a (fn [] (:b (fn [] (:c, nil)))))

leov19:12:02

so basically what triggers the forcing in clojure is the first?

gfredericks19:12:01

technically so does next, but you don't have to think about that half the time

gfredericks19:12:46

lots of whole-sequence functions do too, of course; count, doseq, run!, doall, dorun, reduce, last...

gfredericks19:12:01

but those are all calling first somewhere presumably

iecya23:12:11

hello everyone! 😄 does anyone know a good GUI library for clojure other than seesaw? I need to make an executable program that allows the user to input simple commands and display the data

iecya23:12:38

@rmuslimov thanks 😄 i’ll try that

iecya23:12:42

@tbaldridge thanks a lot 😄