Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programmed mapper #22

Open
maoyang opened this issue Jun 12, 2016 · 1 comment
Open

Programmed mapper #22

maoyang opened this issue Jun 12, 2016 · 1 comment

Comments

@maoyang
Copy link
Contributor

maoyang commented Jun 12, 2016

Kata 來源 https://www.codewars.com/kata/programmed-mapper

Description:

Write a function mapEmUp that conditionally maps an array of inputs according to a set of supplied conditions. The function takes in two arguments:

An array of inputs
An array of key-value pairs of predicates and mappers
For example:

mapEmUp([1, 2, 3], [
                    [ function (i) { return i>2; },
                    function (i) { return i; }],
                    [ function (i) { return i<2; },
                    function () { return 0; }]
                  ]);

returns

[0, -1, 3]

Each item in the second argument array is an array itself, where the first element is a predicate and the second element is a mapper. The function mapEmUp should pick up each item from the supplied array and run it against each item in the second argument. Where a predicate turns out to be true, the corresponding mapper is applied. Note that the search only continues until a match is found. When there is no predicate matching a specific input, return -1;

說明

mapEmUp是一個function, 第一個參數是一個陣列為input的值, 第二個parameter是一個key-value所組成的陣列(predicate, mapper)用來測試輸入的值, 當predicate function條件符合就會由mapper function傳回值
例如這個題目的輸入 [1,2,3] , 就將值傳入測試, 結果傳回 [0,-1,3]

@vampireneo
Copy link
Member

Pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants