WeixiangYan/CodeTransOcean huggingface dataset:
Task: Sum digits of an integer

C: #include <stdio.h>

int SumDigits(unsigned long long n, const int base) {
    int sum = 0;
    for (; n; n /= base)
    	sum += n % base;
    return sum;
}
 
int main() {
    printf("%d %d %d %d %d\n",
        SumDigits(1, 10),
        SumDigits(12345, 10),
        SumDigits(123045, 10),
        SumDigits(0xfe, 16),
        SumDigits(0xf0e, 16) );
    return 0;
}










Fortran: 























module base_mod
  private :: reverse
contains
  subroutine reverse(a)
    integer, dimension(:), intent(inout) :: a
    integer :: i, j, t
    do i=1,size(a)/2
       j = size(a) - i + 1
       t = a(i)
       a(i) = a(j)
       a(j) = t
    end do
  end subroutine reverse  

  function antibase(b, n) result(a)
    integer, intent(in) :: b,n
    integer, dimension(32) :: a
    integer :: m, i
    a = 0
    m = n
    i = 1
    do while (m .ne. 0)
       a(i) = mod(m, b)
       m = m/b
       i = i+1
    end do
    call reverse(a)
  end function antibase
end module base_mod

program digit_sum
  use base_mod
  call still
  call confused
contains
  subroutine still
    character(len=6),parameter :: fmt = '(i9,a)'
    print'(a9,a8)','digit sum','n'
    call process1(fmt,'1',10)
    call process1(fmt,'1234',10)
    call process1(fmt,'fe',16)
    call process1(fmt,'f0e',16)
  end subroutine still

  subroutine process1(fmt,s,b)
    character(len=*), intent(in) :: fmt, s
    integer, intent(in), optional :: b
    integer :: i
    print fmt,sum((/(index('123456789abcdef',s(i:i)),i=1,len(s))/)),' '//s
  end subroutine process1

  subroutine confused
    character(len=5),parameter :: fmt = '(3i7)'
    print*,'sum of digits of n expressed in base is...'
    print'(3a7)','n','base','sum'
    call process0(10,1,fmt)
    call process0(10,1234,fmt)
    call process0(16,254,fmt)
    call process0(16,3854,fmt)
  end subroutine confused

  subroutine process0(b,n,fmt)
    integer, intent(in) :: b, n
    character(len=*), intent(in) :: fmt
    print fmt,n,b,sum(antibase(b, n))
  end subroutine process0
end program digit_sum
Task: Sum digits of an integer





Java: import java.math.BigInteger;
public class SumDigits {
    public static int sumDigits(long num) {
	return sumDigits(num, 10);
    }
    public static int sumDigits(long num, int base) {
	String s = Long.toString(num, base);
	int result = 0;
	for (int i = 0; i < s.length(); i++)
	    result += Character.digit(s.charAt(i), base);
	return result;
    }
    public static int sumDigits(BigInteger num) {
	return sumDigits(num, 10);
    }
    public static int sumDigits(BigInteger num, int base) {
	String s = num.toString(base);
	int result = 0;
	for (int i = 0; i < s.length(); i++)
	    result += Character.digit(s.charAt(i), base);
	return result;
    }

    public static void main(String[] args) {
	System.out.println(sumDigits(1));
	System.out.println(sumDigits(12345));
	System.out.println(sumDigits(123045));
	System.out.println(sumDigits(0xfe, 16));
	System.out.println(sumDigits(0xf0e, 16));
	System.out.println(sumDigits(new BigInteger("12345678901234567890")));
    }
}






Fortran: 























module base_mod
  private :: reverse
contains
  subroutine reverse(a)
    integer, dimension(:), intent(inout) :: a
    integer :: i, j, t
    do i=1,size(a)/2
       j = size(a) - i + 1
       t = a(i)
       a(i) = a(j)
       a(j) = t
    end do
  end subroutine reverse  

  function antibase(b, n) result(a)
    integer, intent(in) :: b,n
    integer, dimension(32) :: a
    integer :: m, i
    a = 0
    m = n
    i = 1
    do while (m .ne. 0)
       a(i) = mod(m, b)
       m = m/b
       i = i+1
    end do
    call reverse(a)
  end function antibase
end module base_mod

program digit_sum
  use base_mod
  call still
  call confused
contains
  subroutine still
    character(len=6),parameter :: fmt = '(i9,a)'
    print'(a9,a8)','digit sum','n'
    call process1(fmt,'1',10)
    call process1(fmt,'1234',10)
    call process1(fmt,'fe',16)
    call process1(fmt,'f0e',16)
  end subroutine still

  subroutine process1(fmt,s,b)
    character(len=*), intent(in) :: fmt, s
    integer, intent(in), optional :: b
    integer :: i
    print fmt,sum((/(index('123456789abcdef',s(i:i)),i=1,len(s))/)),' '//s
  end subroutine process1

  subroutine confused
    character(len=5),parameter :: fmt = '(3i7)'
    print*,'sum of digits of n expressed in base is...'
    print'(3a7)','n','base','sum'
    call process0(10,1,fmt)
    call process0(10,1234,fmt)
    call process0(16,254,fmt)
    call process0(16,3854,fmt)
  end subroutine confused

  subroutine process0(b,n,fmt)
    integer, intent(in) :: b, n
    character(len=*), intent(in) :: fmt
    print fmt,n,b,sum(antibase(b, n))
  end subroutine process0
end program digit_sum
Task: Sum digits of an integer







Python: 




நிரல்பாகம் எண்_கூட்டல்( எண் )
  தொகை = 0
  @( எண் > 0 ) வரை
     d = எண்%10;
     பதிப்பி "digit = ",d
     எண் = (எண்-d)/10;
     தொகை  = தொகை  + d
  முடி
  பின்கொடு தொகை 
முடி


பதிப்பி எண்_கூட்டல்( 1289)
பதிப்பி எண்_கூட்டல்( 123456789)




Fortran: 























module base_mod
  private :: reverse
contains
  subroutine reverse(a)
    integer, dimension(:), intent(inout) :: a
    integer :: i, j, t
    do i=1,size(a)/2
       j = size(a) - i + 1
       t = a(i)
       a(i) = a(j)
       a(j) = t
    end do
  end subroutine reverse  

  function antibase(b, n) result(a)
    integer, intent(in) :: b,n
    integer, dimension(32) :: a
    integer :: m, i
    a = 0
    m = n
    i = 1
    do while (m .ne. 0)
       a(i) = mod(m, b)
       m = m/b
       i = i+1
    end do
    call reverse(a)
  end function antibase
end module base_mod

program digit_sum
  use base_mod
  call still
  call confused
contains
  subroutine still
    character(len=6),parameter :: fmt = '(i9,a)'
    print'(a9,a8)','digit sum','n'
    call process1(fmt,'1',10)
    call process1(fmt,'1234',10)
    call process1(fmt,'fe',16)
    call process1(fmt,'f0e',16)
  end subroutine still

  subroutine process1(fmt,s,b)
    character(len=*), intent(in) :: fmt, s
    integer, intent(in), optional :: b
    integer :: i
    print fmt,sum((/(index('123456789abcdef',s(i:i)),i=1,len(s))/)),' '//s
  end subroutine process1

  subroutine confused
    character(len=5),parameter :: fmt = '(3i7)'
    print*,'sum of digits of n expressed in base is...'
    print'(3a7)','n','base','sum'
    call process0(10,1,fmt)
    call process0(10,1234,fmt)
    call process0(16,254,fmt)
    call process0(16,3854,fmt)
  end subroutine confused

  subroutine process0(b,n,fmt)
    integer, intent(in) :: b, n
    character(len=*), intent(in) :: fmt
    print fmt,n,b,sum(antibase(b, n))
  end subroutine process0
