Fork me on GitHub
#clojure
<
2017-06-29
>
bja01:06:41

which channel is best for posting links to talks?

bja01:06:05

err, found #videos

cigrainger07:06:02

Maybe my google-fu is failing me, but is there a simple way to unzip a file on disk to a a directory using Clojure? Everything I've found is about unzipping a single file, but I have a zip that has several. I want to unzip it and then read a specific one into memory.

cigrainger07:06:56

Trying to build an ETL tool that downloads a zip file, unzips it, reads a specific file into memory, and then does some stuff to it.

cigrainger07:06:33

Ideally I could use .getNextEntry or something similar and read to memory when it matches a regex

mbjarland07:06:43

@cigrainger if you just need the information there is no need to unzip to files - you could read it file directly from the zip into memory

cigrainger07:06:23

Oh that would be ideal! How would I do that with a specific file that's in the zip file? (One of multiple that are zipped in the archive)

mbjarland07:06:39

cigrainger: give me a few - will fire up a repl and see if I can cook up some example code. I’m not senior with clojure but I have spent a lot of time with file manipulations and zip files : )

cigrainger07:06:48

Awesome! Thanks. I'll play around with your suggestion re: ZipEntry as well. That was the general direction I was going but I couldn't find much and I'm not great with clojure yet either.

mbjarland07:06:12

(ns zip-files.core
  (:import (java.util.zip ZipFile)))

