Git Product home page Git Product logo

map-filter-reduce's Introduction

Map, Filter, Reduce in C: recursive high-order functions using pointers

This repository contains a set of functions that serve as a study for a possible implementation of the map, filter and reduce higher-order functions in C, and how to reference functions using pointers. This is meant to be merely a study on recursive data structures and function pointers, and not a proposal for functional programming in C.

If you're interested, you can read the article on the web (or in the file article.md).

Contents:

  • iterators.c: implements a higher-order function that receives a function pointer and a list (linked list), and recursively applies the function to every element
  • linkedlist.c: implements a linked list, a recursive data type used for collecting data in functional programming languages
  • functional.c: implements some functions that can be passed as arguments (through pointers) to our higher-order functions

Lambda functions

Lambda functions are essential for functional programming, because they provide a clear syntax to create function objects that can be passed to and returned from functions.

Functions are not first-class objects in C: they cannot be returned from functions or passed to functions as arguments. However, the language does provide function pointers, pointers to the memory addresses of functions. For example, we can create a function apply that takes a function pointer and a value, and returns the result of applying the function to the value.

typedef int (*fp) (int);

int apply(fp fp, int x){
  return (*fp)(x)
}

For further development

One major problem with our approach is that, in order to achieve the immutability of data structures, we are allocating new data to memory, but we haven't provided any way to clean the unnecessary or unused data. That would require a garbage collector, something that is not provided in C, and is beyond the scope of this exercise.

Another thing worth mentioning is that our functions are limited to working with integer types int. It would be nice to implement some sort of parametric polymorphism, that is, defining of function types to accept any types of arguments.

I would appreciate any comments, corrections, critiques or suggestions.

map-filter-reduce's People

Contributors

escribapetrus avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.