Skip to content

zachallaun/ValueDispatch.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Value Dispatch

Function dispatch on value instead of type. Quite similar to Clojure's multimethods.

Behold:

@dispatch fizzbuzz(n::Int) = (n % 3 == 0, n % 5 == 0)

@on (true,true)  fizzbuzz(n) = "fizzbuzz"
@on (true,false) fizzbuzz(n) = "fizz"
@on (false,true) fizzbuzz(n) = "buzz"
@on :default     fizzbuzz(n) = n

for i in 1:15
    println(fizzbuzz(i))
end
# 1
# 2
# fizz
# 4
# buzz
# fizz
# 7
# 8
# fizz
# buzz
# 11
# fizz
# 13
# 14
# fizzbuzz

About

Function dispatch on values

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages