Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
16
Task/Power-set/D/power-set-3.d
Normal file
16
Task/Power-set/D/power-set-3.d
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Haskell definition:
|
||||
// foldr f z [] = z
|
||||
// foldr f z (x:xs) = x `f` foldr f z xs
|
||||
S foldr(T, S)(S function(T, S) f, S z, T[] rest) {
|
||||
return (rest.length == 0) ? z : f(rest[0], foldr(f, z, rest[1..$]));
|
||||
}
|
||||
|
||||
// Haskell definition:
|
||||
//powerSet = foldr (\x acc -> acc ++ map (x:) acc) [[]]
|
||||
T[][] powerset(T)(T[] set) {
|
||||
import std.algorithm;
|
||||
import std.array;
|
||||
// Note: The types before x and acc aren't needed, so this could be made even more concise, but I think it helps
|
||||
// to make the algorithm slightly clearer.
|
||||
return foldr( (T x, T[][] acc) => acc ~ acc.map!(accx => x ~ accx).array , [[]], set );
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue