{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","results":{"codes":[]},"settings":"","params":[]},"next":{"description":"","pages":[]},"title":"Set-Theoretic Operations","type":"basic","slug":"set-theoretic-operations","excerpt":"How can you combine collections together?","body":"We explore the set-theoretic operations provided by Lasp. Each of these functions takes, as input, a two source collections and creates a single output collection. The output will be maintained as the input is modified, and these operators can be chained arbitrarily. \n[block:api-header]\n{\n \"title\": \"Product Operation\"\n}\n[/block]\nLet's start by creating two input collections and an output collection.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"1> {ok, {LeftId, _, _, _}} = lasp:declare({<<\\\"left\\\">>, state_orset}, state_orset). \\n{ok,{{<<\\\"left\\\">>,state_orset},\\n state_orset,\\n [{clock,[{<<170,227,142,126,63,64,19,227,195,66,39,125,58,\\n 195,75,134,148,109,168,...>>,\\n 1}]}],\\n {state_orset,[]}}}\\n2> {ok, {RightId, _, _, _}} = lasp:declare({<<\\\"right\\\">>, state_orset}, state_orset).\\n{ok,{{<<\\\"right\\\">>,state_orset},\\n state_orset,\\n [{clock,[{<<170,227,142,126,63,64,19,227,195,66,39,125,58,\\n 195,75,134,148,109,168,...>>,\\n 1}]}],\\n {state_orset,[]}}}\\n3> {ok, {ProductId, _, _, _}} = lasp:declare({<<\\\"product\\\">>, state_orset}, state_orset).\\n{ok,{{<<\\\"product\\\">>,state_orset},\\n state_orset,\\n [{clock,[{<<170,227,142,126,63,64,19,227,195,66,39,125,58,\\n 195,75,134,148,109,168,...>>,\\n 1}]}],\\n {state_orset,[]}}}\",\n \"language\": \"erlang\"\n }\n ]\n}\n[/block]\nNow, let's instruct the system to compute the Cartesian product of these two collections, and place the output in it's own collection.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"4> lasp:product(LeftId, RightId, ProductId).\\nok\",\n \"language\": \"erlang\"\n }\n ]\n}\n[/block]\nNow, let's insert some values into the collections.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"5> lasp:update(LeftId, {add, 1}, self()).\\n{ok,{{<<\\\"left\\\">>,state_orset},\\n state_orset,\\n [{clock,[{<<170,227,142,126,63,64,19,227,195,66,39,125,58,\\n 195,75,134,148,109,168,...>>,\\n 2}]}],\\n {state_orset,[{1,\\n [{<<238,140,18,224,53,159,36,82,45,233,15,19,116,65,95,\\n 234,...>>,\\n true}]}]}}}\\n6> lasp:update(RightId, {add, 1}, self()).\\n{ok,{{<<\\\"right\\\">>,state_orset},\\n state_orset,\\n [{clock,[{<<170,227,142,126,63,64,19,227,195,66,39,125,58,\\n 195,75,134,148,109,168,...>>,\\n 2}]}],\\n {state_orset,[{1,\\n [{<<255,58,60,173,193,138,15,227,94,88,201,220,12,106,56,\\n 116,...>>,\\n true}]}]}}}\",\n \"language\": \"erlang\"\n }\n ]\n}\n[/block]\nLet's verify we have the right result: in this case it will be a collection of pairs, represented as Erlang tuples.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"7> {ok, ProductResult} = lasp:query(ProductId), sets:to_list(ProductResult).\\n[{1,1}]\",\n \"language\": \"erlang\"\n }\n ]\n}\n[/block]\n\n[block:api-header]\n{\n \"title\": \"Union\"\n}\n[/block]\nNow, we can also use these initial collections to perform a union.\n\nLet's declare a place for the results.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"8> {ok, {UnionId, _, _, _}} = lasp:declare({<<\\\"union\\\">>, state_orset}, state_orset). \\n{ok,{{<<\\\"union\\\">>,state_orset},\\n state_orset,\\n [{clock,[{<<170,227,142,126,63,64,19,227,195,66,39,125,58,\\n 195,75,134,148,109,168,...>>,\\n 1}]}],\\n {state_orset,[]}}}\",\n \"language\": \"erlang\"\n }\n ]\n}\n[/block]\nLet's perform the union.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"9> lasp:union(LeftId, RightId, UnionId).\\nok\",\n \"language\": \"erlang\"\n }\n ]\n}\n[/block]\nFinally, let's look at the output.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"10> {ok, UnionResult} = lasp:query(UnionId), sets:to_list(UnionResult). \\n[1]\",\n \"language\": \"erlang\"\n }\n ]\n}\n[/block]\n\n[block:api-header]\n{\n \"title\": \"Intersection\"\n}\n[/block]\nIntersection operates in a similar way as well.\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"11> {ok, {IntersectionId, _, _, _}} = lasp:declare({<<\\\"intersection\\\">>, state_orset}, state_orset).\\n{ok,{{<<\\\"intersection\\\">>,state_orset},\\n state_orset,\\n [{clock,[{<<170,227,142,126,63,64,19,227,195,66,39,125,58,\\n 195,75,134,148,109,168,...>>,\\n 1}]}],\\n {state_orset,[]}}}\\n12> lasp:intersection(LeftId, RightId, IntersectionId). \\nok\\n13> {ok, IntersectionResult} = lasp:query(IntersectionId), sets:to_list(IntersectionResult).\\n[1]\",\n \"language\": \"erlang\"\n }\n ]\n}\n[/block]","updates":[],"order":7,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"58f5d5b8df3d1137004d5c90","githubsync":"","parentDoc":null,"project":"5612c2a00fbdb60d00e4c7d1","__v":0,"user":"5612c290c0731b0d00625078","category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Lasp","slug":"api","order":1,"from_sync":false,"reference":false,"_id":"58e6b68eb1eece19008b81fe","project":"5612c2a00fbdb60d00e4c7d1","version":"5612c2a00fbdb60d00e4c7d4","__v":0,"createdAt":"2017-04-06T21:43:42.570Z"},"createdAt":"2017-04-18T09:00:40.677Z","version":{"version":"0.0.1","version_clean":"0.0.1","codename":"","is_stable":true,"is_beta":true,"is_hidden":false,"is_deprecated":false,"categories":["5612c2a10fbdb60d00e4c7d5","58e6b64bc397de1b00173c09","58e6b68eb1eece19008b81fe","58e6b6d185ec873100be9734","58f5e5c2616bbb1b00d54c15","58f5eaa74c413f19007a2858","58f767a79c723f19004e1ca9"],"_id":"5612c2a00fbdb60d00e4c7d4","createdAt":"2015-10-05T18:34:08.611Z","releaseDate":"2015-10-05T18:34:08.611Z","__v":7,"project":"5612c2a00fbdb60d00e4c7d1"}}
Set-Theoretic Operations
How can you combine collections together?