News for hackers. Stuff that matters.
Re:Python: Uncovering the Overlooked Core Functionalities by kragen on Monday July 24, 2023 @09:01PM (#36856432)
any, all, map, filter, min, max, for loops, zip, list, tuple, reduce, list comprehensions, cycle, repeat, islice, and so on in python work on iterables, and iterable is a protocol, not a class. it would certainly be interesting to program in a language where conforming to a protocol (perhaps one that nobody had thought up yet when you wrote your class) would give your class new methods, or where all iterables had to derive from a common base class, but it would be a very different language from python

incidentally in your example, though data does flow from top to bottom, control does not, assuming the filter and map methods are lazy as they are in python; it ping-pongs back and forth up and down the sequence in a somewhat irregular manner, sometimes reaching as far as .min() before going back up, and other times turning around at .filter(...)

i wonder if you could implement the ide functionality you want with a 'wrap' menu of popular functions that are applicable to the thing to the left of your cursor, so when you had

    filter(some_filter, bar.baz())|
(with | representing your cursor) you could select `map` or `min` or whatever from the wrap dropdown and get

    min(filter(some_filter, bar.baz()))|
for any given cursor position in python there are potentially multiple expressions ending there, in cases like

    "y: %s" % y|
but maybe that's not such a hard problem to solve
Re:Python: Uncovering the Overlooked Core Functionalities by hwayne on Monday July 24, 2023 @09:21PM (#36856604)
> it would certainly be interesting to program in a language ... where all iterables had to derive from a common base class, but it would be a very different language from python

You mean Ruby? :P

(All Ruby iteratables mixin Enumerable, which is baaaaaaaasically inheritance.)

Re:Python: Uncovering the Overlooked Core Functionalities by josephg on Monday July 24, 2023 @11:58PM (#36857691)
Or Rust! Everything that implements the Iterator trait gets access to all of Iterator’s goodies, like map, filter, reduce, etc. Implementing iterator just requires adding a single next(&mut self) -> Option<Item> method on your type.

Lifetimes and async are a massive pain in rust. But the trait system is a work of art.

Re:Python: Uncovering the Overlooked Core Functionalities by kragen on Tuesday July 25, 2023 @09:21AM (#36861815)
ruby is closer to what i meant because you can't add methods to rust's iterator, can you? but people add stuff to enumerable all the time
Re:Python: Uncovering the Overlooked Core Functionalities by josephg on Tuesday July 25, 2023 @01:14PM (#36865740)
You can!

    trait MyIterHelpers: Iterator {
        fn dance(&self) {
            println!("wheee");
        }
    }
    
    // And tell rust that all Iterators are also MyIterHelpers.
    impl<I: Iterator> MyIterHelpers for I {}
The one caveat is that using it in a different context will need a use crate::MyIterHelpers; line, so the namespace isn't polluted.
Re:Python: Uncovering the Overlooked Core Functionalities by kragen on Tuesday July 25, 2023 @07:03PM (#36870951)
neat, i didn't know that was possible
Re:Python: Uncovering the Overlooked Core Functionalities by zelphirkalt on Wednesday July 26, 2023 @07:38AM (#36876018)
I like Rust's struct + traits approach, because they avoid inheritance and encourage composition. I am sure people have built bad workarounds though to do inheritance anyway.
Re:Python: Uncovering the Overlooked Core Functionalities by pjmlp on Tuesday July 25, 2023 @02:37AM (#36858758)
Or its inspiration, Smalltalk.
Re:Python: Uncovering the Overlooked Core Functionalities by pid-1 on Tuesday July 25, 2023 @07:06AM (#36860571)
> it would certainly be interesting to program in a language where conforming to a protocol (perhaps one that nobody had thought up yet when you wrote your class)

Not automatic, but you could use a decorator + the protocol as type annotation, I think

Re:Python: Uncovering the Overlooked Core Functionalities by rav on Tuesday July 25, 2023 @08:18AM (#36861168)
> i wonder if you could implement the ide functionality you want with a 'wrap' menu of popular functions that are applicable to the thing to the left of your cursor

This is already implemented in IntelliJ for Java - they call it "Postfix Completion". For example you can type ".cast" after an expression to wrap what's before the cursor in a cast expression, so type "a + b.cast", then pick cast to "float", and pick how large a preceding expression you want to cast, and you can end up with "(float)(a + b)" and go from there. They have postfix completion that can extract expressions into variables, create if-statements and switch-statements from expressions, and so many more things that I wish I had when doing non-trivial Python coding in my IDE of choice (which is not by Jetbrains)...