From 3063608df6ff1668f683d9d22c57fea29d728d0d Mon Sep 17 00:00:00 2001 From: Marcos Magueta Date: Tue, 11 Nov 2025 00:00:43 -0300 Subject: migrating to erlang to attach to mnesia --- src/operations.erl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/operations.erl (limited to 'src/operations.erl') diff --git a/src/operations.erl b/src/operations.erl new file mode 100644 index 0000000..7846caf --- /dev/null +++ b/src/operations.erl @@ -0,0 +1,15 @@ +-module(operations). +-export([select/2, project/2, cartesian_product/2]). + +%% @doc Selection - filter tuples based on a predicate +%% `Predicate` is a function that takes a tuple and returns true/false +select(Relation, Predicate) -> + sets:filter(Predicate, Relation). + +%% @doc Projection - select specific attributes from a relation +%% Note that `AttributeNames` can have duplicate keys, which are ignored +project(Relation, AttributeNames) when is_list(AttributeNames) -> + sets:map(fun (Tuple) -> maps:with(AttributeNames, Tuple) end, Relation). + +cartesian_product(Relation1, Relation2) -> + sets:from_list([maps:merge(Tuple1, Tuple2) || Tuple1 <- sets:to_list(Relation1), Tuple2 <- sets:to_list(Relation2)], [{version, 2}]). -- cgit v1.2.3