It's hard to tell for sure from your code sample, because you don't include
"place" or "get-free-squares", but it's possible your error is in the
reducing function here:
(reduce #(when (not (keyword? %2))
(conj! %1
(if nxt
{:board %2 :pawns (update pwns nxt dec)}
brd)))
(transient [])
(map #(place nxt (first %) (last %) brd)
(get-free-squares brd)))
In your reducing function, if %2 is not a keyword, then you return nil.
This means that the next %1 will be nil, and you'll end up with a null
pointer exception.
I wonder if you meant instead to write:
(reduce #(if (not (keyword? %2))
(conj! %1
(if nxt
{:board %2 :pawns (update pwns nxt dec)}
brd))
%1)
(transient [])
(map #(place nxt (first %) (last %) brd)
(get-free-squares brd)))
- James
On 25 April 2016 at 01:26, Vandr0iy <[email protected]> wrote:
> Hi, Clojurists!
>
> I'm writing some clojure in these days, and I stumbled upon an error that
> I'm unable to easily understand.
> My goal is to asynchronously process a 2D vector in order to solve a
> parametric version of the 8 chess queens problem (where the parameters are:
> the board's dimensions and the chess pieces to be put on the board).
> My solution mainly consists in two functions: the worker and the boss. The
> worker takes in input a board (2d array) and yields a collection of boards
> that contain another chess piece placed in a certain way. This means that I:
> > take all of the free squares from the board
> > try to place the chesspiece on every one of them
> > if I succeed (do not hit other pieces) - it goes to the output.
>
> Until I use the version without the transients - it appears to be working.
> But I wanted to optimize my code, because the last time I solved it using
> recursive pmap - when I gave it an output that's a bit larger - it went
> into a bats**t crazy heap overflow. So I tried to use a transient
> collection.
> And here my problems began. When I try to execute this code it dies after
> just a couple of moves processed, giving me a nullpointer on the row where
> conj! is situated:
>
> NullPointerException clojure.core/conj! (core.clj:3257)
>
> What a hellish issue may it be? Clojure is not helping - its error
> messages are always cryptic, and really hard to understand.
>
> Here is my code. It's not complete though - some functions are omitted,
> for the sake of brevity. Please, help me to understand what am I missing
> here...
>
> Thank you.
> (def fw (agent {}))
>
> (defn worker [subj]
> (let [{brd :board pwns :pawns} subj
> nxt (get-next-pawn pwns)]
> (when (map? subj)
> (when (and brd pwns)
> (into #{}
> (persistent!
> (reduce #(when (not (keyword? %2))
> (conj! %1
> (if nxt
> {:board %2 :pawns (update pwns nxt
> dec)}
> brd)))
> (transient [])
> (map #(place nxt (first %) (last %) brd)
> (get-free-squares brd)))))))))
> (defn boss []
>
> (send fw
> #(into {} %2)
> {:complete 0
> :coll
> #{{:board (blank-board 4 4)
> :pawns
> {:rook 1
> :knight 3
> :bishop 2
> }}}})
> (loop []
> (await fw)
> @fw
> (let [{done :complete col :coll} @fw]
> (if (= done (count col))
> col
> (do (send-off fw
> #(identity {:complete (+ (:complete %1)
> (count (filter
> vector? %2)))
> :coll (into (:coll %1) %2)})
> (reduce #(into %1 %2)
> #{}
> (pmap #(worker %) col)))
> (recur))))))
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.