Fork me on GitHub
#clojure
<
2016-05-30
>
vinnyataide01:05:54

what you mean with pragmatic directives

donyorm03:05:43

does anyone here have experience using FileOutputStreams with clojure?

raynes07:05:25

I bet it can be figured out regardless.

lmergen07:05:18

is there any proper documentation somewhere which sequence functions are lazy and which not? will first, for example, force evaluation of the container ?

seancorfield07:05:45

@fasiha: StringReader and PushbackReader are plain Java. Clojure just sits on top of that stack.

raynes07:05:35

@lmergen: A great way to check is to throw some side effects in and test the theory!

lmergen08:05:21

yes, but if there would be some kind of "rationale" behind it so i can make a reasonable prediction about the behavior, it would help 🙂

raynes08:05:55

(first (map (fn [thing] (println thing) thing) (range 100)))

raynes08:05:31

Well, common sense applies here. Map is lazy and first only needs the first element (chunked seqs will come in small batches regardless and most are chunked) unless the first element is somehow dependent on the realization of the remainder of the seq.

raynes08:05:54

If you keep the head and don't let it GC'd and continue realizing the seq then you'll hold it in memory.

lmergen08:05:39

ok, awesome

lmergen08:05:44

the first was just an example

raynes08:05:43

It's complicated and I expect documented when something you'd expect to not realize an entire lazy seq is going to.

raynes08:05:54

I hope documented,

lmergen08:05:12

well, i'm coming from Haskell, so for me "common sense" is not the type of laziness that Clojure uses

raynes08:05:25

My first language was Haskell.

lmergen08:05:29

so i'm trying to figure out the thought process behind Clojure's implementation of laziness

raynes08:05:55

I too miss the everything-is-lazy-so-there powers.

lmergen08:05:01

knowing Rich Hickey, I expected an elaborate essay about this somewhere 🙂

lmergen08:05:29

yes, well, some things should be lazy, others should not

raynes08:05:45

His various talks discuss it in detail I think, but I can't pull one out of my behind right now.

lmergen08:05:08

then i'll use some google fu to figure it out

raynes08:05:20

Dunno why I added type not sure what I expected.

lmergen08:05:09

here! this definitely does not match my expectation of laziness

lmergen08:05:19

i would expect only the first element to be evaluated

lmergen08:05:47

would using a transducer change this behaviour?

raynes08:05:38

If there's a bang I lose all my expectations.

seancorfield08:05:46

Transducers are eager.

raynes08:05:46

Right out the window.

seancorfield08:05:29

(Although a transducer can short-circuit a reduction so you could still transduce a lazy infinite sequence if it stopped after a finite number of elements I guess)

seancorfield08:05:04

But, yeah, chunked lazy sequences are probably the most surprising thing in Clojure.

raynes08:05:05

@seancorfield: Isn't it cool how I still know like 3 things about Clojure after two years of not writing it. 😄 Feels warm and fuzzy.

seancorfield08:05:33

Good to see you in here again. How's life been treating you?

raynes08:05:13

Meh, I'm working on it.

seancorfield08:05:30

And where are you these days? (West Coast I gather)

raynes08:05:39

Los Angeles

raynes08:05:08

LA is my favorite place ever

seancorfield08:05:26

They say you either love it or hate it.

raynes08:05:55

They say you make better decisions having actually travelled to more than like 4 places too.

raynes08:05:32

I've been to Cabo, Raleigh/Durham, here, and Santa Barbara.

seancorfield08:05:30

I used to stay in SB when I used to do my West Coast fly/drives before I moved out here.

seancorfield08:05:04

Always stayed at the California Hotel. It was near my tattoo artist.

raynes08:05:05

Hahaha nice

donyorm09:05:34

so whenever I try and write a file with a java FileOutputStream it gives me a FileNotFoundException. But the output stream is supposed to create the file, why is it doing this?

hans09:05:44

@donyorm: you'll have to share some code to reproduce your problem, otherwise it'll be rather hard to help. also, consider using clojure's file i/o mechanisms as they're generally easier to use.

donyorm09:05:48

@hans thanks. Here's the code I have:

donyorm09:05:17

tempfile is an uploaded file, file is the output file

donyorm09:05:47

