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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:55:19+00:00 2026-05-24T13:55:19+00:00

In F#, imagine we have an array of bytes representing pixel data with three

  • 0

In F#, imagine we have an array of bytes representing pixel data with three bytes per pixel in RGB order:

[| 255; 0;   0; //Solid red
   0;   255; 0; //Solid green
   0;   0;   255; //Solid blue
   1;   72;  9; 
   34;  15;  155
... |]

I’m having a hard time knowing how to functionally operate on this data as-is, since a single item is really a consecutive block of three elements in the array.

So, I need to first group the triples in the array into something like this:

[| 
   [| 255; 0;   0   |];
   [| 0;   255; 0   |];
   [| 0;   0;   255 |];
   [| 1;   72;  9   |];
   [| 34;  15;  155 |]
... |]

Now, gathering up the triples into sub-arrays is easy enough to do with a for loop, but I’m curious–is there a functional way to gather up groups of array elements in F#? My ultimate goal is not simply to convert the data as illustrated above, but to solve the problem in a more declarative and functional manner. But I have yet to find an example of how to do this without an imperative loop.

  • 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-24T13:55:20+00:00Added an answer on May 24, 2026 at 1:55 pm

    kvb’s answer may not give you what you want. Seq.windowed returns a sliding window of values, e.g., [1; 2; 3; 4] becomes [[1; 2; 3]; [2; 3; 4]]. It seems like you want it split into contiguous chunks. The following function takes a list and returns a list of triples ('T list -> ('T * 'T * 'T) list).

    let toTriples list = 
      let rec aux f = function
        | a :: b :: c :: rest -> aux (fun acc -> f ((a, b, c) :: acc)) rest
        | _ -> f []
      aux id list
    

    Here’s the inverse:

    let ofTriples triples =
      let rec aux f = function
        | (a, b, c) :: rest -> aux (fun acc -> f (a :: b :: c :: acc)) rest
        | [] -> f []
      aux id triples
    

    EDIT

    If you’re dealing with huge amounts of data, here’s a sequence-based approach with constant memory use (all the options and tuples it creates have a negative impact on GC–see below for a better version):

    let (|Next|_|) (e:IEnumerator<_>) =
      if e.MoveNext() then Some e.Current
      else None
    
    let (|Triple|_|) = function
      | Next a & Next b & Next c -> Some (a, b, c) //change to [|a;b;c|] if you like
      | _ -> None
    
    let toSeqTriples (items:seq<_>) =
      use e = items.GetEnumerator()
      let rec loop() =
        seq {
          match e with
          | Triple (a, b, c) -> 
            yield a, b, c
            yield! loop()
          | _ -> ()
        }
      loop()
    

    EDIT 2

    ebb’s question about memory use prompted me to test and I found toSeqTriples to be slow and cause surprisingly frequent GCs. The following version fixes those issues and is almost 4x faster than the list-based version.

    let toSeqTriplesFast (items:seq<_>) =
      use e = items.GetEnumerator()
      let rec loop() =
        seq {
          if e.MoveNext() then
            let a = e.Current
            if e.MoveNext() then 
              let b = e.Current
              if e.MoveNext() then
                let c = e.Current
                yield (a, b, c)
                yield! loop()
        }
      loop()
    

    This has relatively constant memory usage vs a list or array-based approach because a) if you have a seq to start with the entire sequence doesn’t have to be slurped into a list/array; and b) it also returns a sequence, making it lazy, and avoiding allocating yet another list/array.

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

Sidebar

Related Questions

Imagine I have an array of data. Each item in it is another array
Imagine you have a large array of floating point numbers, of all kinds of
I’m working on a multi dimensions array but i have a problem Imagine I
i have an array of points in 3d (imagine the trajectory of a ball)
Imagine you have int[] data = new int [] { 1, 2, 1, 1,
Imagine I have an JS array like this: var a = [1, 2, 3,
Imagine I have an array: A = Array(1, 2, 3, 4, 5, 6, 7,
Imagine I have a vertex array and an index array. Suppose it is possible
Imagine I have a member variable @property(nonatomic,retain)NSArray *array; Now in my viewDidLoad I set
Imagine I have a simple 4x3x2 array in R. > x <- array(1:24,c(4,3,2), dimnames=list(c('a','b','c','d'),c('x','y','z'),1:2))

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.