On Sunday, November 20, 2016 at 2:05:32 PM UTC+1, Luis Sanjuán wrote:
> On Tuesday, November 15, 2016 at 5:30:54 PM UTC+1, Brian Adkins wrote:
> > I'm working on a simple chess engine in Racket as a learning exercise. I 
> > initially wrote this function:
> > 
> > (define (valid-queen-moves board idx is-opposite-color?)
> >   (append (valid-bishop-moves board idx is-opposite-color?)
> >           (valid-rook-moves board idx is-opposite-color?)))
> > 
> > I didn't like the redundancy, so I re-wrote as:
> > 
> > (define (valid-queen-moves board idx is-opposite-color?)
> >   (append-map (λ (f) (apply f (list board idx is-opposite-color?)))
> >               (list valid-bishop-moves valid-rook-moves)))
> > 
> > I thought that was still ugly, so I re-wrote as:
> > 
> > (define (valid-queen-moves . args)
> >   (append-map (λ (f) (apply f args))
> >               (list valid-bishop-moves valid-rook-moves)))
> > 
> > And it was at this point that I finally though, "This has to be a solved 
> > problem already", but my initial searching turned up empty, so I wrote this:
> > 
> > (define (unionify . functions)
> >   (λ args (append-map (λ (f) (apply f args)) functions)))
> > 
> > (define valid-queen-moves (unionify valid-bishop-moves valid-rook-moves))
> > 
> > unionify is a *terrible* name, but my question is whether this higher order 
> > function already exists in the standard Racket library? I came close to 
> > writing flat-map before I found append-map, so I don't want to reinvent 
> > another wheel needlessly.
> > 
> > Brian
> 
> 
> Probably useless for your use case due to performance penalty, but in other 
> cases `conjoin` might do the work:
> 
> https://docs.racket-lang.org/reference/procedures.html?q=conjoin#%28def._%28%28lib._racket%2Ffunction..rkt%29._conjoin%29%29

I meant `disjoin` since you need to or-ing rather than and-ing. The idea is to 
pass the `disjoin`/`conjoin` construct as the first argument to `filter` over 
all possible moves.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" 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.

Reply via email to