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

  • SEARCH
  • Home
  • 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

We have a very high performance multitasking, near real-time C# application. This performance was
I have this idea of writing an application to automatically sync files to 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 problem concerning python packages and testing. I'm writing an application using wx
I have come across an annoying problem while writing some PHP4 code. I renamed
I have a problem, which is not easily described. I'm writing a web application
I have a problem, that I can't solve. I am writing an application that
I have a Qt desktop application that works on Linux and Windows. At some
I'm writing a web crawler for a specific site. The application is a VB.Net

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.