3 ms·
To be fair, both the imperative and the lisp version of that are probably not optimal. In most imperative (OO) languages the first one could hopefully be F
by alkonaut 10y ago
To be fair, both the imperative and the lisp version of that are probably not optimal.
In most imperative (OO) languages the first one could hopefully be
Fun3().Fun2().Fun1()
or even
thing3.thing2.thing1
If some thing is a property of the other
Concrete example from your linke: a projection and sort (I believe it is).
I think that the tail of this expression with 5 parentheses makes this very hard to read (Admittedly it's a matter of experience of course):
(docs (iter (db.sort *game-collection* :all
:field "votes"
:asc nil)))))
Questions I find hard to answer for the above line is: how many args does "iter" have? Is 5 trailing parentheses the right number?
Here is some half-functional half-imperative strawman syntax: for comparison:
x = some_collection
.select(c -> c.documents)
.sort(d -> d["votes"], direction: ascending);
I find that a lot easier to balance, and know how many arguments the projection has compared to the sort.
- kazinator 10y ago> Questions I find hard to answer for the above line is: how many args does "iter" have? It's obvious to me in a millisecond glance that docs and iter are called with one argument. For one thing, the snippet does not contain a single instance of a ) parenthesis being followed by a ( parenthesis. Also, none of the ) parentheses have any material between them. It's just (sym (sym (sym stuff ...))). Only three trailing parens are required to close (docs; the other two are for something else. If iter had additional arguments, it might be written like this: (docs (iter (db.sort *game-collection* :all :field "votes" :asc nil) (another-iter-arg))) It is still obvious that docs has one arg, iter has two and that the :all :field material is under db.sort. https://news.ycombinator.com/item?id=9229339 https://news.ycombinator.com/item?id=9229339
- alkonaut 10y agoAh, the arg-count-from-indentation makes sense with the concrete example, thanks. That said: if the code is neither correctly indented or has the parens correctly balanced - then one is in a pickle. It's easy to balance parens on correctly indented code, and it's trivial to indent correctly balanced code.
- junke 10y ago> That said: if the code is neither correctly indented or has the parens correctly balanced - then one is in a pickle. But you can format the code automatically, and check if all is good by looking at the shape of your code (indentation). If the amount is the same but you put elements in a strange location, like: (defun bar () (let ((a (foo)) (print (list a))))) Simply pretty-printing it with PPRINT will do: (DEFUN BAR () (LET ((A (FOO)) (PRINT (LIST A))) )) (C-c RET, aka. slime-expand-1-inplace, will work too). Indentation and parenthesis introduce a level of error checking by redundancy: the tools works on structured expressions, you look at indentation. Here above you can see that the body of the LET is empty, while there is a bogus binding in it. The above example is in practice quite rare. Most errors are easily spotted with paren highlighting. I use paredit, which makes structural editing easy (but which is not too rigid and allows me to make unstructured changes too). You generally don't have unbalanced parenthesis, and if you don't spot the bad syntax with indentation, the compiler has a chance to complain, too.
- junke 10y ago> I think that the tail of this expression with 5 parentheses makes this very hard to read > how many args does "iter" have? Forget about parentheses and look at indentation. You need properly indented code, but that comes easily with a good editor: (foo (bar ... ... ...) ... (baz (+ x y))) Both foo and bar takes 3 arguments, baz only one. Likewise, "iter" has only one argument, because there is only one subtree at the indentation level where its arguments are expected to appear. If I try to add arguments inside the set of closing parentheses, emacs actually color them in red to signal that this is not easily readable. http://dept-info.labri.u-bordeaux.fr/~idurand/enseignement/PFS/Common/Strandh-Tutorial/indentation.html http://dept-info.labri.u-bordeaux.fr/~idurand/enseignement/P...
- Jach 10y ago> Fun3().Fun2().Fun1() Hope you like Null Pointer Exceptions. (And poorly named methods.)
- alkonaut 10y agoNeither the function names or whether a type systems has null or not depends on the paradigm (functional/OO/imperative/procedural/whatever). I prefer type systems without null if given the choice, but for composition there isn't much different between result|error and result|null (If the null/error case is actually handled. Unless you use C you can usually handle it with something that resembles modads or is at least a pattern matching shorthand C# var managerStreet = employee.Manager?.Address?.Street; Which is very similar looking to how Rust (Which has no null) propagates errors: foo()?.bar()?.baz()?
- kazagistar 10y agoAfaik using `?` for `Option` is part of the `Carrier` trait which is unstable and only in nightly Rust for the time being.