Rosetta Code/Find bare lang tags: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (Added syntax highlighting to Mathematica entry which 'magically' makes previously hidden entries for: Nim, Objeck, Perl, Phix and Python reappear!) |
m (→{{header|Wren}}: Minor tidy) |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 39:
;Extra extra credit:
Use the [
<br><br>
Line 75:
=={{header|Erlang}}==
<
-module( find_bare_lang_tags ).
Line 104:
Stop = string:rstr( Line, "}}==" ),
string:sub_string( Line, Start+1, Stop-1 ).
</syntaxhighlight>
</lang>▼
{{out}}
<pre>
Line 114:
=={{header|Go}}==
<
import (
Line 212:
fmt.Printf(" %d in %-11s %v\n", v.count, k, *v.names)
}
}</
{{out}}
Line 257:
=={{header|Groovy}}==
{{trans|Kotlin}}
<
import java.util.regex.Matcher
import java.util.regex.Pattern
Line 312:
}
}
}</
{{out}}
<pre>2 in Scilab
Line 329:
This solution can be compiled into a program that will either take space-delimited list of files as its argument, or take input from STDIN if no arguments are provided. Additionally, if you specify the -w flag in the first argument, it will take a list of Rosetta Code wiki pages and search them. Note that the page names must be as they appear in your URL bar -- underscores in place of spaces.
<
import Network.HTTP
import Text.Printf
Line 408:
response <- simpleHTTP.getRequest$ url
getResponseBody response
where url = "http://rosettacode.org/mw/index.php?action=raw&title="++title</
Here are the input files I used to test:
Line 470:
The following is a Unicon-specific solution.
<
procedure main()
Line 488:
write(total," bare language tags:\n")
every pair := !sort(tags) do write(pair[2]," in ",pair[1])
end</
Sample run using example given in problem statement:
Line 501:
=={{header|Java}}==
<
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Line 562:
}
}
}</
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def trim: sub("^[ \t]+";"") | sub("[ \t]+$";"");
# Insert into a sorted list using bsearch
def binsert($x):
(-bsearch($x) - 1) as $ix
| if $ix < 0 then .
else .[:$ix] + [$x] + .[$ix:]
end;
def report:
def header:
"==\\s*{{\\s*header\\s*\\|\\s*(?<title>[^\\s\\}]+)\\s*}}\\s*==";
reduce inputs as $line ( { bareCount:0, bareLang: {} };
if .fileName != input_filename
then .lastHeader = "No language"
| .fileName = input_filename
else .
end
| .line = ($line|trim)
| if .line | length == 0 then .
else .header = ((.line | capture(header)) // null)
| if .header
then .lastHeader = .header.title
elif .line|startswith("<lang>")
then .bareCount += 1
| .bareLang[.lastHeader][0] += 1
| .fileName as $fileName
| .bareLang[.lastHeader][1] |= binsert($fileName)
else .
end
end )
| "\(.bareCount) bare language tags:",
(.bareLang
| to_entries[] as {"key": $lang, "value": $value}
| $value[0] as $count
| $value[1] as $names
| ("\($count|lpad(3)) in \($lang|lpad(15))" + ": " + ($names | join(", ")) )) ;
report
</syntaxhighlight>
'''Invocation'''
Using the examples in the [[#Go|Go]] entry:
<pre>
jq -Rnr -f find-bare-lang-tags.jq rc-example1.txt rc-example2.txt
rc-example3.txt
{{output}}
<pre>
5 bare language tags:
2 in No language: rc-example1.txt, rc-example3.txt
1 in Perl: rc-example1.txt
2 in C: rc-example2.txt, rc-example3.txt
=={{header|Julia}}==
<
rosorg = "http://rosettacode.org"
Line 652 ⟶ 716:
println("\nDraft programming tasks:")
qdURI |> getpages |> processtaskpages
</
<pre>
Programming examples at 2019-02-19T06:33:49.951:
Line 766 ⟶ 830:
Total bare <lang> for language zkl: 2
Total bare <lang> for language zonnon: 1
Total bare <lang> for language
Draft programming tasks:
Line 781 ⟶ 845:
Total bare <lang> for language Racket: 1
Total bare <lang> for language uBasic/4tH: 1
Total bare <lang> for language
</pre>
=={{header|Kotlin}}==
<
import java.net.http.HttpClient
import java.net.http.HttpRequest
Line 843 ⟶ 907:
println("Got a ${titleResponse.statusCode()} status code")
}
}</
{{out}}
<pre>1 in 4DOS Batch
Line 856 ⟶ 920:
=={{header|Maple}}==
<
add_lan := proc(language, n, existence, languages, pos)
if (assigned(existence[language])) then
Line 923 ⟶ 987:
end do:
printf("Total number %d", total);
</syntaxhighlight>
▲</lang>
{{Out|Output}}
<pre>15 Puzzle Game is not counted since some language tags are not properly closed.
Line 938 ⟶ 1,002:
There are 3 bare lang tags in PicoLisp
There are 15 bare lang tags in CoffeeScript
There are 29 bare lang tags in
There are 2 bare lang tags in APL
There are 10 bare lang tags in ERRE
Line 1,071 ⟶ 1,135:
1 in 360 Assembly ([[FizzBuzz]])
1 in 4DOS Batch ([[100 doors]])
71 in
2 in 6502 Assembly ([[FizzBuzz]], [[String case]])
2 in 6800 Assembly ([[Hello world/Text]], [[Loops/Infinite]])
Line 1,180 ⟶ 1,244:
=={{header|Nim}}==
{{trans|Julia}}
<
import sequtils, strformat, strscans, tables, times, xmltree
import strutils except escape
Line 1,252 ⟶ 1,316:
echo "\nDraft programming tasks:"
client.processTaskPages(client.getPages(QdUri))</
{{out}}
Line 1,308 ⟶ 1,372:
=={{header|Objeck}}==
With extra credit
<
use Query.RegEx;
use Collection.Generic;
Line 1,402 ⟶ 1,466:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,436 ⟶ 1,500:
=={{header|Perl}}==
This is a simple implementation that does not attempt either extra credit.
<
my $total = 0;
my %blanks = ();
Line 1,457 ⟶ 1,521:
print "$k in $v\n"
}
}</
=={{header|Phix}}==
Line 1,640 ⟶ 1,704:
=={{header|Python}}==
<
"""Count bare `lang` tags in wiki markup. Requires Python >=3.6.
Line 1,953 ⟶ 2,017:
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)
example()
</syntaxhighlight>
{{out}}
Line 1,981 ⟶ 2,045:
1 in Python ['Abelian sandpile model']
1 in GFA Basic ['Abundant, deficient and perfect number classifications']
1 in
1 in Nim ['Active object']
1 in Go ['Address of a variable']
Line 1,992 ⟶ 2,056:
Note that this follows the task, but the output is completely bogus since the actual <tt><lang></tt> tags that it finds are in <tt><pre></tt> and in code...
<
#lang racket
Line 2,021 ⟶ 2,085:
(find-bare-tags "Rosetta Code/Find bare lang tags")
</syntaxhighlight>
{{out}}
<pre>
Line 2,033 ⟶ 2,097:
===More-extra credit===
Add the following code at the bottom, run, watch results.
<
(define (get-category cat)
(let loop ([c #f])
Line 2,050 ⟶ 2,114:
(printf "Page: ~a " page)
(find-bare-tags page))
</syntaxhighlight>
=={{header|Raku}}==
Line 2,056 ⟶ 2,120:
{{trans|Perl}}
The only tricky thing here is the use of the <tt>ms</tt> form of match, short for <tt>m:sigspace</tt>. This causes whitespace in the regex to be considered "significant", that is, it matches optional whitespace at those positions, as if you'd put <tt>\s*</tt> there. Of course, the regexes themselves are in Raku syntax, which is quite different from Perl 5 regex syntax (and arguably much cleaner). Regex syntax is perhaps the area in which Raku diverges most from Perl 5.
<syntaxhighlight lang="raku"
my $total = 0;
my %blanks;
Line 2,071 ⟶ 2,135:
say "$total bare language tag{ 's' if $total != 1 }\n";
say .value, ' in ', .key for %blanks.sort;</
{{out}}
<pre>2 bare language tags
Line 2,079 ⟶ 2,143:
=={{header|REXX}}==
<
parse arg iFID . /*obtain optional argument from the CL.*/
if iFID=='' | iFID="," then iFID= 'BARELANG.HTM' /*Not specified? Then assume default*/
Line 2,090 ⟶ 2,154:
$= space($) /*elide superfluous blanks from record.*/
if $=='' then iterate /*if a blank line, then skip any tests.*/
call testHead /*process possible ==((header|
call testLang /* " " <lang
end /*recs*/
Line 2,098 ⟶ 2,162:
if bare==0 then bare= 'no'; say right(bare, 9) " bare language tags."; say
do #=1 for words(head); _= word(head, #) /*maybe show <lang> for language
if !._\==0 then say right(!._, 9) ' in' _ /*show the count for a particular lang.*/
end /*#*/
Line 2,104 ⟶ 2,168:
if noLa==0 then noLa= 'no'; say right(noLa, 9) " in no specified language."
exit 0
/*--------------------------------------------------------------------------------------*/
testHead: @head= '=={{header|'; @foot= "}}==" /*define two literals. */
hh= pos(@head, $ ); if hh==0 then return /*get start of literal.*/
Line 2,111 ⟶ 2,175:
head= substr($, or+1, hb-or-1) /*get the language name*/
if head\=='' then header= head /*Header? Then use it.*/
if wordpos(head, heads)==0 then heads= heads head /*Is lang?
return
/*--------------------------------------------------------------------------------------*/
testLang: @lang= '<lang'; @back= ">" /*define two literals. */
s1= pos(@lang, $ ); if s1==0 then return /*get start of literal.*/
Line 2,122 ⟶ 2,186:
if @lang\=='' & header=='' then noLa= noLa + 1 /*bump noLang counter.*/
if @lang\=='' & header\=='' then !.head= !.head + 1 /*bump a lang " */
return</
{{out|output|text= when using the default input:}}
<pre>
Line 2,135 ⟶ 2,199:
=={{header|Ruby}}==
Quoting from the FAQ: "If you just want the raw wikitext without any other information whatsoever, it's best to use index.php's action=raw mode instead of the API"
<
require "cgi"
Line 2,157 ⟶ 2,221:
puts "\n#{result.values.map(&:count).inject(&:+)} bare language tags.\n\n"
result.each{|k,v| puts "#{v.count} in #{k} (#{v.tasks})"}</
{{Output}}
<pre>
Line 2,169 ⟶ 2,233:
1 in gnuplot (["Greatest_common_divisor"])
1 in Bracmat (["Greatest_element_of_a_list"])
2 in
1 in ooRexx (["Greatest_element_of_a_list"])
2 in Mathprog (["Greatest_subsequential_sum", "Greatest_subsequential_sum"])
Line 2,176 ⟶ 2,240:
=={{header|Rust}}==
<
extern crate regex;
Line 2,232 ⟶ 2,296:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,239 ⟶ 2,303:
=={{header|Scala}}==
To analyse RosettaCode pages, invoke Java with <code>-Dhttp.agent=Anything</code> to work around CloudFlare blocking Java from accessing the RosettaCode site.
<
val headerFormat = "==[{]+header[|]([^}]*)[}]+==".r
val langFormat = "<lang([^>]*)>".r
Line 2,271 ⟶ 2,335:
def summary = format(mapReduce) mkString "\n"
}
def mapReduce(inputs: Seq[BareLangFinder]) = reduced(inputs.flatMap(_.map))</
'''Examples:'''
<pre>val test = """
Line 2,302 ⟶ 2,366:
2 in Mathprog (Greatest_subsequential_sum)
1 in gnuplot (Greatest_common_divisor)
2 in
1 in Bracmat (Greatest_element_of_a_list)
1 in PHP (Greatest_subsequential_sum)
Line 2,313 ⟶ 2,377:
{{tcllib|textutil::split}}
{{tcllib|uri}}
<
package require http
package require json
Line 2,417 ⟶ 2,481:
findBareTags $task [getTaskContent $task]
}
printResults</
=={{header|Wren}}==
Line 2,426 ⟶ 2,490:
{{libheader|Wren-fmt}}
Uses Go's example files.
<
import "./pattern" for Pattern
import "./set" for Set
import "./sort" for Sort
import "./fmt" for Fmt
var p = Pattern.new("/=/={{header/|[+0/y]}}/=/=", Pattern.start)
Line 2,465 ⟶ 2,529:
Sort.insertion(names)
Fmt.print(" $2d in $-11s $n", count, lang, names)
}</
{{out}}
Line 2,478 ⟶ 2,542:
{{trans|Ruby}}
Uses shared library cURL.
<
partURI="http://rosettacode.org/wiki?action=raw&title=%s",
langRE=RegExp(0'!\s*==\s*{{\s*header\s*\|(.+)}}!), // == {{ header | zkl }}
Line 2,505 ⟶ 2,569:
tasks:=results[lang];
println("%d in %s: %s".fmt(tasks.len(),lang,tasks.concat(",")));
}</
{{out}}
<pre>
Line 2,522 ⟶ 2,586:
1 in ooRexx: Greatest_element_of_a_list
2 in uBasic/4tH: Greatest_common_divisor,Greatest_element_of_a_list
2 in
</pre>
|