# Defining Set Operations / Group Operations

**URL:** <https://discourse.julialang.org/t/defining-set-operations-group-operations/96060>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 14, 2023, 3:37pm UTC](https://discourse.julialang.org/t/defining-set-operations-group-operations/96060 "2023-03-14T15:37:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunshine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunshine/32/47687_2.png) [@Sunshine](https://discourse.julialang.org/u/Sunshine)\
**Post date:** [March 14, 2023, 3:37pm UTC](https://discourse.julialang.org/t/defining-set-operations-group-operations/96060/1 "2023-03-14T15:37:36Z")

</div>

Hey All,

I’m exploring coding concepts from Group theory.

There’s a lot of questions I can ask here so I wanted to put them into one place and see if anyone can support me in finding the answers.

- If I construct a Set of elements (undefined/abstract), how can I define an operation that will work (only) on those elements?
- How can I call upon an element from a Set?
- How can I add new elements to a Set, if the Set is already defined?

The sets and rules I am looking to build will impact different parts of the elements in a given set. The most simple functions are rotations. So if the function is applied to an element of the Set, it outputs another element of the Set.

And addition; so when I piece two elements in the Set, it makes a new element with some changes. I want this element to sit in another Set (to be defined).

Appreciate this is abstract and may need more detail; I may also be posting other threads along these lines 🙂

All the best!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 14, 2023, 3:45pm UTC](https://discourse.julialang.org/t/defining-set-operations-group-operations/96060/2 "2023-03-14T15:45:41Z")

</div>

Are these homework questions?

Have you read the [documentation](https://docs.julialang.org/en/v1/base/collections/#Set-Like-Collections) on sets? These questions should all be answered in the documentation, except for this one:

> - If I construct a Set of elements (undefined/abstract), how can I define an operation that will work (only) on those elements?

This might be best handled via the type system.

---

<div class="post-metadata">

**Author:** ![Sunshine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunshine/32/47687_2.png) [@Sunshine](https://discourse.julialang.org/u/Sunshine)\
**Post date:** [March 14, 2023, 3:49pm UTC](https://discourse.julialang.org/t/defining-set-operations-group-operations/96060/3 "2023-03-14T15:49:53Z")

</div>

Thank you pdeffebach!

No these are not homework haha :). I am trying to build something, that links colour, through group sets, to data for analysis. But I am struggling to get my head around how to explain it, and get uncertain as to which part to focus on

I will take a read over the documentation again; thank you for sharing!

---

<div class="post-metadata">

**Author:** ![Sunshine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunshine/32/47687_2.png) [@Sunshine](https://discourse.julialang.org/u/Sunshine)\
**Post date:** [March 14, 2023, 5:16pm UTC](https://discourse.julialang.org/t/defining-set-operations-group-operations/96060/4 "2023-03-14T17:16:49Z")

</div>

The documentation is not covering what I am looking for.

Is it possible to define a function, such that the input is an element from a given Set, and the output is an element from a given Set.

e.g: I have three elements. a,b,c. I define a set S = {a,b,c}

I define a function f(x) for x belonging to S, such that:  
f(a) = b  
f(b) = c  
f(c) = a

One step further would be:  
Let S1 = {a,b,c} , Let S2 = {d,e,f}

g(x) maps S1 → S2, for x belonging to S1 and g(x) belonging to S2:  
f(a) = d  
f(b) = e  
f(c) = f

Any help is appreciated!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 14, 2023, 5:20pm UTC](https://discourse.julialang.org/t/defining-set-operations-group-operations/96060/5 "2023-03-14T17:20:50Z")

</div>

`Enum`s will probably do what you want. See documentation [here](https://docs.julialang.org/en/v1/base/base/#Base.Enums.@enum)

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [March 14, 2023, 5:39pm UTC](https://discourse.julialang.org/t/defining-set-operations-group-operations/96060/6 "2023-03-14T17:39:32Z")

</div>

From what I understand, you are trying to define a function on a finite domain. For that, I would just use a dictionary maybe wrapped in a type to make it callable:

```julia
julia> struct FiniteMap{In,Out}
           mapping::Dict{In,Out}
       end

julia> FiniteMap(ins, outs) = FiniteMap(Dict(zip(ins, outs)))
FiniteMap

julia> (f::FiniteMap)(x) = f.mapping[x]

julia> f = FiniteMap([:a, :b, :c], [1, 3, 2])
FiniteMap{Symbol, Int64}(Dict(:a => 1, :b => 3, :c => 2))

julia> xs = rand([:a, :b, :c], 8)
8-element Vector{Symbol}:
 :a
 :b
 :b
 :b
 :a
 :c
 :b
 :a

julia> f.(xs)
8-element Vector{Int64}:
 1
 3
 3
 3
 1
 2
 3
 1

```
