WeixiangYan/CodeTransOcean huggingface dataset:
Task: Truncatable primes

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_PRIME 1000000
char *primes;
int n_primes;


void init_primes()
{
	int j;
	primes = malloc(sizeof(char) * MAX_PRIME);
	memset(primes, 1, MAX_PRIME);
	primes[0] = primes[1] = 0;
	int i = 2;
	while (i * i < MAX_PRIME) {
		for (j = i * 2; j < MAX_PRIME; j += i)
			primes[j] = 0;
		while (++i < MAX_PRIME && !primes[i]);
	}
}

int left_trunc(int n)
{
	int tens = 1;
	while (tens < n) tens *= 10;

	while (n) {
		if (!primes[n]) return 0;
		tens /= 10;
		if (n < tens) return 0;
		n %= tens;
	}
	return 1;
}

int right_trunc(int n)
{
	while (n) {
		if (!primes[n]) return 0;
		n /= 10;
	}
	return 1;
}

int main()
{
	int n;
	int max_left = 0, max_right = 0;
	init_primes();

	for (n = MAX_PRIME - 1; !max_left;  n -= 2)
		if (left_trunc(n)) max_left = n;

	for (n = MAX_PRIME - 1; !max_right; n -= 2)
		if (right_trunc(n)) max_right = n;

	printf("Left: %d; right: %d\n", max_left, max_right);
	return 0;
}










Fortran: module primes_mod
  implicit none
  
  logical, allocatable :: primes(:)
  
contains

subroutine Genprimes(parr)
  logical, intent(in out) :: parr(:)
  integer :: i

  parr = .true.
  parr (1) = .false.
  parr (4 : size(parr) : 2) = .false.
  do i = 3, int (sqrt (real (size(parr)))), 2
    if (parr(i)) parr(i * i : size(parr) : i) = .false.
  end do

end subroutine

function is_rtp(candidate)
  logical :: is_rtp
  integer, intent(in) :: candidate
  integer :: n

  is_rtp = .true.
  n = candidate / 10
  do while(n > 0)
    if(.not. primes(n)) then
      is_rtp = .false.
      return
    end if
    n = n / 10
  end do
  
end function

function is_ltp(candidate)
  logical :: is_ltp
  integer, intent(in) :: candidate
  integer :: i, n
  character(10) :: nstr

  write(nstr, "(i10)") candidate
  is_ltp = .true.
  do i = len_trim(nstr)-1, 1, -1
    n = mod(candidate, 10**i)
    if(.not. primes(n)) then
      is_ltp = .false.
      return
    end if
  end do
end function

end module primes_mod

program Truncatable_Primes
  use primes_mod
  implicit none
  
  integer, parameter :: limit = 999999
  integer :: i
  character(10) :: nstr
 

  allocate(primes(limit))
  call Genprimes(primes)
   

  do i = limit, 1, -1
    write(nstr, "(i10)") i
    if(index(trim(nstr), "0") /= 0) cycle      
    if(is_ltp(i)) then
      write(*, "(a, i0)") "Largest left truncatable prime below 1000000 is ", i
      exit
    end if
  end do


  do i = limit, 1, -1
    write(nstr, "(i10)") i
    if(index(trim(nstr), "0") /= 0) cycle      
    if(is_rtp(i)) then
      write(*, "(a, i0)") "Largest right truncatable prime below 1000000 is ", i
      exit
    end if
  end do
end program
Task: Truncatable primes




Go: package main

import "fmt"

func main() {
    sieve(1e6)
    if !search(6, 1e6, "left", func(n, pot int) int { return n % pot }) {
        panic("997?")
    }
    if !search(6, 1e6, "right", func(n, _ int) int { return n / 10 }) {
        panic("7393?")
    }
}

var c []bool

func sieve(ss int) {
    c = make([]bool, ss)
    c[1] = true
    for p := 2; ; {
        p2 := p * p
        if p2 >= ss {
            break
        }
        for i := p2; i < ss; i += p {
            c[i] = true
        }
        for {
            p++
            if !c[p] {
                break
            }
        }
    }
}

func search(digits, pot int, s string, truncFunc func(n, pot int) int) bool {
    n := pot - 1
    pot /= 10
smaller:
    for ; n >= pot; n -= 2 {
        for tn, tp := n, pot; tp > 0; tp /= 10 {
            if tn < tp || c[tn] {
                continue smaller
            }
            tn = truncFunc(tn, tp)
        }
        fmt.Println("max", s, "truncatable:", n)
        return true
    }
    if digits > 1 {
        return search(digits-1, pot, s, truncFunc)
    }
    return false
}







Fortran: module primes_mod
  implicit none
  
  logical, allocatable :: primes(:)
  
contains

subroutine Genprimes(parr)
  logical, intent(in out) :: parr(:)
  integer :: i

  parr = .true.
  parr (1) = .false.
  parr (4 : size(parr) : 2) = .false.
  do i = 3, int (sqrt (real (size(parr)))), 2
    if (parr(i)) parr(i * i : size(parr) : i) = .false.
  end do

end subroutine

function is_rtp(candidate)
  logical :: is_rtp
  integer, intent(in) :: candidate
  integer :: n

  is_rtp = .true.
  n = candidate / 10
  do while(n > 0)
    if(.not. primes(n)) then
      is_rtp = .false.
      return
    end if
    n = n / 10
  end do
  
end function

function is_ltp(candidate)
  logical :: is_ltp
  integer, intent(in) :: candidate
  integer :: i, n
  character(10) :: nstr

  write(nstr, "(i10)") candidate
  is_ltp = .true.
  do i = len_trim(nstr)-1, 1, -1
    n = mod(candidate, 10**i)
    if(.not. primes(n)) then
      is_ltp = .false.
      return
    end if
  end do
end function

end module primes_mod

program Truncatable_Primes
  use primes_mod
  implicit none
  
  integer, parameter :: limit = 999999
  integer :: i
  character(10) :: nstr
 

  allocate(primes(limit))
  call Genprimes(primes)
   

  do i = limit, 1, -1
    write(nstr, "(i10)") i
    if(index(trim(nstr), "0") /= 0) cycle      
    if(is_ltp(i)) then
      write(*, "(a, i0)") "Largest left truncatable prime below 1000000 is ", i
      exit
    end if
  end do


  do i = limit, 1, -1
    write(nstr, "(i10)") i
    if(index(trim(nstr), "0") /= 0) cycle      
    if(is_rtp(i)) then
      write(*, "(a, i0)") "Largest right truncatable prime below 1000000 is ", i
      exit
    end if
  end do
end program
Task: Truncatable primes





Java: import java.util.BitSet;

public class Main {

	public static void main(String[] args){

		final int MAX = 1000000;

		
		BitSet primeList = new BitSet(MAX>>1); 
		primeList.set(0,primeList.size(),true); 

		int sqroot = (int) Math.sqrt(MAX); 
		primeList.clear(0); 
		for(int num = 3; num <= sqroot; num+=2) 
		{ 
			if( primeList.get(num >> 1) ) 
			{ 
				int inc = num << 1;
				for(int factor = num * num; factor < MAX; factor += inc) 
				{ 
					
					
					primeList.clear(factor >> 1); 
					
				} 
			} 
		}
		

		
		int rightTrunc = -1, leftTrunc = -1;
		for(int prime = (MAX - 1) | 1; prime >= 3; prime -= 2)
		{
			if(primeList.get(prime>>1))
			{
				
				if(rightTrunc == -1)
				{
					int right = prime;
					while(right > 0 && right % 2 != 0 && primeList.get(right >> 1)) right /= 10;
					if(right == 0) rightTrunc = prime;
				}

				
				if(leftTrunc == -1 )
				{
					
					String left = Integer.toString(prime);
					if(!left.contains("0"))
					{
						while( left.length() > 0 ){
							int iLeft = Integer.parseInt(left);
							if(!primeList.get( iLeft >> 1)) break;
							left = left.substring(1);
						}
						if(left.length() == 0) leftTrunc = prime;
					}
				}
				if(leftTrunc != -1 && rightTrunc != -1) 
				{
					break;
				}
			}
		}
		System.out.println("Left  Truncatable : " + leftTrunc);
		System.out.println("Right Truncatable : " + rightTrunc);
	}
}






Fortran: module primes_mod
  implicit none
  
  logical, allocatable :: primes(:)
  
contains

subroutine Genprimes(parr)
  logical, intent(in out) :: parr(:)
  integer :: i

  parr = .true.
  parr (1) = .false.
  parr (4 : size(parr) : 2) = .false.
  do i = 3, int (sqrt (real (size(parr)))), 2
    if (parr(i)) parr(i * i : size(parr) : i) = .false.
  end do

end subroutine

function is_rtp(candidate)
  logical :: is_rtp
  integer, intent(in) :: candidate
  integer :: n

  is_rtp = .true.
  n = candidate / 10
  do while(n > 0)
    if(.not. primes(n)) then
      is_rtp = .false.
      return
    end if
    n = n / 10
  end do
  
end function

function is_ltp(candidate)
  logical :: is_ltp
  integer, intent(in) :: candidate
  integer :: i, n
  character(10) :: nstr

  write(nstr, "(i10)") candidate
  is_ltp = .true.
  do i = len_trim(nstr)-1, 1, -1
    n = mod(candidate, 10**i)
    if(.not. primes(n)) then
      is_ltp = .false.
      return
    end if
  end do
end function

end module primes_mod

program Truncatable_Primes
  use primes_mod
  implicit none
  
  integer, parameter :: limit = 999999
  integer :: i
  character(10) :: nstr
 

  allocate(primes(limit))
  call Genprimes(primes)
   

  do i = limit, 1, -1
    write(nstr, "(i10)") i
    if(index(trim(nstr), "0") /= 0) cycle      
    if(is_ltp(i)) then
      write(*, "(a, i0)") "Largest left truncatable prime below 1000000 is ", i
      exit
    end if
  end do


  do i = limit, 1, -1
    write(nstr, "(i10)") i
    if(index(trim(nstr), "0") /= 0) cycle      
    if(is_rtp(i)) then
      write(*, "(a, i0)") "Largest right truncatable prime below 1000000 is ", i
      exit
    end if
  end do
end program
Task: Truncatable primes







Python: maxprime = 1000000

def primes(n):
    multiples = set()
    prime = []
    for i in range(2, n+1):
        if i not in multiples:
            prime.append(i)
            multiples.update(set(range(i*i, n+1, i)))
    return prime

def truncatableprime(n):
    'Return a longest left and right truncatable primes below n'
    primelist = [str(x) for x in primes(n)[::-1]]
    primeset = set(primelist)
    for n in primelist:
        
        alltruncs = set(n[i:] for i in range(len(n)))
        if alltruncs.issubset(primeset):
            truncateleft = int(n)
            break
    for n in primelist:
        
        alltruncs = set([n[:i+1] for i in range(len(n))])
        if alltruncs.issubset(primeset):
            truncateright = int(n)
            break
    return truncateleft, truncateright

print(truncatableprime(maxprime))




Fortran: module primes_mod
  implicit none
  
  logical, allocatable :: primes(:)
  
contains

subroutine Genprimes(parr)
  logical, intent(in out) :: parr(:)
  integer :: i

  parr = .true.
  parr (1) = .false.
  parr (4 : size(parr) : 2) = .false.
  do i = 3, int (sqrt (real (size(parr)))), 2
    if (parr(i)) parr(i * i : size(parr) : i) = .false.
  end do

end subroutine

