6 kyu

zipWith

5,068 of 7,259JohanWiltink

Description:

Task

zipWith ( or zip_with ) takes a function and two arrays and zips the arrays together, applying the function to every pair of values.
The function value is one new array.

If the arrays are of unequal length, the output will only be as long as the shorter one.
(Values of the longer array are simply not used.)

Inputs should not be modified.

Examples

zip_with(pow,4,{10,10,10,10},4,{0,1,2,3},*z,*a) =>
a = {1,10,100,1000}
z = 4
zip_with(max,3,{2,7,5},5,{8,7,4,1,3},*z,*a)     =>
a = {8,7,5}
z = 3
zipWith( Math.pow, [10,10,10,10], [0,1,2,3] )      =>  [1,10,100,1000]
zipWith( Math.max, [1,4,7,1,4,7], [4,7,1,4,7,1] )  =>  [4,7,7,4,7,7]

zipWith( function(a,b) { return a+b; }, [0,1,2,3], [0,1,2,3] )  =>  [0,2,4,6]  // Both forms are valid
zipWith( (a,b) => a+b,                  [0,1,2,3], [0,1,2,3] )  =>  [0,2,4,6]  // Both are functions
zip_with( pow, [10,10,10,10], [0,1,2,3] )     =>  [1,10,100,1000]
zip_with( max, [1,4,7,1,4,7], [4,7,1,4,7,1])  =>  [4,7,7,4,7,7]

def add(a,b): return a+b; # or from operator import add
zip_with( add,             [0,1,2,3], [0,1,2,3] )  =>  [0,2,4,6]  # Both forms are valid
zip_with( lambda a,b: a+b, [0,1,2,3], [0,1,2,3] )  =>  [0,2,4,6]  # Both are functions
zip_with(i32::pow, &[10,10,10,10], [0,1,2,3])     => [1,10,100,1000]
zip_with(i32::max, &[1,4,7,1,4,7], [4,7,1,4,7,1]) => [4,7,7,4,7,7]

fn add(a: i32, b: i32) -> i32 { a + b }  // or i32::add
zip_with(add, &[0,1,2,3], &[0,1,2,3])         => [0,2,4,6]  // Both forms are valid
zip_with(|a,b| a + b, &[0,1,2,3], &[0,1,2,3]) => [0,2,4,6]  // Both are functions
zip_with ( * ) [10; 10; 10; 10] [0; 1; 2; 3] (* = [0; 10; 20; 30] *)
zip_with max [1; 4; 7; 1; 4; 7] [4; 7; 1; 4; 7; 1] (* = [4; 7; 7; 4; 7; 7] *)

zip_with        (+)         [0; 1; 2; 3] [0; 1; 2; 3] (* = [0; 2; 4; 6] *)
zip_with (fun a b -> a + b) [0; 1; 2; 3] [0; 1; 2; 3] (* = [0; 2; 4; 6] *)
zip_with(math.pow, { 10, 10, 10, 10 }, { 0, 1, 2, 3 }) => { 1, 10, 100, 1000 }
zip_with(math.max, { 1, 4, 7, 1, 4, 7 }, { 4, 7, 1, 4, 7, 1 }) => { 4, 7, 7, 4, 7, 7 }

local function add(x, y) return x + y end
zip_with(add, { 0, 1, 2, 3 }, { 0, 1, 2, 3 }) => { 0, 2, 4, 6 } -- Both forms are valid
zip_with(function(x, y) return x + y end, { 0, 1, 2, 3 }, { 0, 1, 2, 3 }) => { 0, 2, 4, 6 } -- Both are functions

Input validation

Assume all input is valid.

Lists
Arrays
Functional Programming
Algorithms

Stats:

CreatedNov 11, 2016
PublishedNov 11, 2016
Warriors Trained9766
Total Skips123
Total Code Submissions21533
Total Times Completed7259
JavaScript Completions5068
Python Completions1074
Rust Completions133
C Completions110
OCaml Completions36
Scala Completions22
Lua Completions14
Total Stars96
% of votes with a positive feedback rating94% of 802
Total "Very Satisfied" Votes712
Total "Somewhat Satisfied" Votes77
Total "Not Satisfied" Votes13
Total Rank Assessments9
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • JohanWiltink Avatar
  • GiacomoSorbi Avatar
  • donaldsebleung Avatar
  • rowcased Avatar
  • monadius Avatar
  • Awesome A.D. Avatar
  • trashy_incel Avatar
  • user8436785 Avatar
  • KayleighWasTaken Avatar
  • metatable Avatar
Ad