Function frequency: Difference between revisions

Added Wren
(implement in nim-lang with quirks to be solved)
(Added Wren)
Line 2,136:
lappend 351
</pre><!-- note that it's certainly possible that not all commands are found; break and continue are likely underrepresented -->
 
=={{header|Wren}}==
{{libheader|Wren-pattern}}
{{libheader|Wren-set}}
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
Wren distinguishes between functions and methods.
 
The former are first-class standalone objects whereas the latter are always members of a class and can be either instance or static.
 
Functions are always invoked by their call() method.
 
Some kinds of methods (getters, setters and operators) are not followed by an argument list but, in the interests of simplicity, we only count 'function methods' i.e. methods which are followed by a (possibly empty) argument list for the purposes of this task.
 
In the absence of any other feasible approach, we simply search for method/function calls in a given Wren source file and count them to find the 'top ten'.
<lang ecmascript>import "io" for File
import "os" for Process
import "/pattern" for Pattern
import "/set" for Bag
import "/sort" for Sort
import "/fmt" for Fmt
 
var args = Process.arguments
if (args.count != 1) {
Fiber.abort("There should be exactly one argument - the file path to be analyzed")
}
var p = Pattern.new("[+1/x.+1/x](")
var source = File.read(args[0])
var matches = p.findAll(source)
var bag = Bag.new(matches.map { |m| m.captures[0].text })
var methodCalls = bag.toMap.toList
var cmp = Fn.new { |i, j| (j.value - i.value).sign }
Sort.quick(methodCalls, 0, methodCalls.count-1, cmp)
System.print("Top ten method/function calls in %(args[0]):\n")
System.print("Called Method/Function")
System.print("------ ----------------")
for (mc in methodCalls.take(10)) {
Fmt.print(" $2d $s", mc.value, mc.key)
}</lang>
 
{{out}}
Using the source file for the [https://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface#wren Catmull-Clark task] as an example:
<pre>
Top ten method/function calls in catmull_clark.wren:
 
Called Method/Function
------ ----------------
13 Point.new
8 List.filled
6 sumPoint.call
5 Int.cantorPair
4 mulPoint.call
4 switchNums.call
4 newFaces.add
4 divPoint.call
3 mergedEdges.add
3 centerPoint.call
</pre>
9,487

edits