function is_rtp(candidate)
  logical :: is_rtp
  integer, intent(in) :: candidate
  integer :: n

  is_rtp = .true.
  n = candidate / 10
  do while(n > 0)
    if(.not. primes(n)) then
      is_rtp = .false.
      return
    end if
    n = n / 10
  end do
  
end function

function is_ltp(candidate)
  logical :: is_ltp
  integer, intent(in) :: candidate
  integer :: i, n
  character(10) :: nstr

  write(nstr, "(i10)") candidate
  is_ltp = .true.
  do i = len_trim(nstr)-1, 1, -1
    n = mod(candidate, 10**i)
    if(.not. primes(n)) then
      is_ltp = .false.
      return
    end if
  end do
end function

end module primes_mod

program Truncatable_Primes
  use primes_mod
  implicit none
  
  integer, parameter :: limit = 999999
  integer :: i
  character(10) :: nstr
 

  allocate(primes(limit))
  call Genprimes(primes)
   

  do i = limit, 1, -1
    write(nstr, "(i10)") i
    if(index(trim(nstr), "0") /= 0) cycle      
    if(is_ltp(i)) then
      write(*, "(a, i0)") "Largest left truncatable prime below 1000000 is ", i
      exit
    end if
  end do


  do i = limit, 1, -1
    write(nstr, "(i10)") i
    if(index(trim(nstr), "0") /= 0) cycle      
    if(is_rtp(i)) then
      write(*, "(a, i0)") "Largest right truncatable prime below 1000000 is ", i
      exit
    end if
  end do
end program
Task: Truncatable primes








VB: start_time = Now

lt = 0
rt = 0

For h = 1 To 1000000
	If IsLeftTruncatable(h) And h > lt Then
		lt = h
	End If
	If IsRightTruncatable(h) And h > rt Then
		rt = h
	End If
Next

end_time = now

WScript.StdOut.WriteLine "Largest LTP from 1..1000000: " & lt
WScript.StdOut.WriteLine "Largest RTP from 1..1000000: " & rt
WScript.StdOut.WriteLine "Elapse Time(seconds)       : " & DateDiff("s",start_time,end_time)


Function IsLeftTruncatable(n)
	IsLeftTruncatable = False
	c = 0
	For i = Len(n) To 1 Step -1
		If InStr(1,n,"0") > 0 Then
			Exit For
		End If
		If IsPrime(Right(n,i)) Then
			c = c + 1
		End If
	Next
	If c = Len(n) Then
		IsLeftTruncatable = True
	End If
End Function

Function IsRightTruncatable(n)
	IsRightTruncatable = False
	c = 0
	For i = Len(n) To 1 Step -1
		If InStr(1,n,"0") > 0 Then
			Exit For
		End If
		If IsPrime(Left(n,i)) Then
			c = c + 1
		End If
	Next
	If c = Len(n) Then
		IsRightTruncatable = True
	End If
End Function

Function IsPrime(n)
	If n = 2 Then
		IsPrime = True
	ElseIf n <= 1 Or n Mod 2 = 0 Then
		IsPrime = False
	Else
		IsPrime = True
		For i = 3 To Int(Sqr(n)) Step 2
			If n Mod i = 0 Then
				IsPrime = False
				Exit For
			End If
		Next
	End If
End Function



Fortran: module primes_mod
  implicit none
  
  logical, allocatable :: primes(:)
  
contains

subroutine Genprimes(parr)
  logical, intent(in out) :: parr(:)
  integer :: i

  parr = .true.
  parr (1) = .false.
  parr (4 : size(parr) : 2) = .false.
  do i = 3, int (sqrt (real (size(parr)))), 2
    if (parr(i)) parr(i * i : size(parr) : i) = .false.
  end do

end subroutine

function is_rtp(candidate)
  logical :: is_rtp
  integer, intent(in) :: candidate
  integer :: n

  is_rtp = .true.
  n = candidate / 10
  do while(n > 0)
    if(.not. primes(n)) then
      is_rtp = .false.
      return
    end if
    n = n / 10
  end do
  
