WeixiangYan/CodeTransOcean huggingface dataset:
Task: Entropy







Python: from __future__ import division
import math

def hist(source):
    hist = {}; l = 0;
    for e in source:
        l += 1
        if e not in hist:
            hist[e] = 0
        hist[e] += 1
    return (l,hist)

def entropy(hist,l):
    elist = []
    for v in hist.values():
        c = v / l
        elist.append(-c * math.log(c ,2))
    return sum(elist)

def printHist(h):
    flip = lambda (k,v) : (v,k)
    h = sorted(h.iteritems(), key = flip)
    print 'Sym\thi\tfi\tInf'
    for (k,v) in h:
        print '%s\t%f\t%f\t%f'%(k,v,v/l,-math.log(v/l, 2))
    
    

source = "1223334444"
(l,h) = hist(source);
print '.[Results].'
print 'Length',l
print 'Entropy:', entropy(h, l)
printHist(h)




Fortran: 








program shannonEntropy
  implicit none
  integer :: num, L, status
  character(len=2048) :: s
  num = 1
  call get_command_argument(num, s, L, status)
  if ((0 /= status) .or. (L .eq. 0)) then
    write(0,*)'Expected a command line argument with some length.'
  else
    write(6,*)'Shannon entropy of '//(s(1:L))//' is ', se(s(1:L))
  endif

contains
  
  
  
  
  

  
  
  
  
  
  real function se(s)
    implicit none
    character(len=*), intent(in) :: s
    integer, dimension(256) :: tallies
    real, dimension(256) :: norm
    tallies = 0
    call TallyKey(s, tallies)
    
    
    norm = tallies / real(len(s))
    se = sum(-(norm*log(merge(1.0, norm, norm .eq. 0))/log(2.0)))
  end function se

  subroutine TallyKey(s, counts)
    character(len=*), intent(in) :: s
    integer, dimension(256), intent(out) :: counts
    integer :: i, j
    counts = 0
    do i=1,len(s)
      j = iachar(s(i:i))
      counts(j) = counts(j) + 1
    end do
  end subroutine TallyKey

end program shannonEntropy
Task: Entropy






PHP: <?php

function shannonEntropy($string) {
    $h = 0.0;
    $len = strlen($string);
    foreach (count_chars($string, 1) as $count) {
        $h -= (double) ($count / $len) * log((double) ($count / $len), 2);
    }
    return $h;
}

$strings = array(
    '1223334444',
    '1225554444',
    'aaBBcccDDDD',
    '122333444455555',
    'Rosetta Code',
    '1234567890abcdefghijklmnopqrstuvwxyz',
);

foreach ($strings AS $string) {
    printf(
        '%36s : %s' . PHP_EOL,
        $string,
        number_format(shannonEntropy($string), 6)
    );
}





Fortran: 








program shannonEntropy
  implicit none
  integer :: num, L, status
  character(len=2048) :: s
  num = 1
  call get_command_argument(num, s, L, status)
  if ((0 /= status) .or. (L .eq. 0)) then
    write(0,*)'Expected a command line argument with some length.'
  else
    write(6,*)'Shannon entropy of '//(s(1:L))//' is ', se(s(1:L))
  endif

contains
  
  
  
  
  

  
  
  
  
  
  real function se(s)
    implicit none
    character(len=*), intent(in) :: s
    integer, dimension(256) :: tallies
    real, dimension(256) :: norm
    tallies = 0
    call TallyKey(s, tallies)
    
    
    norm = tallies / real(len(s))
    se = sum(-(norm*log(merge(1.0, norm, norm .eq. 0))/log(2.0)))
  end function se

  subroutine TallyKey(s, counts)
    character(len=*), intent(in) :: s
    integer, dimension(256), intent(out) :: counts
    integer :: i, j
    counts = 0
    do i=1,len(s)
      j = iachar(s(i:i))
      counts(j) = counts(j) + 1
    end do
  end subroutine TallyKey

