Re: Filtering 'intersection' of two lists

189 views
Skip to first unread message

Janis Voigtländer

unread,
Nov 24, 2013, 3:13:12 AM11/24/13
to elm-d...@googlegroups.com
What is the type of your notcollision function, actually? From the
code you posted, it looks like

notcollision :: [Object] -> Object -> Bool

But I assume it ultimately reduces to a checking on an individual pair
of objects, only?

notcollisionSingle :: Object -> Object -> Bool

Or actually, for the sake of making my code below a bit briefer, it's
negated and uncurried variant:

collisionSingle :: (Object, Object) -> Bool

If you do have a function like the latter one, then I think the
appropriate algorithmic solution to your problem would look something
like:

let
pairs = crossproduct tmpd1 tmpd2
collided = filter collisionSingle pairs
nextd1 = diff tmpd1 (map fst collided)
nextd2 = diff tmpd2 (map snd collided)
in ...

The above assumes availability of functions:

crossproduct :: [a] -> [b] -> [(a,b)]
diff :: [comparable] -> [comparable] -> [comparable]

What the first does should be obvious, and should be an easy exercise
to defined. What the second does is the list version of Set.diff, and
indeed maybe List.diff could be usefully made available in the
standard library. (The corresponding Haskell standard function is:
http://hackage.haskell.org/package/base-4.6.0.1/docs/Data-List.html#v:-92--92-)

Best,
Janis.


2013/11/24 Hot Belgo <hotb...@gmail.com>:
> Hi,
> need a style tip. I am building a game where each period the two types of
> object iterate their movement, should check for collisions and only return
> elements that have not collided. I can't help thinking that mapping and
> filtering is overkill but I can't see a way of combining them (basically i
> was seeing the first filter remove the elements in collisions such that the
> second filter did not find anything)
>
> dataObject d1 d2 =
> let
> tmpd1 =
> map move d1
> tmpd2 =
> map move d2
> in let
> nextd1 =
> filter (notcollision tmpd2) tmpd1
> nextBullets =
> filter (notcollision tmpd1) tmpd2
> ... dataObject nextd1 nextd2
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elm-discuss...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Hot Belgo

unread,
Nov 24, 2013, 5:20:46 AM11/24/13
to elm-d...@googlegroups.com
That's fascinating - create a bigger data structure! I love functional programming approaches!
thx

John Mayer (gmail)

unread,
Nov 24, 2013, 9:53:10 AM11/24/13
to elm-d...@googlegroups.com, elm-d...@googlegroups.com
Depending on how ambitious you want to be, you may way to look at the R-tree family of data structures. 

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages