Skip to content

Color-Of-Code/FP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages