About Me

Programming has always been one my favourites and I deeply enjoyed system programming until I came to know about this ERP thing.  Since then my focus in programming has been on finding ways to develop business applications more efficiently.

 

I blog on http://BahmanM.com.

 

Currying In Different Languages

Snippets of currying mechanisms in different languages which support currying

Introduction

It took me about an hour to understand what is currying (when reading a Scala book) and this was while I already knew what is currying and when to use it; I just didn't know the technical term.

 

Please feel free to add your snippets for other languages here.

 

Currying: New function

As the name implies, in this experiment, the goal is to write a function that returns another function.

Problem Definition

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/*
 * PSEUDO CODE - problem definition
 */
function g(int a, int b) {
  return new function f(int c) {
    return a + b + c;
  }
}

/*
 * PSEUDO CODE - tests
 */
let f1 = g(10, 20);
assert( f1(30) equals 60 );  // should return TRUE

 

Scala Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Implementation
def g(a: Int, b: Int): (Int => Int) = {
  def f(c: Int): Int = a + b + c
  f
}

// Tests
scala> val f1 = g(10, 20)
f1: Int => Int = <function1>

scala> f1(30)
res6: Int = 60

 

Clojure Implementation

1
2
3
4
5
6
7
8
9
;; Implementation
(defn g [a b]
  (fn [c] (+ a b c)))

;; Tests
user=> (def f1 (g 10 20))
#<user$g$fn__177 user$g$fn__177@3e98613b>
user=> (f1 30)
60

 

Python Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Implementation
def g(a, b):
    def f(c):
        return a + b + c
    return f

# Tests
>>> f1 = g(10, 20)
>>> f1(30)
60

Erlang Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
% Implementation
g1(A,B) -> fun(C) -> A + B + C end.

% Tests (from the Erlang shell, assuming curry.erl)
1> c(curry).
{ok,curry}
2> F = curry:g1(10, 20).
#Fun<curry.1.62851824>
3> F(30).
60

Currying: Partial Application

This is basically the same as New Function problem above but usually languages provide some sort of shortcut for this type of currying.

Problem Definition

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/*
 * PSEUDO CODE - problem definition
 */
function f(int a, int b, int c) {
  return a + b + c;
}

function g(int a, int b) {
  let fp = partial_apply(f, a, b);
  return fp;
}

/*
 * PSEUDO CODE - tests
 */
let f1 = g(10, 20);
assert( f1(30) equals 60 );  // should return TRUE

 

Scala Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Implementation
def f(a: Int, b: Int, c: Int): Int =
  a + b + c

def g(a: Int, b: Int): (Int => Int) =
  f(a, b, _)

// Tests
scala> val f1 = g(10, 20)
f1: Int => Int = <function1>

scala> f1(30)
res6: Int = 60

 

Clojure Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
;; Implementation
(defn f [a b c]
  (+ a b c))

(defn g [a b]
  (partial f a b))

;; Tests
user=> (def f1 (g 10 20))
#'user/f2
user=> (f1 30)
60

 

Python Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Implementation
def f(a, b, c):
    return a + b + c

def g(a, b):
    from functools import partial
    return partial(f, a, b)

# Tests
>>> f1 = g(10, 20)
>>> f1(30)
60

Note:  functools is part of Python standard library.

Erlang Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
% Implementation
f2(A, B, C) -> A + B + C end.
g2(A, B) -> fun(C) -> f(A, B, C) end.

% Tests (from the Erlang shell, assuming curry.erl)
1> c(curry).
{ok,curry}
2> F = curry:g2(10, 20).
#Fun<curry.1.62851824>
3> F(30).
60

Conclusion

Nothing!  This topic is just so small to judge a language based on it.

 

Credits

Filed under: , , , , ,