You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
This discussion was converted from issue #65 on August 03, 2023 19:16.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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
Beta Was this translation helpful? Give feedback.
All reactions