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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:46:03+00:00 2026-06-07T22:46:03+00:00

This is related to this question: Don't know where to start with mutable Vectors

  • 0

This is related to this question:

Don't know where to start with mutable Vectors

How would I write a function that took values from one vector, transformed them and put the results into a second vector? More specifically, it should iterate over all the indexes of a source array, and hand that index and the array to a function, then store the result of the function in another array.

I think the signature would look something like this:

imapInto :: (PrimMonad m, Unbox a) => (MVector (PrimState m) a -> Int -> a) -> MVector (PrimState m) a -> MVector (PrimState m) a  -> m ()

And would be called something like this:

import Data.Vector.Unboxed.Mutable MV

...
let dst = MV.replicate 10 0.0 in
let src = MV.replicate 10 1.0 in
imapInto (\src' i -> (src' * 2.0)) dst src
...

Which would multiply all the elements in src by 2 and put the results into dst, yielding a state unit.

  • 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-07T22:46:05+00:00Added an answer on June 7, 2026 at 10:46 pm

    I don’t think there is a builtin function which do what you want. But with these primitives it should be possible to write imperative code for your task. And if it will be general enough, you should be able even use it in pure code with create.
    I guess it will be something like (just sketch, untested) this:

    imapInto :: (PrimMonad m, Unbox a) => (MVector (PrimState m) a -> Int -> a) -> MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
    imapInto f d s = go 0
        where
            go i = when (i < length d) $ write d i (f s i) >> go (i+1)
    

    Your type of mapping function seems weird though. Didn’t you mean a -> Int -> a? Then the code above will require some little changes.

    UPDATE

    Here is the example of usage, along with updated version of the above function. The least possible tweak was to add m type constructor to mapper function:

    module Main where
    
    import qualified Data.Vector.Unboxed.Mutable as MV
    import qualified Data.Vector.Unboxed as U
    import Control.Monad (forM_)
    import Control.Monad.Primitive
    
    imapInto :: (PrimMonad m, MV.Unbox a) 
             => (MV.MVector (PrimState m) a -> Int -> m a)
             -> MV.MVector (PrimState m) a 
             -> MV.MVector (PrimState m) a 
             -> m ()
    imapInto f d s = forM_ [0..MV.length s - 1] $ \i -> do
        v <- f s i
        MV.write d i v
    
    main = do
        -- Create two vectors
        v1 <- MV.replicate 10 1
        v2 <- MV.new 10
        -- Map v1 into v2 using function mapper defined below
        imapInto mapper v2 v1
        -- Print the source and the result
        uv1 <- U.unsafeFreeze v1
        uv2 <- U.unsafeFreeze v2
        print $ U.toList uv1   -- [1,1,1,1,1,1,1,1,1,1]
        print $ U.toList uv2   -- [0,1,2,3,4,5,6,7,8,9]
    
        where
            -- Mapper reads a value from the vector and multiplies it by its index
            mapper v i = fmap (*i) $ MV.read v i
    

    Since you’re beginner I tried to be as simple as possible, ask if something is not clear.

    As you can see, I took advantage of Louis’ comment and used forM_ function to make imapInto even more simpler (forM_ is mapM_ with arguments swapped). It now looks like ordinary for loop from imperative languages. Also as I said I changed mapping function type from (MVector (PrimState m) a -> Int -> a) to (MVector (PrimState m) a -> Int -> m a). This is required because you cannot do much with a mutable vector (it is passed as first parameter here) outside its monad. But inside the monad we can do anything with it, and here we are simply reading i-th element and multiplying it by i.

    Note that in this version of function you can do anything with source vector from inside the mapping function. If you do not require this and you only use i-th element in conjunction with i itself, you can simplify this even more:

    imapInto' :: (PrimMonad m, MV.Unbox a, MV.Unbox b) 
             => (a -> Int -> b)
             -> MV.MVector (PrimState m) b
             -> MV.MVector (PrimState m) a
             -> m ()
    imapInto' f d s = forM_ [0..MV.length s - 1] $ \i -> do
        v <- MV.read s i
        MV.write d i (f v i)
    

    Here mapping function is pure and takes only i-th element as first argument, not the vector itself. Also I’ve generalized this version, so it can map first vector into a vector with another type, so source and destination vectors are not required to be of the same type. This is something which could be done to the previous version, too.

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

Sidebar

Related Questions

I don't know is this question more related to mathematics or programming and I'm
Actually I don't know whether my question is related to this thread. I want
I have a question related to this one . I don't want to do
I found this related question: How do I use composition with inheritance? I would
This is a related question to: Android NFC start service In Android, can an
Important : Please see this very much related question: Return multiple values in C++
I know that there are several very similarly-related questions on this website, however after
This question is an extension of this Related question . Taking Derick's advice, I
I have searched here, GooBingHooVista'd the world and read this related question for VS
This is a related question to this but with EXISTS instead of IN SELECT

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.