proc fft(input: seq[complex[float]]): seq[complex[float]] =
let n = input.len
if n == 1:
return input
var even = @[input[i] | i in 0 ..< n by 2]
var odd = @[input[i] | i in 1 ..< n by 2]
var y = fft(even) & fft(odd)
for k in 0 ..< n div 2:
let t = y[k]
let angle = -2.0 * math.pi * k / n
let u = y[k + n div 2] * exp(complex(0.0, angle))
y[k] = t + u
y[k + n div 2] = t - u
return y
Thoughts?
HTML parser...
import htmlparser
type
NodeKind = enum
textNode, elementNode
Node = object
kind: NodeKind
case kind:
of textNode:
text: string
of elementNode:
tag: string
attrs: seq[(string, string)]
children: seq[Node]
proc parseHTML(html: string): Node =
var root = Node(kind: elementNode, tag: "root", attrs: @[], children: @[])
var stack = @[root]
var parser = newHtmlParser(html)
while parser.nextNode():
case parser.nodeType():
of htmlText:
let text = parser.text()
if text.stripLen > 0:
let node = Node(kind: textNode, text: text)
stack.last.children.add(node)
of htmlStartTag:
let tag = parser.tagName().lower()
var attrs: seq[(string, string)] = @[]
while parser.nextAttr():
let name = parser.attrName().lower()
let value = parser.attrValue()
attrs.add((name, value))
let node = Node(kind: elementNode, tag: tag, attrs: attrs, children: @[])
stack.last.children.add(node)
stack.add(node)
of htmlEndTag:
stack.pop()
else:
# Ignore other node types
return root.children[0]
var even = @[input[i] | i in 0 ..< n by 2]
var odd = @[input[i] | i in 1 ..< n by 2]
It still uses the old list comprehension syntax :(chat-gpt is convincingly wrong at best with anything more than basic examples.
I was very very positive surprised about the Nim knowledge of GPT, so I immediately added a note at the end of the about section: https://ssalewski.de/nimprogramming.html#_about_this_book. GPT seems to write much better texts than my own ramblings :-)
The first one is quite close actually. Besides the comprehension, there is the change math.pi -> math.Pi and complex -> Complex.
import std/[complex, sugar, math]
proc fft(input: seq[Complex[float]]): seq[Complex[float]] =
let n = input.len
if n == 1:
return input
var even = collect(for i in countup(0, n-1, 2): input[i])
var odd = collect(for i in countup(1, n-1, 2): input[i])
var y = fft(even) & fft(odd)
for k in 0 ..< n div 2:
let t = y[k]
let angle = -2.0 * math.Pi * (k / n)
let u = y[k + n div 2] * exp(complex(0.0, angle))
y[k] = t + u
y[k + n div 2] = t - u
return y
The second example is stranger. There is no such API in std/htmlparser (it would simply be htmlparser.parseHtml(string)), there is something similar in std/xmlparser but it would need a significant rewrite at first glance.
GPT-4 with Karax simple example. I had to tell is about $ though.
import karax / [vdom, jdict]
type
Person = object
firstName, lastName: string
proc renderPerson(p: Person): VNode =
result = buildHtml(tdiv):
text("Full Name: ")
text(p.firstName & " " & p.lastName)
# Example usage
let person1 = Person(firstName: "John", lastName: "Doe")
echo $person1.renderPerson()
I asked it to write ruby code to turn string into ngrams.
def string_to_ngrams(string, n)
# Convert string to lowercase and remove all non-alphanumeric characters
string = string.downcase.gsub(/[^a-z0-9]/, '')
# Initialize an empty array to store n-grams
ngrams = []
# Iterate over the string, creating n-grams of length n
(0..string.length-n).each do |i|
ngrams << string[i...i+n]
end
# Return the list of n-grams
return ngrams
end
Quite good... I think it maybe 5-10 years for modern computer languages left. The future programming languages going to be significantly different.
Those performance optimisations are trivial things, and machines surely will be able to do it, if not now, soon enough.
It's also impressive, that it was able to answer the second question, and explain why it did what it did.
Diese Implementierung verwendet einen rekursiven Ansatz namens Cooley-Tukey FFT-Algorithmus. Die Funktion nimmt eine Eingabefolge komplexer Zahlen und teilt sie rekursiv in gerade und ungerade indizierte Elemente auf, berechnet die FFT für jede Hälfte und kombiniert dann die Ergebnisse mithilfe von Twiddle-Faktoren, um die endgültige FFT-Ausgabe zu erzeugen.
Bitte beachten Sie, dass es sich hierbei um eine vereinfachte Implementierung und möglicherweise nicht um die optimierteste Version handelt. Es sollte Ihnen ein grundlegendes Verständnis der Funktionsweise des FFT-Algorithmus vermitteln. Sie können den Code jederzeit nach Bedarf für Ihren spezifischen Anwendungsfall ändern und optimieren. https://nachrichtenmorgen.de/