Skip to content

Instantly share code, notes, and snippets.

@otaconix
Last active February 26, 2017 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otaconix/052b980a3fd506e91b92c3bee96b74d5 to your computer and use it in GitHub Desktop.
Save otaconix/052b980a3fd506e91b92c3bee96b74d5 to your computer and use it in GitHub Desktop.
cond construct
rand := (Random value * 99 + 1) round
rand println
cond := method(
if(call message argCount % 2 != 0, Exception raise("cond needs pairs of arguments (arg count: #{call message argCount})" interpolate));
inner := method(c, i,
if (i >= c message argCount, Exception raise("cond needs at least one condition to be true"));
if (c evalArgAt(i), return c evalArgAt(i + 1), return inner(c, i + 2)))
inner(call, 0))
for(i, 1, 10,
write("[#{i}] What's your guess? " interpolate)
guess := File standardInput readLine asNumber
cond(
guess == rand, "You guessed it!"; break,
guess > rand, "Too high",
guess < rand, "Too low"
) println)
@otaconix
Copy link
Author

Usage:

cond(
  1 == 1, "Math works!" println,
  1 == 2, "Oh no!" println)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment