Hacker News new | ask | show | jobs
by juskrey 2275 days ago
Once upon a time I have implemented POP3 server protocol state machine in Clojure. 180 lines in total, of which 40 were FSM declaration, 140 command handling functions. Not sure I'll ever want to approach FSM in any other language, unless no other choice.
2 comments

Did you iterate to a FSM solution, from many nested conditions, or use it right from the start? I'm fighting the urge to write any FSM without having the absolute certainty that it is needed. There seems no other way than by refactoring to an FSM.
I have started from already existing FSM libs (which did not fit well) and after couple of experiments understood that creating own custom FSM processor is a no-brainer with clojure/core.match
Can you perhaps point to it? Sounds interesting to read that code!
Yes. Can't share the whole repo (it's our internal product), but here are two files (FSM macro and FSM itself) you can explore. Let me know it that helps.

https://gist.github.com/juskrey/61148c98bdde871a8d3743f54b82...

https://gist.github.com/juskrey/127cf8456fc527d20ed5e244ce01...

Interesting, thanks a lot for being able to share those examples!

I think you managed to find a bug in the GitHub syntax highlighter, as https://gist.github.com/juskrey/61148c98bdde871a8d3743f54b82... has no colors after line ~108.

Not OP but there is tons of FSM implementations/libs in Clojure scattered around GitHub and GitLab if you search for them.

Some of them:

- https://github.com/ztellman/automat

- https://github.com/metosin/tilakone

- https://github.com/cdorrat/reduce-fsm