[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Isn't Python comfy? How do you write this in your favorite
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 53
Thread images: 4
File: pythononeliner.png (24 KB, 782x301) Image search: [Google]
pythononeliner.png
24 KB, 782x301
Isn't Python comfy?

How do you write this in your favorite language?
>>
My favourite language right now is a pass between Java and Python.
>>
let find_file dir predicate =
let rec find dir =
let children = Sys.readdir dir in
let lim = Array.length children in
let rec loop i =
if i = lim then
None
else
let child = children.(i) in
if predicate child then
Some child
else if Sys.is_directory child then
match find (Filename.concat dir child) with
| None -> loop (succ i)
| Some _ as file -> file
else
loop (succ i) in
loop 0 in
match find dir with
| None -> raise Not_found
| Some file -> file
;;
>>
Python is my favorite language tho
>>
>>54202896
What language is that?
>>
>>54202976
It's OCaml, but it doesn't do the same thing as OP. I'm rewriting it.
>>
>>54202615
Javathon?
>>
let is_directory f =
try
Sys.is_directory f
with
| _ -> false
;;

let readdir f =
try
Sys.readdir f
with
| _ -> [||]
;;

let find_file pathname visitors =
let rec visit file =
let rec loop = function
| [] -> ()
| visitor :: visitors ->
if visitor file then
loop visitors in
loop visitors in
let rec find dir =
let children = readdir dir in
let lim = Array.length children in
let rec loop i =
if i = lim then
()
else
let child = children.(i) in
let child = Filename.concat dir child in
if is_directory child then
find child
else
visit child;
loop (succ i) in
loop 0 in
find pathname
;;

let main () =
let dir = Sys.argv.(1) in
let regexp = Str.regexp ".*\\.pyc" in
let predicate file = Str.string_match regexp file 0 in
let rm file =
Printf.eprintf "removing %S" file;
prerr_newline ();
(* Sys.remove file; *)
true in
find_file dir [predicate; rm]
;;

let () = main ();;
>>
        public static void FindFile(string pathname, params Func<string, bool>[] visitors)
{
Directory.EnumerateFileSystemEntries(pathname).AsParallel()
.ForAll(s => visitors.TakeWhile(f => f(s)).ToList());
}


C# is just too superior. Note that this will run in parallel.
>>
>>54205232
Doesn't look like it does the same thing. It applies some amount of visitors on each file, right? OP's filters the files with each visitor
>>
>>54202370
>that awkward attempt at being a lisp
>>
>>54202370
embedded functions instead of pipes looks like crap
>>
>>54205291
>It applies some amount of visitors on each file, right? OP's filters the files with each visitor
I don't understand what you mean. Here's what it does:

1- gets all the files in pathname (EnumerateFile...)
2- for each one of them, it starts running visitors until one of them returns false (or until the visitors list ends). That's the TakeWhile(). That means the last visitor only runs if all the previous visitors are true.

I think the result is the same as OP, with a different approach.

I tried with a different example though: deleting all the .txt files from a test dir. Pretty much the same as OP example.

Also that AsParallel() is stupid, ignore it I'm retarded.
>>
>>54205479
Yeah sorry about that, my head is full of fuck right now. It does seem like it should work similarly, only with a different logic (apply each visitor to all the files sequentially vs apply all visitors sequentially to each file)
>>
ES2015 Javascript

import fs from 'fs';
import path from 'path';

function findFiles (pathname, ...visitors) {

let files = fs.readdirSync(pathname)
.map(entry => fs.statSync(entry).isDirectory()
? findFiles(entry, ...visitors)
: entry)
.every(...visitors);

}

findFiles('~/files', file => /\.pyc$/, fs.unlink);
>>
>>54206109
>]
what does the variable 'visitors' do?
so you're searching for files in a directory and... ?
i'm trying to understand the OP's code
>>
>>54206149
a
visitor
in OP's code is a test function (or callback if you understand that better) that must return a truthy value in order to continue to the next one.
>>
>>54206137
looks nice, aside from the regex, but that's my personal stance against their use
>>
>>54206213
Well you made me look it up, and it seems like String.prototype.endsWith() made it in ES2015

thanks about that
>>
>>54206208
hmm so how do you call this function?

pathname('/home/anon/test/',?)


what do you put in visitors?
>>
>>54202370
(require 'dash)
(require 'f)

(defun find-files (path &rest visitors)
(-reduce (lambda (r f) (-filter f r))
`(,(f-files path nil t) ,@visitors)))

(find-files "/foo/bar/bla"
(lambda (f) (string-suffix-p ".py" f))
(lambda (f) (message "DELETE: %s" f)))
>>
>>54206137
Thats not how the 'every' method works.
>>
>>54206321
What lisp is this?
>>
>>54206738
Emacs lisp
>>
>>54202370
So what happens if the contents of the directory change while you're searching it? All I'm seeing is race conditions, access control violations, and race conditions.
>>
>>54202370
I was under the impression that typically higher order functions are not regarded as "pythonic"?

If that's the style you like why not use a functional language?
>>
>>54206826
Knock knock.

Race condition.

Who's there?
>>
>>54206929
ur mom
>>
The only correct solution:

var FindFiles = require("node-find-files");
var path = require('path');
var fileName = 'op.fag';

var finder = new FindFiles({
rootFolder : "/",
filterFunction : function (path, stat) {
return path.basename(path) == fileName;
}
});

finder.on("match", function(strPath, stat) {
console.log(strPath);
});

finder.startSearch();
>>
>I need an entire programming language to perform a file search

Never change /g/

find ./ -name 'my.file'
>>
It'll take less than 10 lines with PHP.
>>
>>54207101
We mimic stack overflow.
>>
File: 1459901191312.jpg (175 KB, 1328x1600) Image search: [Google]
1459901191312.jpg
175 KB, 1328x1600
>>54202896
>succ i
>>
>>54207199
What? Are you against succ?
>>
>>54207101
>I need a shell and an UNIX install to perform a file search
Never change /g/
>>
File: 1461348951235.jpg (69 KB, 520x678) Image search: [Google]
1461348951235.jpg
69 KB, 520x678
>>54202370
>datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
>Isn't Python comfy?

JUST
>>
>>54207231
Much better than Java's datetime.
>>
I can do this in fewer than 1000 lines of assembly. 2ez.
>>
>>54203006
pyva
>>
>>54207208
>>
Python is the language of the gods

for i in range(1, 101): print('FizzBuzz'[i*i%3*4:8--i**4%5] or i)
>>
I don't understand what this is even doing op
so, take a pathname (or dir), and recursively include only those files that pass a filter?
>>
>>54207578
Don't worry. The code is objectively shit.
>>
>>54207578
In short, yes. Take all sub-files and apply the filters one by one to all of them.
>>
>>54202615
http://www.jython.org/

Compiles Python to JVM bytecode, lets you use Java libraries in Python
>>
find -type f -name "*.pyc" -exec rm {} \;
>>
>>54205232
nice
>>
>>54202370
I don't know who wrote that, but with python as my favorite language, I still wouldn't write it like that.

The one thing I find really fucky about python is that so many people care so much about less code. Lambda is kinda deprecated, this has major race condition issues, screwed readability and no return value. Plus it looks like hell. (but don't worry stackoverflow is going to love it)
>>
>>54207530
>using a loop instead of tail-optimized recursion
> 1, 101
>inherently non-pure side effects

ABSOLUTELY DISGUSTING
>>
>>54209562
>Lambda is kinda deprecated

Lambda is the ultimate abstraction. The statement that 'Lambda is deprecated' relative to a language φ directly implies the statement 'φ is deprecated'.
>>
>>54209562
These people are not professionals. They may have professional jobs, but they know not how to code like a professional. Being professional means writing code that is easy to understand. It's more challenging than most think.

I would highly recommend everyone read this http://www.amazon.com/Clean-Coder-Conduct-Professional-Programmers/dp/0137081073
>>
>>54209657
>I would highly recommend everyone read this http://www.amazon.com/Clean-Coder-Conduct-Professional-Programmers/dp/0137081073
Or, just pay attention in their classes as this should be in them

>>54209644
You're confusing the lambda python statement with lambda calculus. The status last time I checked was 'werks great but has implementation issues', while many other lambda calculus constructs are recommended because they are implented better/'more pythonic' and werk too
>>
>>54202370
Stuff like that annoys me. Why use a high level language such as python when you write cryptic, unreadable code like that? Why even bother? Abstractions do not exist to exploit them to look smart. Sure, it's short, but I have no clue what it does from looking at it.
Thread replies: 53
Thread images: 4

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.