I'm trying to implement uploading as detailed here: http://www.luminusweb.net/docs/routes.md#handling_file_uploads

hans09:05:49

i'd try the individual steps on the repl. is it really the output stream that is failing for you? it seems to me that the problem is not really the FileOutputStream class but rather that there is something wrong with the arguments that you pass to it.

hans09:05:52

also, what exactly is the error message? maybe you want to look at it more closely. do all directories in the file path exist?

donyorm09:05:31

ok that's really weird

donyorm09:05:43

it started working now. Maybe the computer restart did it

donyorm09:05:55

thanks for the help anyway @hans

hans09:05:10

glad that it works now.

donyorm09:05:20

never mind, it isn't working. Here's the actually error message: java.io.FileNotFoundException: <path> (No such file or directory) All the directories in the path exist. I can copy and paste it to the terminal and cd to it

hans09:05:37

And you're sure that it is not the FileInputStream that poses the problem? Is it possible that your handler is being called before the file and path is created?

donyorm09:05:36

yeah I'm sure, the stacktrace makes it very clear it's the output stream

donyorm09:05:07

I would use spit/slurp but I'm dealing with image files, and they don't work as fell for that (as for as I can tell)

hans09:05:11

spit uses FileOutputStream as well, so you'll have to figure out what your problem is. you can be pretty sure that the problem is on your end of it, and not on the end of FileOutputStream.

hans09:05:35

try spitting to the file instead, at the same point in your code.

hans09:05:52

is the path an absolute path?

hans09:05:29

because in the sample code, they're doing something to their output file before handing it to FileOutputStream, likely for a reason.

donyorm09:05:43

spit gives exact same error

donyorm09:05:15

yeah, they do you the channels in their example code, but I pretty much copied it word for word.

hans09:05:56

have a close look. out (new FileOutputStream (file-path path filename))

donyorm09:05:36

yeah, I copied that function too. Just tried again using it, same error

hans09:05:28

you're either using a relative path or your function is called before the output directory exists. you'll have to figure out why.

donyorm10:05:42

ok, it works when I have it go to /tmp rather than my resources folder. Sorry that took so long. I'll look into why it doesn't like my resouces folder

donyorm10:05:12

dang, all the trouble and it has something to do with the permissions on my ntfs drive

donyorm10:05:29

well I guess I'll just use a symbolic link for testing purposes

cupello14:05:19

Good morning guys! Has anyone a good stub lib?

plexus14:05:09

@lucelios: the usual answer to that is with-redefs

mattsfrey14:05:16

anyone know the preferred / idiomatic way of mapping or recursing through a list/vector and modifying an item based on it being the last item in that list/vector?

mattsfrey14:05:12

I’m guessing you’d want to use some sort of recursion with a check on (when (not (rest coll)) do-modify) or some such

hans14:05:36

@mattsfrey: it certainly depends what datatypes you need to support and how large your lists are

hans14:05:07

@mattsfrey: but butlast and last could be quite proper tools for you in some cases, and they might be the easiest to understand.

nathanmarz14:05:15

@mattsfrey: easy with Specter: (transform LAST inc [1 2 3 4]) -> [1 2 3 5]

mattsfrey14:05:48

that is definitely handy

nathanmarz14:05:04

It's also compositional, e.g. here's how to increment the last odd number in a sequence:

(transform [(filterer odd?) LAST]
              inc
              [2 1 3 6 9 4 8])
;; => [2 1 3 6 10 4 8]

nha23:05:50

Is there a more idiomatic way to do this ?

(with-open [rdr ( "/path-to-big-file")]
  (let [lseq (line-seq rdr)
        a (atom nil)]
    (dorun (reset! a (pmap some-operation lseq)))
    @a))

gastove23:05:45

I don’t immediately grasp why you need the atom in there at all? Why not just return the result of pmap?

nha23:05:33

It returns a lazy sequence, I need it realized. Anyway I am using a reduce after so it will force the realization.

gastove23:05:50

You can realize a lazy sequence with doall — no need to put it in an atom first. But you’re right, the reduce will also do it.

nha23:05:39

Yes - only problem with doall is that is puts everything in memory (not good with big files). Thanks anyway 🙂

gastove23:05:37

Oh huh — does using the dorun-atom one-two avoid the memory usage somehow?