Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 582113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:39:08+00:00 2026-05-13T14:39:08+00:00

I have problems with writing an specific application in a scala-esque and elegant way.

  • 0

I have problems with writing an specific application in a scala-esque and elegant way. I tried this for some time now, but I cannot find a “good” solution to this Problem:

Given that I have the following List:

List("foo", "bar", "baz", "blah")

I want to Iterate over this list, not only giving me the current element for each Iteration but also the element before and after the current element. This might be a Tuple3 but is not required to. This could be the Tuple signature:

(Option[T], T, Option[T])

To clarify what i mean, this is the proposed Tuple for each iteration over a List[String], ending after the fourth.

Iteration 1: (None, "foo", Some("bar"))

Iteration 2: (Some("foo"), "bar", Some("baz"))

Iteration 3: (Some("bar"), "baz", Some("blah"))

Iteration 4: (Some("baz"), "blah", None)

How could I achieve such a result? Again: I am not bound to the Tuple3, any other solution is also very appreciated!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:39:09+00:00Added an answer on May 13, 2026 at 2:39 pm

    Here’s one approach. It uses a new Scala 2.8 collection method sliding.

    def window[A](l: List[A]): Iterator[List[Option[A]]] = 
       (None :: l.map(Some(_)) ::: List(None)) sliding 3
    
    window(List(1, 2, 3, 4, 5)).toList
    
    // List(List(None, Some(1), Some(2)), List(Some(1), Some(2), Some(3)), List(Some(2), Some(3), Some(4)), List(Some(3), Some(4), Some(5)), List(Some(4), Some(5), None))
    

    Update: Heres a version that works for Streams.

    def windowS[A](s: Stream[A]): Stream[List[Option[A]]] = 
      (None #:: s.map(Some(_): Option[A]) #::: Stream(None: Option[A])).sliding(3).toStream.map(_.toList)  
    
    val posInts = Stream.range(1, Integer.MAX_VALUE)
    windowS(posInts).take(5).toList
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a Java Desktop Application but have some confusions in choosing a
I'm writing some client-server-application where I have to deal with multiple threads. I've got
I have problem in some JavaScript that I am writing where the Switch statement
I have a asp.net page I'm writing and I'm perplexed by this problem. I
I have some problems with Miktex installed on Windows Vista Business SP1/32 bit. I
I have some problems on a site with the concurrent access to a list.
I'm writing a GUI application that will have a user log in feature. Each
I'm currently writing a PyGTK application and I'd like some advice as to the
i have a input tag which is non editable, but some times i need
When writing GUIs, I've frequently come over the following problem: Assume you have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.