Giter Club home page Giter Club logo

cutlass's Introduction

cutlass

experimental auto-currying for rust functions

example

this currently works only on nightly with the type_alias_impl_trait feature enabled.

#![feature(type_alias_impl_trait)]

#[cutlass::curry]
fn add(x: u32, y: u32, z: u32) -> u32 {
    return x + y + z;
}

fn main() {
    let plus_3 = add(1)(2);
    let v: Vec<u32> = (1..=3).map(plus_3).collect();
    assert_eq!(v, vec![4, 5, 6]);
}

how it works

the #[curry] proc-macro expands the above add function to something like this (roughly):

type T0 = u32;
type T1 = impl Fn(u32) -> T0;
type T2 = impl Fn(u32) -> T1;
fn add(x: u32) -> T2 {
    return (move |y| move |z| x + y + z);
}

gotchas

  • doesn't yet work for method functions (signatures with self)
  • the function has to have a return value
  • works only on nightly with type_alias_impl_trait enabled

testing

test this crate with cargo +nightly test.

to view macro expansions, install cargo-expand and run cargo expand --test <test name>. expand tests/smoke.rs for example:

cargo expand --test smoke

cutlass's People

Contributors

nerdypepper avatar vbrandl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cutlass's Issues

Ideas for `self` functions

Do you have any ideas on how to curry functions taking self as a parameter? I thought about keeping the first two arguments together so it looks like this:

struct Foo(i32);
impl Foo {
  fn add(&self, x: i32, y: i32, z: i32) -> i32 {
    self.0 + x + y +z
  }
}

self.add(1)(2)(3)

I tried something like that but ran into lifetime issues and stopped experimenting before asking for you opinion.

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.