Xah Talk Show 2025-07-04 Ep674 Golang Map (hashtable) vs Wolfram language

xah talk show ep674 2025-07-05 126ee
xah talk show ep674 2025-07-05 126ee
package main

import (
	"fmt"
	"io/fs"
	"path/filepath"
	"time"
)

/*
Create a hashtable, each key is a full file path of a dir, including subdirectory. But excluding a given list of dir names.
then, test speed by go thru the hashtable, check each key existance
*/

var xinputdir = "c:/Users/xah/web/"

var dir_names_ignore_list = []string{
	".git",
	"emacs_manual",
	"js_es2011",
	"js_es2015",
	"node_api",
}

// strInArray return true if x equals any of y
// version 2018-11-12
func strInArray(x string, y []string) bool {
	for _, v := range y {
		if x == v {
			return true
		}
	}
	return false
}

/*
fbuilt_table build a map of a directory.
return a map.
key is filename full path.
value is 1.
result does not contain dir path.

xinputdir is a dir full path
pignorelist is a array of dir names to ignore. files in these dir are ignored.
*/

func fbuilt_table(xinputdir string, pignorelist []string) map[string]int {

	var xtable = make(map[string]int)

	var doFile = func(xpath string, xinfo fs.DirEntry, xerr error) error {
		// first thing to do, check error. and decide what to do about it
		if xerr != nil {
			fmt.Printf("error [%v] at a path [%q]\n", xerr, xpath)
			return xerr
		}

		// find out if it's a dir or file, if file, print info
		if xinfo.IsDir() {
			if strInArray(filepath.Base(xpath), pignorelist) {
				return filepath.SkipDir
			}
		} else {
			xtable[filepath.ToSlash(filepath.Clean(xpath))] = 1
		}
		return nil
	}

	err := filepath.WalkDir(xinputdir, doFile)

	if err != nil {
		fmt.Printf("error walking the path %q: %v\n", xinputdir, err)
	}

	return xtable

}

func main() {

	var startime = time.Now()

	var xtable = fbuilt_table(filepath.ToSlash(filepath.Clean(xinputdir)), dir_names_ignore_list)

	var endtime = time.Now()
	var xtimediff = endtime.Sub(startime).Seconds()
	fmt.Printf("time spend in seconds: %.1f\n", xtimediff)
	fmt.Printf("total items: %v\n", len(xtable))

	for kk, _ := range xtable {
		if xtable[kk] == 1 {
			fmt.Printf("%v\n", kk)
		}
	}

}
(*
Create a hashtable, each key is a full file path of a dir, including subdirectory. But excluding a given list of dir names.
then, test speed by go thru the hashtable, check each key existance
 *)

inputDir = "c:/Users/xah/web/xahlee_info/talk_show/";

dirsToSkip = {
".git",
"emacs_manual",
"js_es2011",
"js_es2015",
"node_api"
};

(* HHHH------------------------------ *)

xfileList = FileNames[
StringExpression[ (_).., {"advent",} , (_).. ],
inputDir,
Infinity]

(*
failed.
problem is, we are trying to write a StringExpression
that matches file full path, but except a list of them.
we failed in creating a negation, e.g.
a StringExpression pattern that match full file path (not just parts),
but does not contain advent.

todo in future.
 *)

hashtable fight