How to filter to multiple paths using functional programming

MotKohn

I have the following pseudo-code:

let array = getData();
array.filter(x => condition1(x)).doSomething1...
array.filter(x => condition2(x)).doSomething2...
array.filter(x => condition3(x)).doSomething3...

Obviously this is not efficient because it iterates the array 3 times.

I was wondering if I there is a way to do something like:

array.filterMany([
    x => condition1(x).doSomething1...,
    x => condition2(x).doSomething2...,
    x => condition3(x).doSomething3...
])

So that the array gets iterated only once?

Naftali aka Neal

You can use array reduce functionality.

For example:

    const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    
    const split = arr.reduce(([odd, even], current) => {
        if (current % 2 === 0) {
           even.push(current);
        } else {
           odd.push(current);
        }
    
        return [odd, even];
    }, [[], []]);
    
    console.log('Odd / Even', split);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to properly replace 'extends', using functional programming?

Stream and Filter using Functional Programming for Server object in List<Server>

Java - Functional Programming (Map, Filter)

find() using Functional Programming

Using functional programming with ggplot

How to scan multiple paths using the @ComponentScan annotation?

bash - how to build paths using multiple variables

Java: How to properly manipulate BigDecimal array using functional programming?

How to rewrite this code in Scala using a Functional Programming approach

How to manage DB connection in Scala using functional programming style?

How to append data in Functional programming style using kotlin with arrow library

How can I get started with functional programming using react?

How to transform this code using a functional approach in the Kotlin programming language

how to merge this two method together using functional programming in java

Iteration using counters in functional programming?

functional programming javascript chain filter operation defaults

javascript convert for loop filter on index to functional programming

Functional Programming - then() between chained filter/map calls

How to use MLFlow in a functional style / functional programming?

How to use for cycles in functional programming?

How to define a function in functional programming?

How to filter an array of objects by the element inside an inner array - Functional Programming Javascript

Passing multiple arguments into Predicate, Functional Programming

Using dplyr filter() in programming

How to filter on multiple fields in AngularJS using $filter('filter')

functional programming, how can I efficiently build multiple lists at once from a single iterator?

How to perform distinct while having multiple paths using Cypher

How to extract the stems out of multiple file paths using pathlib?

How to cmp multiple files with relative paths using bash

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive