-
-
Notifications
You must be signed in to change notification settings - Fork 359
Euclid algorithm in bash #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Euclid algorithm in bash #497
Conversation
@@ -1,4 +1,4 @@ | |||
# Thomas Algorithm | |||
s# Thomas Algorithm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you typed an s
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait this file should not be here at all!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops got to undo commits
@@ -111,8 +111,6 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e | |||
[import, lang:"java"](code/java/thomas.java) | |||
{% sample lang="hs" %} | |||
[import, lang:"haskell"](code/haskell/thomas.hs) | |||
{% sample lang="go" %} | |||
[import, lang:"go"](code/golang/thomas.go) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're still removing lines, you shouldn't touch thomas_algorithm.md
at all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops yeah
should i use let or $(()), imo let is nicer to look at but still pretty disgusting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the code looks pretty clear, but I'll leave it up for a bit in case anyone else wants to review it. Here's just a quick fix.
book.json
Outdated
@@ -163,6 +163,10 @@ | |||
{ | |||
"lang": "lolcode", | |||
"name": "LOLCODE" | |||
}, | |||
{ | |||
"lang": "bash", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually make lang
the file extension so sh
in this case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First shell script, you brave soul.
- It is very easy to mix them, because everyone does it, but don't write
sh
constructs when there are newer betterbash
ones. - I'll submit an EditorConfig PR (which everyone should be using), but bash should use spaces for indentation, not tabs, and I prefer 4 rather than 2 spaces.
- All shell scripts should have the shebang at the top:
#!/usr/bin/env bash
@@ -0,0 +1,38 @@ | |||
abs() { | |||
local ret=$1 | |||
if [ $ret -lt 0 ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ ... ]
is sh
(the original Bourne shell). Please use [[ ... ]]
for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though, you can do even better considering the ((...))
syntax (left as an exercise for the reader.
abs() { | ||
local ret=$1 | ||
if [ $ret -lt 0 ]; then | ||
let "ret = $ret * -1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are working with integers, do general arithmetic instead, using more modern Bash:
((ret *= -1))
let "b = $a % $b" | ||
let "a = tmp" | ||
done | ||
echo $a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer printf
for function returns. The echo
s at the end of the script are fine.
echo $a | ||
} | ||
|
||
result=$(euclid_mod 143 693) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the other examples for consistency:
result=$(euclid_mod $((64 * 67)) $((64 * 81)))
etc.
Oh, and |
all my bash scripts are also .sh haha, ok editing |
Not specific to the review, and I'll document it elsewhere, but
|
Ok. Yeah, you guys are right. The scripts don't need to be |
oh wow shellcheck seems quite cool, giong to update code based on that |
The most common shellcheck error is about quoted variables. While this is the most important thing for daily scripting, it isn't important here at all, so ignore it. |
You still need to fix the filename. |
@@ -59,6 +59,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two | |||
[import:2-17, lang:"emojicode"](code/emojicode/euclidean_algorithm.emojic) | |||
{% sample lang="lolcode" %} | |||
[import:25-40, lang="LOLCODE"](code/lolcode/euclid.lol) | |||
{% sample lang="bash" %} | |||
[import:10-22, lang="bash"](code/bash/euclid.bash) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've swapped the subtraction and modulus versions in the text.
#!/usr/bin/env bash | ||
abs() { | ||
local ret=$1 | ||
if (( $ret < 0 )); then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for $
in any math expression.
Ok should be fixed |
I can't pick it apart further, thanks! |
umm quite useless but slightly better than writing in bf haha, also displayed nicely on gitbook too:>