Hi there. I am just starting to learn nim, and I am asking the GPT-3.5-turbo to write a function for me (see the following). I am impressed with the result, although it does not compile. What is the correct way to write this function?
01:user: when I write code in nimlang, how to find the functions that take a certain type object as its first argument, for example, "split" take a object of "string" type
02:assistant: In Nim, you can use the find function from the system module to search for functions that take a certain type object as their first argument. Here's an example of how you can use it to find functions that take a string object as the first argument:
import system
proc findFunctionsWithType[T](t: T): seq[string] =
var result: seq[string] = @[]
for sym in allSymbols():
if sym.kind == skProc and sym.name != "main":
let procType = sym.type
if procType.paramCount > 0 and procType.paramTypes[0] == typeof(t):
result.add(sym.name)
return result
# Example usage:
let functions = findFunctionsWithType("".type)
echo functions
In the above code, the findFunctionsWithType procedure takes a type object t as its argument. It iterates over all symbols in the program using the allSymbols function from the system module. For each symbol that represents a procedure (sym.kind == skProc) and is not the main procedure, it checks if the first parameter type of the procedure matches the type t. If it does, the procedure name is added to the result sequence.
You can then call the findFunctionsWithType procedure with the type object of string ("".type) to find functions that take a string as the first argument. The resulting sequence of function names is printed using echo.
I'm perfectly happy to help here, but given that you seem to want to rely on AI tooling from the jump I would prefer if I could give you an explanation that actually helps with your understanding of the language.
Could you rephrase what you hope to achieve, how you interpret the currently generated code and how it helps you achieve your goal?
Thank you!
I use python and julia as my main language. In python, I find the following snippet very helpful.
from rich import inspect as ri
alist = [1, 2, 3]
ri(alist, help=True, methods=True)
As for nim, I want to know which function will act on alist(seq). I would like to use IDE for such purpose, but I only use sublime text and encounter some problem setting up the config (https://github.com/PMunch/nimlsp/issues/167).
Just browse the official documentation and focus on these modules for seq:
system.nim, sequtils.nim, algorithm.nim
Or come up with the verb you need to use, why do you care if e.g. deduplicate exists for seq when you don't need to deduplicate seq entries...
Thanks for the suggestions, I am honoured to hear from you.
I will try to familiarise myself with the std documentation.
Ah, you're trying to apply a python workflow to a compiled language like nim. Basically ideally you'd want to have runtime introspection like go into a repl and get a list of all methods defined for a type.
This is sadly not quite possible given how nim works in general, due to being a more low-level language (at least afaik that's the reason). Typically you will have to browse the docs.
Or rather what I typically do, is I go to the general docs site and use the search bar there to look for keywords.
If that fails, I scan the overview of modules in there for one that sounds like what I want to do/work with. Like sequtils looks correct for finding out more stuff about seqs (the equivalent for python list).
Note that the explainers in nim for python programmers may also help you some.