Skip to content

Sacul0457/if-else-is-even

Repository files navigation

About

This is just a silly repo that consists of code generators in different languages to see if a number is even.

Table of contents

Interpreted languages:

  1. Python
  2. Perl
  3. Lua
  4. TypeScript

Compiled languages:

  1. C
  2. Rust
  3. Pascal

Esoteric languages:

  1. HTMLish

Python Version

Generating the code

python python.py 10

If you want to run the generated script, run these commands instead:

  • python python.py 10 > is_even.py
  • python is_even.py

Running the function directly

x = 10
func = is_even(x, return_func=True) # This will return a callable than you can call
print(func()) # Call the function which prints 'True'

(GO TO HOME)

Perl Version

Make sure you have perl installed.

  1. Run the script generator:
perl perl.pl - 5 > is_even.pl

Note

The - symbol after the script is to pass the argument to the script. If you want to pass negative number. You can do something like this:

perl perl.pl - -10 > is_even.pl
  1. Run the generated script:
perl is_even.pl
# 0

(GO TO HOME)

Lua Version

Run the script generator:

Only generate the code
lua lua.lua 9
Redirect the output to lua to run it directly
lua lua.lua 9 | lua
# false
Redirect the output to a file to run it later:
lua lua.lua 9 > is_9_even.lua

Later on, if you forget if 9 is even or not, just run the script:

lua is_9_even.lua
# false

(GO TO HOME)


C Version

  1. Compile:
gcc c_code.c -o c_code
  1. Usage:
# Unix
./c_code 10 > is_even.c

# Windows
.\c_code.exe 10 > is_even.c
  1. Compile that newly baked program:
gcc is_even.c -o is_even
  1. Check the result, is 10 even?:
# Unix
./is_even

# Windows
.\is_even.exe

# true

(GO TO HOME)

Rust Version

Make sure you have rustup installed.

  1. Compile:
rustc rust.rs
  1. Usage:
# Unix
./rust 10 > is_even.rs

# Windows
.\rust.exe 10 > is_even.rs
  1. Compile that newly baked program:
rustc is_even.rs
  1. Check the result, is 10 even?:
# Unix
./is_even

# Windows
.\is_even.exe

# true

(GO TO HOME)

Pascal Version

I use fpc to compile. Use whatever free pascal compiler you have.

  1. Compile:
fpc pascal.pp
  1. Usage:
# Unix:
./pascal 9 > isEven.pp

# Windows:
.\pascal.exe 9 > isEven.pp
  1. Compile the new isEven program:
fpc isEven.pp
  1. Run it:
# Unix:
./isEven

# Windows:
.\isEven.exe

# FALSE

(GO TO HOME)


HTMLish Version

Import function

<head>
  <import name="is even" module="if-else-is-even"/>
</head>

Usage

<body>
  <call name="is even">1</call> # False
  <print>
    <call name="is even">2</call> # True
  </print>
</body>

Execution

$ python -m htmlish htmlish.html [--code]

Options:

  • --code: Displays the Python-equivalent of the HTMLish code

(GO TO HOME)


TypeScript Version

Import

Copy typescript.ts into your repository and import it directly. In this example, we have name the file "ifElseIsEven.ts":

import { evaluate } from "ifElseIsEven.ts";

Usage

import { evaluate } from "ifElseIsEvent.ts";
 
function fn() {
    const parityOf3 = evaluate(3); // "Odd"
    const parityOf8 = evaluate(8); // "Even"
    const output = `3 is ${parityOf3} and 8 is ${parityOf8}`;
    console.log(output); // Prints "3 is Odd and 8 is Even"
}

Notes

Caution

Calling evaluate for numbers with an absolute value > 100,000,000 will likely take at least 10 seconds to process and consume a very high amount of RAM (estimated between 40 and 80 bytes per number). The program is structured in a way that does not overflow the stack; it will continue running until it has exhausted your system's memory and may cause your runtime or operating system to crash.

  • Supports negative inputs
  • Does not support Infinity or -Infinity (an error message will display)
  • Does not support NaN (an error message will display)
  • The return type of evaluate is a string literal - "Even" or "Odd"

(GO TO HOME)

About

Code generator to see if a number is even (in many languages)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors