How do you write a fun that’s recursive in Erlang?

Since OTP 17.0 there are named funs: 1> Perms = fun F([]) -> [[]]; F(L) -> [[H|T] || H <- L, T <- F(L–[H])] end. #Fun<erl_eval.30.54118792> 2> Perms([a,b,c]). [[a,b,c],[a,c,b],[b,a,c],[b,c,a],[c,a,b],[c,b,a]] Before that you could do this with a little argument trick: 1> Foo = fun(F, X) -> F(F, X) end. #Fun<erl_eval.12.113037538> 2> Foo(Foo, a). <…infinite loop!> … Read more

Recursive listing of all files matching a certain filetype in Groovy

This should solve your problem: import static groovy.io.FileType.FILES new File(‘.’).eachFileRecurse(FILES) { if(it.name.endsWith(‘.groovy’)) { println it } } eachFileRecurse takes an enum FileType that specifies that you are only interested in files. The rest of the problem is easily solved by filtering on the name of the file. Might be worth mentioning that eachFileRecurse normally recurses … Read more

Java 8: Copy directory recursively?

In this way the code looks a bit simpler import static java.nio.file.StandardCopyOption.*; public void copyFolder(Path src, Path dest) throws IOException { try (Stream<Path> stream = Files.walk(src)) { stream.forEach(source -> copy(source, dest.resolve(src.relativize(source)))); } } private void copy(Path source, Path dest) { try { Files.copy(source, dest, REPLACE_EXISTING); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } … Read more

The idiomatic way to implement generators (yield) in Golang for recursive functions

I. Alternatives Foreword: I will use a much simpler generator, because the problem does not concern the generator complexity but rather the signals between the generator and consumer, and the call of the consumer itself. This simple generator just generates the integer numbers from 0 to 9. 1. With a function value A generate-consumer pattern … Read more

recursive variadic template to print out the contents of a parameter pack

There’s actually a very elegant way to end the recursion: template <typename Last> std::string type_name () { return std::string(typeid(Last).name()); } template <typename First, typename Second, typename …Rest> std::string type_name () { return std::string(typeid(First).name()) + ” ” + type_name<Second, Rest…>(); } I initially tried template <typename Last> and template <typename First, typename …Rest> but that was … Read more

When is tail recursion guaranteed in Rust?

Shepmaster’s answer explains that tail call elimination is merely an optimization, not a guarantee, in Rust. But “never guaranteed” doesn’t mean “never happens”. Let’s take a look at what the compiler does with some real code. Does it happen in this function? As of right now, the latest release of Rust available on Compiler Explorer … Read more

Does CUDA support recursion?

It does on NVIDIA hardware supporting compute capability 2.0 and CUDA 3.1: New language features added to CUDA C / C++ include: Support for function pointers and recursion make it easier to port many existing algorithms to Fermi GPUs http://developer.nvidia.com/object/cuda_3_1_downloads.html Function pointers: http://developer.download.nvidia.com/compute/cuda/sdk/website/CUDA_Advanced_Topics.html#FunctionPointers Recursion: I can’t find a code sample on NVIDIA’s website, but on … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)