User:Btx40/Sandbox

From Wikipedia, the free encyclopedia
using System.IO;

public DateTime ReadDate(this TextReader txt)
{
    return DateTime.Parse(txt.ReadLine());
}

public int ReadInt(this TextReader txt)
{
    return int.Parse(txt.ReadLine());
}

public float ReadFloat(this TextReader txt)
{
    return float.Parse(txt.ReadLine());
}

public decimal ReadDec(this TextReader txt)
{
    return decimal.Parse(txt.ReadLine());
}

public bool ReadYN()
{
    char c;
    
    do
    {
        c = (char)Console.Read();
        c = c.ToLower();
    } while (c != 'y' || c != 'n')
    
    return c == 'y';
}

public string Prompt(string question)
{
    string s;

    Console.Write(question);
    s = Console.ReadLine();
    
    return s;
}

public bool PromptYN(string question)
{
    Console.Write(question);
    return ReadYN();;
}

public bool IsPrime(this int num)
{
    bool prime;
    
    if (num <= 0 || num == 1)
        prime = false;
    else
    {
        int factors;

        for (int i = 1; i <= int.MaxValue; i++)
        {
            if (num % i == 0)
                factors++;
        }

        if (factors == 2)
            prime = true;
    }
    
    return prime;
}
use v6;

our Bool sub infix{'<|-'}($target is rw, &block)
{
    my $new = block();
    $target = $new;
    if $target eqv $new { return True; }
    else { fail "Target not set"; }
    CATCH { fail $!; }
}

our Bool sub infix:{'-|>'}(&block, $target is rw)
{
    return ($target <|- &block);
}

# test case
# should print:
# $x set to 22
# $x not set
my Int $x = 0;
if ($x <|- { 22 })
{
    "\$x set to $x".say;
    if (x <|- { 22 / 0 })
    {
        "\$x set to $x".say;
    }
    else
    {
        "\$x not set".say;
    }
}
else
{
    "\$x not set".say;   
}