Skip to content

Add curry! macro #6634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,55 @@ pub fn core_macros() -> ~str {
$(if $pred $body)else+
);
)

//
// Declares a named or anonymous curried function.
//
// # Example
//
// ~~~
// curry!(
// fn mul(x: int, y: int) -> int {
// x * y
// }
// )
//
// assert_eq!(mul(2)(3), 6);
//
// let mul2 = mul(2);
// assert_eq!(mul2(3), 6);
//
// let f: @fn(int) -> @fn(int) -> int = curry!(|x,y| x * y);
// assert_eq!(f(2)(3), 6);
// ~~~
//
// # Notes:
//
// - It would be nicer if the syntax for named declarations could be
// `fn foo x: int -> y: int -> int` to reinforce the curried nature of the
// resulting function, but this causes an ambiguity during macro expansion.
// - Unfortunately generics and lifetimes are not supported due to the
// difficulty of implementing type parameter lists in macros.
// - Anonymous curried functions have difficulty inferring types.
//
macro_rules! curry(
// named curried function
(fn $fn_name:ident ($arg:ident : $arg_ty:ty,
$($arg_rest:ident : $arg_rest_ty:ty),*)
-> $ret_ty:ty $fn_body:expr
) => (
fn $fn_name($arg: $arg_ty) $(-> @fn($arg_rest: $arg_rest_ty))* -> $ret_ty {
curry!(|$($arg_rest),*| $fn_body )
}
);
// anonymous curried function
(|$arg:ident, $($arg_rest:ident),*| $fn_body:expr) => (
|$arg| curry!(|$($arg_rest),*| $fn_body)
);
(|$arg:ident| $fn_body:expr) => (
|$arg| $fn_body
);
)
}";
}

Expand Down
28 changes: 28 additions & 0 deletions src/test/run-pass/curry-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

curry!(
fn mul(x: int, y: int, z: int) -> int {
x * y * z
}
)

fn main() {
assert_eq!(mul(2)(3)(2), 12);

let mul2 = mul(2);
assert_eq!(mul2(3)(2), 12);

// type error :(
// assert_eq!(curry!(|x,y,z| x * y * z)(2)(3)(2), 12);

let local_mul: @fn(int) -> @fn(int) -> @fn(int) -> int = curry!(|x,y,z| x * y * z);
assert_eq!(local_mul(2)(3)(2), 12);
}