Files
2012-02-21 01:15:00 -05:00

1 line
5.4 KiB
JSON

[{"user_id": 32737, "stars": [], "topic_id": 36470, "date_created": 1306175063.2562921, "message": "Come to think of it, having a second argument doesn't really buy much... it's only a little shorter than typing out `keep` explicitly. The primary benefit is that it's two less parentheses if listdir and listdirall accept a second argument.", "group_id": 9739, "id": 1155288}, {"user_id": 32737, "stars": [], "topic_id": 36470, "date_created": 1306174322.410147, "message": "There's also another thing... listdir and listdirall accept a second argument: a function that will be applied on each item. So for instance, if you want to grab a list of all the folders in the current directory, you can use this:\n\n(listdir nil dirname)\n\nAnd if you want all the files, but not the folders:\n\n(listdir nil basename)\n\nAnd if you want to exclude hidden files:\n\n(listdir nil ~hidden-file)\n\nBasically, listdir just calls `keep`, but it's nice to be able to just pass in an arg, rather than calling keep yourself. I might get rid of that later, I'm not sure.\n\nIt could also be trues rather than keep, letting you both filter and map at the same time, that might be kinda neat.", "group_id": 9739, "id": 1155139}, {"user_id": 32737, "stars": [], "topic_id": 36470, "date_created": 1306174169.5122859, "message": "I've been working on something that's exciting me. Python has os.listdir, which simply returns a list of a directory's contents. So if a directory \"foo\" has the files \"bar\", \"qux\", and \"corge\", then calling os.listdir(\"foo\") will return ['bar', 'qux', 'corge']\n\nPretty nice, to be able to list the contents of directories, so hey, let's add in listdir to arc.arc! And so I did. I think it's better than Python's listdir in a few ways:\n\n1) Unlike Python's listdir, which isn't guaranteed to be sorted, Arc's listdir is sorted first by directories and files, then by the alphabet. It does this simply because it's using \"ls\", and \"ls\" knows how to do that nifty stuff.\n\n2) You don't need to pass in an argument. In Python, you *neeeeeed* to pass in an arg, but with Arc's listdir, it just defaults to the current directory.\n\n3) I also added in listdirall, which is like listdir, but it returns a list of all the files recursively, so it traverses into folders. If I recall, it's kinda clunky to get this ability in Python. Possible, but clunky. Basically, you end up writing your own version of listdirall, but in Arc it's built-in. :P", "group_id": 9739, "id": 1155114}, {"user_id": 32737, "stars": [], "topic_id": 36470, "date_created": 1306175251.8042049, "message": "On the other hand, it IS shorter, and it is two fewer parentheses. And you still have the option of calling `keep` explicitly if you want to... so having a second parameter is probably a good thing overall.", "group_id": 9739, "id": 1155325}, {"user_id": 25438, "stars": [], "topic_id": 36470, "date_created": 1306174775.7765861, "message": "So the difference between Arc 3.1's dir and your listdir is that the names are sorted, and you can pass in a function to be applied to each item?", "group_id": 9739, "id": 1155245}, {"user_id": 32737, "stars": [], "topic_id": 36470, "date_created": 1306176543.6820159, "message": "Okay, here's the differences between my listdir and Arc/pg's dir:\n\n1) Mine puts a trailing / at the end of folders, so the folder foo will be foo/ but Arc/pg just uses foo\n\n2) Sorted\n\n3) Accepts a function which lets you filter the results\n\nThat's pretty much it, in addition to the listdirall function, which could be written for Arc/pg as well.", "group_id": 9739, "id": 1155579}, {"user_id": 32737, "stars": [], "topic_id": 36470, "date_created": 1306176167.0517371, "message": "\"So the difference between Arc 3.1's dir and your listdir is that the names are sorted, and you can pass in a function to be applied to each item?\"\n\nArc had a function that did this? :P Well, ar doesn't, but it does now.", "group_id": 9739, "id": 1155524}, {"user_id": 32737, "stars": [], "topic_id": 36470, "date_created": 1306176327.4190509, "message": "Wait, what? I searched the Racket docs for something that returned a list of files, but I couldn't find one, so I just used ls. But turns out there is a function that does that: directory-list. Ugh. Oh well, I can try converting my code over to use that. Also, `dir` is a pretty darn short name. I might rename listdir over to it.", "group_id": 9739, "id": 1155549}, {"user_id": 32737, "stars": [], "topic_id": 36470, "date_created": 1306175970.15066, "message": "Oh, and here's an excellent area where Arc's function composition is incredibly awesome. Let's say you want all the non-hidden folders:\n\n(listdir nil ~hidden-file&dirname)\n\nOr all the hidden files:\n\n(listdir nil hidden-file&basename)\n\nAnd such forth and so on. And these are just ordinary functions, so you can easily filter the list with whatever you please.", "group_id": 9739, "id": 1155501}, {"user_id": 32737, "stars": [], "topic_id": 36470, "date_created": 1306177437.235116, "message": "Oh, wow! I just realized that having a second argument with dirall is *drastically* faster than using keep (even though dir calls keep internally). The difference in speed for plain-old dir is negligable.", "group_id": 9739, "id": 1155807}, {"user_id": 32737, "stars": [], "topic_id": 36470, "date_created": 1306176790.3874879, "message": "Alright, I'll rename them to dir and dirall, not only because that would be more compatible with Arc/pg, but they're shorter too!", "group_id": 9739, "id": 1155647}]