├── .gitignore ├── LICENSE ├── Makefile ├── README ├── rebar ├── rebar.config ├── src ├── transducers.app.src └── transducers.erl └── test └── transducers_tests.erl /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.dump 3 | /deps 4 | /ebin 5 | /.eunit 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Sam Chapin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ERL ?= erl 2 | APP := transducers 3 | 4 | .PHONY: deps 5 | 6 | all: deps 7 | @./rebar compile 8 | 9 | deps: 10 | @./rebar get-deps 11 | 12 | clean: 13 | @./rebar clean 14 | 15 | distclean: clean 16 | @./rebar delete-deps 17 | 18 | docs: 19 | @erl -noshell -run edoc_run application '$(APP)' '"."' '[]' 20 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Some examples of Clojure's "transducer" concept, omitting only the "init" 2 | behaviour. Stateful transducers operate by spawning a process at 3 | transformation-time. 4 | -------------------------------------------------------------------------------- /rebar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sfwc/erlang-transducers/786bfab55009e4f7fd0252c608d41200b3011b60/rebar -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sfwc/erlang-transducers/786bfab55009e4f7fd0252c608d41200b3011b60/rebar.config -------------------------------------------------------------------------------- /src/transducers.app.src: -------------------------------------------------------------------------------- 1 | {application, transducers, [ 2 | {description, "An abstraction for iterative processes"}, 3 | {vsn, "0.0.1"}, 4 | {registered, [transducers]}, 5 | {applications, [kernel, stdlib]} 6 | ]}. 7 | -------------------------------------------------------------------------------- /src/transducers.erl: -------------------------------------------------------------------------------- 1 | -module(transducers). 2 | 3 | -export([compose/2, drop_while/1, filter/1, list/2, map/1, stateful/3]). 4 | 5 | -type reduction(A) :: {ok, A} | {halt, A}. 6 | -type reduction() :: reduction(any()). 7 | -type step(A) :: fun ((reduction(), A) -> reduction()). 8 | -type stateful_step(A) :: fun ((A, reduction(), any()) -> {A, reduction()}). 9 | -type finalizer() :: fun ((reduction()) -> reduction()). 10 | -type stateful_finalizer(A) :: fun ((A, reduction()) -> reduction()). 11 | -type reducer(A) :: {step(A), finalizer()}. 12 | -type reducer() :: reducer(any()). 13 | -type transducer() :: fun ((reducer()) -> reducer()). 14 | 15 | -type predicate() :: fun ((any()) -> boolean()). 16 | 17 | -spec list(transducer(), list()) -> list(). 18 | list(Transduce, List) -> 19 | {Step, Finalize} = Transduce({ 20 | fun ({Type, Acc}, Input) -> {Type, [Input | Acc]} end, 21 | fun ({Type, Acc}) -> {Type, lists:reverse(Acc)} end 22 | }), 23 | {_, Result} = fun 24 | Feed(Acc={halt, _}, _) -> Finalize(Acc); 25 | Feed(Acc, []) -> Finalize(Acc); 26 | Feed(Acc, [Input | Rest]) -> Feed(Step(Acc, Input), Rest) 27 | end({ok, []}, List), 28 | Result. 29 | 30 | -spec compose(transducer(), transducer()) -> transducer(). 31 | compose(T1, T2) -> fun (R) -> T1(T2(R)) end. 32 | 33 | -spec filter(predicate()) -> transducer(). 34 | filter(Pred) -> 35 | fun ({Step, Finalize}) -> 36 | {fun (Acc, Input) -> 37 | case Pred(Input) of 38 | true -> Step(Acc, Input); 39 | false -> Acc 40 | end 41 | end, 42 | Finalize} 43 | end. 44 | 45 | -spec map(fun ((any()) -> any())) -> transducer(). 46 | map(F) -> 47 | fun ({Step, Finalize}) -> 48 | {fun (Acc, Input) -> 49 | Step(Acc, F(Input)) 50 | end, Finalize} 51 | end. 52 | 53 | -spec stateful(A, stateful_step(A), stateful_finalizer(A)) -> reducer(). 54 | stateful(InitialState, Step, Finalize) -> 55 | Self = self(), 56 | P = spawn_link(fun () -> 57 | fun Remember(State) -> 58 | receive 59 | {finalize, Reduction} -> Self ! {self(), Finalize(State, Reduction)}; 60 | {step, Reduction, Input} -> 61 | {NewState, NewReduction} = Step(State, Reduction, Input), 62 | Self ! {self(), NewReduction}, 63 | Remember(NewState) 64 | end 65 | end(InitialState) 66 | end), 67 | {fun (Reduction, Input) -> 68 | P ! {step, Reduction, Input}, 69 | receive {P, NewReduction} -> NewReduction end 70 | end, 71 | fun (Reduction) -> 72 | P ! {finalize, Reduction}, 73 | receive {P, NewReduction} -> NewReduction end 74 | end}. 75 | 76 | -spec drop_while(predicate()) -> transducer(). 77 | drop_while(Pred) -> 78 | fun ({Step, Finalize}) -> 79 | stateful(Pred, fun (CurrentPred, Acc, Input) -> 80 | case CurrentPred(Input) of 81 | true -> {Pred, Acc}; 82 | false -> {fun (_) -> false end, Step(Acc, Input)} 83 | end 84 | end, fun (_CurrentPred, Acc) -> Finalize(Acc) end) 85 | end. 86 | -------------------------------------------------------------------------------- /test/transducers_tests.erl: -------------------------------------------------------------------------------- 1 | -module(transducers_tests). 2 | 3 | -import(transducers, [compose/2, drop_while/1, filter/1, list/2, map/1]). 4 | 5 | -include_lib("eunit/include/eunit.hrl"). 6 | 7 | 8 | transducer_test_() -> [ 9 | [?_assertEqual([1, 2, 3], list(fun (X) -> X end, [1, 2, 3]))], 10 | [?_assertEqual([4, 5], 11 | list(filter(fun (X) -> X > 3 end), [1, 2, 3, 4, 5]))], 12 | [?_assertEqual([true, true, false], 13 | list(map(fun (N) -> N < 3 end), [1, 2, 3]))], 14 | [?_assertEqual(["4", "5"], 15 | list(compose(filter(fun (N) -> N > 3 end), 16 | map(fun (N) -> integer_to_list(N) end)), 17 | [1, 2, 3, 4, 5]))], 18 | [?_assertEqual([3, 4, 1], 19 | list(drop_while(fun (X) -> X < 3 end), [1, 2, 3, 4, 1]))] 20 | ]. 21 | --------------------------------------------------------------------------------