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 8734385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:49:53+00:00 2026-06-13T09:49:53+00:00

I translate my problem in this little oversimplified example. Objective is to return only

  • 0

I translate my problem in this little “oversimplified” example. Objective is to return only the latest computation based on a simple function execution on each element of an iterator object.

val l = List(1,2,3,4).toIterator
def myPersonnalMultiply(number:Int) = number * 2

For this list, i want my iterator evaluation return ‘8’

I started to try with a yield , but it seems i cannot compute two things like that :

val result = for (
  e <- l
  computation = myPersonnalMultiply(e)
if ((l.hasNext) == false) yield computation

So, there is the solution to map then slice to conserve only the latest element evaluated, but after discussion with a collegue, it seems this is not really beautiful functionnal code compliant, and there is a better approach to do that (really ? ).

Based on this, i want to iterate and compute my function on all element of the list, but only the latest computation is returned.

Edit : my exemple is “too oversimplified”, sorry for this mistake.

In reality, equivalent to myPersonallMultiply function equal an execution of a simulation which can need current state, or the previous state to be computed.

The states function of my simulator return an iterator of state :

 def states(implicit aprng: Random = new Random): Iterator[State] = {

    // Initial State loaded from file
    val loadedState = initState(this.getClass.getClassLoader.getResourceAsStream("init-situation.txt"))
    val territory = computeTerritory(loadedState)

    def simulationStep(state: State): State = {
        // 3b - Apply on each city the function evolveCity() which contain the sequence of action for each city
        // and return a list of tuple (city, exchange) which represent a new state at time t+1 for these cities
        val evolved =
          state.cities.map {
            city => evolveCity(city.id, state.cities, territory(city.id), state.date)
          }

        // 4 - take only the first object (cities) in the list "evolved"
        new State {
          val cities = evolved.map { case(city, _) => city }
          val date = state.date + 1
        }        
    }

    def ended(state: State) = {
      val maxInnov = maxInnovation(state.cities)
      val maxCity = maxCities(state.cities)

      // 3a - Break the simulation loop if one of these conditions is true
      state.date >= 4000 || /*maxPop > 70.0 || */ maxInnov > maxInnovation
    }

    // 1 - Launch the recursive simulation loop with initial loaded step, a the date 1
    val initialState: State = new State {
      val cities = loadedState
      val date = 1
    }

    Iterator.iterate(initialState)(simulationStep).takeWhile(s => !ended(s))
  }

When you compute each of this state (step of my simulation = 1 state) you need to store/write result anywhere, so i encapsulate run of simulation into a Writer class, here a ‘CSVwriter’.

class CSVWriter(path: String, idExp:Int, seed:Long) {

  def apply(s: Simulation)(implicit aprng: Random) = {
    val writer = new BufferedWriter(new FileWriter(new File(path)))

    // TODO: Reader Nomad ? http://mergeconflict.com/reading-your-future/
    // Run stepWriter on each state, and write the result
    // Actually this code run the writer but don't return the final state ...

    try {
      writer.append("v_idn, v_exp, v_ticks, v_seed, v_pop" + "\n")
      s.states.foreach(stepWriter(_, writer))
    } finally writer.close
   }

  def stepWriter(dataToWrite: State, writer: Writer) = {
    writer.synchronized {
      val cities = dataToWrite.cities
      val year = dataToWrite.date
      cities.map {
        c => writer.append(List[Any](c.id.toInt, idExp.toInt, year.toInt, seed, c.population).map{_.toString}.mkString(",") + "\n")
      }
    }
  }

}

1 – As you can see, here, each step don’t need the previous state, but in a really near future, this is the case : for example, to compute new exchange between our cities in our simulation a state T, we need to access to the exchanger created at the previous state T-1

2 – I need to return the last state to compute some score for my simulation, so i need the CSVWriter class write each state returned by s.states, and return also the last state computed.

3 I think it’s possible to create a better and more generic solution for this problem : perhaps the reader monad can help me to create a better interface for this complex writer behaviour for state iterator but perhaps i mistake ?

I hope my question is more clear.

After some research i found the monad reader functionnal pattern, but if i understand globally the interest of this approach, i don’t understand how i can translate the different example i read on the web ( like the first example here http://mergeconflict.com/reading-your-future/ ) to this simple problem. I try different code without success at this time :/

Do you have some simple explanation or pointers to help me understand the ‘monad reader’ ?

Or perhaps i totally mistake, and i can’t resolve my problem with this approach ?

  • 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-06-13T09:49:55+00:00Added an answer on June 13, 2026 at 9:49 am

    Maybe you oversimplified your case, but if you need to process all the elements indipendently (meaning that the result of your operation on the ith-element is not needed for operation on the ith+1-element) you don’t need a monad.

    You can simply operate on all elements (even in parallel!), put the results back and get the last
    one

    def doAndGetLast[A, B](input: List[A])(computation: A => B): B = 
        (input.par map computation).seq.last
    
    val inputs = List(1,2,3,4)
    
    def myMul(n: Int) = n * 2
    
    val withInputs: (Int => Int) => Int = doAndGetLast(inputs) _
    
    val result = withInputs(myMul) // or directly doAndGetLast(inputs)(myMul)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this little problem making this to work: I have silverlight client
The Code I have this problem, I am trying to create a simple rectangle
I have a problem with this algorithm: function crc16 = crc16eval(D) % CRC16EVAL CRC-CCITT
So I'm getting experience on this stuff little by little, but this problem seems
I've got a little objective-c utility program that renders a convex hull. (This is
I got a problem to translate the default validation messages in mongoid. I create
What is the best way to translate the following problem in SQL table structure:
Help me translate following block of the Haskell code. The run function produces text
Does anyone have a translate function for x/y positions after rotation in javascript? for
i have to translate this EBNF to bison: <compound-statement> ::= begin [ <statement> (

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.