ビールから意識を遠ざけるための暇つぶし
酒飲めずあまりに暇だったのでつまらない時間つぶし、ファイルかディレクトリか判断するだけのコード
import os
def main():
r = []
fs = os.listdir(".")
for f in fs:
r.append(os.path.isdir(f))
return r
if __name__ == '__main__':
r = main()
print(r)Pythonはカレントディレクトリ 「.」 と 親ディレクトリ 「..」 は親切に返してこない、これはいい
これほぼ同じコードをhaskellで書いてみた
import System.Directory
import System.Posix
import Control.Monad
main :: IO ()
main = do
r <- return . map isDirectory =<< mapM getFileStatus =<< getDirectoryContents "."
putStrLn $ show rgetFileStatus で stat っぽい事してるので少し違うけど心地良い、ループがないけど最後の map でループしてて [チョメチョメ] が帰ってきて、 isDirectory の型さえ調べればもはや [Bool] これとすぐ分かる。 一番右の getDirectoryContents “.” と一番左の map isDirectory だけで真ん中はそんなに重要でなくなる。
そして、これをphpで書いてみた
<?php
$r = main();
var_dump($r);
function main() {
$d = opendir('.');
$res = array();
while (($file = readdir($d)) !== false) {
if (is_dir($file)) {
array_push($res, true);
} else {
array_push($res, false);
}
}
closedir($d);
return $res;
}久しぶりにphp書いたけど、結構エラーで叱られる、たまには違う言語も書かないとだめですね。
ちなみにphpはワンライナーでもやれる
<?php $r = main();var_dump($r);function main() {$d = opendir('.');$res = array();while (($file = readdir($d)) !== false) {if (is_dir($file)) {array_push($res, true);} else {array_push($res, false);}}closedir($d);return $res;}phpつおい、ちなみに私は、 ZendFramework派 です。
Posted on 2021-09-11 17:10:36
