GithubHelp home page GithubHelp logo

currying's Introduction

Currying

基于 C++11 的可调用对象柯里化库

如何使用

包含头文件

#include "currying.hpp"

将可调用对象柯里化

double func(int i, double d) {
    return i + d;
}

func 柯里化:

cedar::currying<double(int, double)> c(func);

auto c = cedar::make_currying(func);

两种方式.

传参

auto c1 = c(2);
auto c2 = c1(3.3);

亦可

auto c2 = c(2)(3.3);

2020.09.17 新添了一种传参方式!

auto c2 = c(2, 3.3); // 可以传递 [1, args_size] 个参数

这时候, c2 依旧是一个柯里化对象, 还没有进行实际调用.

调用

std::cout << c2() << std::endl;

std::cout << c2 << std::endl;

5.3

第一种方式的理解, 是因为 c2 依旧是个可调用对象, 只不过参数列表为空.
而第二种方式的存在, 是因为 参数列表为空的柯里化对象 具有一个到返回值的隐式转换. 设计第二种方式的原因很简单:

std::cout << c(2)(3.3) << std::endl;

我不希望如上代码会因为没有第二种方式的原因写成这样:

std::cout << c(2)(3.3)() << std::endl;

需要注意的是, 在没有对 c2 进行调用操作, 也就是说没有调用 operator()operator R 时, 不会做 对 被柯里化的可调用对象 的调用操作!
这一切都是惰性的!

注意事项

double f(int &i, double d) {
    return i + d;
}

_currying_impl<double(int &, double), std::tuple<int &>, double>
create() {
    int i = 2;
    return make_currying(f)(i);
}

使用如上 create() 构建的柯里化对象将导致未定义行为, 因为存在悬垂引用!
f 的函数签名改为

double f(const int &i, double d);

依旧会形成悬垂引用, 导致未定义行为.

后续优化

  1. 我希望我在将来能有能力优化形参为 const & 的情况下的柯里化行为, 使用户可定义柯里化类内部是否存储一份副本, 以避免悬垂引用的产生.
  2. 我注意到柯里化类的类型非常复杂, 导致在没有 C++14 支持的情况下 (C++14 的返回值可写 auto) 想返回一个柯里化对象有着巨大的不便, 我将提供一个 using 简化这一工作.

谢谢.

currying's People

Contributors

cedarhuang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

mintonmu

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.