Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when/case + general cleanup #1762

Merged
merged 156 commits into from
Nov 8, 2024
Merged

Conversation

drkameleon
Copy link
Collaborator

Description

Continuing from here: #1653

Type of change

  • Code cleanup
  • Enhancement (implementation update, or general performance enhancements)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (documentation-related additions)

…radually-eliminate-if-else

# Conflicts:
#	version/build
@drkameleon
Copy link
Collaborator Author

drkameleon commented Nov 8, 2024

Final version

(nothing written in stone; but to avoid going over it till the end of time and not merging it, let's merge it and we can change it as we go, if we need to do so. also, the approach is quite simplistic, less intrusive than initially designed, so that we don't overcomplicate things too much too soon; we have all the time in the world for that... lol)

case

match given argument against different values and execute corresponding block

x: 2

; the main block is always evaluated!
case x [
    1   -> print "x is one!"
    2   -> print "x is two!"
    any -> print "x is none of the above"
]
; x is two!
key: "one"
case key [
    "one" 1,        ; we can also return
    "two" 2         ; simple constant values directly
]
; => 2
case "hello" #[
    hello: "hola"
    adios: "goodbye"
]
; => "hola"

when

check conditions one by one and execute corresponding block accordingly

; the main block is always evaluated!
when [
    prime? 4 -> print "yes, 4 is prime - wait, what?!"
    prime? 5 -> print "yes, 5 is prime
    prime? 7 -> print "yes, 7 is prime
    true     -> print "none of the above was true"
]
; yes, 5 is prime
when.any [
    prime? 4 -> print "yes, 4 is prime - wait, what?!"
    prime? 5 -> print "yes, 5 is prime"
    prime? 7 -> print "yes, 7 is prime"
]
; yes, 5 is prime
; yes, 7 is prime
x: 2
when.has: x [
    [=0] -> print "x is zero!"
    [<1] -> print "x is less than 1"
    [<4] -> print "x is less than 4"
    true -> print "x is >= 4"
]
; x is less than 4

@drkameleon
Copy link
Collaborator Author

And...
ready to merge! 🚀

@drkameleon drkameleon merged commit ba1432d into master Nov 8, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
→ Core documentation Improvements or additions to documentation installer Issues related to the Installation script library Issues related to the standard library → Logic → Reflection repl Issues related to the REPL rosetta code Issues and PRs related to Rosetta Code snippets unit-test Unit tests values Issues related to the core Value type workflows Issues related to GitHub workflows, building, releases, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants