Skip to content

Latest commit

 

History

History
90 lines (64 loc) · 1.11 KB

README.md

File metadata and controls

90 lines (64 loc) · 1.11 KB

Functional Programming

Introduction

basics basics

Composition

basics

Examples

implementation of "composition" and usage

C#

static Func<Ta, Tc> Compose<Ta, Tb, Tc>(
    Func<Tb, Tc> g,
    Func<Ta, Tb> f
) {
    return x => g(f(x));
}

// use:
var h = Compose(g,f);

Ruby

# Patch Proc and add * operator
class Proc
    def self.compose(g, f)
        lambda { |*args| g[f[*args]] }
    end

    def *(f)
        Proc.compose(self, f)
    end
end

# use:
h = g * f

C++

#include <functional>

template <typename A, typename B, typename C> 
std::function<C(A)> compose(
    std::function<C(B)> g,
    std::function<B(A)> f
) {
    return [g,f](A x) { return g(f(x)); };
}

// use:
auto h = compose(g,f);

JavaScript

const compose = (g, f) => x => g(f(x));

// use:
const h = compose(g,f);

Python

def compose(g, f):
    return lambda x: g(f(x))

# use:
h = compose(g,f)

Haskell

Compose natively exists in form of the . operator

h = g . f