lfex / lutil

LFE Utility Modules (successor to lfe-utils)
Other
18 stars 8 forks source link

Added a reduce macro. #36

Closed useronym closed 9 years ago

useronym commented 9 years ago

This makes things more concise when using lists:foldl as a reduce. The documentation provides some example usage.

oubiwann commented 9 years ago

Nice! I'd been meaning to add a foldl that had a default accumulator -- I like your solution, thanks :-)

oubiwann commented 9 years ago

I had to change it from a macro to a function, since there were issues expanding the macro. I also added an alias for lists:foldl so that there is reduce/2 and reduce/3 so you can use the same name for both use cases.

useronym commented 9 years ago

Cool idea with the foldl alias :) Can you go into a bit more detail about why there's trouble expanding the macro? I only tested it in the REPL. By the way, how do the tests work? make check is failing for me (./test/lutil-core-tests.lfe:5: error expanding (include-lib "ltest/include/ltest-macros.lfe") ).

edit: apparently I'm missing ltest

oubiwann commented 9 years ago

I'm cc'ing @rvirding so he can confirm or correct my guess:

I suspect that the reason it was failing in the unit tests vs. working in the REPL is the different manner in which code is handled in each (evaluation vs. compilation?). In particular, I think that the compiled code hits a predefined function that can't be shadowed, so when one is thinking that by including the file that holds your reduce macro you'll be able to call it, you're in fact calling a different macro (the one that does reductions of a different sort in LFE/Erlang).

I could be completely wrong, though ... Robert can sort me out in that case :-)

One of the nice things about your reduce being converted from a macro to a function is that it will be easier (and in some cases possible) to compose.

Glad you got the test thing sorted! Once you got ltest, did all tests compile and pass?

useronym commented 9 years ago

First, about getting ltest: it seems like the makefile (make get-deps) is trying to call lfetool with a non existing command, download, to install ltest into the test/ directory. I'm on lfetool 1.4.0 When I manually copied deps/ltest/ into test/, the test compilation succeeded, but the tests crash the VM: Crash dump was written to: erl_crash.dump init terminating in do_boot () {"init terminating in do-boot",{{badmatch,{error,{1,erl-parse,["syntax error before: ","','"]}}},[{init,start-it,1,[]},{init,start-em,1,[]}]}}

oubiwann commented 9 years ago

So, there are some things you'll need to do:

1) get the latest dev version of lfetool (it's pretty much ready for release to stable) 2) remove the deps directory and its contents 3) remove ebin/*.beam 4) make compile

That should do it. Let me know ...

On Sun, May 31, 2015 at 5:04 AM, Adam K. notifications@github.com wrote:

First, about getting ltest: it seems like the makefile (make get-deps) is trying to call lfetool with a non existing command, download, to install ltest into the test/ directory. I'm on lfetool 1.4.0 When I manually copied deps/ltest into /test, the test compilation succeeded, but the tests crash the VM: Crash dump was written to: erl_crash.dump init terminating in do_boot () {"init terminating in do-boot",{{badmatch,{error,{1,erl-parse,["syntax error before: ","','"]}}},[{init,start-it,1,[]},{init,start-em,1,[]}]}}

— Reply to this email directly or view it on GitHub https://github.com/lfex/lutil/pull/36#issuecomment-107151359.

useronym commented 9 years ago

Yup, unit tests pass now. Thank you for the help :)

oubiwann commented 9 years ago

@osense You are most welcome!

FYI, @rvirding and I are still digging into the problem with the macro working in the REPL but failing in the unit tests. It seems that my guess as to why it might have failed is probably not the reason.

Also, I've tweaked the reduce function again, and thought you might enjoy the cons pattern matching in the function head :-)

(defun reduce
 ((func `(,head . ,tail))
  (lists:foldl func head tail)))