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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:04:44+00:00 2026-05-13T21:04:44+00:00

I am trying to write an efficient algorithm that will effectively let me merge

  • 0

I am trying to write an efficient algorithm that will effectively let me merge data sets (like a sql join). I think I need to use Array.tryFindIndex, but the syntax has me lost.

Based on the data below, I am calling arrX my “host” array, and want to return an int array that has its length, and tells me the positions of each of its elements in arrY (returning -1 if its not in there). (Once I know these indices I can then use them on arrays of data that of length arrY.length)

let arrX= [|"A";"B";"C";"D";"E";"F"|]
let arrY = [|"E";"A";"C"|];


let desiredIndices = [|1; -1; 2; -1; 0; -1|]

It looks like I need to use an option type somehow, and I think a mapi2 in there as well.
Does anyone know how to get this done? (I think it could be a very useful code snippet for people who are merging data sets from different sources)

Thanks!

//This code does not compile, can't figure out what to do here
let d = Array.tryFindIndex (fun x y -> x = y) arrX
  • 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-13T21:04:45+00:00Added an answer on May 13, 2026 at 9:04 pm

    The tryFindIndex function searches for a single element in the array specified as the second argument. The lambda function gets only a single parameter and it should return true if the parameter is the element you are looking for. The type signature of the tryFindIndex function shows this:

    ('a -> bool) -> 'a [] -> int option
    

    (In your example, you’re giving it a function that takes two parameters of type 'a -> 'a -> bool, which is incompatible with the expected type). The tryFindIndex function returns an option type, which means that it gives you None if no element matches the predicate, otherwise it gives you Some(idx) containing the index of the found element.

    To get the desired array of indices, you need to run tryFindIndex for every element of the input array (arrX). This can be done using the Array.map function. If you want to get -1 if the element wasn’t found, you can use pattern matching to convert None to -1 and Some(idx) to idx:

    let desired = 
      arrX |> Array.map (fun x ->
        let res = Array.tryFindIndex (fun y -> x = y) arrY 
        match res with 
        | None -> -1 
        | Some idx -> idx)
    

    The same thing can be written using sequence expression (instead of map), which may be more readable:

    let desired = 
      [| for x in arrX do 
           let res = Array.tryFindIndex (fun y -> x = y) arrY 
           match res with 
           | None -> yield -1 
           | Some idx -> yield idx |]
    

    Anyway, if you need to implement a join-like operation, you can do it more simply using sequence expressions. In the following example, I also added some values (in addition to the string keys), so that you can better see how it works:

    let arrX= [|"A",1; "B",2; "C",3; "D",4; "E",5; "F",6|] 
    let arrY = [|"E",10; "A",20; "C",30|]
    
    [| for x, i in arrX do 
        for y, j in arrY do
          if x = y then 
            yield x, i, j |]
    
    // Result:   [|("A", 1, 20); ("C", 3, 30); ("E", 5, 10)|]
    

    The sequence expression simply loops over all arrX elements and for each of them, it loops over all arrY element. Then it tests whether the keys are the same and if they are, it produces a single element. This isn’t particularly efficient, but in most of the cases, it should work fine.

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

Sidebar

Ask A Question

Stats

  • Questions 377k
  • Answers 377k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Based on the docs on the project page it looks… May 14, 2026 at 8:49 pm
  • Editorial Team
    Editorial Team added an answer It can't be done. Fortunately, banks face exactly the same… May 14, 2026 at 8:49 pm
  • Editorial Team
    Editorial Team added an answer Just like the jQuery() function, find() takes a CSS selector… May 14, 2026 at 8:49 pm

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.