end function

function is_ltp(candidate)
  logical :: is_ltp
  integer, intent(in) :: candidate
  integer :: i, n
  character(10) :: nstr

  write(nstr, "(i10)") candidate
  is_ltp = .true.
  do i = len_trim(nstr)-1, 1, -1
    n = mod(candidate, 10**i)
    if(.not. primes(n)) then
      is_ltp = .false.
      return
    end if
  end do
end function

end module primes_mod

program Truncatable_Primes
  use primes_mod
  implicit none
  
  integer, parameter :: limit = 999999
  integer :: i
  character(10) :: nstr
 

  allocate(primes(limit))
  call Genprimes(primes)
   

  do i = limit, 1, -1
    write(nstr, "(i10)") i
    if(index(trim(nstr), "0") /= 0) cycle      
    if(is_ltp(i)) then
      write(*, "(a, i0)") "Largest left truncatable prime below 1000000 is ", i
      exit
    end if
  end do


  do i = limit, 1, -1
    write(nstr, "(i10)") i
    if(index(trim(nstr), "0") /= 0) cycle      
    if(is_rtp(i)) then
      write(*, "(a, i0)") "Largest right truncatable prime below 1000000 is ", i
      exit
    end if
  end do
end program
Task: Singleton



C#: public sealed class Singleton1 
{
    private static Singleton1 instance;
    private static readonly object lockObj = new object();
    
    public static Singleton1 Instance {
        get {
            lock(lockObj) {
                if (instance == null) {
                    instance = new Singleton1();
                }
            }
            return instance;
        }
    }
}









Ada: package Global_Singleton is
   procedure Set_Data (Value : Integer);
   function Get_Data return Integer;
private
   type Instance_Type is record
      
      Data : Integer := 0;
   end record;
   Instance : Instance_Type;
end Global_Singleton;
Task: Singleton

C: #ifndef SILLY_H
#define SILLY_H

extern void JumpOverTheDog( int numberOfTimes);
extern int  PlayFetchWithDog( float weightOfStick);

#endif











Ada: package Global_Singleton is
   procedure Set_Data (Value : Integer);
   function Get_Data return Integer;
private
   type Instance_Type is record
      
      Data : Integer := 0;
   end record;
   Instance : Instance_Type;
end Global_Singleton;
Task: Singleton


C++: #include <stdexcept>

template <typename Self>
class singleton
{
protected:
	static Self*
		sentry;
public:	
	static Self& 
		instance()
	{
		return *sentry;
	}
	singleton()
	{
		if(sentry)
			throw std::logic_error("Error: attempt to instantiate a singleton over a pre-existing one!");
		sentry = (Self*)this;
	}
	virtual ~singleton()
	{
		if(sentry == this)
			sentry = 0;
	}
};
template <typename Self>
Self* 
	singleton<Self>::sentry = 0;



#include <iostream>
#include <string>

using namespace 
	std;

class controller : public singleton<controller>
{
public:
	controller(string const& name)
	: name(name)
	{
		trace("begin");
	}
	~controller()
	{
		trace("end");
	}
	void
		work()
	{
		trace("doing stuff");
	}
	void
		trace(string const& message)
	{
		cout << name << ": " << message << endl;
	}
	string
		name;
};
int
	main()
{
	controller*
		first = new controller("first");
	controller::instance().work();
	delete first;
	
	controller
		second("second");
	controller::instance().work();
	try
	{
	
		controller
			goner("goner");
		controller::instance().work();
	}
	catch(exception const& error)
	{
		cout << error.what() << endl; 
	}
	controller::instance().work();

	controller
		goner("goner");
	controller::instance().work();
}










Ada: package Global_Singleton is
   procedure Set_Data (Value : Integer);
   function Get_Data return Integer;
private
   type Instance_Type is record
      
      Data : Integer := 0;
   end record;
   Instance : Instance_Type;
