break out of filter() function

Daniel

I'd like to optimize my functions which rely on filter(). In some cases I want to break out of them when they reach a certain elements. (For example, I may have an array of distinct elements. Or, I just want to implement a kind of findFirst functionality.) In such instances it seems inefficient for the function to continue until the end of the array.

This is something very easy with loops, but I would like to apply the optimisations to functional programming principals. (The compiler wouldn't be able to perform such a optimization by itself, since it doesn't know my array and my intention.)

Can this be done?

Airspeed Velocity

filter isn’t written to break out like that. I don’t believe there’s an out-of-the-box way to do the kind of thing you’re looking for.

Generally it’s better to avoid making functions more flexible to cover narrow cases. Adding early bail might be useful sometimes but would come at the cost of complicating filter, and the code would probably be hard to read (bear in mind one of the main goals of using functions like filter, map etc is to make the code easier to read and to be certain the code is correct). Some functions do support early exit though, when it’s fundamental to their purpose - for example, contains or indexOf.

But it’s not too hard to create your own higher-order functions to do what you want, the name of which makes their intent pretty clear. For example, to take all the elements in a sequence up to the first one that doesn’t match a pattern, you could write takeWhile like this:

extension SequenceType {
    func takeWhile(condition: Generator.Element -> Bool) -> [Generator.Element] {
        var result: [Generator.Element] = []
        for x in self {
            guard condition(x) else { break }
            result.append(x)
        }
        return result
    }
}



let nums = [1,3,1,2]
let isOdd = { $0%2 == 1 }
let initialOdd = nums.takeWhile(isOdd)
print(initialOdd)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"break;" out of "if" statement?

From Dev

Break out of overflow:hidden

From Dev

Break out of a while loop using a function

From Dev

How to break out of a doseq?

From Dev

Jquery: How to break out of each() and the function it's inside in?

From Dev

SAS Function to filter out dates

From Dev

Break out of select loop?

From Dev

Break Out of then promises in Angularjs

From Dev

How to break out of the IF statement

From Dev

Break out of chained functions

From Dev

Break out of an if statement

From Dev

How to break out of a sourced Bash script's function

From Dev

How to break out a function without return or break in Python

From Dev

How to break out of parent function?

From Dev

Break Out of Function After Returning Promise - JavaScript

From Dev

Trying to use trap to break out of a shell function

From Dev

Break out of synchronized block?

From Dev

JSTL Break out of loop

From Dev

How to break out of a doseq?

From Dev

How can I break out of forEach or re structure my function?

From Dev

Powershell function would not break out of loop

From Dev

Break out of function that calls eval from within the eval

From Dev

Declare as local var will break a function and log out "1: number expected"

From Dev

How to break out of loop if a function returned a value

From Dev

Break Out of Function After Returning Promise - JavaScript

From Dev

Break out of parent function?

From Dev

JS Break out of function

From Dev

split filter callback function out

From Dev

Javascript filter function to filter out falsy values