end program shannonEntropy
Task: Entropy






PHP: <?php

function shannonEntropy($string) {
    $h = 0.0;
    $len = strlen($string);
    foreach (count_chars($string, 1) as $count) {
        $h -= (double) ($count / $len) * log((double) ($count / $len), 2);
    }
    return $h;
}

$strings = array(
    '1223334444',
    '1225554444',
    'aaBBcccDDDD',
    '122333444455555',
    'Rosetta Code',
    '1234567890abcdefghijklmnopqrstuvwxyz',
);

foreach ($strings AS $string) {
    printf(
        '%36s : %s' . PHP_EOL,
        $string,
        number_format(shannonEntropy($string), 6)
    );
}
Task: Entropy






PHP: <?php

function shannonEntropy($string) {
    $h = 0.0;
    $len = strlen($string);
    foreach (count_chars($string, 1) as $count) {
        $h -= (double) ($count / $len) * log((double) ($count / $len), 2);
    }
    return $h;
}

$strings = array(
    '1223334444',
    '1225554444',
    'aaBBcccDDDD',
    '122333444455555',
    'Rosetta Code',
    '1234567890abcdefghijklmnopqrstuvwxyz',
);

foreach ($strings AS $string) {
    printf(
        '%36s : %s' . PHP_EOL,
        $string,
        number_format(shannonEntropy($string), 6)
    );
}






Ada: with Ada.Text_IO, Ada.Float_Text_IO, Ada.Numerics.Elementary_Functions;

procedure Count_Entropy is

   package TIO renames Ada.Text_IO;

   Count: array(Character) of Natural := (others => 0);
   Sum:   Natural := 0;
   Line: String := "1223334444";

begin
   for I in Line'Range loop   
      Count(Line(I)) := Count(Line(I))+1;
      Sum := Sum + 1;
   end loop;

   declare   
      function P(C: Character) return Float is (Float(Count(C)) / Float(Sum));
      use Ada.Numerics.Elementary_Functions, Ada.Float_Text_IO;
      Result: Float := 0.0;
   begin
      for Ch in Character loop
         Result := Result -
          (if P(Ch)=0.0 then 0.0 else P(Ch) * Log(P(Ch), Base => 2.0));
      end loop;
      Put(Result, Fore => 1, Aft => 5, Exp => 0);
   end;
end Count_Entropy;
Task: Host introspection



C#: static void Main()
{
  Console.WriteLine("Word size = {0} bytes,",sizeof(int));

  if (BitConverter.IsLittleEndian)
    Console.WriteLine("Little-endian.");
  else
    Console.WriteLine("Big-endian.");
}









Ada: with Ada.Text_IO;  use Ada.Text_IO;
with System;       use System;

procedure Host_Introspection is
begin
   Put_Line ("Word size" & Integer'Image (Word_Size));
   Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
end Host_Introspection;
Task: Host introspection

C: #include <stdio.h>
#include <stddef.h> 
#include <limits.h> 

int main() {
    int one = 1;

    
    printf("word size = %d bits\n", (int)(CHAR_BIT * sizeof(size_t)));

    
    if (*(char *)&one)
        printf("little endian\n");
    else
        printf("big endian\n");
    return 0;
}











Ada: with Ada.Text_IO;  use Ada.Text_IO;
with System;       use System;

procedure Host_Introspection is
begin
   Put_Line ("Word size" & Integer'Image (Word_Size));
   Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
end Host_Introspection;
Task: Host introspection


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

int main()
{
    std::cout << "int is " << sizeof(int) << " bytes\n";
    std::cout << "a pointer is " << sizeof(int*) << " bytes\n\n";

    if (std::endian::native == std::endian::big)
    {
        std::cout << "platform is big-endian\n";
    }
    else
    {
        std::cout << "host is little-endian\n";
    }
}










Ada: with Ada.Text_IO;  use Ada.Text_IO;
with System;       use System;

procedure Host_Introspection is
begin
   Put_Line ("Word size" & Integer'Image (Word_Size));
   Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
end Host_Introspection;
Task: Host introspection




Go: package main

import (
	"fmt"
	"io/ioutil"
	"runtime"
	"strconv"
	"strings"
	"unsafe"
)

func main() {
	fmt.Println(runtime.Version(), runtime.GOOS, runtime.GOARCH)

	
	x := uint32(0x01020304)
	switch *(*byte)(unsafe.Pointer(&x)) {
	case 0x01:
		fmt.Println("big endian")
	case 0x04:
		fmt.Println("little endian")
	default:
		fmt.Println("mixed endian?")
	}

	
	

	
	fmt.Println("         strconv.IntSize =", strconv.IntSize)
	
	intSize := 32 << uint(^uint(0)>>63)
	fmt.Println("32 << uint(^uint(0)>>63) =", intSize)

	
	
	
	
	const bitsPerByte = 8
	fmt.Println("  sizeof(int)     in bits:", unsafe.Sizeof(int(0))*bitsPerByte)
	fmt.Println("  sizeof(uintptr) in bits:", unsafe.Sizeof(uintptr(0))*bitsPerByte)
	
	
	archSize := unsafe.Sizeof(int(0))
	if psize := unsafe.Sizeof(uintptr(0)); psize > archSize {
		archSize = psize
	}
	fmt.Println("  compiled with word size:", archSize*bitsPerByte)

	
	
	
	c, err := ioutil.ReadFile("/proc/cpuinfo")
	if err != nil {
		fmt.Println(err)
		return
	}
	ls := strings.Split(string(c), "\n")
	for _, l := range ls {
		if strings.HasPrefix(l, "flags") {
			for _, f := range strings.Fields(l) {
				if f == "lm" { 
					fmt.Println("64 bit word size")
					return
				}
			}
			fmt.Println("32 bit word size")
			return
		}
	}
}








Ada: with Ada.Text_IO;  use Ada.Text_IO;
with System;       use System;

procedure Host_Introspection is
begin
   Put_Line ("Word size" & Integer'Image (Word_Size));
   Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
end Host_Introspection;
Task: Host introspection





Java: import java.nio.ByteOrder;

public class ShowByteOrder {
    public static void main(String[] args) {
        
        System.out.println(ByteOrder.nativeOrder());
    }
}







Ada: with Ada.Text_IO;  use Ada.Text_IO;
with System;       use System;

procedure Host_Introspection is
begin
   Put_Line ("Word size" & Integer'Image (Word_Size));
   Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
end Host_Introspection;
Task: Host introspection







Python: >>> import platform, sys, socket
>>> platform.architecture()
('64bit', 'ELF')
>>> platform.machine()
'x86_64'
>>> platform.node()
'yourhostname'
>>> platform.system()
'Linux'
>>> sys.byteorder
little
>>> socket.gethostname()
'yourhostname'
>>>





Ada: with Ada.Text_IO;  use Ada.Text_IO;
with System;       use System;

procedure Host_Introspection is
begin
   Put_Line ("Word size" & Integer'Image (Word_Size));
   Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
end Host_Introspection;
Task: Host introspection



C#: static void Main()
{
  Console.WriteLine("Word size = {0} bytes,",sizeof(int));

  if (BitConverter.IsLittleEndian)
    Console.WriteLine("Little-endian.");
  else
    Console.WriteLine("Big-endian.");
}
Task: Host introspection


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

int main()
{
    std::cout << "int is " << sizeof(int) << " bytes\n";
    std::cout << "a pointer is " << sizeof(int*) << " bytes\n\n";

    if (std::endian::native == std::endian::big)
    {
        std::cout << "platform is big-endian\n";
    }
    else
    {
        std::cout << "host is little-endian\n";
    }
}
Task: Host introspection





Java: import java.nio.ByteOrder;

public class ShowByteOrder {
    public static void main(String[] args) {
        
        System.out.println(ByteOrder.nativeOrder());
    }
}
Task: Host introspection







Python: >>> import platform, sys, socket
>>> platform.architecture()
('64bit', 'ELF')
>>> platform.machine()
'x86_64'
>>> platform.node()
'yourhostname'
>>> platform.system()
'Linux'
>>> sys.byteorder
little
>>> socket.gethostname()
'yourhostname'
>>>
Task: Host introspection




Go: package main

import (
	"fmt"
	"io/ioutil"
	"runtime"
	"strconv"
	"strings"
	"unsafe"
)

func main() {
	fmt.Println(runtime.Version(), runtime.GOOS, runtime.GOARCH)

	
	x := uint32(0x01020304)
	switch *(*byte)(unsafe.Pointer(&x)) {
	case 0x01:
		fmt.Println("big endian")
	case 0x04:
		fmt.Println("little endian")
	default:
		fmt.Println("mixed endian?")
	}

	
	

	
	fmt.Println("         strconv.IntSize =", strconv.IntSize)
	
	intSize := 32 << uint(^uint(0)>>63)
	fmt.Println("32 << uint(^uint(0)>>63) =", intSize)

	
	
	
	
	const bitsPerByte = 8
	fmt.Println("  sizeof(int)     in bits:", unsafe.Sizeof(int(0))*bitsPerByte)
	fmt.Println("  sizeof(uintptr) in bits:", unsafe.Sizeof(uintptr(0))*bitsPerByte)
	
	
	archSize := unsafe.Sizeof(int(0))
	if psize := unsafe.Sizeof(uintptr(0)); psize > archSize {
		archSize = psize
	}
	fmt.Println("  compiled with word size:", archSize*bitsPerByte)

	
	
	
	c, err := ioutil.ReadFile("/proc/cpuinfo")
	if err != nil {
		fmt.Println(err)
		return
	}
	ls := strings.Split(string(c), "\n")
	for _, l := range ls {
		if strings.HasPrefix(l, "flags") {
			for _, f := range strings.Fields(l) {
				if f == "lm" { 
					fmt.Println("64 bit word size")
					return
				}
			}
			fmt.Println("32 bit word size")
			return
		}
	}
}
Task: Host introspection

C: #include <stdio.h>
#include <stddef.h> 
#include <limits.h> 

int main() {
    int one = 1;

    
    printf("word size = %d bits\n", (int)(CHAR_BIT * sizeof(size_t)));

    
    if (*(char *)&one)
        printf("little endian\n");
    else
        printf("big endian\n");
    return 0;
}
Task: Host introspection



C#: static void Main()
{
  Console.WriteLine("Word size = {0} bytes,",sizeof(int));

  if (BitConverter.IsLittleEndian)
    Console.WriteLine("Little-endian.");
  else
    Console.WriteLine("Big-endian.");
}








Fortran:    integer :: i
   character(len=1) :: c(20)
   equivalence (c, i)

   WRITE(*,*) bit_size(1)  
                           
   i = 1

   IF (ichar(c(1)) == 0) THEN
      WRITE(*,*) "Big Endian"
   ELSE
     WRITE(*,*) "Little Endian"
   END IF
Task: Host introspection


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

int main()
{
    std::cout << "int is " << sizeof(int) << " bytes\n";
    std::cout << "a pointer is " << sizeof(int*) << " bytes\n\n";

    if (std::endian::native == std::endian::big)
    {
        std::cout << "platform is big-endian\n";
    }
    else
    {
        std::cout << "host is little-endian\n";
    }
}









Fortran:    integer :: i
   character(len=1) :: c(20)
   equivalence (c, i)

   WRITE(*,*) bit_size(1)  
                           
   i = 1

   IF (ichar(c(1)) == 0) THEN
      WRITE(*,*) "Big Endian"
   ELSE
     WRITE(*,*) "Little Endian"
   END IF
Task: Host introspection

C: #include <stdio.h>
#include <stddef.h> 
#include <limits.h> 

int main() {
    int one = 1;

    
    printf("word size = %d bits\n", (int)(CHAR_BIT * sizeof(size_t)));

    
    if (*(char *)&one)
        printf("little endian\n");
    else
        printf("big endian\n");
    return 0;
}










Fortran:    integer :: i
   character(len=1) :: c(20)
   equivalence (c, i)

   WRITE(*,*) bit_size(1)  
                           
   i = 1

   IF (ichar(c(1)) == 0) THEN
      WRITE(*,*) "Big Endian"
   ELSE
     WRITE(*,*) "Little Endian"
   END IF
Task: Host introspection





Java: import java.nio.ByteOrder;

public class ShowByteOrder {
    public static void main(String[] args) {
        
        System.out.println(ByteOrder.nativeOrder());
    }
}






Fortran:    integer :: i
   character(len=1) :: c(20)
   equivalence (c, i)

   WRITE(*,*) bit_size(1)  
                           
   i = 1

   IF (ichar(c(1)) == 0) THEN
      WRITE(*,*) "Big Endian"
   ELSE
     WRITE(*,*) "Little Endian"
   END IF
Task: Host introspection







Python: >>> import platform, sys, socket
>>> platform.architecture()
('64bit', 'ELF')
>>> platform.machine()
'x86_64'
>>> platform.node()
'yourhostname'
>>> platform.system()
'Linux'
>>> sys.byteorder
little
>>> socket.gethostname()
'yourhostname'
>>>




Fortran:    integer :: i
   character(len=1) :: c(20)
   equivalence (c, i)

   WRITE(*,*) bit_size(1)  
                           
   i = 1

   IF (ichar(c(1)) == 0) THEN
      WRITE(*,*) "Big Endian"
   ELSE
     WRITE(*,*) "Little Endian"
   END IF
Task: Currying



C#: public delegate int Plus(int y); 
public delegate Plus CurriedPlus(int x);
public static CurriedPlus plus = 
      delegate(int x) {return delegate(int y) {return x + y;};};
static void Main()
{
    int sum = plus(3)(4); 
    int sum2= plus(2)(plus(3)(4)) 
}









Ada: generic
    type Argument_1 (<>) is limited private;
    type Argument_2 (<>) is limited private;
    type Argument_3 (<>) is limited private;
    type Return_Value (<>) is limited private;

    with function Func
           (A : in Argument_1;
            B : in Argument_2;
            C : in Argument_3)
        return Return_Value;
package Curry_3 is

    generic
        First : in Argument_1;
    package Apply_1 is

        generic
            Second : in Argument_2;
        package Apply_2 is

            function Apply_3
                   (Third : in Argument_3)
                return Return_Value;

        end Apply_2;

    end Apply_1;

end Curry_3;
Task: Currying

C: #include<stdarg.h>
#include<stdio.h>

long int factorial(int n){
	if(n>1)
		return n*factorial(n-1);
	return 1;
}

long int sumOfFactorials(int num,...){
	va_list vaList;
	long int sum = 0;
	
	va_start(vaList,num);
	
	while(num--)
		sum += factorial(va_arg(vaList,int));
	
	va_end(vaList);
	
	return sum;
}

int main()
{
	printf("\nSum of factorials of [1,5] : %ld",sumOfFactorials(5,1,2,3,4,5));
	printf("\nSum of factorials of [3,5] : %ld",sumOfFactorials(3,3,4,5));
	printf("\nSum of factorials of [1,3] : %ld",sumOfFactorials(3,1,2,3));
	
	return 0;
}











Ada: generic
    type Argument_1 (<>) is limited private;
    type Argument_2 (<>) is limited private;
    type Argument_3 (<>) is limited private;
    type Return_Value (<>) is limited private;

    with function Func
           (A : in Argument_1;
            B : in Argument_2;
            C : in Argument_3)
        return Return_Value;
package Curry_3 is

    generic
        First : in Argument_1;
    package Apply_1 is

        generic
            Second : in Argument_2;
        package Apply_2 is

            function Apply_3
                   (Third : in Argument_3)
                return Return_Value;

        end Apply_2;

    end Apply_1;

end Curry_3;
Task: Currying




Go: package main

import (
        "fmt"
        "math"
)

func PowN(b float64) func(float64) float64 {
        return func(e float64) float64 { return math.Pow(b, e) }
}

func PowE(e float64) func(float64) float64 {
        return func(b float64) float64 { return math.Pow(b, e) }
}

type Foo int

func (f Foo) Method(b int) int {
        return int(f) + b
}

func main() {
        pow2 := PowN(2)
        cube := PowE(3)

        fmt.Println("2^8 =", pow2(8))
        fmt.Println("4³ =", cube(4))

        var a Foo = 2
        fn1 := a.Method   
        fn2 := Foo.Method 

        fmt.Println("2 + 2 =", a.Method(2)) 
        fmt.Println("2 + 3 =", fn1(3))
        fmt.Println("2 + 4 =", fn2(a, 4))
        fmt.Println("3 + 5 =", fn2(Foo(3), 5))
}








Ada: generic
    type Argument_1 (<>) is limited private;
    type Argument_2 (<>) is limited private;
    type Argument_3 (<>) is limited private;
    type Return_Value (<>) is limited private;

    with function Func
           (A : in Argument_1;
            B : in Argument_2;
            C : in Argument_3)
        return Return_Value;
package Curry_3 is

    generic
        First : in Argument_1;
    package Apply_1 is

        generic
            Second : in Argument_2;
        package Apply_2 is

            function Apply_3
                   (Third : in Argument_3)
                return Return_Value;

        end Apply_2;

    end Apply_1;

end Curry_3;
Task: Currying





Java: module CurryPower
    {
    @Inject Console console;
    void run()
        {
        function Int(Int, Int) divide = (x,y) -> x / y;

        function Int(Int) half = divide(_, 2);
        function Int(Int) partsOf120 = divide(120, _);

        console.print($|half of a dozen is {half(12)}
                       |half of 120 is {partsOf120(2)}
                       |a third is {partsOf120(3)}
                       |and a quarter is {partsOf120(4)}
                     );
        }
    }







Ada: generic
    type Argument_1 (<>) is limited private;
    type Argument_2 (<>) is limited private;
    type Argument_3 (<>) is limited private;
    type Return_Value (<>) is limited private;

    with function Func
           (A : in Argument_1;
            B : in Argument_2;
            C : in Argument_3)
        return Return_Value;
package Curry_3 is

    generic
        First : in Argument_1;
    package Apply_1 is

        generic
            Second : in Argument_2;
        package Apply_2 is

            function Apply_3
                   (Third : in Argument_3)
                return Return_Value;

        end Apply_2;

    end Apply_1;

end Curry_3;
Task: Currying







Python:  def addN(n):
     def adder(x):
         return x + n
     return adder





Ada: generic
    type Argument_1 (<>) is limited private;
    type Argument_2 (<>) is limited private;
    type Argument_3 (<>) is limited private;
    type Return_Value (<>) is limited private;

    with function Func
           (A : in Argument_1;
            B : in Argument_2;
            C : in Argument_3)
        return Return_Value;
package Curry_3 is

    generic
        First : in Argument_1;
    package Apply_1 is

        generic
            Second : in Argument_2;
        package Apply_2 is

            function Apply_3
                   (Third : in Argument_3)
                return Return_Value;

        end Apply_2;

    end Apply_1;

end Curry_3;
Task: Currying








VB: Option Explicit On
Option Infer On
Option Strict On

Module Currying
    
    Function Curry(Of T1, TResult)(func As Func(Of T1, TResult)) As Func(Of T1, TResult)
        
        Return Function(a) func(a)
    End Function

    Function Curry(Of T1, T2, TResult)(func As Func(Of T1, T2, TResult)) As Func(Of T1, Func(Of T2, TResult))
        Return Function(a) Function(b) func(a, b)
    End Function

    Function Curry(Of T1, T2, T3, TResult)(func As Func(Of T1, T2, T3, TResult)) As Func(Of T1, Func(Of T2, Func(Of T3, TResult)))
        Return Function(a) Function(b) Function(c) func(a, b, c)
    End Function

    
End Module




Ada: generic
    type Argument_1 (<>) is limited private;
    type Argument_2 (<>) is limited private;
    type Argument_3 (<>) is limited private;
    type Return_Value (<>) is limited private;

    with function Func
           (A : in Argument_1;
            B : in Argument_2;
            C : in Argument_3)
        return Return_Value;
package Curry_3 is

    generic
        First : in Argument_1;
    package Apply_1 is

        generic
            Second : in Argument_2;
        package Apply_2 is

            function Apply_3
                   (Third : in Argument_3)
                return Return_Value;

        end Apply_2;

    end Apply_1;

end Curry_3;
Task: Currying

C: #include<stdarg.h>
#include<stdio.h>

long int factorial(int n){
	if(n>1)
		return n*factorial(n-1);
	return 1;
}

long int sumOfFactorials(int num,...){
	va_list vaList;
	long int sum = 0;
	
	va_start(vaList,num);
	
	while(num--)
		sum += factorial(va_arg(vaList,int));
	
	va_end(vaList);
	
	return sum;
}

int main()
{
	printf("\nSum of factorials of [1,5] : %ld",sumOfFactorials(5,1,2,3,4,5));
	printf("\nSum of factorials of [3,5] : %ld",sumOfFactorials(3,3,4,5));
	printf("\nSum of factorials of [1,3] : %ld",sumOfFactorials(3,1,2,3));
	
	return 0;
}
Task: Currying



C#: public delegate int Plus(int y); 
public delegate Plus CurriedPlus(int x);
public static CurriedPlus plus = 
      delegate(int x) {return delegate(int y) {return x + y;};};
static void Main()
{
    int sum = plus(3)(4); 
    int sum2= plus(2)(plus(3)(4)) 
}
Task: Currying





Java: module CurryPower
    {
    @Inject Console console;
    void run()
        {
        function Int(Int, Int) divide = (x,y) -> x / y;

        function Int(Int) half = divide(_, 2);
        function Int(Int) partsOf120 = divide(120, _);

        console.print($|half of a dozen is {half(12)}
                       |half of 120 is {partsOf120(2)}
                       |a third is {partsOf120(3)}
                       |and a quarter is {partsOf120(4)}
                     );
        }
    }
Task: Currying







Python:  def addN(n):
     def adder(x):
         return x + n
     return adder
Task: Currying








VB: Option Explicit On
Option Infer On
Option Strict On

Module Currying
    
    Function Curry(Of T1, TResult)(func As Func(Of T1, TResult)) As Func(Of T1, TResult)
        
        Return Function(a) func(a)
    End Function

    Function Curry(Of T1, T2, TResult)(func As Func(Of T1, T2, TResult)) As Func(Of T1, Func(Of T2, TResult))
        Return Function(a) Function(b) func(a, b)
    End Function

    Function Curry(Of T1, T2, T3, TResult)(func As Func(Of T1, T2, T3, TResult)) As Func(Of T1, Func(Of T2, Func(Of T3, TResult)))
        Return Function(a) Function(b) Function(c) func(a, b, c)
    End Function

    
End Module