Programming-Idioms

Implementation
D

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating resource.

Please try to avoid dependencies to third-party libraries and frameworks.

Implementation edit is for fixing errors and enhancing with metadata.

Instead of changing the code of the snippet, consider creating another D implementation.

Other implementations
y = [T(e) for e in x if P(e)]
y = pack (t(x), mask=p(x))
y = x.filter(e => P(e)).map(e => T(e))
var y []Result
for _, e := range x {
	if P(e) {
		y = append(y, T(e))
	}
}
template <typename SeqT>
auto transform_copy_if(const SeqT i, auto p, auto f)
{
 using namespace std;

 SeqT o;

 for_each(begin(i), end(i),
          [&](const auto& x) {
              if(p(x))
               o.push_back(f(x));
          }
         );

 return o;
}
my @y = map { T($_) } grep { P($_) } @x;
using System.Linq;
var y = x.Where(P).Select(T).ToList();
y = x.each_with_object([]){|e, ar| ar << t(e) if p(e)}
list.collect { case x if p(x) => T(x) }
classes
type
  TListPredicate = function(e: pointer): Boolean;
  TListElementFunc = function(e: pointer): pointer;

function NewList(X: TList; P: TListPredicate; T: TListElementFunc): TList;
var
  e: pointer;
begin
  Result := TList.Create;
  for e in X do
  begin
    if P(e) then
      Result.Add(T(e));
  end;
end;
y = x.filter_map{|e| t(e) if p(e)}
y = (map t . filter p) x
let y = x.iter()
	.filter_map(|e| if P(e) {Some(T(e))} else {None})
	.collect::<Vec<_>>();
y = x
|> Enum.filter(&P/1)
|> Enum.map(&P(&T/1)