-
-
Save schacon/1820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Issac Trotts' demonstration of pattern matching in Nu | |
(function people-to-string (people) | |
(match people | |
(() "no people") | |
((p1) "one person: #{p1}") | |
((p1 p2) "two people: #{p1} and #{p2}") | |
(else "too many people: #{(people length)}"))) | |
(assert_equal "no people" | |
(people-to-string '())) | |
(assert_equal "one person: Tim" | |
(people-to-string '(Tim))) | |
(assert_equal "two people: Tim and Matz" | |
(people-to-string '(Tim Matz))) | |
(assert_equal "too many people: 3" | |
(people-to-string '(Tim Guido Matz))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment