Programming-Idioms

New implementation

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.

Other implementations
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;
}
using System.Linq;
var y = x.Where(P).Select(T).ToList();
import std.algorithm;
y = x.filter!(P).map!(T);
y = pack (t(x), mask=p(x))
var y []Result
for _, e := range x {
	if P(e) {
		y = append(y, T(e))
	}
}
y = (map t . filter p) x
y = x.filter(e => P(e)).map(e => T(e))
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;
my @y = map { T($_) } grep { P($_) } @x;
y = [T(e) for e in x if P(e)]
y = x.each_with_object([]){|e, ar| ar << t(e) if p(e)}
y = x.filter_map{|e| t(e) if p(e)}
let y = x.iter()
	.filter_map(|e| if P(e) {Some(T(e))} else {None})
	.collect::<Vec<_>>();
list.collect { case x if p(x) => T(x) }