Skip to content

Example Code

David Khristepher Santos edited this page Apr 9, 2022 · 10 revisions

Fibonacci

Calculates the first 50 fibonacci numbers and writes them to the console

BiteFibo

module MainModule;

import System;
using System;

// Fibonacci Example

function FindFibonacciNumber(n)
{
    var count= 2;
    var a = 1;
    var b = 1;
    var c = 1;
    if(n == 0)
    {
        return 0;
    }
    while(count<n)
    {
        c = a + b;
        a = b;
        b = c; 
        count++;
    }

    return c;
}

for(var i = 0; i < 50; i++)
{
    PrintLine(FindFibonacciNumber(i));
}

Primes

The following code will calculate and print the 2nd, 4th, 8th, 16th, 32nd and 64th prime numbers.

BitePrime

module MainModule;

import System;
using System;

function FindPrimeNumber(n)
{
    var count=0;
    var a = 2;
    while(count<n)
    {
        var b = 2;
        var prime = 1;
        while(b * b <= a )
        {
            if(a % b == 0)
            {
                prime = 0;
                break;
            }
            b++;
        }
        if(prime > 0)
        {
            count++;
        }
        a++;
    }
    return (--a);
}

PrintLine(FindPrimeNumber(2));
PrintLine(FindPrimeNumber(4));
PrintLine(FindPrimeNumber(8));
PrintLine(FindPrimeNumber(16));
PrintLine(FindPrimeNumber(32));
PrintLine(FindPrimeNumber(64));

Class Inheritance

Demonstrates the use of inheritance and access of members.

BiteClasses

module MainModule;

import System;
using System;

class Foo
{
    var x = 5;
}

class TestClass : Foo
{
    function TestClass(n)
    {
        x = n;
    }
}

function TestFunction(n)
{
    n.x = 10;
}

var t = new TestClass(500);
PrintLine(t.x);

Dynamic Arrays

The following code will create a dynamic array and fill it with strings, and then print out the array elements.

biteDynamic

module MainModule;

import System;
using System;

var c = new Object();
var f = 0;
for(var x = 0; x < 10; x++)
{
    for(var y = 0; y < 10; y++)
    {
        for(var z = 0; z < 10; z++)
        {
           c[x][y][z] = "Number: " + f;
           f++;
        }
    }
}

for(var x = 0; x < 10; x++)
{
    for(var y = 0; y < 10; y++)
    {
        for(var z = 0; z < 10; z++)
        {
           PrintLine(c[x][y][z]);
        }
    }
}

Higher-order functions

The following code will create a function that returns a function and call the returned function.

BiteFuncAsReturn

module MainModule;

import System;
using System;

// Fibonacci function as return value Example
function FindFibonacciNumber(n)
{
    var count= 2;
    var a = 1;
    var b = 1;
    var c = 1;
    if(n == 0)
    {
        return 0;
    }
    while(count<n)
    {
        c = a + b;
        a = b;
        b = c; 
        count++;
    }

    return c;
}

function FuncGiver()
{
    return FindFibonacciNumber;
}


var a = FuncGiver();

for(var i = 0; i < 50; i++)
{
    PrintLine(a(i));
}

Functions as First-class Citizens

The following code will pass a function to a function and call the passed function.

BiteFuncAsArg

module MainModule;

import System;
using System;

// Fibonacci function as argument Example
function FindFibonacciNumber(n)
{
    var count= 2;
    var a = 1;
    var b = 1;
    var c = 1;
    if(n == 0)
    {
        return 0;
    }
    while(count<n)
    {
        c = a + b;
        a = b;
        b = c; 
        count++;
    }

    return c;
}

function FuncCall(n)
{
    for(var i = 0; i < 50; i++)
    {
        PrintLine(n(i));
    }
}


FuncCall(FindFibonacciNumber);