Logo

Programming-Idioms

History of Idiom 5 > diff from v347 to v348

Edit summary for version 348 by :
comment

Version 347

2015-11-18, 11:59:20

Version 348

2015-11-18, 12:00:44

Idiom #5 Create a 2D Point data structure

Declare a container type for two floating-point numbers x and y

Idiom #5 Create a 2D Point data structure

Declare a container type for two floating-point numbers x and y

Code
-module(points).
-export([new/2, x/1, y/1]).

-opaque point() :: #{x => float(), y => float()}.
-export_type([point/0]).

-spec new(float(), float()) -> point().
new(X, Y) -> #{x => X, y => Y}.

-spec x(point()) -> float().
x(#{x := X}) -> X.

-spec y(point()) -> float().
y(#{y := Y}) -> Y.
Code
-module(points).
-export([new/2, x/1, y/1]).

-opaque point() :: #{x => float(), y => float()}.
-export_type([point/0]).

-spec new(float(), float()) -> point().
new(X, Y) -> #{x => X, y => Y}.

-spec x(point()) -> float().
x(#{x := X}) -> X.

-spec y(point()) -> float().
y(#{y := Y}) -> Y.
Comments bubble
to use this module from somewhere else you do stuff like...
Point = points:new(X, Y),
X = points:x(Point),
Y = points:y(Point).