- cross-posted to:
- programmerhumor@lemmy.ml
- cross-posted to:
- programmerhumor@lemmy.ml
cross-posted from: https://lemmy.ml/post/39334581
R:
\()While
Cfeels fine without having a keyword for function, I feel likebashwould have benefitted from it.basic:
def fnfuncitonIdk why but that’s how I type it half the time.
You press c and t using the same finger, and i with another. So since you need to use the same finger twice in a row, also moving it a fair distance in between, your other finger just presses the button a little bit too soon, and that’s how you end up with
funciton‘c’ and ‘t’ should definitely be hit with different fingers if you do touch-typing. But with one hand, that’s true.

there’s no ‘i’ on that keyboard?
deleted by creator
Won’t you take me to
funcitooon?
Won’t you take me to
funcitoon.
Best no contest
Anyone tried lisp? Looks something like this. ((()))()())))
Remarkable how if the parenthesis is shifted from
lambda()to(lambda), people lose the ability to comprehend things.Isn’t it more like
foo(){…}->(define foo (lambda ()))
tbf?In clojure it’s (def (fn [])) or short (defn []).
In Emacs Lisp, you use one of these two:
(defun funcname (arg1 arg2) (+ arg1 arg2))(lambda (arg1 arg2) (+ arg1 arg2))— with the latter typically being an argument to another function or macro.
deleted by creator
Not sure I’d call what bash has functions. They’re closer to subroutines in Basic than functions in other languages, as in you can’t return a value from them (they can only return their exit code, and you can capture their stdout and stderr). But even then, they are full subshells. It’s one of the reasons I don’t really like Bash, you’re forced into globally or at least broadly-scoped variables. Oh, and I have no clue right now how to find where in your pipe you got a non-null exit code.
It’s not a big problem for simple scripting, but it makes things cumbersome once you try to do more.
You’re not forced into global forced variables, but they’re the default. Use the
localkeyword in front of the variable declaration for nicely scoped variable.It’s not that cumbersome to do things like
local date=`date` echo "$date"but in all honesty the syntax sucks ass because it’s not intuitive. If statements suck ass, passing variables has to be done via command line arguments sucks ass, switch statements suck ass, making structured data sucks ass (
jqis nice though).I agree with you that bash really sucks when you get to anything more than 10 lines and at that point I’d take literally prefer Dreamberd.
global variables
vomits
where in your pipe you got a non-null exit code
First thing you want is
set -eandset -o pipefail. That should report the errors in human-parseable form.Second, to capture exit codes from each command/program, you have to run each of them in sequence yourself, connected by pipes that you create via
mkfifo— the same way as you would do it in any other programming environment. Bash’s|pipes are just a convenient shorthand for this,so if you want full control, you have to ditch the convenience.Functions are definitely not subshells in Bash, seeing as anything modifying the environment, like
pyenvand such, is implemented as functions instead of scripts — specifically because functions are run in the same shell instance.Unless ‘subshell’ means something in the vein of ‘like a new shell, but not really’.
Functions are definitely not subshells in Bash
You’re right, my bad, I got this mixed up with something else.
Try doing
cdinside a function and thenpwdin the main script. Does it move the main script too?I’m gonna bet yes for the simple reason that various helper scripts exist that do advanced
cdhistory, with fuzzy search and whatnot, and they can’t be implemented as anything other than functions.
I really like bash when dealing with even somewhat advanced scripting. Like the 300 LOC scraper I have written over the past two days which horribly parses HTML files using grep | sed.
It’s genuinely so much more fun to do this with Bash than, say, Python. I have once written a scraper using Beautifulsoup and I have no desire to do so ever again.
Honestly, only Haskell manages to beat Bash in how satisfying it feels when you manage to get something working well.

Bash has its upsides too, like the fact that it has arrays / lists and dictionaries / hashmaps. In my opinion, it gets iffy though when you need to do stuff with IFS; at that point one might be better off just using specialized tools.
Not saying working bash isn’t good enough, but it can break in very surprising ways is my experience.
Kotlin seams fun
It is. Also *seems
A pointer?
To a dictionary
Related: Every
Fnkey on a keyboard is a missed opportunity! That’s not fun at all!
Begs the question, what’s the other shift key labeled!
Doesn’t matter: Nobody uses right shift for anything but pinball games!
okay, now i gotta figure out how to start a keyboard rave when i press fn
That’s a cool looking keyboard!
Meanwhile Haskell:
=
\x -> …
The examples on the meme don’t bind any variables. If those are lambdas, the Haskell version is just the
part.
def ():is pretty niceEdit: also as someone doing a bunch of CI work right now, Bash can GTFO (unless the alternative is whatever Windows is doing)
Windows’s powershell has OOP :D
Nushell is pretty nice. It’s the good parts of “what Microsoft is doing”, i.e. real structured data in a shell-like language and real error handling.
also better to type with one hand
You QWERTY people…
/jk
Colemak is great though
() => {}
C++ has
[]{}.(You can also add more brackets if you wish to do nothing longer:
[]<>[[]]()[[]]{}())Then rust has
||{}Sadly we can’t add more complexity without adding an argument:
|_:&'_[()]|{}
function: ... goto function;Or perhaps
call functionif you’ve got a call stack going.Nevermind that is C or something right? Otherwise it would be
jmp function?Yeah that’s C.
I added the
gototo put emphasis on the function being a label instead of a real function.I’ve done that in C before. I was just confused because the labels need to be in scope of a function as far as I am aware. In assembly you don’t really have that.
We’ll just put everything in
int main(). No worries
Kotlin also lets you do
fun x() = y()I have no idea why you’d need that especially since return y() is pretty easy, but… I want it!
(Actually, I guess a super simple way of overloading a method, like fun x() = x(defaultValue) could be neat)
This can also be a side product for code blocks being expressions instead of statements.
In rust for example they are, so it’s not rare to see functions like:
fn add_one(x: i32) -> i32 { x+1 }This lets you do amazing things like:
let x = if y < 0.0 { 0.0 } else { y }which is the same as
x = y < 0.0 ? 0.0 : yBut is much better for more complex logic. So you can forget about chaining 3-4 ternary operations in a single line.
Lisp programmers seeing these ‘amazing things’:

But yeah, every time I’m trying to do a ternary in Lua, I miss being able to just throw in an
if. Thankfully it can be amended with Fennel.
default values is one of my pet-peeves after using Python regularly. I wish more languages would let you just do something like
def do_thing(arg=default_value)without hoops like builder pattern, function overloading, or whatnot(Kotlin does support that, with the same
fun do_thing(arg: Int = 2)syntax.)I mean, the go-to approach in Lisp, for example, is to have null as the default value (which doubles for false in there). And check for that in the function.
That sounds awfully similar to JavaScript, which uses undefined as default values.
In Lisp, at least the Emacs Lisp with which I have experience, it’s customary to put in
nil(Lisp’s null) for any omitted arguments in the middle that you can’t be arsed to specify — aside from just leaving off arguments at the end. In JS, typing inundefinedin every such case would probably be an annoyance, so I’m guessing coders need to check for bothundefinedandnullin these circumstances.Overall, it’s remarkable how Lisp teaches one to be much more relaxed about programming practices than is typical for mainstream languages. Design patterns? Data structures? Shit, just pass in a list or an assoc array, and maybe a function here and there. Also everything is an expression, enjoy your ternary
(if)at any point anywhere.









