I like using it to clean up confusing if then else scenarios. It enables very clean readable code.
I put together this potential pattern to use match to dispatch actions based on truth table values.
Steve
DispatchPattern.jl
"""Implement a pattern for dispatching actions based on Truth table values using Match.jl"""
module DispatchPattern
using Match
"Basic truth structure"
struct Truth
a::Bool
b::Bool
end
"Actions for Dispatch"
actionAB() =println("Do A and B")
actionA!B() =println("Do A not B")
action!AB() =println("Do B not A")
action!A!B() =println("Do nothing")
"Pattern for dispatching based on multiple truth cases"
dispatch!(item::Truth)=@match (item.a, item.b) begin
(true, true) => actionAB()
(true, false) => actionA!B()
(false, true) => action!AB()
(false, false) => action!A!B()
bad => println("Unknown dispatch: $bad")
end
Thank you for writing Match.
I like using it to clean up confusing if then else scenarios. It enables very clean readable code. I put together this potential pattern to use match to dispatch actions based on truth table values.
Steve
DispatchPattern.jl
"""Implement a pattern for dispatching actions based on Truth table values using Match.jl"""
module DispatchPattern using Match
"Basic truth structure" struct Truth a::Bool b::Bool end
"Actions for Dispatch" actionAB() =println("Do A and B") actionA!B() =println("Do A not B") action!AB() =println("Do B not A") action!A!B() =println("Do nothing")
"Pattern for dispatching based on multiple truth cases" dispatch!(item::Truth)=@match (item.a, item.b) begin (true, true) => actionAB() (true, false) => actionA!B() (false, true) => action!AB() (false, false) => action!A!B() bad => println("Unknown dispatch: $bad") end
truths=[Truth(true, true), Truth(true, false), Truth(false, true), Truth(false, false)]
map(dispatch!, truths)
end