1
alert
4y

Python builtins are great
But why the fuck there is STILL no first() function
It's mildlyinfuriating

Comments
  • 3
    What that function should be doing?
  • 0
    It's pretty easy to write a function that returns at the first match in a set. Get on it ;)
  • 0
    @SortOfTested yeah I know, but that's so obvious it should be in builtins
    If we have any(), then why no alternative that returns the element
  • 1
    @iiii there is a function in python builtins called any() which returns True if any of the elements in iteratable are True
    first() should be it's equivalent, but return the element itself instead of a boolean
  • 0
    @dhvcc I'm suggesting a wrapper buut it's in JS. Think it like a psuedo code.

    @highlight
    /* global:somePatternMatcher*/
    //Use this to match element

    /* global:set*/
    //The set we are searching in

    //the this context is Set.
    function first(matcher){
    let foundElement;
    this.any(elem =>{
    return matcher(elem) &&
    (foundElement= elem)
    });
    return foundElement;
    }

    //usage 1. (Not recommended)

    Set.prototype.first = first;
    set.first(somePatternMatcher);

    //usage 2.

    var setFirst = first.bind(set);
    setFirst(somePatternMatcher);
  • 0
  • 1
    @melezorus34

    I don't need the code for the function, I can write it myself

    I'm just ranting that it's not in builtins :)

    We already have itertools, with filter and other goodies

    But built-in first would be a much cleaner way of doing this
  • 0
    @dhvcc
  • 0
    Whoops, didn't notice it was a bot
  • 1
Add Comment