DEV Community

Discussion on: Go for Java developers

Collapse
 
theodesp profile image
Theofanis Despoudis

Great job. However I'm curious about one thing. Why does the match need to modify the internal matchers state? If you call it 2 times with the same reference object and text it will return different results. For example

pf := newProfileFilter("Theo")
fmt.Println(pf.match("#Theo")) // True
fmt.Println(pf.match("#Theo")) // False ??? Side effects???

Collapse
 
napicella profile image
Nicola Apicella

Hello! Thank you :)

So yes it modifies the internal state. The idea is to keep a state within the profileFilter so it matches the header, name and surname in the order.
i.e. after matching the header, the filter must transition in a state in which is going to match 'name' next, and so on.
It has side side effects.

We can transform it in a pure function(no side effects) by passing the filter state itself as additional argument.

Something along those lines:
pf := newProfileFilter("Theo")
isAMatch, pf = pf.match("#Theo", pf)

The match function returns two values, a boolean as before and the profileFilter itself.
With this change the match function is pure because given the same input returns the same output.

Hope this answer your question.