Logo

Programming-Idioms

Multiply all the elements of the list elements by a constant c
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

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

Other implementations
for E of elements loop
   E := E * c;
end loop;
for(int i = 0; i < elements_size; ++i) elements[i] *= c;
(map #(* c %1) elements)
(map #(* c %) elements)
(map (fn [e] (* c e)) elements)
(map (partial * c) elements)
for (auto &it : elements)
	it *= c;
#include <algorithm>
std::transform(std::begin(elements), std::end(elements), std::begin(elements), [c](auto i){
        return i*c;
    });
elements.Select(x => x*c)
elements[] *= c;
elements = [for (var e in elements) e * c];
elements = elements.map((e) => e * c).toList();
elements = Enum.map(elements, &(&1 * c))
elements = elements * c
for i := range elements {
	elements[i] *= c
}
map (*c) elements
elements = elements.map(x => x * c)
import static java.lang.System.out;
import java.util.List;
elements = elements.stream()
                   .map(x -> x * c)
                   .toList();
import java.util.stream.Collectors;
elements = elements.stream().map(e -> e*c).collect(Collectors.toList());
(mapcar (lambda (n) (* n c))
        elements)
local function map(t,fn)
 for i,v in ipairs(t) do
  t[i]=fn(v)
 end
 return t
end

local ret=map(elements,function(x) return x*c end)
array_map(function($v) use ($_c) {
    return $v*$c;
}, $_elements);
for i := Low(elements) to High(elements) do 
  elements[i] := elements[i] * c;
@elements = map {$_ * $c} @elements;
f = lambda x: x * c
elements = [*map(f, elements)]
elements = [c * x for x in elements]
for i, x in enumerate(elements):
    elements[i] = x * c
elements.map { |el| el * c }
elements.iter_mut().for_each(|x| *x *= c);
let elements = elements.into_iter().map(|x| c * x).collect::<Vec<_>>();
elements.map(_ * c)