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

builtin: Add rand and strings functions #82

Merged
merged 3 commits into from
Feb 18, 2023
Merged

builtin: Add rand and strings functions #82

merged 3 commits into from
Feb 18, 2023

Conversation

juliaogris
Copy link
Member

@juliaogris juliaogris commented Feb 17, 2023

Add random number generation functions:

  • rand n returns an integer in the half open interval [0,n)
  • rand1 returns an float64 in the half open interval [0,1)

Add string functions upper, lower, index, trim,
startswith, endswith, replace.

This functions are mapped to Go's math/rand package. It seems that
tinygo still requires seeding (tested).

Add a demos for clarity.

Minor bug fix for non 2xx source response: don't load random HTLM,
write error to console instead.

https://evy-lang--82-r0m72zv4.web.app/#source=samples/rand.evy&show=.read
https://evy-lang--82-r0m72zv4.web.app/#strings

@github-actions
Copy link

github-actions bot commented Feb 17, 2023

firebase-deployment: https://evy-lang--82-r0m72zv4.web.app (5348d79)

@juliaogris juliaogris changed the title builtin: Add rand and rand1 functions builtin: Add rand and strings functions Feb 17, 2023
@juliaogris
Copy link
Member Author

juliaogris commented Feb 17, 2023

From Mali: hey mum, the sample code would be better like this:

while true
  print "╔════════════════════════╗"
  print "║ Guess my number (1-10) ║"
  print "╚════════════════════════╝"

  n := 1 + (rand 10)
  guess := readn

  while guess != n
    if guess > 10
      print guess "Guess higher (1-10)."
    else if guess < 1
      print guess " Guess lower (1-10)."
    else if guess < n
       print guess "Guess higher."
    else 
       print  guess "Guess lower."
    end
    guess = readn
  end
  print guess "You got it 🥳."
  print
end

// readn reads a number. 
// If you enter a string that is not number
func readn:num
  while true
    str := read
    n := str2num str
    if !err
      return n
    end
    print "💣 '"+str+"'is not a number. Try again."
  end
  return -1
end

@juliaogris juliaogris changed the title builtin: Add rand and strings functions builtin: Add rand and strings functions Feb 18, 2023
Copy link
Contributor

@camh- camh- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦚 LGTM

I noticed when I tried the guessing game that the input line was not focused, so I typed a number (nothing happened) and I pressed enter, and that stopped the program because the Stop button was still focused. Perhaps calling read should focus on the input?

Also, I guessed the right number 3 times, so I think I deserve a cookie.


while guess != n
if guess > 10
print guess "Guess higher (1-10)."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be "Guess lower" and the next "Guess higher"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍪
thanks for the review!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this up.
Your comment re input focus is a really good one. also fixed.

Only load evy source via URL fragment for 2xx response.
Add random number generation functions:

* rand n returns an integer in the half open interval [0,n)
* rand1 returns an float64 in the half open interval [0,1)

This functions are mapped to Go's math/rand package. It seems that
tinygo still requires seeding (tested).

Add a demo for clarity.

Focus input textbox when jsRead is called for better usability.
Add more strings functions for good measure and because it was the only
thing I was capable of on this hot afternoon.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants