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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:32:47+00:00 2026-05-14T01:32:47+00:00

How would you make the folowing code functional with the same speed? In general,

  • 0

How would you make the folowing code functional with the same speed? In general, as an input I have a list of objects containing position coordinates and other stuff and I need to create a 2D array consisting those objects.

let m = Matrix.Generic.create 6 6 []
let pos = [(1.3,4.3); (5.6,5.4); (1.5,4.8)]  
pos |> List.iter (fun (pz,py) ->
  let z, y = int pz, int py
  m.[z,y] <- (pz,py) :: m.[z,y]
)

It could be probably done in this way:

let pos = [(1.3,4.3); (5.6,5.4); (1.5,4.8)]
Matrix.generic.init 6 6 (fun z y ->
  pos |> List.fold (fun state (pz,py) ->
    let iz, iy = int pz, int py
    if iz = z && iy = y then (pz,py) :: state else state
  ) []
)

But I guess it would be much slower because it loops through the whole matrix times the list versus the former list iteration…

PS: the code might be wrong as I do not have F# on this computer to check it.

  • 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-14T01:32:47+00:00Added an answer on May 14, 2026 at 1:32 am

    It depends on the definition of “functional”. I would say that a “functional” function means that it always returns the same result for the same parameters and that it doesn’t modify any global state (or the value of parameters if they are mutable). I think this is a sensible definition for F#, but it also means that there is nothing “dis-functional” with using mutation locally.

    In my point of view, the following function is “functional”, because it creates and returns a new matrix instead of modifying an existing one, but of course, the implementation of the function uses mutation.

    let performStep m =
      let res = Matrix.Generic.create 6 6 [] 
      let pos = [(1.3,4.3); (5.6,5.4); (1.5,4.8)]   
      for pz, py in pos do
        let z, y = int pz, int py 
        res.[z,y] <- (pz,py) :: m.[z,y] 
      res
    

    Mutation-free version:
    Now, if you wanted to make the implementation fully functional, then I would start by creating a matrix that contains Some(pz, py) in the places where you want to add the new list element to the element of the matrix and None in all other places. I guess this could be done by initializing a sparse matrix. Something like this:

    let sp = pos |> List.map (fun (pz, py) -> int pz, int py, (pz, py)) 
    let elementsToAdd = Matrix.Generic.initSparse 6 6 sp
    

    Then you should be able to combine the original matrix m with the newly created elementsToAdd. This can be certainly done using init (however, having something like map2 would be maybe nicer):

    let res = Matrix.init 6 6 (fun i j -> 
      match elementsToAdd.[i, j], m.[i, j] with
      | Some(n), res -> n::res
      | _, res -> res )
    

    There is still quite likely some mutation hidden in the F# library functions (such as init and initSparse), but at least it shows one way to implement the operation using more primitive operations.

    EDIT: This will work only if you need to add at most single element to each matrix cell. If you wanted to add multiple elements, you’d have to group them first (e.g. using Seq.groupBy)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 408k
  • Answers 409k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This also works correctly, using a static: <?php interface ICollection… May 15, 2026 at 6:59 am
  • Editorial Team
    Editorial Team added an answer Check this tutorial Creating a Java Stand-Alone Client. Basically you… May 15, 2026 at 6:59 am
  • Editorial Team
    Editorial Team added an answer Yes, from p.223 of the Beamer guide: To remove navigation… May 15, 2026 at 6:58 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.