Leveraging AI techs to create cross code references

Leveraging AI techs to create cross code references

Table of contents

No heading

No headings in the article.

AI does a great job relating a lot of information, We can do the same and even better, this requires work, enhance memory, and creating cross reference between concepts.

Example: With a LLM you can take a description of a code and the snippet itself and ask for the same version in different languages, even a better code snippet. This way you create cross references of the same code.

Prompt:

Get all the ancestors of the element in typescript, rust, golang, python, propose a better javascript version than the next one:

const getAncestors = el => { 
    let ancestors = []; 
    while (el) { 
        ancestors.unshift(el); el = el.parentNode; 
    } 
    return ancestors; 
}; 
getAncestors(document.querySelector('nav'));

Answer:

TypeScript:

const getAncestors = (el: Element | null): Element[] => {
  let ancestors: Element[] = [];
  while (el) {
    ancestors.unshift(el);
    el = el.parentElement;
  }
  return ancestors;
};

const element = document.querySelector('nav');
const ancestorsTS = getAncestors(element);
console.log(ancestorsTS);

Rust:

fn get_ancestors<'a>(el: Option<&'a web_sys::Element>) -> Vec<&'a web_sys::Element> {
    let mut ancestors = Vec::new();
    let mut current_el = el;

    while let Some(el) = current_el {
        ancestors.insert(0, el);
        current_el = el.parent_element();
    }

    ancestors
}

// Assuming you're using the web-sys crate for web development in Rust
let element = document.query_selector("nav").unwrap();
let ancestors_rust = get_ancestors(Some(&element));

Go (Golang):

codepackage main

import "fmt"

func getAncestors(el *html.Node) []*html.Node {
    ancestors := []*html.Node{}
    for el != nil {
        ancestors = append([]*html.Node{el}, ancestors...)
        el = el.Parent
    }
    return ancestors
}

func main() {
    element := // get your element here
    ancestors := getAncestors(element)
    fmt.Println(ancestors)
}

Python:

pythonCopy codedef get_ancestors(el):
    ancestors = []
    while el:
        ancestors.insert(0, el)
        el = el.parentNode
    return ancestors

# Assuming you have a way to get the element in Python
element =  # get your element here
ancestors_py = get_ancestors(element)
print(ancestors_py)

Better version:

const getAncestorsImproved = (el) => {
  const ancestors = [];
  while (el && el.parentNode) {
    ancestors.unshift(el);
    el = el.parentNode;
  }
  return ancestors;
};

const element = document.querySelector('nav');
const ancestorsImproved = getAncestorsImproved(element);
console.log(ancestorsImproved);

The proposed JavaScript version checks el.parentNode within the loop condition, avoiding potential issues if el is null or undefined. This makes the code more robust and concise.

That's it for today you get a core concept as this one and apply it holistically several times until get a big code library on your memory.

/