-
Notifications
You must be signed in to change notification settings - Fork 49
Code Snippets Comparisons
Ilya Sher edited this page Jan 13, 2019
·
16 revisions
This page contains random pieces of code and their counterparts in NGS, reminiscent of Rosetta Code.
I frequently feel the need to see how a piece of code would look in NGS. Here are some of the comparisons.
package useragent // import "github.com/docker/docker/pkg/useragent"
import (
"strings"
)
Currently none. Maybe later, when NGS explicitly supports very large projects.
// VersionInfo is used to model UserAgent versions.
type VersionInfo struct {
Name string
Version string
}
doc VersionInfo is used to model UserAgent versions.
type VersionInfo
F init(vi:VersionInfo, name:Str, version:Str) init(args())
F Str(vi:VersionInfo) "${vi.name}/${vi.version}"
func (vi *VersionInfo) isValid() bool {
const stopChars = " \t\r\n/"
name := vi.Name
vers := vi.Version
if len(name) == 0 || strings.ContainsAny(name, stopChars) {
return false
}
if len(vers) == 0 || strings.ContainsAny(vers, stopChars) {
return false
}
return true
}
F is_valid(vi:VersionInfo) {
STOP_CHARS = " \t\r\n/"
F contains_any(s:Str, chars:Str) s.any(X in chars)
[vi.name, vi.version].all(F(field) {
field and not(field.contains_any(STOP_CHARS))
})
}
func AppendVersions(base string, versions ...VersionInfo) string {
if len(versions) == 0 {
return base
}
verstrs := make([]string, 0, 1+len(versions))
if len(base) > 0 {
verstrs = append(verstrs, base)
}
for _, v := range versions {
if !v.isValid() {
continue
}
verstrs = append(verstrs, v.Name+"/"+v.Version)
}
return strings.Join(verstrs, " ")
}
F append_versions(base:Str, *version_info) {
guard version_info.all(VersionInfo)
not(version_info) returns base
base +? ' ' + version_info.filter(is_valid).map(Str).join(' ')
}
NGS official website is at https://ngs-lang.org/