Xah Talk Show 2022-12-23 Advent of Code Day 7 B, in WolframLang, Live Coding
- 17:51 map function to specific level of a tree. not in JavaScript python ruby lisp haskell
- 41:45 naming of predicate in programing languages, lisp ruby wolframlang
is it possible to determine a given full is a dir or file, from out xpaths. the answer seems no. because:
- dir names never contains a dot
- some file names contains a dot, but not all
- any file, is a leaf node.
- but a dir, may be a leaf node, or a non-leaf node. because, a dir may have 0 children, or, no ls is performed on the dir.
- we are going to built a tree (by nested dict), from a list of full pahts (our xpaths). do we need to know, if a path, is a dir or file?
- answer: no. because a list of full path fully specifies the tree structure.
(* problem: turn x1 into a nested Association *) x1 = { {"/"}, {"/", "cprq.fmm"}, {"/", "lthcng.gnf"}, {"/", "nblfzrb.mrr"}, {"/", "sfrbjmmh.jnj"}, {"/", "vmpgqbcd"}, {"/", "btsgrbd"}, {"/", "btsgrbd", "qwnml.bqn"}, {"/", "btsgrbd", "sdwnsgwv.mjm"}, {"/", "btsgrbd", "vzgwwjq.zbp"}, {"/", "btsgrbd", "zvspnvfr.zbc"}, {"/", "btsgrbd", "cmfdm"} }; dirTree = Association[ ]; Map[ f , { {"/"}, {"/", "cprq.fmm"} } ] (* first, check if the fullpath is a file or dir, by checking sizeLookupTable *) curPath isFileQ = KeyExistsQ[ sizeLookupTable, curPath ] fileSize = Lookup[ sizeLookupTable, curPath] dirTree["/"] = If[isFileQ, fileSize , Association[ ] ] dirTree["/"]["cprq.fmm"] = If[isFileQ, fileSize , Association[ ] ] dirTree["/"]["b"]["c"] = If[isFileQ, fileSize , Association[ ] ] we want to turn this {"a", "b", "c"} into this dirTree["a"]["b"]["c"] how to do it ssss--------------------------------------------------- {"a", "b", "c"} //. f_[{a_, rest__}] -> f[a] f[{1,2,3,4,5}] //. f_[{a_, rest__}] -> f[a][{rest}] into this dirTree["a"]["b"]["c"] (((dirTree["a"]) ["b"]) ["c"]) tr @@ Next[ f, ] Fold[ f[#1][#2] &, {"a", "b", "c"} ] Fold[ f[#1] &, {"a", "b", "c"} ] a, b f[a][b] f[a][b], c f[a][b][c] f f[a] f[a][b] Nest[ ] Lookup[Lookup[Lookup[assoc, "a"], "b"], "c"] dirTree["a"]["b"]["c"] = x Lookup[Lookup[assoc, "a"], "b"]["c"] = x assocVar[key]=val AssociateTo[ assocVar, key -> val ] FullForm[Hold [ assocVar[key]=val ] ] [dirname2][dirname3] = val dirTree[dirname1][dirname2][dirname3] = val
f[{1, 2, 3, 4, 5}] //. f_[{a_, rest___}] -> f[a][{rest}]