end Global_Singleton;
Task: Singleton




Go: package main

import (
    "log"
    "math/rand"
    "sync"
    "time"
)

var (
    instance string
    once     sync.Once 
)

func claim(color string, w *sync.WaitGroup) {
    time.Sleep(time.Duration(rand.Intn(1e8))) 
    log.Println("trying to claim", color)
    once.Do(func() { instance = color })
    log.Printf("tried %s. instance: %s", color, instance)
    w.Done()
}

func main() {
    rand.Seed(time.Now().Unix())
    var w sync.WaitGroup
    w.Add(2)
    go claim("red", &w) 
    go claim("blue", &w)
    w.Wait()
    log.Println("after trying both, instance =", instance)
}








Ada: package Global_Singleton is
   procedure Set_Data (Value : Integer);
   function Get_Data return Integer;
private
   type Instance_Type is record
      
      Data : Integer := 0;
   end record;
   Instance : Instance_Type;
end Global_Singleton;
Task: Singleton





Java: class Singleton
{
    private static Singleton myInstance;
    public static Singleton getInstance()
    {
        if (myInstance == null)
        {
            synchronized(Singleton.class)
            {
                if (myInstance == null)
                {
                    myInstance = new Singleton();
                }
            }
        }

        return myInstance;
    }

    protected Singleton()
    {
        
    }

    
}







Ada: package Global_Singleton is
   procedure Set_Data (Value : Integer);
   function Get_Data return Integer;
private
   type Instance_Type is record
      
      Data : Integer := 0;
   end record;
   Instance : Instance_Type;
end Global_Singleton;
Task: Singleton







Python: >>> class Borg(object):
	__state = {}
	def __init__(self):
		self.__dict__ = self.__state
	

	
>>> b1 = Borg()
>>> b2 = Borg()
>>> b1 is b2
False
>>> b1.datum = range(5)
>>> b1.datum
[0, 1, 2, 3, 4]
>>> b2.datum
[0, 1, 2, 3, 4]
>>> b1.datum is b2.datum
True
>>> 





Ada: package Global_Singleton is
   procedure Set_Data (Value : Integer);
   function Get_Data return Integer;
private
   type Instance_Type is record
      
      Data : Integer := 0;
   end record;
   Instance : Instance_Type;
end Global_Singleton;
Task: Singleton

C: #ifndef SILLY_H
#define SILLY_H

extern void JumpOverTheDog( int numberOfTimes);
extern int  PlayFetchWithDog( float weightOfStick);

#endif
Task: Singleton



C#: public sealed class Singleton1 
{
    private static Singleton1 instance;
    private static readonly object lockObj = new object();
    
    public static Singleton1 Instance {
        get {
            lock(lockObj) {
                if (instance == null) {
                    instance = new Singleton1();
                }
            }
            return instance;
        }
    }
}
Task: Singleton


C++: #include <stdexcept>

template <typename Self>
class singleton
{
protected:
	static Self*
		sentry;
public:	
	static Self& 
		instance()
	{
		return *sentry;
	}
	singleton()
	{
		if(sentry)
			throw std::logic_error("Error: attempt to instantiate a singleton over a pre-existing one!");
		sentry = (Self*)this;
	}
	virtual ~singleton()
	{
		if(sentry == this)
			sentry = 0;
	}
};
template <typename Self>
Self* 
	singleton<Self>::sentry = 0;



#include <iostream>
#include <string>

using namespace 
	std;

class controller : public singleton<controller>
{
public:
	controller(string const& name)
	: name(name)
	{
		trace("begin");
	}
	~controller()
	{
		trace("end");
	}
	void
		work()
	{
		trace("doing stuff");
	}
	void
		trace(string const& message)
	{
		cout << name << ": " << message << endl;
	}
	string
		name;
};
int
	main()
{
	controller*
		first = new controller("first");
	controller::instance().work();
	delete first;
	
	controller
		second("second");
	controller::instance().work();
	try
	{
	
		controller
			goner("goner");
		controller::instance().work();
	}
	catch(exception const& error)
	{
		cout << error.what() << endl; 
	}
	controller::instance().work();

	controller
		goner("goner");
	controller::instance().work();
}
Task: Singleton