(defn get-entry-data [zip-file-path entry-name]
  (let [zip-file (ZipFile. ( zip-file-path))
        entries (enumeration-seq (.entries zip-file))
        matching    (filter #(= entry-name (.getName %)) entries)]
    (if (not-empty matching)
      (slurp (.getInputStream zip-file (first matching)))
      (println "no entry" entry-name "found!"))))

mbjarland07:06:38

(get-entry-data "test.zip" "test.txt")
=> "Hello World!\n\n"

mbjarland07:06:30

where test.txt was one of many files within test.zip and the contents of test.txt were “Hello World!\n\n”

mbjarland07:06:32

returns a string in this case or nil if no entry was found

cigrainger08:06:55

Awesome! Thank you!

mbjarland07:06:47

I’m live translating this from some groovy code I wrote a while back (we’ll see if I get lynched here), but you can iterate through the entries within a zip file and then call the ZipEntry (.getInputStream zip-file zip-entry) and then I believe you should be able to use slurp to get the contents of the input stream

mbjarland07:06:40

to find the right entry within the zip you can filter on zipEntry.name

fatihict07:06:35

Hey folks, I have the following snippet:

(let [coll [1 2 3]] 
    (map #(println :test %) coll) 
    (map #(println :test2 %) coll))
This results in: :test2 1 :test2 2 :test2 3 (nil nil nil) And not my expectation: :test 1 :test 2 :test 3 :test2 1 :test2 2 :test2 3 (nil nil nil) Can anyone explain this behavior?

jgeraert07:06:52

@fatihict Yes, don't use map to execute side-effecting functions like println since map basically returns a lazy seq

jgeraert07:06:09

you can wrap your map's in a (doall ... ) call, but it would be more idiomatic to use doseq for this case

noisesmith12:06:12

jgeraert: doseq works great for this, but there's also run! which like this usage of map takes a function and a collection as args, but is run for side effects eagerly.

noisesmith12:06:43

@fatihict meant to tag you above

fatihict12:06:01

@U051SS2EU Ah I see, thanks for the tip 🙂

fatihict07:06:23

Great, thanks 🙂

mbjarland08:06:44

@jgeraert thanks for the so link - looks more or less like what I came up with

jgeraert08:06:17

And be careful with slurp'ing in data, you'll have all in memory

Olical08:06:39

Hey! I was wondering if repeated subvecs would ever allow the excluded data to be garbage collected if there was no way to access it any more? Or would I need to use (vec (rest x)) if I wanted to actually drop the first item in a vector?

Olical08:06:58

I would use a persistent queue but I want in place updates too, performance of (vec (rest x)) being O(n) is a non-issue for me too, looking for the "idiomatic" solution more than anything 🙂

nikki08:06:51

damn didn't realize for re-evals the inner range expression

nikki08:06:53

boot.user=> (for [x (do (println "x") [1 2]) y (do (println "y") [1 2])] [x y])
x
y
y
([1 1] [1 2] [2 1] [2 2])

bronsa08:06:49

it's by design

bronsa08:06:13

how would (for [x [[1 2] [3 4]] y x] y) work otherwise

nikki08:06:18

i was p confused for a bit because i was using (q/random ...) (from quil) inside the inner one xD

nikki08:06:37

yea makes sense 😮

a1309:06:01

@fatihict or you can make something like that

(let [coll [1 2 3]
      res (concat (map #(vector :test %) coll)
                  (map #(vector :test2 %) coll))]
              (doseq [x res]
                (apply println x)))

a1309:06:27

I prefer not to mix side-effects with pure stuff

sandeep.balaji12:06:31

Someone Please suggest real-time-messaging clojure library. I might also need Screen Sharing and video chat.

rickmoynihan14:06:32

Is this expected on clojure 1.9-alpha17?

(keyword :foo) ;; :foo
(keyword "anamespace" :foo) ;; ClassCastException

bronsa14:06:55

yes, the 2 arity of keyword only accepts strings

rickmoynihan14:06:46

I understand that supporting coercions can be problematic but it is a little counter intuitive given the arity 1 behaviour

mjo324_5615:06:46

can anybody tell me how to use pprint? I mean in a file, not the REPL. What is the magic :require :use line i need to use?

mjo324_5615:06:12

i google this shit with no examples

mpenet15:06:03

(with-out-str (clojure.pprint/pprint x))

mpenet15:06:29

then you can spit it in the file

mjo324_5615:06:26

I build an uberjar

mjo324_5615:06:39

does compilation fails because it does not trecognize pprint

mjo324_5615:06:49

it is not properly imported

mjo324_5615:06:20

java.lang.RuntimeException: No such var: clojure.core/clojure.pprint

mpenet15:06:34

you need to require clojure.pprint first

mjo324_5615:06:44

i know that i have to rewuire it

mjo324_5615:06:50

i am asking how to do it

mjo324_5615:06:19

[clojure.pprint :as pprint] does not work

mjo324_5615:06:51

in the (:require clause

mpenet15:06:49

the error says you're trying to call clojure.pprint, if you aliased it you need to call (pprint/pprint x), or (clojure.pprint/pprint x)

mjo324_5615:06:21

what i need is one WORKING example, which unfotrtunately i was not able to find :D:D

mjo324_5615:06:46

one working example, i can copy paste, tht's all i need

mjo324_5615:06:27

so basically two line i need

mjo324_5615:06:35

1) how to import prettyprint

mjo324_5615:06:41

2) how to call it

mjo324_5615:06:02

currently i have

mjo324_5615:06:24

[clojure.pprint :as pprint] (pprint/pprint ...

mjo324_5615:06:32

rhat does not work

bja15:06:53

(ns foo (:require [clojure.pprint :as pprint]))(pprint/pprint {:this :is :so :pretty})

mjo324_5615:06:54

Exception in thread "main" java.lang.RuntimeException: No such var: clojure.core/clojure.pprint,

mjo324_5615:06:16

i got exactly what you suggested

mjo324_5615:06:26

what else could got fucked up?

mjo324_5615:06:33

my projectfile?

bja15:06:46

have you tried that in a bare repl?

mjo324_5615:06:26

wait, i got it

mjo324_5615:06:41

[clojure.pprint :as pprint] was in the ns clause, but outside the (:require clause

mjo324_5615:06:51

thanks for the quick help guys 🙂

bja15:06:53

spec in 1.9 is really nice for that sort of thing

bja15:06:12

if you're just playing with the language, I recommend trying out the 1.9.0-alpha17

mjo324_5615:06:15

well, ia m not just playing around with the language, i want to make money with it 🙂

mjo324_5615:06:31

so i wont try anything with "alpha" in it

bja15:06:11

I run my startup on so many "alpha"s and pre-alpha spur-of-the-moment forks that I've lost count.

mjo324_5615:06:29

and those alphas run somewhere on production?

qqq15:06:50

(defkeyframes blinkBlackGreyKF
  [:0.00001% {:fill "#ddd"}]
  [:50% {:fill "#000"}]
  [:100% {:fill "#ddd"}])
works, but if I change the 0.00001 to 0.0 or 0 , it fails -- somehow the css refuses to display the % if it's 0

mjo324_5615:06:52

well, tha is inspiering 🙂

bja15:06:38

I usually prefer to have bugfixes and features over some mythical stability

bja15:06:09

lein ancient, which is a plugin for one of the common clojure build tools, leiningen, actually makes it really easy to speculatively upgrade packages and run the entire test suite, only keeping the upgrades if your tests pass

mjo324_5615:06:14

hmmm... maybe the aplphas are so stable becuase of clojure's nature, i think it's harder to make bugs in clojure

bja15:06:16

of course, that requires that you actually bother to write tests

mjo324_5615:06:33

do you bother?

bja15:06:43

when they make sense

bja15:06:49

whether to write tests and what kind of tests to write depends on how much time I have, what I'm writing, and what the lifespan of the code is expected to be

bja15:06:06

things like a compiler for a dsl ends up with a lot of tests

bja15:06:11

sometimes I'm wrong about how much time I have or what the lifespan of the code is going to be, and tests get added on a later iteration

bja15:06:01

i.e. I expect something to be a one-off prototype and it ends up in production. it'll grow tests sooner rather than later so I can be confident about happy paths when I need to make changes.

mjo324_5615:06:19

today i had the following problem: after one day braek of coding nothing worked,something was fucked up, i needed 3 hours to get it back to life

bja15:06:51

that seems like a git-diff kinda problem

mjo324_5615:06:17

last time i wordked for corporate i solved this problem in that everyday one hour before going home i started to clean up and do notes about everything that was doing problems

mjo324_5615:06:51

and put a piece of paper describing these problems on my keyboard

mjo324_5615:06:06

i think it'[s rather a psychological problem than a git one 😄

bja15:06:47

I find that only really happens to me if I'm not constantly evaling my code and tests during development. i.e. if I spend 3 hours just writing code without evaling for some reason. that code will take much longer to get working than if I had just been evaling the code and my tests while going.

bja15:06:03

@mjo324_56 but if you had working code one day and two days later your code doesn't work, isn't the solution just to stash, revert to the working code, make sure it still works like you expect, then diff your stash and that working code?

mjo324_5615:06:09

i think you undrerstand my problems, i am curretly figuring out the perfect workflow for me

bja15:06:12

that's what I meant by a git-diff problem

bja15:06:50

sometimes I end up writing pseudo-code that is only somewhat working lisp when trying to get ideas out of my head. that's the kind of code that takes forever to get actually working. I often put it into its own namespace that isn't required anywhere (a persistent *scratch* of sorts) and then reference it in another coding session when I actually write tests and eval it as I'm going. I've found that if I try to get the code working, as opposed to using stream of consciousness code as "notes", I spend a ton of time just making it work. As a consequence, I try to be disciplined about making sure I eval everything as I go, to avoid the kind of scenarios where I write unusable code for several hours and then spend the rest of the day trying to make it usable.

mjo324_5615:06:47

how do you do the architecture?

mjo324_5616:06:02

also like a scratch ?

mjo324_5616:06:11

that gets proper?

mjo324_5616:06:18

or do you take a piece of paper?

mjo324_5616:06:54

it's really an interesting experience to write in clojure for me

mjo324_5616:06:04

recently i was coding the whole night

mjo324_5616:06:17

and in my opinion i did 1-2 weeks of work

mjo324_5616:06:26

in comparison with other languages

mjo324_5616:06:41

so i need to think totally different

mjo324_5616:06:53

i mean i have to organize my work different

rickmoynihan17:06:07

depends what you mean by architecture @mjo324_56 and what you’re doing. There are lots of orthogonal concerns; code layout (namespace hierarchy / load orders), runtime data flow, use of polymorphism, state, building for composition, structuring data etc…

ghadi18:06:16

Anyone ever generated a "compact ECDSA" signature (bitcoin style) from Java?

ghadi18:06:07

this is a #{64 65} byte signature -- as opposed to DER encoding which is #{70 71 72} byte sig

ghadi18:06:04

I've been using the bitcoinj library but can't figure out the incantation

joelsanchez19:06:04

@cfleming Any way to temporarily disable paredit to make parens balanced again after pasting unbalanced parens or deleting code leaving it unbalanced?

zylox19:06:50

bottom right is a menu that lets you change your structural editing pattern

zylox19:06:17

turn to Off -> fix -> turn back to paredit

zylox19:06:25

also there is a #cursive fyi

zylox19:06:59

there might be a quicker way though ¯\(ツ)

joelsanchez19:06:30

I know about the menu, but I was thinking about, CTRL + "(" or something (tried that)

joelsanchez19:06:35

Thanks for the channel

zylox19:06:38

ah ya no idea

zylox19:06:30

Toggle Structural Editing Style in keymaps @joelsanchez

joelsanchez19:06:07

Let's give it a try!

joelsanchez19:06:16

That's wonderful 🙂

lwhorton19:06:09

i often find myself with too many options to get something “polymorphic” done in clojure.. was wondering if someone could speak to this: say I need to switch on a data’s type. there’s the standard map approach {:type :some-custom-type}. then you can wrap that :type with a more formal multimethod and defmethod :type/foo :type/bar, etc.. you can also create a record to further formalize the map, and thus get access to things like (instance? _). you can also define a protocol for the various types and gain access to things like (satisfies? _). now with spec.alpha you can go another step and do things like (if (s/conforms? _) ...). surely there’s a good blog post or two out there illuminating such matters… but every time I reach for polymorphism i find myself almost paralyzed with options, not really sure which is best in each case.

joelsanchez19:06:36

i was asking myself the same question when i stumbled upon that

lwhorton19:06:49

oh neat-o .. ill definitely take a look

joelsanchez19:06:22

even the sample is very helpful

hcarvalhoaves19:06:40

@lwhorton I guess it boils down to the definition of "polymorphism". In Clojure you're not tied to any particular approach because you can dispatch on arbitrary fns, so you can have the equivalent of dispatching based on OO hierarchy (Java classes), nominal typing (your own taxonomy), structural typing (spec, etc) ...

hcarvalhoaves19:06:12

While in other languages (most I guess?), polymorphism bundles type system + dispatch/pattern matching together

mjo324_5622:06:10

I'd like to run a couple of functions in my main function

mjo324_5622:06:43

how do i do this?

joshjones22:06:05

@mjo324_56 this slack has a message limit that, when hit, removes the messages. I’m not sure whether multiple short messages like yours are counted as one, or as many, but consider combining them into a single, formatted message (a nice bonus is, they are more readable). just fyi

joshjones22:06:40

to answer your question — what does “I’d like to run a couple of functions” mean exactly? What you’ve posted certainly does just that. But what is the point?

joshjones22:06:14

(defn -main []
  (func1)
  (func2))
The result of the above will be whatever (func2) evaluates to. Of course, if (func1) produces side-effects (I/O, etc), these side-effects will occur.

eriktjacobsen22:06:37

*unless (func1) produces a lazy sequence which would never be realized.

mjo324_5623:06:55

you are right, it didnt compile because of some other mistake

mjo324_5623:06:01

(defn -main "This should be pretty simple." [] ( (println "hallo") (println "hallo2") ))

mjo324_5623:06:10

prints both hellos

mjo324_5623:06:22

but gives after it a null pointer exceptoin

joshjones23:06:14

yes, try this, and see the difference:

(defn mainnn
  "This should be pretty simple."
  []
  (println "hallo")
  (println "hallo2"))

joshjones23:06:12

You have an extra paren before the first println. This evaluates the result of the first (println "hallo"), which is nil, as a function, and this is what creates the NPE.

mjo324_5623:06:11

thanks, i nearly solved my problem

mjo324_5623:06:43

i'll come later back to it after I enjoy some nightlife in Wrocław

mtbkapp23:06:50

Hello, I have a small question about spec. How does one specify a constant value in a regex spec? Like a vector that always must start with a keyword. I can do something like this (spec/cat :type #{:message} :contents string?) (as part of a multi spec), but is there a way to say the first thing in the sequence must be :message?

wei00:06:28

you might have better luck asking in #clojure-spec