end program digit_sum
Task: Sum digits of an integer








VB: function SumDigits(number, nBase)
    if number < 0 then number = -1 * number 
    if nBase < 2 then nBase = 2             
    sum = 0
    while number > 0
        sum = sum + (number mod nBase)
        number = int(number / nBase)
    wend
    SumDigits = sum
end function

print "The sums of the digits are:\n"
print "1    base 10 : "; SumDigits(1, 10)
print "1234 base 10 : "; SumDigits(1234, 10)
print "fe   base 16 : "; SumDigits(hexdec("FE"), 16)
print "f0e  base 16 : "; SumDigits(hexdec("F0E"), 16)
end



Fortran: 























module base_mod
  private :: reverse
contains
  subroutine reverse(a)
    integer, dimension(:), intent(inout) :: a
    integer :: i, j, t
    do i=1,size(a)/2
       j = size(a) - i + 1
       t = a(i)
       a(i) = a(j)
       a(j) = t
    end do
  end subroutine reverse  

  function antibase(b, n) result(a)
    integer, intent(in) :: b,n
    integer, dimension(32) :: a
    integer :: m, i
    a = 0
    m = n
    i = 1
    do while (m .ne. 0)
       a(i) = mod(m, b)
       m = m/b
       i = i+1
    end do
    call reverse(a)
  end function antibase
end module base_mod

program digit_sum
  use base_mod
  call still
  call confused
contains
  subroutine still
    character(len=6),parameter :: fmt = '(i9,a)'
    print'(a9,a8)','digit sum','n'
    call process1(fmt,'1',10)
    call process1(fmt,'1234',10)
    call process1(fmt,'fe',16)
    call process1(fmt,'f0e',16)
  end subroutine still

  subroutine process1(fmt,s,b)
    character(len=*), intent(in) :: fmt, s
    integer, intent(in), optional :: b
    integer :: i
    print fmt,sum((/(index('123456789abcdef',s(i:i)),i=1,len(s))/)),' '//s
  end subroutine process1

  subroutine confused
    character(len=5),parameter :: fmt = '(3i7)'
    print*,'sum of digits of n expressed in base is...'
    print'(3a7)','n','base','sum'
    call process0(10,1,fmt)
    call process0(10,1234,fmt)
    call process0(16,254,fmt)
    call process0(16,3854,fmt)
  end subroutine confused

  subroutine process0(b,n,fmt)
    integer, intent(in) :: b, n
    character(len=*), intent(in) :: fmt
    print fmt,n,b,sum(antibase(b, n))
  end subroutine process0
end program digit_sum
Task: Sum digits of an integer






PHP: <?php
function sumDigits($num, $base = 10) {
    $s = base_convert($num, 10, $base);
    foreach (str_split($s) as $c)
        $result += intval($c, $base);
    return $result;
}
echo sumDigits(1), "\n";
echo sumDigits(12345), "\n";
echo sumDigits(123045), "\n";
echo sumDigits(0xfe, 16), "\n";
echo sumDigits(0xf0e, 16), "\n";
?>





Fortran: 























module base_mod
  private :: reverse
contains
  subroutine reverse(a)
    integer, dimension(:), intent(inout) :: a
    integer :: i, j, t
    do i=1,size(a)/2
       j = size(a) - i + 1
       t = a(i)
       a(i) = a(j)
       a(j) = t
    end do
  end subroutine reverse  

  function antibase(b, n) result(a)
    integer, intent(in) :: b,n
    integer, dimension(32) :: a
    integer :: m, i
    a = 0
    m = n
    i = 1
    do while (m .ne. 0)
       a(i) = mod(m, b)
       m = m/b
       i = i+1
    end do
    call reverse(a)
  end function antibase
end module base_mod

program digit_sum
  use base_mod
  call still
  call confused
contains
  subroutine still
    character(len=6),parameter :: fmt = '(i9,a)'
    print'(a9,a8)','digit sum','n'
    call process1(fmt,'1',10)
    call process1(fmt,'1234',10)
    call process1(fmt,'fe',16)
    call process1(fmt,'f0e',16)
  end subroutine still

  subroutine process1(fmt,s,b)
    character(len=*), intent(in) :: fmt, s
    integer, intent(in), optional :: b
    integer :: i
    print fmt,sum((/(index('123456789abcdef',s(i:i)),i=1,len(s))/)),' '//s
  end subroutine process1

  subroutine confused
    character(len=5),parameter :: fmt = '(3i7)'
    print*,'sum of digits of n expressed in base is...'
    print'(3a7)','n','base','sum'
    call process0(10,1,fmt)
    call process0(10,1234,fmt)
    call process0(16,254,fmt)
    call process0(16,3854,fmt)
  end subroutine confused

  subroutine process0(b,n,fmt)
    integer, intent(in) :: b, n
    character(len=*), intent(in) :: fmt
    print fmt,n,b,sum(antibase(b, n))
  end subroutine process0
end program digit_sum
Task: Sum digits of an integer






PHP: <?php
function sumDigits($num, $base = 10) {
    $s = base_convert($num, 10, $base);
    foreach (str_split($s) as $c)
        $result += intval($c, $base);
    return $result;
}
echo sumDigits(1), "\n";
echo sumDigits(12345), "\n";
echo sumDigits(123045), "\n";
echo sumDigits(0xfe, 16), "\n";
echo sumDigits(0xf0e, 16), "\n";
?>
Task: Sum digits of an integer






PHP: <?php
function sumDigits($num, $base = 10) {
    $s = base_convert($num, 10, $base);
    foreach (str_split($s) as $c)
        $result += intval($c, $base);
    return $result;
}
echo sumDigits(1), "\n";
echo sumDigits(12345), "\n";
echo sumDigits(123045), "\n";
echo sumDigits(0xfe, 16), "\n";
echo sumDigits(0xf0e, 16), "\n";
?>






Ada: with Ada.Integer_Text_IO;

procedure Sum_Digits is
   
   

   function Sum_Of_Digits(N: Natural; Base: Natural := 10) return Natural is
      Sum: Natural := 0;
      Val: Natural := N;
   begin
      while Val > 0 loop
         Sum := Sum + (Val mod Base);
         Val := Val / Base;
      end loop;
      return Sum;
   end Sum_Of_Digits;

   use Ada.Integer_Text_IO;

begin 
   Put(Sum_OF_Digits(1));            
   Put(Sum_OF_Digits(12345));        
   Put(Sum_OF_Digits(123045));       
   Put(Sum_OF_Digits(123045,  50));  
   Put(Sum_OF_Digits(16#fe#,  10));  
   Put(Sum_OF_Digits(16#fe#,  16));  
   Put(Sum_OF_Digits(16#f0e#, 16));  
end Sum_Digits;
Task: Sum digits of an integer








VB: Function sumDigits(num As Variant, base As Long) As Long
    
    Dim outp As Long
    Dim validNums As String, tmp As Variant, x As Long, lennum As Long
    
    validNums = Left$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", base)
    lennum = Len(num)
    For L0 = lennum To 1 Step -1
        x = InStr(validNums, Mid$(num, L0, 1)) - 1
        If -1 = x Then Exit Function
        tmp = tmp + (x * (base ^ (lennum - L0)))
    Next
    While tmp
        outp = outp + (tmp Mod base)
        tmp = tmp \ base
    Wend
    sumDigits = outp
End Function

Sub tester()
    Debug.Print sumDigits(1, 10)
    Debug.Print sumDigits(1234, 10)
    Debug.Print sumDigits(&HFE, 16)
    Debug.Print sumDigits(&HF0E, 16)
    Debug.Print sumDigits("2", 2)
End Sub
Task: Apply a callback to an array



C#: int[] intArray = { 1, 2, 3, 4, 5 };

int[] squares1 = intArray.Select(x => x * x).ToArray();


int[] squares2 = (from x in intArray
                  select x * x).ToArray();


foreach (var i in intArray)
    Console.WriteLine(i * i);









Ada: with Ada.Text_Io;
 with Ada.Integer_text_IO;
 
 procedure Call_Back_Example is
    
    
   
    
    procedure Display(Location : Positive; Value : Integer) is
    begin
       Ada.Text_Io.Put("array(");
       Ada.Integer_Text_Io.Put(Item => Location, Width => 1);
       Ada.Text_Io.Put(") = ");
       Ada.Integer_Text_Io.Put(Item => Value * Value, Width => 1);
       Ada.Text_Io.New_Line;
    end Display;
   
    
    type Call_Back_Access is access procedure(L : Positive; V : Integer);
   
    
    type Value_Array is array(Positive range <>) of Integer;
   
    
    procedure Map(Values : Value_Array; Worker : Call_Back_Access) is
    begin
       for I in Values'range loop
          Worker(I, Values(I));
       end loop;
    end Map;
   
    
    Sample : Value_Array := (5,4,3,2,1);
   
 begin
    Map(Sample, Display'access);   
 end Call_Back_Example;
Task: Apply a callback to an array

C: #ifndef CALLBACK_H
#define CALLBACK_H


void map(int* array, int len, void(*callback)(int,int));

#endif











Ada: with Ada.Text_Io;
 with Ada.Integer_text_IO;
 
 procedure Call_Back_Example is
    
    
   
    
    procedure Display(Location : Positive; Value : Integer) is
    begin
       Ada.Text_Io.Put("array(");
       Ada.Integer_Text_Io.Put(Item => Location, Width => 1);
       Ada.Text_Io.Put(") = ");
       Ada.Integer_Text_Io.Put(Item => Value * Value, Width => 1);
       Ada.Text_Io.New_Line;
    end Display;
   
    
    type Call_Back_Access is access procedure(L : Positive; V : Integer);
   
    
    type Value_Array is array(Positive range <>) of Integer;
   
    
    procedure Map(Values : Value_Array; Worker : Call_Back_Access) is
    begin
       for I in Values'range loop
          Worker(I, Values(I));
       end loop;
    end Map;
   
    
    Sample : Value_Array := (5,4,3,2,1);
   
 begin
    Map(Sample, Display'access);   
 end Call_Back_Example;
Task: Apply a callback to an array


C++: #include <iostream> 
#include <algorithm> 


void print_square(int i) {
  std::cout << i*i << " ";
}

int main() {
  
  int ary[]={1,2,3,4,5};
  
  std::for_each(ary,ary+5,print_square);
  return 0;
}











Ada: with Ada.Text_Io;
 with Ada.Integer_text_IO;
 
 procedure Call_Back_Example is
    
    
   
    
    procedure Display(Location : Positive; Value : Integer) is
    begin
       Ada.Text_Io.Put("array(");
       Ada.Integer_Text_Io.Put(Item => Location, Width => 1);
       Ada.Text_Io.Put(") = ");
       Ada.Integer_Text_Io.Put(Item => Value * Value, Width => 1);
       Ada.Text_Io.New_Line;
    end Display;
   
    
    type Call_Back_Access is access procedure(L : Positive; V : Integer);
   
    
    type Value_Array is array(Positive range <>) of Integer;
   
    
    procedure Map(Values : Value_Array; Worker : Call_Back_Access) is
    begin
       for I in Values'range loop
          Worker(I, Values(I));
       end loop;
    end Map;
   
    
    Sample : Value_Array := (5,4,3,2,1);
   
 begin
    Map(Sample, Display'access);   
 end Call_Back_Example;
Task: Apply a callback to an array




Go: package main

import "fmt"

func main() {
    for _, i := range []int{1, 2, 3, 4, 5} {
        fmt.Println(i * i)
    }
}








Ada: with Ada.Text_Io;
 with Ada.Integer_text_IO;
 
 procedure Call_Back_Example is
    
    
   
    
    procedure Display(Location : Positive; Value : Integer) is
    begin
       Ada.Text_Io.Put("array(");
       Ada.Integer_Text_Io.Put(Item => Location, Width => 1);
       Ada.Text_Io.Put(") = ");
       Ada.Integer_Text_Io.Put(Item => Value * Value, Width => 1);
       Ada.Text_Io.New_Line;
    end Display;
   
    
    type Call_Back_Access is access procedure(L : Positive; V : Integer);
   
    
    type Value_Array is array(Positive range <>) of Integer;
   
    
    procedure Map(Values : Value_Array; Worker : Call_Back_Access) is
    begin
       for I in Values'range loop
          Worker(I, Values(I));
       end loop;
    end Map;
   
    
    Sample : Value_Array := (5,4,3,2,1);
   
 begin
    Map(Sample, Display'access);   
 end Call_Back_Example;
Task: Apply a callback to an array





Java: public class ArrayCallback7 {

    interface IntConsumer {
        void run(int x);
    }

    interface IntToInt {
        int run(int x);
    }

    static void forEach(int[] arr, IntConsumer consumer) {
        for (int i : arr) {
            consumer.run(i);
        }
    }

    static void update(int[] arr, IntToInt mapper) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = mapper.run(arr[i]);
        }
    }

    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        forEach(numbers, new IntConsumer() {
            public void run(int x) {
                System.out.println(x);
            }
        });

        update(numbers, new IntToInt() {
            @Override
            public int run(int x) {
                return x * x;
            }
        });

        forEach(numbers, new IntConsumer() {
            public void run(int x) {
                System.out.println(x);
            }
        });
    }
}







Ada: with Ada.Text_Io;
 with Ada.Integer_text_IO;
 
 procedure Call_Back_Example is
    
    
   
    
    procedure Display(Location : Positive; Value : Integer) is
    begin
       Ada.Text_Io.Put("array(");
       Ada.Integer_Text_Io.Put(Item => Location, Width => 1);
       Ada.Text_Io.Put(") = ");
       Ada.Integer_Text_Io.Put(Item => Value * Value, Width => 1);
       Ada.Text_Io.New_Line;
    end Display;
   
    
    type Call_Back_Access is access procedure(L : Positive; V : Integer);
   
    
    type Value_Array is array(Positive range <>) of Integer;
   
    
    procedure Map(Values : Value_Array; Worker : Call_Back_Access) is
    begin
       for I in Values'range loop
          Worker(I, Values(I));
       end loop;
    end Map;
   
    
    Sample : Value_Array := (5,4,3,2,1);
   
 begin
    Map(Sample, Display'access);   
 end Call_Back_Example;
Task: Apply a callback to an array







Python: def square(n):
    return n * n
  
numbers = [1, 3, 5, 7]

squares1 = [square(n) for n in numbers]     

squares2a = map(square, numbers)            

squares2b = map(lambda x: x*x, numbers)     

squares3 = [n * n for n in numbers]         
                                            

isquares1 = (n * n for n in numbers)        

import itertools
isquares2 = itertools.imap(square, numbers) 





Ada: with Ada.Text_Io;
 with Ada.Integer_text_IO;
 
 procedure Call_Back_Example is
    
    
   
    
    procedure Display(Location : Positive; Value : Integer) is
    begin
       Ada.Text_Io.Put("array(");
       Ada.Integer_Text_Io.Put(Item => Location, Width => 1);
       Ada.Text_Io.Put(") = ");
       Ada.Integer_Text_Io.Put(Item => Value * Value, Width => 1);
       Ada.Text_Io.New_Line;
    end Display;
   
    
    type Call_Back_Access is access procedure(L : Positive; V : Integer);
   
    
    type Value_Array is array(Positive range <>) of Integer;
   
    
    procedure Map(Values : Value_Array; Worker : Call_Back_Access) is
    begin
       for I in Values'range loop
          Worker(I, Values(I));
       end loop;
    end Map;
   
    
    Sample : Value_Array := (5,4,3,2,1);
   
 begin
    Map(Sample, Display'access);   
 end Call_Back_Example;
Task: Apply a callback to an array








VB: Option Explicit

Sub Main()
Dim arr, i
    
    arr = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

    
    For i = LBound(arr) To UBound(arr): arr(i) = Fibonacci(arr(i)): Next
    
    
    Debug.Print Join(arr, ", ")
End Sub

Private Function Fibonacci(N) As Variant
    If N <= 1 Then
        Fibonacci = N
    Else
        Fibonacci = Fibonacci(N - 1) + Fibonacci(N - 2)
    End If
End Function




Ada: with Ada.Text_Io;
 with Ada.Integer_text_IO;
 
 procedure Call_Back_Example is
    
    
   
    
    procedure Display(Location : Positive; Value : Integer) is
    begin
       Ada.Text_Io.Put("array(");
       Ada.Integer_Text_Io.Put(Item => Location, Width => 1);
       Ada.Text_Io.Put(") = ");
       Ada.Integer_Text_Io.Put(Item => Value * Value, Width => 1);
       Ada.Text_Io.New_Line;
    end Display;
   
    
    type Call_Back_Access is access procedure(L : Positive; V : Integer);
   
    
    type Value_Array is array(Positive range <>) of Integer;
   
    
    procedure Map(Values : Value_Array; Worker : Call_Back_Access) is
    begin
       for I in Values'range loop
          Worker(I, Values(I));
       end loop;
    end Map;
   
    
    Sample : Value_Array := (5,4,3,2,1);
   
 begin
    Map(Sample, Display'access);   
 end Call_Back_Example;
Task: Apply a callback to an array

C: #ifndef CALLBACK_H
#define CALLBACK_H


void map(int* array, int len, void(*callback)(int,int));

#endif
Task: Apply a callback to an array



C#: int[] intArray = { 1, 2, 3, 4, 5 };

int[] squares1 = intArray.Select(x => x * x).ToArray();


int[] squares2 = (from x in intArray
                  select x * x).ToArray();


foreach (var i in intArray)
    Console.WriteLine(i * i);
Task: Apply a callback to an array


C++: #include <iostream> 
#include <algorithm> 


void print_square(int i) {
  std::cout << i*i << " ";
}

int main() {
  
  int ary[]={1,2,3,4,5};
  
  std::for_each(ary,ary+5,print_square);
  return 0;
}
Task: Apply a callback to an array





Java: public class ArrayCallback7 {

    interface IntConsumer {
        void run(int x);
    }

    interface IntToInt {
        int run(int x);
    }

    static void forEach(int[] arr, IntConsumer consumer) {
        for (int i : arr) {
            consumer.run(i);
        }
    }

    static void update(int[] arr, IntToInt mapper) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = mapper.run(arr[i]);
        }
    }

    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        forEach(numbers, new IntConsumer() {
            public void run(int x) {
                System.out.println(x);
            }
        });

        update(numbers, new IntToInt() {
            @Override
            public int run(int x) {
                return x * x;
            }
        });

        forEach(numbers, new IntConsumer() {
            public void run(int x) {
                System.out.println(x);
            }
        });
    }
}
Task: Apply a callback to an array







Python: def square(n):
    return n * n
  
numbers = [1, 3, 5, 7]

squares1 = [square(n) for n in numbers]     

squares2a = map(square, numbers)            

squares2b = map(lambda x: x*x, numbers)     

squares3 = [n * n for n in numbers]         
                                            

isquares1 = (n * n for n in numbers)        

import itertools
isquares2 = itertools.imap(square, numbers)