From cb14ad76f3cc386bce964e822733e5cb9e011ed8 Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Wed, 6 May 2015 14:33:46 -0400 Subject: [PATCH] Implement function threading. --- src/hope.app.src | 2 +- src/hope_fun.erl | 7 +++++++ test/hope_fun_SUITE.erl | 9 +++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/hope.app.src b/src/hope.app.src index 197db7c..45b460d 100644 --- a/src/hope.app.src +++ b/src/hope.app.src @@ -1,7 +1,7 @@ {application, hope, [ {description, "Higher Order Programming in Erlang"}, - {vsn, "3.2.0"}, + {vsn, "3.3.0"}, {registered, []}, {applications, [ kernel, diff --git a/src/hope_fun.erl b/src/hope_fun.erl index 96a39cd..df0d786 100644 --- a/src/hope_fun.erl +++ b/src/hope_fun.erl @@ -6,6 +6,7 @@ , compose/1 % alias for compose_right/1 , compose_right/1 , compose_left/1 + , thread/2 ]). -spec id(A) -> @@ -41,6 +42,12 @@ compose_right(Fs) -> compose_left(Fs) -> compose_given_fold(Fs, fun lists:foldl/3). +-spec thread([fun((A) -> B)], A) -> + B. +thread(Fs, X) -> + F = compose_left(Fs), + F(X). + %% ============================================================================ diff --git a/test/hope_fun_SUITE.erl b/test/hope_fun_SUITE.erl index 7b5051b..333c81f 100644 --- a/test/hope_fun_SUITE.erl +++ b/test/hope_fun_SUITE.erl @@ -11,7 +11,7 @@ [ t_specs/1 , t_id/1 , t_curry/1 - , t_compose/1 + , t_compose_and_thread/1 ]). @@ -31,7 +31,7 @@ groups() -> [ t_specs , t_id , t_curry - , t_compose + , t_compose_and_thread ], Properties = [parallel], [ {?GROUP, Properties, Tests} @@ -66,11 +66,12 @@ t_curry(_Cfg) -> H = H2(b), {a, b, c} = H(c). -t_compose(_Cfg) -> +t_compose_and_thread(_Cfg) -> A2B = fun (a) -> b end, B2C = fun (b) -> c end, C2D = fun (c) -> d end, Fs = [C2D, B2C, A2B], d = (hope_fun:compose ( Fs ))(a), d = (hope_fun:compose_right ( Fs ))(a), - d = (hope_fun:compose_left (lists:reverse(Fs) ))(a). + d = (hope_fun:compose_left (lists:reverse(Fs) ))(a), + d = hope_fun:thread (lists:reverse(Fs), a). -- 2.20.1