Java: class Singleton
{
    private static Singleton myInstance;
    public static Singleton getInstance()
    {
        if (myInstance == null)
        {
            synchronized(Singleton.class)
            {
                if (myInstance == null)
                {
                    myInstance = new Singleton();
                }
            }
        }

        return myInstance;
    }

    protected Singleton()
    {
        
    }

    
}
Task: Singleton







Python: >>> class Borg(object):
	__state = {}
	def __init__(self):
		self.__dict__ = self.__state
	

	
>>> b1 = Borg()
>>> b2 = Borg()
>>> b1 is b2
False
>>> b1.datum = range(5)
>>> b1.datum
[0, 1, 2, 3, 4]
>>> b2.datum
[0, 1, 2, 3, 4]
>>> b1.datum is b2.datum
True
>>>
Task: Singleton




Go: package main

import (
    "log"
    "math/rand"
    "sync"
    "time"
)

var (
    instance string
    once     sync.Once 
)

func claim(color string, w *sync.WaitGroup) {
    time.Sleep(time.Duration(rand.Intn(1e8))) 
    log.Println("trying to claim", color)
    once.Do(func() { instance = color })
    log.Printf("tried %s. instance: %s", color, instance)
    w.Done()
}

func main() {
    rand.Seed(time.Now().Unix())
    var w sync.WaitGroup
    w.Add(2)
    go claim("red", &w) 
    go claim("blue", &w)
    w.Wait()
    log.Println("after trying both, instance =", instance)
}
Task: Singleton






PHP: class Singleton {
  protected static $instance = null;
  public $test_var;
  private function __construct(){

  }
  public static function getInstance(){
    if (is_null(self::$instance)){
      self::$instance = new self();
    }
    return self::$instance;
  }
}

$foo = Singleton::getInstance();
$foo->test_var = 'One';

$bar = Singleton::getInstance();
echo $bar->test_var; //Prints 'One'

$fail = new Singleton(); //Fatal error






Ada: package Global_Singleton is
   procedure Set_Data (Value : Integer);
   function Get_Data return Integer;
private
   type Instance_Type is record
      
      Data : Integer := 0;
   end record;
   Instance : Instance_Type;
end Global_Singleton;
Task: Singleton






PHP: class Singleton {
  protected static $instance = null;
  public $test_var;
  private function __construct(){

  }
  public static function getInstance(){
    if (is_null(self::$instance)){
      self::$instance = new self();
    }
    return self::$instance;
  }
}

$foo = Singleton::getInstance();
$foo->test_var = 'One';

$bar = Singleton::getInstance();
echo $bar->test_var; //Prints 'One'

$fail = new Singleton(); //Fatal error
Task: Nested function



C#: string MakeList(string separator)
{
    int counter = 1;

    Func<string, string> makeItem = item => counter++ + separator + item + "\n";

    return makeItem("first") + makeItem("second") + makeItem("third");
}

Console.WriteLine(MakeList(". "));









Ada: with Ada.Text_IO;

procedure Nested_Functions is 
   
   type List is array(Natural range <>) of String(1 .. 10);
   
   function Make_List(Separator: String) return List is
      Counter: Natural := 0;
      
      function Make_Item(Item_Name: String) return String is
      begin
	 Counter := Counter + 1; 
	 return(Natural'Image(Counter) & Separator & Item_Name);
      end Make_Item;
      
   begin
      return (Make_Item("First "), Make_Item("Second"), Make_Item("Third "));
   end Make_List;
   
begin 
   for Item of Make_List(". ") loop
      Ada.Text_IO.Put_Line(Item);
   end loop;
end Nested_Functions;