Python Forum
wishing for a 3-way loop construct
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wishing for a 3-way loop construct
#12
I'd still like to see an example, but I felt inspired to show how this might be done in Scala
def handler[T](list: List[T])(f: T => Unit)(g: T => Unit)(h: T => Unit) = {
    f(list.head)
    list.tail.init.map(g)
    h(list.last)
}

handler(List(1, 2, 3, 4, 5))(first => println(first)) { x =>
    println(x * x)
} (last => println(last))
Output:
1 4 9 16 5
The main thing that makes this halfway decent is proper lambdas. The multiple parameter list syntax is nice for the definition, but Python could use nested functions for the definitions.

In Scala, parenthesis and braces are largely interchangeable, so this (lazily named) handler can easily have bigger / smaller parts.

Added in edit: this is just an example, not optimized for performance, cases where the list length is less than tree, or anything else than a super lean example.
Reply


Messages In This Thread
wishing for a 3-way loop construct - by Skaperen - Feb-13-2017, 03:07 AM
RE: wishing for a 3-way loop construct - by Larz60+ - Feb-13-2017, 03:29 AM
RE: wishing for a 3-way loop construct - by nilamo - Feb-13-2017, 04:52 AM
RE: wishing for a 3-way loop construct - by Larz60+ - Feb-13-2017, 05:53 AM
RE: wishing for a 3-way loop construct - by Ofnuts - Feb-13-2017, 08:37 AM
RE: wishing for a 3-way loop construct - by Larz60+ - Feb-13-2017, 08:52 PM
RE: wishing for a 3-way loop construct - by nilamo - Feb-15-2017, 03:58 AM
RE: wishing for a 3-way loop construct - by micseydel - Feb-15-2017, 05:51 AM
RE: wishing for a 3-way loop construct - by Ofnuts - Mar-22-2017, 01:38 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020