{"_id":"58f5d5b8df3d1137004d5c90","parentDoc":null,"project":"5612c2a00fbdb60d00e4c7d1","__v":0,"user":"5612c290c0731b0d00625078","category":{"_id":"58e6b68eb1eece19008b81fe","project":"5612c2a00fbdb60d00e4c7d1","version":"5612c2a00fbdb60d00e4c7d4","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-04-06T21:43:42.570Z","from_sync":false,"order":1,"slug":"api","title":"Lasp"},"version":{"_id":"5612c2a00fbdb60d00e4c7d4","__v":7,"project":"5612c2a00fbdb60d00e4c7d1","createdAt":"2015-10-05T18:34:08.611Z","releaseDate":"2015-10-05T18:34:08.611Z","categories":["5612c2a10fbdb60d00e4c7d5","58e6b64bc397de1b00173c09","58e6b68eb1eece19008b81fe","58e6b6d185ec873100be9734","58f5e5c2616bbb1b00d54c15","58f5eaa74c413f19007a2858","58f767a79c723f19004e1ca9"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"0.0.1","version":"0.0.1"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2017-04-18T09:00:40.677Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":7,"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]","excerpt":"How can you combine collections together?","slug":"set-theoretic-operations","type":"basic","title":"Set-Theoretic Operations"}

Set-Theoretic Operations

How can you combine collections together?

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. [block:api-header] { "title": "Product Operation" } [/block] Let's start by creating two input collections and an output collection. [block:code] { "codes": [ { "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,[]}}}", "language": "erlang" } ] } [/block] Now, let's instruct the system to compute the Cartesian product of these two collections, and place the output in it's own collection. [block:code] { "codes": [ { "code": "4> lasp:product(LeftId, RightId, ProductId).\nok", "language": "erlang" } ] } [/block] Now, let's insert some values into the collections. [block:code] { "codes": [ { "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}]}]}}}", "language": "erlang" } ] } [/block] Let's verify we have the right result: in this case it will be a collection of pairs, represented as Erlang tuples. [block:code] { "codes": [ { "code": "7> {ok, ProductResult} = lasp:query(ProductId), sets:to_list(ProductResult).\n[{1,1}]", "language": "erlang" } ] } [/block] [block:api-header] { "title": "Union" } [/block] Now, we can also use these initial collections to perform a union. Let's declare a place for the results. [block:code] { "codes": [ { "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,[]}}}", "language": "erlang" } ] } [/block] Let's perform the union. [block:code] { "codes": [ { "code": "9> lasp:union(LeftId, RightId, UnionId).\nok", "language": "erlang" } ] } [/block] Finally, let's look at the output. [block:code] { "codes": [ { "code": "10> {ok, UnionResult} = lasp:query(UnionId), sets:to_list(UnionResult). \n[1]", "language": "erlang" } ] } [/block] [block:api-header] { "title": "Intersection" } [/block] Intersection operates in a similar way as well. [block:code] { "codes": [ { "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]", "language": "erlang" } ] } [/block]