Fork me on GitHub
#clojure
<
2015-09-27
>
jrychter09:09:46

Ahh, finally figured out what was eating all the newlines in my <pre> tags in the XML blog files that I'm importing. Turns out that clojure.data.zip.xml/text doesn't just return the content of the node, it does whitespace removal as well.

jrychter09:09:36

I'm puzzled: does anybody really want the current behavior where it replaces the regexp of (str "[\\s" (char 160) "]+") with a single space?

scriptor14:09:17

is it possible to create a defmethod that will match on any value for a given argument?

scriptor14:09:31

:default isn't really what I want, I thin

scriptor14:09:37

actually, it might be

jeremyraines17:09:59

anyone have a strong understanding of Perlin noise and want to help me with my clojure implementation of it? simple_smile happy to pay for your time if that shot in the dark came anywhere near you

amacdougall17:09:18

Say I have two collections, and I want to compare the first to the first, second to the second, etc. Like so:

(let [f = ; but it could be anything!
      exemplars [1 2 3 4]
      candidates [1 2 3 4]]
  (for [[e c] (map vector exemplars candidates)]
    (f e c)))
Is there a nicer way? Bear in mind that I want to be able to run any function on the pairs, so the many ways of testing deep equality are out of bounds.

amacdougall17:09:53

@jeremyraines: Do you need a Clojure implementation of Perlin noise, or are you specifically trying to get your version working? If it's important enough that you're willing to pay, then the former might be true...

amacdougall17:09:18

And in either case, there are a few existing implementations in Clojure that might steer you the right way.

jeremyraines17:09:15

it's the latter, for understanding. I'm willing to pay just because I'm frustrated

amacdougall17:09:26

The closest I ever came to Perlin noise was watching Ken Perlin give a talk on it, which was fascinating, but light on technical details. Sorry I can't help personally.

jeremyraines17:09:41

on your question: is f a comparator so what you want back is a collection of trues and falses?

pguillebert17:09:50

look at every? but you seem on the rigt track

amacdougall17:09:09

Yeah, I could do the same thing with (every? (fn [[e c]] (f e c)) (map vector exemplars candidates)), and it might be a bit more intention-revealing.

amacdougall17:09:44

In my specific case, exemplars is a collection of maps, where I expect the candidates to have the values from the exemplar maps, but possibly additional ones. To be exact, I'm just testing database stuff, where I don't care about the timestamps and whatnot. It's not exactly rocket science, but anything I learn while doing basic stuff can only help me when doing more complicated stuff.

amacdougall17:09:24

In particular, doing the (map vector coll1 coll2) thing to get a pairwise collection feels like a roundabout way of doing something simple.

amacdougall17:09:11

(partition 2 (interleave coll1 coll2)) is very similar, and more self-documenting, I guess. map vector, I have no idea how that even works.

amacdougall17:09:13

I mean, I vaguely remember that (map f coll1) runs (f element) on each element, and (map f coll1 coll2) runs (f element1 element2), so that's how it works, but it's tricky.

jeremyraines17:09:31

(map vector c1 c2) works because map takes a variable number of collections, yeah

amacdougall17:09:36

Just thinking aloud.

jeremyraines17:09:07

that version with every? seems good. I could be wrong but I think it has the benefit of being lazy, too, where the for version isn't

amacdougall17:09:38

True, since partition and interleave both return lazy seqs if possible, also.

potetm18:09:48

@amacdougall: Does (map = exemplars candidates) not work?

jeremyraines18:09:09

oh - thanks! not sure why I had that misunderstanding

potetm18:09:54

If you wanted to get to the first false comparison you could do (first (filter false? (map = exemplars candidates)))

potetm18:09:03

Probably thinking of doseq simple_smile

masonbrowne20:09:52

Is there something that looks vaguely like the opposite of interleave? Something that takes coll, n, and returns an n-element collection of collections? E.g., (foo 3 [1 2 3 4 5 6]) ;; => ((1 4) (2 5) (3 6))

masonbrowne20:09:31

ahh partition

masonbrowne20:09:35

jeez, shoulda scrolled up

masonbrowne20:09:57

Though, that partitions by number of elements, not number of resulting collections

jeremyraines21:09:41

(defn foo [n coll]
  (let [size (/ (count coll) n)]
    (partition size coll)))

jeremyraines21:09:02

although you'd have to handle sizes that aren't cleanly divisible

masonbrowne22:09:49

@jeremyraines: apparently my question was asked an answered in 2009 (https://groups.google.com/forum/#!topic/clojure/f31RdxAbwZI)

(defn unravel [n coll] 
  (map #(take-nth n (drop % coll)) (range n))) 

jeremyraines22:09:06

nice. Love seeing Chouser's always-more-concise solutions after I finish a 4clojure problem simple_smile