Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Add pointwise mapping #10

Open
fredcy opened this issue Mar 6, 2017 · 0 comments
Open

Add pointwise mapping #10

fredcy opened this issue Mar 6, 2017 · 0 comments

Comments

@fredcy
Copy link
Member

fredcy commented Mar 6, 2017

[2017-03-06: I copied this issue, created by @folkertdev, from https://github.com/elm-community/elm-linear-algebra/issues/18 per https://github.com//issues/7. -fredcy]

As it stands, the types in this library are somewhat tedious to work with. Especially when applying
the same operation to multiple components. @fredcy mentioned on slack that conversion to List/Array is planned, but this still requires two conversions (to and from List/Array).

suggestion

Implement a pointwise map and map2 which are equivalent (in the Vec2 case) to

map : (Float -> Float) -> Vec2 -> Vec2
map f = 
    fromTuple << (\(a, b) -> (f a, f b)) << toTuple

map2 : (Float -> Float -> Float) -> Vec2 -> Vec2 -> Vec2
map2 f a b = 
    let (left, right) = (toRecord a, toRecord b) 
    in 
	fromTuple ( f left.x right.x, f left.y right.y )

edit: implementing map3 in terms of map2 isn't very practical

concrete use case:

-- maximal and minimal components of two vectors 
boundingBox a b = (map2 min a b, map2 max a b) 

points of discussion

  • A logical extension would be to also introduce mapX, mapY, map...
  • pointwise is an alternative name to map2 that reads nicer (but is less consistent)
  • map2 is restricted to functions of type Float -> Float -> Float, where Float -> Float -> a would be more useful. Rather than adding functionality to Vector, a Tuple.Extra package could implement the various mapping functions.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant